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. }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值