Search Unity

Multiple Render Targets for non-image effects?

Discussion in 'Shaders' started by Lipoly, May 24, 2018.

  1. Lipoly

    Lipoly

    Joined:
    Feb 11, 2014
    Posts:
    42
    I've been looking into using MRT (Multiple Render Targets) to create a mask which will later be used to by some image effects. The effect should be applied only to particular objects on the screen.

    One way to do this would be to have the regular camera render a pass with a special shader which simply outputs the mask. The cameras culling mask would be set to only render the specific objects.

    Another method that I'd like to try is to use our existing shader (which is specific to these objects) and have it output both its normal color and to an additional texture, which would simply output 1 to create the mask. Since the shader is specific to these objects, it will end up producing the mask I need.

    However, all the MRT examples I've seen have been specific to image effects and setup the multiple render targets in OnRenderImage().

    TLDR:
    Are there any examples/guidance for how you output to an additional render target in an existing "regular" shader (i.e. not an image effect)?
     
    qdeanc and AlejUb like this.
  2. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Basic template - assign four render textures and single shader will render to them.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class mrt : MonoBehaviour
    4. {
    5.     public RenderTexture[] RenderTextures;
    6.     public Material material;
    7.  
    8.     void MRT (RenderTexture[] rt, Material mat, int pass = 0)
    9.     {
    10.         RenderBuffer[] rb = new RenderBuffer[rt.Length];
    11.         for(int i = 0; i < rt.Length; i++) rb[i] = rt[i].colorBuffer;
    12.         Graphics.SetRenderTarget(rb, rt[0].depthBuffer);
    13.         GL.Clear(true, true, Color.clear);
    14.         GL.PushMatrix();
    15.         GL.LoadOrtho();
    16.         mat.SetPass(pass);
    17.         GL.Begin(GL.QUADS);
    18.         GL.Vertex3(0.0f, 0.0f, 0.1f);
    19.         GL.TexCoord2(0.0f, 0.0f);
    20.         GL.Vertex3(1.0f, 0.0f, 0.1f);
    21.         GL.TexCoord2(1.0f, 0.0f);
    22.         GL.Vertex3(1.0f, 1.0f, 0.1f);
    23.         GL.TexCoord2(1.0f, 1.0f);
    24.         GL.Vertex3(0.0f, 1.0f, 0.1f);
    25.         GL.TexCoord2(0.0f, 1.0f);
    26.         GL.End();
    27.         GL.PopMatrix();
    28.     }
    29.  
    30.     void Update ()
    31.     {
    32.         MRT(RenderTextures,material,0);
    33.     }
    34. }
    Code (CSharp):
    1. //https://github.com/przemyslawzaworski/Unity3D-CG-programming
    2.  
    3. Shader "MRT"
    4. {
    5.     SubShader
    6.     {
    7.         Pass
    8.         {
    9.             ZTest Always Cull Off ZWrite Off        
    10.             CGPROGRAM
    11.             #pragma vertex vertex_shader
    12.             #pragma fragment pixel_shader
    13.             #pragma exclude_renderers nomrt
    14.             #pragma target 3.0
    15.  
    16.             struct structureVS
    17.             {
    18.                 float4 vertex : SV_POSITION;
    19.                 float2 uv : TEXCOORD0;
    20.             };
    21.  
    22.             struct structurePS
    23.             {
    24.                 float4 target00 : SV_Target0;
    25.                 float4 target01 : SV_Target1;
    26.                 float4 target02 : SV_Target2;
    27.                 float4 target03 : SV_Target3;
    28.             };
    29.  
    30.             structureVS vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0)
    31.             {
    32.                 structureVS vs;
    33.                 vs.vertex = UnityObjectToClipPos( vertex );          
    34.                 vs.uv = uv;
    35.                 return vs;
    36.             }
    37.  
    38.             structurePS pixel_shader (structureVS vs)
    39.             {
    40.                 structurePS ps;
    41.                 ps.target00 = float4(1,0,0,1);
    42.                 ps.target01 = float4(0,1,0,1);
    43.                 ps.target02 = float4(0,0,1,1);
    44.                 ps.target03 = float4(1,1,1,1);
    45.                 return ps;
    46.             }
    47.             ENDCG
    48.         }
    49.     }
    50.     FallBack "Diffuse"
    51. }
     
    SunnySunshine and Lipoly like this.
  3. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    Is this methodology compatible with URP?
     
    Zenchuck and DotusX like this.