Search Unity

Shared Rendertarget + 4 Cameras + ColorMask = Possible ?

Discussion in 'Shaders' started by dotmos, May 16, 2013.

  1. dotmos

    dotmos

    Joined:
    Jan 2, 2011
    Posts:
    41
    Hi,

    I'm currently trying to render the content of 4 cameras into one shared rendertarget, where each camera should render in one channel of the shared rendertarget.

    Camera 1 = Red Channel
    Camera 2 = Green Channel
    Camera 3 = Blue Channel
    Camera 4 = Alpha Channel

    Some years ago i developed a deferred renderer and i think while working on PSSM in used a shared rendertarget to render the 4 splits into one texture so i can later fetch the shadowmap texture with one single texture fetch instead of four. I later scrapped that approach and used atlasing, but thats another story. The main thing is, that i THINK it is possible to render the content of 4 cameras into individual channels of a rendertarget (e.g. ARGB32)... but i'm not really sure anymore.

    The way i think it worked:
    - Bind rendertarget to camera 1
    - Activate camera 1 and use ColorMask R when rendering into the texture
    - Deactivate camera 1 and unbind rendertarget
    - Bind rendertarget to camera 2
    - Activate camera 2 and use ColorMask G when rendering into the texture
    - Deactivate camera 2 and unbind rendertarget
    - etc.

    My question: How can i do that in Unity? I'm currently using 4x R8 textures and manually compose them to an ARGB32 texture with the help of a simple shader, but this is very unelegant and seems like an unneeded slowdown.
    Also, this won't work on Android since there is no support for R8 textures (at least not on the devices i currently have access to) so i'd have to use ARGB32 on Android which would be a terrible waste AND performance drop.

    Any ideas?
     
    Last edited: May 16, 2013
  2. dotmos

    dotmos

    Joined:
    Jan 2, 2011
    Posts:
    41
    Totally forgot to mention, that i have everything set up, but it seems as if the rendertarget is cleared before the next camera tries to render into it, thus loosing the data that has been rendered into before. Either that or using ColorMask in the shader simply renders 0 to the non defined channels...

    As a start, how can i prevent a rendertarget from being cleared when a camera renders into it?
     
  3. dotmos

    dotmos

    Joined:
    Jan 2, 2011
    Posts:
    41
    Sometimes simply writing down your problem helps you in solving it. :)

    It's working now, i just had to set Camera.clearFlags = CameraClearFlags.Nothing; for camera 2,3, 4.
    This way only Camera 1 clears the rendertarget and Camera 2 3 and 4 just render on top of the previous content.
    Finally i apply the Shader that should process the rendertarget to Camera 4, so all data from Cameras 1,2,3 and 4 is processed at once.
     
    Last edited: May 16, 2013