【bug】获取图片像素数据,返回的数据全是 0

【bug】获取图片像素数据,返回的数据全是 0

本来想把数据写入图片(利用png的压缩功能),然后用 rendertexture 读出,但是读出的数据全是0,而且还提示 framebuffer incomplete,不知道是什么原因

Creator 版本:2.1.2

目标平台:Web

详细报错信息:
[.Offscreen-For-WebGL-0CAE8F08]GL ERROR :GL_INVALID_FRAMEBUFFER_OPERATION : glReadPixels: framebuffer incomplete

重现方式:
var dataImg = cc.loader.loadRes(‘img’, cc.Texture2D, function(error, texture2D) {
var renderTexture = new cc.RenderTexture();
renderTexture.initWithSize(texture2D.width, texture2D.height);
renderTexture.drawTextureAt(texture2D, 0, 0);

var data = renderTexture.readPixels();
console.log(data);

});

这个请参考官方example里面的07_capture_texture测试用例

那个例子是截屏的,需要画上屏幕,我的图片根本不需要上屏啊

而且如果屏幕缩放了,那么数据也会改变

目前这边有些问题,我反馈一下,web上你可以使用canvas去实现你这类需求的。

请问要是现在用的是2.1.2又要读取图片像素数据的话有没有什么方案呢

顶一下,请问现在有最新的解决办法嘛?

压缩之后还要解压缩 何苦呢

framebuffer 不是刚创建出来就能操作的:https://www.khronos.org/opengl/wiki/Framebuffer_Object
请等待下一帧再试试(并且要绑定到 camera)

小程序大小限制,没办法

我延迟了0.1秒依然不行,还是报那个Framebuffer is incomplete

请问如何绑定到camera?

兄弟解决了没?头疼中

兄弟怎么解决的?那个解决方案我看不懂。。。

兄弟解决了没

let GameCommon = require(‘GameCommon’);

cc.dynamicAtlasManager.enabled = true;

cc.macro.CLEANUP_IMAGE_CACHE = false;

cc.Class({

extends: cc.Component,

properties: {

    nodeTestHit: false,

    transp: false,

},

// LIFE-CYCLE CALLBACKS:

onLoad() {

    this.initEvent();

    this.initUI();

},

// start () {

// },

setContent(content) {

    this.content = content;

    this.initContent();

},

initContent() {

},

removeSelf() {

    this.content = null;

},

onDestroy() {

    // this.removeSelf();

},

initUI() {

},

initEvent() {

    

    if (!this.nodeTestHit) this.node._hitTest = this.hitTest.bind(this);

    this.node.on(GameCommon.TOUCH_MOVE, this.touchMove, this);

    this.node.on(GameCommon.TOUCH_END, this.touchEnd, this);

    this.node.on(GameCommon.TOUCH_START, this.touchStart, this);

    this.node.on(GameCommon.TOUCH_CANCEL, this.touchCancel, this);

},

canMove(grid) {

    return true;

},

touchCancel(e) {

    if (this.content) {

        this.content.touchCancel(e);

    }

},

touchMove(e) {

    if (this.content) {

        this.content.touchMove(e);

    }

},

touchEnd(e) {

    if (this.content) {

        this.content.touchEnd(e);

    }

},

touchStart(e) {

    if (this.content) {

        this.content.touchStart(e);

    }

},

hitTest(location) {

    let spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame

    if (spriteFrame == null) {

        return false

    }

    let node = this.node;

    let posInNode = this.node.convertToNodeSpaceAR(location)

    var size = node.getContentSize();

    let rect = spriteFrame.getRect()

    let offset = spriteFrame.getOffset()

    if(Math.abs(posInNode.x)>size.width/2||Math.abs(posInNode.y)>size.height/2)

    {

        return false

    }

    else if(require("UI3dtouch").s_instance.isTouch){

        return false;

    }

    else {

        if(!this.camera)

            this.camera = require("UIDecorate").s_instance.check_camera;

        if(!this.camera)return;    

        this.camera.node.active = true;

        let posInRect = cc.v2(parseInt(posInNode.x - offset.x + rect.width / 2), parseInt(posInNode.y - offset.y + rect.height / 2))

        let tex = spriteFrame.getTexture()

        let data

        this.camera.enable = true;

        let destory = false;

        if( tex instanceof cc.RenderTexture){

            this.node.group = "imgafter";

            rt = tex;

        }

        else{

            this.node.group = "camera";

            var rt = new cc.RenderTexture()

            if(tex.width == 2048)console.log("@@@@@@@@@@@@@@@@@@@@@@@" + this.node.name);

            rt.initWithSize(tex.width, tex.height, cc.gfx.RB_FMT_S8)

            rt.drawTextureAt(tex, 0, 0);

            destory = true;

        }

        // data就是这个texture的rgba值数组

        this.camera.targetTexture = rt;

        this.camera.render(undefined);

        if (spriteFrame.isRotated()) {

            data = rt.readPixels(null, rect.x + posInRect.y, rect.y + posInRect.x, 1, 1)

        }

        else {

            data = rt.readPixels(null, rect.x + posInRect.x, rect.y + rect.height - posInRect.y, 1, 1)

        }

        this.camera.targetTexture = null;

        this.camera.enable = false;

        this.camera.node.active = false;

        this.node.group = "imgafter";

        destory && rt.destroy();

        if (data[3] <= 200) {

            return false

        }

        else {

            return true

        }

    }

},

onDestroy: function() {

    if (!this.nodeTestHit) this.node._hitTest = this.hitTest.bind(null);

    // eachAll(_textureIdMapDataContainer, (v, k)=>{

    //     v.destory();

    // })

},

});

1赞

我的最终解决方案是这个,需要相机先照一下然后读取像素信息

非常感谢!