How to clear multiple render buffers (MRT)

Hi, I’m trying to use multiple render targets for a game I’m working on. Since I haven’t done that before I created a small project to try it. I have several questions :

  • what are the differences between :

    void Camera.SetTargetBuffers(RenderBuffer colorBuffer, RenderBuffer depthBuffer);

and

static void Graphics.SetRenderTarget(RenderBuffer[] colorBuffers, RenderBuffer depthBuffer);
  • Camera.SetTargetBuffers doesn’t seem to work well : if I call it the buffers are not filled (they are black), they don’t cover the “render quad” and are placed on the bottom left of the screen. Am I missing something ?

  • where/when should I call those methods : OnPreRender (it seems to work) ? OnRenderImage ? …

  • The second buffer is not cleared each frames. Is there a way to clear a buffer manually (ask for it to be cleared) ?

Here is the small example project so you can see what I did. The project only render a sphere, the red channel of the sphere is rendered in the first target, the green channel of the sphere is rendered in the second target. They are then combine with the first target being on the left side of the screen and the second target being on the right side (they are squeezed and thats intended).
[30894-mrt.zip|30894]

And screenshots of what I get :
alt text

As you can see the right side (second target) the buffer is not cleared.
alt text

Thanks !

Dude i got tell you thx for your example project, i was hammering my head trying to put MRT working and your example seems to work for the most part, i still get some weird results sometimes because the render texture leaks when developing and you can only get it to work again by closing and reopening unity.

Anyway, regarding your question it seems to work differently with dx11 and without it but one thing that worked with me was to use the GL command “Clear”, the way you do it is by switching to the second target and clear it and then return to the first.

here’s a simple code example, probably not really elegant but it works, put it in the beginning of PreRender

RenderTexture lastActive = RenderTexture.active;
RenderTexture.active = m_target2;
GL.Clear(false, true, Color.clear);
RenderTexture.active = lastActive;

You could probably don’t use the lastActive RT and simply point to the first target, it’s up to you