-
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];
- {
- }
- 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(渲染到多個目標)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.