Search Unity

MRT example?

Discussion in 'Shaders' started by Nikolai-Sander, Sep 21, 2012.

  1. Nikolai-Sander

    Nikolai-Sander

    Joined:
    Feb 2, 2011
    Posts:
    12
    Does anyone have a simple example of MRT's with ImageEffects?

    I really can't seem to get it to work :(

    I tried it with SetRenderBuffer and Blit:

    Code (csharp):
    1.     void OnRenderImage (RenderTexture source, RenderTexture destination)
    2.     {      
    3.        
    4.         RenderTexture meanCol = RenderTexture.GetTemporary(source.width, source.height);
    5.         RenderTexture meanPos = RenderTexture.GetTemporary(source.width, source.height);
    6.        
    7.         //MeanShiftMaterial().SetTexture("_MainTex",source);       
    8.        
    9.         RenderBuffer[] buffers = new RenderBuffer[2];
    10.         buffers[0] = meanCol.colorBuffer;
    11.         buffers[1] = meanPos.colorBuffer;
    12.        
    13.         Graphics.SetRenderTarget(buffers,meanCol.depthBuffer);     
    14.         Graphics.Blit(source,meanCol,MeanShiftMaterial(),0);
    15. }
    16.  
    Doesn't work.

    I tried it with Graphics.DrawTexture, nothing.

    Any help hugely appreciated!
     
  2. epifire

    epifire

    Joined:
    Dec 24, 2012
    Posts:
    1
    You can't use the Graphics.Blit since it only set one RenderTexture internally.
    When using MRT, you have to render the full screen quad manually for Blit.
    Here's a simple example:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. [RequireComponent(typeof(Camera))]
    6.  
    7. public class TestMRT : PostEffectsBase
    8. {
    9.     private Material testMRTMaterial = null;
    10.     private RenderTexture[] mrtTex = new RenderTexture[2];
    11.     private RenderBuffer[] mrtRB = new RenderBuffer[2];
    12.  
    13.     void Start ()
    14.     {
    15.         mrtTex[0] = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
    16.         mrtTex[1] = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
    17.         mrtRB[0] = mrtTex[0].colorBuffer;
    18.         mrtRB[1] = mrtTex[1].colorBuffer;
    19.     }
    20.  
    21.     public override bool CheckResources()
    22.     {
    23.         CheckSupport(true);
    24.  
    25.         testMRTMaterial = CheckShaderAndCreateMaterial(Shader.Find("Custom/TestMRT"), testMRTMaterial);
    26.  
    27.         if (!isSupported)
    28.             ReportAutoDisable();
    29.         return isSupported;
    30.     }
    31.  
    32.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    33.     {
    34.         if (CheckResources() == false)
    35.         {
    36.             Graphics.Blit(source, destination);
    37.             return;
    38.         }
    39.  
    40.         RenderTexture oldRT = RenderTexture.active;
    41.  
    42.         Graphics.SetRenderTarget(mrtRB, mrtTex[0].depthBuffer);
    43.  
    44.         GL.Clear(false, true, Color.clear);
    45.  
    46.         GL.PushMatrix();
    47.         GL.LoadOrtho();
    48.  
    49.         testMRTMaterial.SetPass(0);     //Pass 0 outputs 2 render textures.
    50.  
    51.         //Render the full screen quad manually.
    52.         GL.Begin(GL.QUADS);
    53.         GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(0.0f, 0.0f, 0.1f);
    54.         GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(1.0f, 0.0f, 0.1f);
    55.         GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(1.0f, 1.0f, 0.1f);
    56.         GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(0.0f, 1.0f, 0.1f);
    57.         GL.End();
    58.  
    59.         GL.PopMatrix();
    60.  
    61.         RenderTexture.active = oldRT;
    62.  
    63.         //Show the result
    64.         testMRTMaterial.SetTexture("_Tex0", mrtTex[0]);
    65.         testMRTMaterial.SetTexture("_Tex1", mrtTex[1]);
    66.         Graphics.Blit(source, destination, testMRTMaterial, 1);
    67.     }
    68. }
    And the shader code:

    Code (csharp):
    1. Shader "Custom/TestMRT" {
    2.     Properties {
    3.         _MainTex ("", 2D) = "" {}
    4.     }
    5.    
    6.     CGINCLUDE
    7.    
    8.     #include "UnityCG.cginc"
    9.      
    10.     struct v2f {
    11.         float4 pos : POSITION;
    12.         float2 uv : TEXCOORD0;
    13.     };
    14.    
    15.     struct PixelOutput {
    16.         float4 col0 : COLOR0;
    17.         float4 col1 : COLOR1;
    18.     };
    19.    
    20.     sampler2D _MainTex;
    21.     sampler2D _Tex0;
    22.     sampler2D _Tex1;
    23.    
    24.     v2f vert( appdata_img v )
    25.     {
    26.         v2f o;
    27.         o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    28.         o.uv = v.texcoord.xy;
    29.         return o;
    30.     }
    31.    
    32.     PixelOutput fragTestMRT(v2f pixelData)
    33.     {
    34.         PixelOutput o;
    35.         o.col0 = float4(1.0f, 0.0f, 0.0f, 1.0f);
    36.         o.col1 = float4(0.0f, 1.0f, 0.0f, 1.0f);
    37.         return o;
    38.     }
    39.    
    40.     float4 fragShowMRT(v2f pixelData) : COLOR0
    41.     {
    42.         return tex2D(_Tex0, pixelData.uv);
    43.         //return tex2D(_Tex1, pixelData.uv);
    44.     }
    45.    
    46.     ENDCG
    47.    
    48. Subshader {
    49.  Pass {
    50.       ZTest Always Cull Off ZWrite Off
    51.       Fog { Mode off }
    52.  
    53.       CGPROGRAM
    54.       #pragma glsl
    55.       #pragma fragmentoption ARB_precision_hint_fastest
    56.       #pragma vertex vert
    57.       #pragma fragment fragTestMRT
    58.       #pragma target 3.0
    59.       ENDCG
    60.   }
    61.  Pass {
    62.       ZTest Always Cull Off ZWrite Off
    63.       Fog { Mode off }
    64.  
    65.       CGPROGRAM
    66.       #pragma glsl
    67.       #pragma fragmentoption ARB_precision_hint_fastest
    68.       #pragma vertex vert
    69.       #pragma fragment fragShowMRT
    70.       #pragma target 3.0
    71.       ENDCG
    72.   }
    73. }
    74.  
    75. Fallback off
    76.    
    77. }
     
    paraself likes this.