- Home /
Where to Graphics.Blit()?
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?
For reference, it's not that you're getting "dull" colors. It's that the objects in the render have transparency applied to them. What are the attributes of the material/shader being used during the Blit() call?
Compared to the expected result, the pipes do look dull to me :) I don't do any blending, and set alpha to 1 in the shader (I added the shader to the question).
On first impression, that seems like it ought to be fine. Just for reference, does it still give the same result if you force _Alpha to be 1? (I ask simply because your second image looks like ~50% opacity layering)
// Example: comment out the line...
color.a = color.a * _Alpha;
Answer by g-augview · Apr 12, 2021 at 10:17 PM
I fixed this by modifying the pipe's transparent shader: I replaced
Blend SrcAlpha OneMinuseSrcAlpha
with
Blend One OneMinusSrcAlpha
Your answer
Follow this Question
Related Questions
How Blit internally works? 2 Answers
Change the camera RenderTexture before rendering it to screen? 1 Answer
Rendering a screen aligned quad with Graphics.blit - not working? 0 Answers
Using Graphics.Blit with the same source and destination 1 Answer
dynamic hole in layer / texture / camera 0 Answers