Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

SetRenderTarget mystery

Discussion in 'General Graphics' started by jzhang1, Mar 25, 2015.

  1. jzhang1

    jzhang1

    Joined:
    Jan 11, 2011
    Posts:
    7
    I have been trying to figure out how SetRenderTarget works exactly. I have the following code:

    Code (CSharp):
    1.  
    2. RenderTargetIdentifier rtID = new RenderTargetIdentifier(this.rt);
    3. buffer.SetRenderTarget(rtID);
    4. buffer.DrawMesh(mesh, matrix, this.MeshMat, 0, -1, MaterialProperties);
    5. this.RenderCamera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, buffer);
    6.  
    However because the Camera's render target isn't set explicitly, it still renders to screen. How exactly does the command buffer's SetRenderTarget work in contrast to setting the render target on the Camera script explicitly?
     
  2. AkashicMike

    AkashicMike

    Joined:
    Mar 9, 2015
    Posts:
    1
    Maybe you've already figured it out by now, but it works exactly the same way. The only difference here with command buffers is when the action happens. That is to say, it's just another operation in the list of operations that makes up the whole command buffer. It only happens whenever the camera event is triggered, and only after all the previous operations that was added to it.

    When there is no explicit render target specified, the camera actually renders to the "back buffer" (i.e. directly onto the viewport). If you want to render elsewhere, you can use the buffer.GetTemporaryRT() command to get another target to render to before calling SetRenderTarget on it.
     
  3. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    @AkashicMike Actually I think you missunderstood, as currentCamera.targetTexture != Graphics.SetRenderTarget .
    The former sets where the camera should end up with it's result. (if nothing set then backbuffer).
    The latter sets where the currently used drawMesh/blit commands are rendered to. Then after that the camera continues on and will set different render textures depending on where in the pipeline it is.