-
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:-
{
-
-
-
//MeanShiftMaterial().SetTexture("_MainTex",source);
-
-
RenderBuffer [ ] buffers = new RenderBuffer [ 2 ];
-
-
}
Doesn't work.
I tried it with Graphics.DrawTexture, nothing.
Any help hugely appreciated! -
-
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:-
using UnityEngine;
-
using System. Collections;
-
-
[ExecuteInEditMode ]
-
-
public class TestMRT : PostEffectsBase
-
{
-
private RenderBuffer [ ] mrtRB = new RenderBuffer [ 2 ];
-
-
{
-
mrtTex [ 0 ] = new RenderTexture ( Screen. width, Screen. height, 24, RenderTextureFormat. ARGB32 );
-
mrtTex [ 1 ] = new RenderTexture ( Screen. width, Screen. height, 24, RenderTextureFormat. ARGB32 );
-
}
-
-
public override bool CheckResources ( )
-
{
-
CheckSupport ( true );
-
-
-
ReportAutoDisable ( );
-
return isSupported;
-
}
-
-
{
-
if (CheckResources ( ) == false )
-
{
-
return;
-
}
-
-
-
-
-
-
-
//Render the full screen quad manually.
-
-
-
RenderTexture. active = oldRT;
-
-
//Show the result
-
}
-
}
And the shader code:
Code:-
Properties {
-
_MainTex ( "", 2D ) = "" { }
-
}
-
-
CGINCLUDE
-
-
#include "UnityCG.cginc"
-
-
struct v2f {
-
float4 pos : POSITION;
-
float2 uv : TEXCOORD0;
-
};
-
-
struct PixelOutput {
-
float4 col0 : COLOR0;
-
float4 col1 : COLOR1;
-
};
-
-
sampler2D _MainTex;
-
sampler2D _Tex0;
-
sampler2D _Tex1;
-
-
v2f vert ( appdata_img v )
-
{
-
v2f o;
-
o. pos = mul (UNITY_MATRIX_MVP, v. vertex );
-
o. uv = v. texcoord. xy;
-
return o;
-
}
-
-
PixelOutput fragTestMRT (v2f pixelData )
-
{
-
PixelOutput o;
-
o. col0 = float4 ( 1.0f, 0.0f, 0.0f, 1.0f );
-
o. col1 = float4 ( 0.0f, 1.0f, 0.0f, 1.0f );
-
return o;
-
}
-
-
float4 fragShowMRT (v2f pixelData ) : COLOR0
-
{
-
return tex2D (_Tex0, pixelData. uv );
-
//return tex2D(_Tex1, pixelData.uv);
-
}
-
-
ENDCG
-
-
Subshader {
-
Pass {
-
ZTest Always Cull Off ZWrite Off
-
Fog { Mode off }
-
-
CGPROGRAM
-
#pragma glsl
-
#pragma fragmentoption ARB_precision_hint_fastest
-
#pragma vertex vert
-
#pragma fragment fragTestMRT
-
#pragma target 3.0
-
ENDCG
-
}
-
Pass {
-
ZTest Always Cull Off ZWrite Off
-
Fog { Mode off }
-
-
CGPROGRAM
-
#pragma glsl
-
#pragma fragmentoption ARB_precision_hint_fastest
-
#pragma vertex vert
-
#pragma fragment fragShowMRT
-
#pragma target 3.0
-
ENDCG
-
}
-
}
-
-
Fallback off
-
-
}
unity MRT(渲染到多个目标)
最新推荐文章于 2025-03-12 16:14:05 发布