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): void OnRenderImage (RenderTexture source, RenderTexture destination) { RenderTexture meanCol = RenderTexture.GetTemporary(source.width, source.height); RenderTexture meanPos = RenderTexture.GetTemporary(source.width, source.height); //MeanShiftMaterial().SetTexture("_MainTex",source); RenderBuffer[] buffers = new RenderBuffer[2]; buffers[0] = meanCol.colorBuffer; buffers[1] = meanPos.colorBuffer; Graphics.SetRenderTarget(buffers,meanCol.depthBuffer); Graphics.Blit(source,meanCol,MeanShiftMaterial(),0); } Doesn't work. I tried it with Graphics.DrawTexture, nothing. Any help hugely appreciated!
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): using UnityEngine; using System.Collections; [ExecuteInEditMode] [RequireComponent(typeof(Camera))] public class TestMRT : PostEffectsBase { private Material testMRTMaterial = null; private RenderTexture[] mrtTex = new RenderTexture[2]; private RenderBuffer[] mrtRB = new RenderBuffer[2]; void Start () { mrtTex[0] = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32); mrtTex[1] = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32); mrtRB[0] = mrtTex[0].colorBuffer; mrtRB[1] = mrtTex[1].colorBuffer; } public override bool CheckResources() { CheckSupport(true); testMRTMaterial = CheckShaderAndCreateMaterial(Shader.Find("Custom/TestMRT"), testMRTMaterial); if (!isSupported) ReportAutoDisable(); return isSupported; } void OnRenderImage(RenderTexture source, RenderTexture destination) { if (CheckResources() == false) { Graphics.Blit(source, destination); return; } RenderTexture oldRT = RenderTexture.active; Graphics.SetRenderTarget(mrtRB, mrtTex[0].depthBuffer); GL.Clear(false, true, Color.clear); GL.PushMatrix(); GL.LoadOrtho(); testMRTMaterial.SetPass(0); //Pass 0 outputs 2 render textures. //Render the full screen quad manually. GL.Begin(GL.QUADS); GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(0.0f, 0.0f, 0.1f); GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(1.0f, 0.0f, 0.1f); GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(1.0f, 1.0f, 0.1f); GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(0.0f, 1.0f, 0.1f); GL.End(); GL.PopMatrix(); RenderTexture.active = oldRT; //Show the result testMRTMaterial.SetTexture("_Tex0", mrtTex[0]); testMRTMaterial.SetTexture("_Tex1", mrtTex[1]); Graphics.Blit(source, destination, testMRTMaterial, 1); } } And the shader code: Code (csharp): Shader "Custom/TestMRT" { 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 }