I have 2 cameras, one for a video background, one rendering objects. Both have a target render texture. I want to use a material as a post-processing effect to add a global transparency to the objects (each may already have an individual transparency), so I want to blit the result of a material to screen. However when I call graphics.blit in the built-in render pipeline (in OnRenderImage) I don’t get the same result as in the material preview (see pictures).
Expected result (material preview) by setting the material’s main texture:
Graphics.Blit in OnRenderImage by blitting in OnImageRender:
private void OnPreRender()
{
material.SetFloat(variableName, Multiplier);
material.SetTexture(backgroundTextName, backgroundRt);
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(source, destination, material);
}
The shader:
Shader "PostProcessing/AlphaBlend"
{
Properties
{
_MainTex("Main Texture", 2D) = "white" {}
_BackgroundTex("Background Texture", 2D) = "white" {}
_Alpha("Alpha Multiplier", Range(0,1)) = 0.5
}
SubShader{
Tags{ "PreviewType" = "Plane" }
Pass {
ZWrite Off
Lighting Off
ColorMask RGBA
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _BackgroundTex;
float _Alpha;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
}
float4 frag(v2f_img i) : COLOR
{
float4 color = tex2D(_MainTex, i.uv);
color.a = color.a * _Alpha;
float4 backgroundColor = tex2D(_BackgroundTex, i.uv);
float4 output = (color.a * color + (1-color.a) * backgroundColor);
output.a = 1;
return output;
}
ENDCG
}
}
}
As you can see the pipe colours are very dull in the second case.
I have tried to blit to a different render texture, then blit that render texture in Update, LateUpdate, but in those cases I get a black screen.
Does someone understand why blitting in OnImageRender results in dull colors (more transparency than I’d expect)?
Where/how can I use my material since it does what I want given the preview?