如何获取图片某一点的像素值,3.8.7版本

  • Creator 版本: 3.8.7

  • 目标平台:Android,微信小游戏

3.8.7版本,怎么获取一张图片上,点x,y的像素值啊。没有找到获取像素的方法了,求助

从rendertexture拿 试试

RenderTexture的那个方法,找过了,没有找到相关的

/**修改图片像素-目标图片需不被打包近图集中 */

public static ModifyPixelsOfPicture(targetSprit: SpriteFrame) {

    let texture_v = targetSprit.texture;

    // 获取像素数据

    let pixels = RenderTexture.prototype.readPixels.call(texture_v, 0, 0, texture_v.width, texture_v.height);

    //图像数据

    let colors: Color[][] = [];

    let x = 0;

    let y = 0;

    let h = texture_v.height;

    let w = texture_v.width;

    for (let j = 0; j < h; j++) {

        colors[j] = [];

        for (let i = 0; i < w; i++) {

            const idx = ((j + y) * w + (i + x)) * 4;

            colors[j][i] = new Color().fromHEX('#fb1d3d');

            if (j > h / 2) {

                pixels[idx] = 255;

                pixels[idx + 1] = 255;

                pixels[idx + 2] = 255;

                pixels[idx + 3] = 255;

            } else {

                pixels[idx] = 255;

                pixels[idx + 1] = 0;

                pixels[idx + 2] = 0;

                pixels[idx + 3] = 255;

            }

        }

    }

    //@ts-ignore

    texture_v.uploadData(pixels)

}
1赞

大佬,这个方案H5可以,原生有解决思路么

RenderTexture不能读原生?

昨天拿原生模拟器测试了一下,接口报错

 public readPixels (x?: number, y?: number, width?: number, height?: number, buffer?: Uint8Array): Uint8Array | null {
        x = x || 0;
        y = y || 0;
        width = width || this.width;
        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[] = [];
        const regions: BufferTextureCopy[] = [];

        const region0 = new BufferTextureCopy();
        region0.texOffset.x = x;
        region0.texOffset.y = y;
        region0.texExtent.width = width;
        region0.texExtent.height = height;
        regions.push(region0);

        bufferViews.push(buffer);
        gfxDevice?.copyTextureToBuffers(gfxTexture, bufferViews, regions);
        return buffer;
    }

看这个代码,都能跑

这个是引擎h5端代码,原生不是走这个接口吧
大佬能写个demo么

获取Texture2D的某个像素,兼容原生:

const buffer = new Uint8Array(4);
const region = new gfx.BufferTextureCopy();
region.texOffset.x = 0;
region.texOffset.y = 0;
region.texExtent.width = 1;
region.texExtent.height = 1;

function readPixel(texture: Texture2D, x: number, y: number): math.Color | null {
    region.texOffset.x = x;
    region.texOffset.y = y;
    const gfxTexture = texture.getGFXTexture();
    if (!gfxTexture) {
        return null;
    }
    director.root?.device.copyTextureToBuffers(gfxTexture, [buffer], [region]);
    return new math.Color(...buffer);
}
1赞

this.getGFXTexture();this._getGFXDevice();获取的都是什么类型的对象啊

这就是引擎底层代码啊 你断点就知道了 :joy:

还是不行,大佬能否整个简单demo?

具体位置得你自己调

这个方法在windows的模拟器上闪退