Clear multiple render textures with different colors

Setting the clearColor property of a MultiRenderTarget will clear all the attached textures with the same color. Is there a way to clear each texture with a different color?

I think you could iterate all textures of the MultiRenderTarget and clear them by the color of your choice.

It seems like the Texture does not offer a method to clear color directly. Am I supposed to add a draw call to render to it?

Use texture.updateURL to update the texture.

This way I have to upload data from cpu to gpu every frame :joy::joy::joy:

I don’t know what exactly do you want to achieve but this might help you:

1 Like

Thanks! This thread is quite helpful. I finally did things as follow:

const gl = scene.getEngine().getRenderingCanvas().getContext("webgl2");
multiRenderTarget.onClearObservable.add(() => {
    gl.drawBuffers([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1]);
    gl.clearBufferfv(gl.COLOR, 0, [color1.r, color1.g, color1.b, color1.a]);
    gl.clearBufferfv(gl.COLOR, 1, [color2.r, color2.g, color2.b, color2.a]);

By registering onClearObservable the auto clear of RTT will be overwritten. And I can use clearBufferfv() to set two different colors to the two color attachments.

1 Like