图像可以用作材质中的贴图,并且具有名为“THREE.Texture
”的属性。图像可以用作材质中的贴图,但没有名为“THREE.WebGLRenderTarget
”的属性。
如何从WebGLRenderTarget
中检索纹理数据?我想将它保存到一个文件中(或者,如果不可能,则保存为一个字节数组)。
发布于 2016-07-30 03:22:00
现在有了新的功能:
WebGLRenderer.readRenderTargetPixels ( renderTarget, x, y, width, height, buffer )
将像素数据从renderTarget读取到传入的缓冲区中。Buffer应该是使用新的Uint8Array( renderTargetWidth * renderTargetWidth *4)实例化的Javascript Uint8Array,以说明大小和颜色信息。这是gl.readPixels的包装器。
发布于 2012-11-20 15:13:14
我自己还没有尝试过,但是应该可以使用传递呈现目标的(私有) __webglTexture
属性的WebGL的readPixels函数来获得一个字节数组。
发布于 2021-07-22 13:51:39
将THREE.WebGLRenderTarget转换为2D canvas的代码片段:
// allocate RGBA array
let pixels = new Uint8Array(_floor_width * _floor_height * 4);
let can = document.createElement("canvas"), ctx, ctxData, color;
// _floor_target is instance of THREE.WebGLRenderTarget of dimension _floor_width x _floor_height and _renderer is THREE.WebGLRenderer
_renderer.readRenderTargetPixels(_floor_target, 0, 0, _floor_width, _floor_height, pixels);
can.width = _floor_width;
can.height = _floor_height;
ctx = can.getContext('2d');
ctxData = ctx.getImageData(0, 0, can.width, can.height);
for (var fy = 0; fy < _floor_height; fy++) {
for (var fx = 0; fx < _floor_width; fx++) {
// retrieve exact rendered pixel
color = [
pixels[fy * _floor_width * 4 + fx * 4 + 0],
pixels[fy * _floor_width * 4 + fx * 4 + 1],
pixels[fy * _floor_width * 4 + fx * 4 + 2],
pixels[fy * _floor_width * 4 + fx * 4 + 3]
];
// put pixel to canvas image data (this whole thing might be done much faster)
ctxData.data[(fy * can.width * 4) + (fx * 4) + 0] = color[0];
ctxData.data[(fy * can.width * 4) + (fx * 4) + 1] = color[1];
ctxData.data[(fy * can.width * 4) + (fx * 4) + 2] = color[2];
ctxData.data[(fy * can.width * 4) + (fx * 4) + 3] = color[3];
}
}
// update canvas image data
ctx.putImageData(ctxData, 0, 0);
document.body.appendChild(can);
https://stackoverflow.com/questions/13475209
复制