I want to use the view of RenderTargetTexture in GUI Image. Is there a way to use RenderTargetTexture as the source of GUI Image?
I have tried the readPixels to get image url in scene.beforeRender , but this will lower FPS.
scene.beforeRender = function () {
if (tmpThis._switch.value) {
var texture = tmpThis._rendertargetTexture;
let size = texture.getSize();
let canvas = tmpThis._renderCanvas;
if (canvas === null) {
canvas = document.createElement('canvas');
tmpThis._renderCanvas = canvas;
}
canvas.width = size.width;
canvas.height = size.height;
let ctx = canvas.getContext('2d');
texture.readPixels(0, undefined, undefined, false)!.then((data) => {
var imageData = new ImageData(new Uint8ClampedArray(data.buffer), size.width, size.height);
var h = imageData.height;
var w = imageData.width;
var i, i2, t;
for (var dy = 0; dy < h; dy++) {
for (var dx = 0; dx < w / 2; dx++) {
i = (dy << 2) * w + (dx << 2)
i2 = ((dy + 1) << 2) * w - ((dx + 1) << 2)
for (var p = 0; p < 4; p++) {
t = imageData.data[i + p];
imageData.data[i + p] = imageData.data[i2 + p];
imageData.data[i2 + p] = t;
}
}
}
ctx.putImageData(imageData, 0, 0);
let dataUrl = canvas.toDataURL();
Another question is the highlight can’t be show in RenderTargetTexture.