Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix readpixels #9900

Merged
merged 5 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions EngineErrorMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 smaller than %d

### 7700

On the web is always keep the aspect ratio
Expand Down
18 changes: 13 additions & 5 deletions cocos/core/assets/render-texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '../platform/debug';

export interface IRenderTextureCreateInfo {
name?: string;
Expand Down Expand Up @@ -178,14 +179,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 = width || this.height;
height = height || this.height;
const gfxTexture = this.getGFXTexture();
if (!gfxTexture) {
errorID(7606);
return null;
}
const needSize = 4 * width * height;
if (buffer === undefined) {
buffer = new Uint8Array(needSize);
} else if (buffer.length < needSize) {
errorID(7607, needSize);
return null;
}

const gfxDevice = this._getGFXDevice();

const bufferViews: ArrayBufferView[] = [];
Expand All @@ -198,11 +209,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;
}
}
Expand Down