Search Unity

using MRT with DrawInstanced

Discussion in 'General Graphics' started by geroppo, Mar 14, 2017.

  1. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    Hi, I'm having a bit of trouble trying to use multiple render targets along with DrawInstanced. Currently I have something like this:
    Code (CSharp):
    1. void Update()
    2. {
    3.  
    4.   color_buffers[0] = Graphics.activeColorBuffer
    5.  
    6.   Graphics.SetRenderTarget(color_buffers, Graphics.activeDepthBuffer);
    7.   Graphics.DrawMeshInstanced(some params);
    8.  
    9.   Graphics.SetRenderTarget(color_buffers[0], Graphics.activeDepthBuffer);
    10. }
    color_buffers is an array of 2 RenderBuffers, the first will always contain the scene (whatever that is in the Update method...), and the second will contain my own texture.
    Now, there are 2 things that bothers me.
    The first is that I have to use DrawMeshInstanced in the Update method, otherwise it doesn't seem to work ( would like to put it on OnPostRender or something like that). The documentation examples also put it in the Update method...

    The second is that this throws the following error
    And it is not entirely clear to me what the problem is. I've used MRT before but always when using post processing image effects, in the OnRenderImage method, but I'm lost here.


    EDIT: I just noticed that using a command buffer also works if you put it after the opaque stage, but I don't know how to "append" my extra render texture so when rendering the meshes they output the color to the current render target, and my mask to the secondary texture...

    EDIT2: Alright I feel a bit embarrased now, after a few hours of getting myself to know the CommandBuffer API I now have everything working... If anyone else finds this thread, what I had to do to recover the current color buffer was to use this:
    https://docs.unity3d.com/ScriptReference/Rendering.BuiltinRenderTextureType.html
    in conjuntion with
    https://docs.unity3d.com/ScriptReference/Rendering.RenderTargetIdentifier.html
    and the commandBuffer.SetRenderTarget.
    Cheers
     
    Last edited: Mar 14, 2017