近期用了论坛里经验分享:Creator 原生平台截屏方案的方法进行截图,在1.8.1版本下做了点修改,想在webgl下生成图片,然后在web浏览器端显示,但是发现图片是全透明的。
我这边采用了下面的方式获取像素数据:
const texture = renderTexture.getSprite().getTexture();
// Create a framebuffer backed by the texture
const framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture._webTextureObj, 0);
// Read the contents of the framebuffer
const data = new Uint8Array(width * height * 4);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data);
gl.deleteFramebuffer(framebuffer);
结果是data里面的像素数据全是0。请问是我的用法不对吗?还是别的问题?
下面是代码压缩包:
ScreenCaptureDemo.zip (448.0 KB)