From b276ff6cdd7e691e189572199d8e83b0ce54011f Mon Sep 17 00:00:00 2001 From: liqiao Date: Wed, 29 Dec 2021 16:04:54 +0800 Subject: [PATCH 1/5] fix function readPixels height setting --- cocos/core/assets/render-texture.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/core/assets/render-texture.ts b/cocos/core/assets/render-texture.ts index a06dcf60eb7..dd4183b29fa 100644 --- a/cocos/core/assets/render-texture.ts +++ b/cocos/core/assets/render-texture.ts @@ -181,7 +181,7 @@ export class RenderTexture extends TextureBase { */ public readPixels (x = 0, y = 0, width?: number, height?: number) : Uint8Array | null { width = width || this.width; - height = width || this.height; + height = height || this.height; const gfxTexture = this.getGFXTexture(); if (!gfxTexture) { return null; From 27165f28ad68b637185cf581f74713a964637ee8 Mon Sep 17 00:00:00 2001 From: liqiao Date: Wed, 29 Dec 2021 18:49:45 +0800 Subject: [PATCH 2/5] add paramter to receive pixel data for readPixels. --- cocos/core/assets/render-texture.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cocos/core/assets/render-texture.ts b/cocos/core/assets/render-texture.ts index dd4183b29fa..dbab4a3b87a 100644 --- a/cocos/core/assets/render-texture.ts +++ b/cocos/core/assets/render-texture.ts @@ -178,14 +178,24 @@ export class RenderTexture extends TextureBase { * @param y 起始位置Y轴坐标 * @param width 像素宽度 * @param height 像素高度 + * @param buffer 像素缓存 */ - public readPixels (x = 0, y = 0, width?: number, height?: number) : Uint8Array | null { + public readPixels (x = 0, y = 0, width?: number, height?: number, buffer?: Uint8Array) : Uint8Array | null { width = width || this.width; height = height || this.height; const gfxTexture = this.getGFXTexture(); if (!gfxTexture) { + console.error('getGFXTexture null'); return null; } + const needSize = 4 * width * height; + if (buffer === undefined) { + buffer = new Uint8Array(needSize); + } else if (buffer.length < needSize) { + console.error('buffer size need larger than 4 * width * height'); + return null; + } + const gfxDevice = this._getGFXDevice(); const bufferViews: ArrayBufferView[] = []; @@ -198,11 +208,8 @@ export class RenderTexture extends TextureBase { region0.texExtent.height = height; regions.push(region0); - const buffer = new Uint8Array(width * height * 4); bufferViews.push(buffer); - gfxDevice?.copyTextureToBuffers(gfxTexture, bufferViews, regions); - return buffer; } } From 429931bcb7fc0613a4f74ea45829d3b5d082bb12 Mon Sep 17 00:00:00 2001 From: liqiao Date: Thu, 30 Dec 2021 13:40:37 +0800 Subject: [PATCH 3/5] use errorID --- EngineErrorMap.md | 8 ++++++++ cocos/core/assets/render-texture.ts | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/EngineErrorMap.md b/EngineErrorMap.md index 5f21bfcb7f2..37a35952709 100644 --- a/EngineErrorMap.md +++ b/EngineErrorMap.md @@ -2881,6 +2881,14 @@ saveToFile isn't supported on Cocos2d-Html5 newCCImage isn't supported on Cocos2d-Html5 +### 7606 + +GFXTexture is null + +### 7607 + +readPixels buffer size need larger than %d + ### 7700 On the web is always keep the aspect ratio diff --git a/cocos/core/assets/render-texture.ts b/cocos/core/assets/render-texture.ts index dbab4a3b87a..fa9a1cc59d6 100644 --- a/cocos/core/assets/render-texture.ts +++ b/cocos/core/assets/render-texture.ts @@ -38,6 +38,7 @@ import { RenderWindow, IRenderWindowInfo } from '../renderer/core/render-window' import { Root } from '../root'; import { TextureBase } from './texture-base'; import { BufferTextureCopy } from '../gfx/base/define'; +import { errorID } from '..'; export interface IRenderTextureCreateInfo { name?: string; @@ -185,14 +186,14 @@ export class RenderTexture extends TextureBase { height = height || this.height; const gfxTexture = this.getGFXTexture(); if (!gfxTexture) { - console.error('getGFXTexture null'); + errorID(7606); return null; } const needSize = 4 * width * height; if (buffer === undefined) { buffer = new Uint8Array(needSize); } else if (buffer.length < needSize) { - console.error('buffer size need larger than 4 * width * height'); + errorID(7607, needSize); return null; } From caf6577d6456b7b274b28baabea70661604672e9 Mon Sep 17 00:00:00 2001 From: liqiao Date: Thu, 30 Dec 2021 13:48:52 +0800 Subject: [PATCH 4/5] modify error description --- EngineErrorMap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EngineErrorMap.md b/EngineErrorMap.md index 37a35952709..7c789742015 100644 --- a/EngineErrorMap.md +++ b/EngineErrorMap.md @@ -2887,7 +2887,7 @@ GFXTexture is null ### 7607 -readPixels buffer size need larger than %d +readPixels buffer size smaller than %d ### 7700 From a307bcfe76c930df73d02fec6b4cf8d91efd9616 Mon Sep 17 00:00:00 2001 From: liqiao Date: Thu, 30 Dec 2021 14:07:17 +0800 Subject: [PATCH 5/5] fix import { errorID } --- cocos/core/assets/render-texture.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/core/assets/render-texture.ts b/cocos/core/assets/render-texture.ts index fa9a1cc59d6..ea03bb10cd9 100644 --- a/cocos/core/assets/render-texture.ts +++ b/cocos/core/assets/render-texture.ts @@ -38,7 +38,7 @@ import { RenderWindow, IRenderWindowInfo } from '../renderer/core/render-window' import { Root } from '../root'; import { TextureBase } from './texture-base'; import { BufferTextureCopy } from '../gfx/base/define'; -import { errorID } from '..'; +import { errorID } from '../platform/debug'; export interface IRenderTextureCreateInfo { name?: string;