重写代码就是抄引擎里面的代码,放在你自己的公用库或者你认为合适的地方,实现如下:
public readPixels (texture:Texture2D, x = 0, y = 0, width?: number, height?: number) : Uint8Array | null {
width = width || texture.width;
height = width || texture.height;
const gfxTexture = texture.getGFXTexture();
if (!gfxTexture) {
return null;
}
const bufferViews: ArrayBufferView[] = [];
const regions: gfx.BufferTextureCopy[] = [];
const region0 = new gfx.BufferTextureCopy();
region0.texOffset.x = x;
region0.texOffset.y = y;
region0.texExtent.width = width;
region0.texExtent.height = height;
regions.push(region0);
const buffer = new Uint8Array(width * height * 4);
bufferViews.push(buffer);
director.root?.device.copyTextureToBuffers(gfxTexture, bufferViews, regions)
return buffer;
}
// 使用的方法mainTexture就是你想要修改的贴图,readPixels出来的就是贴图的详细信息,想怎么改就怎么改了
// 获取旧贴图的内存数据 是一个线性数组 一个矩阵
let imgData = this.readPixels(mainTexture);