unity MRT(渲染到多個目標)

  1. Unity--延遲渲染
    延遲渲染就是將相機視野中的物體渲染到屏幕之前對這些物體數據進行處理。這些數據可以是法線、位置等。
    unity中我們可以在OnPreRender() 函數中去設置相機渲染方式,我們可以看一下Graphics.SetRenderTarget(....)函數的使用。setRenderTarget後,我們可以在shader中取出我們所需要的法線、位置等信息。這樣在OnRenderImage()函數中我們就可以對這些數據進行處理或直接顯示在屏幕上。

    MRT example?

    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:  
    1.     {      
    2.        
    3.         RenderTexture meanCol = RenderTexture.GetTemporary(source.width, source.height);
    4.         RenderTexture meanPos = RenderTexture.GetTemporary(source.width, source.height);
    5.        
    6.         //MeanShiftMaterial().SetTexture("_MainTex",source);       
    7.        
    8.         RenderBuffer[] buffers = new RenderBuffer[2];
    9.         buffers[0] = meanCol.colorBuffer;
    10.         buffers[1] = meanPos.colorBuffer;
    11.        
    12.         Graphics.SetRenderTarget(buffers,meanCol.depthBuffer);     
    13.         Graphics.Blit(source,meanCol,MeanShiftMaterial(),0);
    14. }

    Doesn't work. 

    I tried it with Graphics.DrawTexture, nothing.

    Any help hugely appreciated!
  2. 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:  
    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.     {
    33.         if (CheckResources() == false)
    34.         {
    35.             Graphics.Blit(source, destination);
    36.             return;
    37.         }
    38.  
    39.         RenderTexture oldRT = RenderTexture.active;
    40.  
    41.         Graphics.SetRenderTarget(mrtRB, mrtTex[0].depthBuffer);
    42.  
    43.         GL.Clear(false, true, Color.clear);
    44.  
    45.         GL.PushMatrix();
    46.         GL.LoadOrtho();
    47.  
    48.         testMRTMaterial.SetPass(0);     //Pass 0 outputs 2 render textures.
    49.  
    50.         //Render the full screen quad manually.
    51.         GL.Begin(GL.QUADS);
    52.         GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(0.0f, 0.0f, 0.1f);
    53.         GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(1.0f, 0.0f, 0.1f);
    54.         GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(1.0f, 1.0f, 0.1f);
    55.         GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(0.0f, 1.0f, 0.1f);
    56.         GL.End();
    57.  
    58.         GL.PopMatrix();
    59.  
    60.         RenderTexture.active = oldRT;
    61.  
    62.         //Show the result
    63.         testMRTMaterial.SetTexture("_Tex0", mrtTex[0]);
    64.         testMRTMaterial.SetTexture("_Tex1", mrtTex[1]);
    65.         Graphics.Blit(source, destination, testMRTMaterial, 1);
    66.     }
    67. }

    And the shader code:

    Code:  
    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. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章