Can't get Image Effect Transparency to work
I'm trying to write a image effect to transition one camera into the other but I'm stuck on this last problem for a while now and tried googling for solutions and reading up about Blending and Colormasks but so far came up dry. I hope that someone here can help me on my way.
The problem is that instead of Camera1 transitioning to transparent to show Camera2 underneath it, it transitions to black. My guess is that I'm doing something wrong with either the stencil buffer not setting pixels to transparent but to black or overlooking something in the shader setting alpha channel. If I applied the shader to a UI/Image the transparency did work though.
The setup is as followed:
Camera1: Depth Only, Ortho, Depth: 1
Camera 2: Depth Only, Ortho, Depth: 0
This is the code on Camera1 for Blitting:
void OnRenderImage (RenderTexture src, RenderTexture dst)
{
if (TransitionMaterial != null) {
RenderTexture rnd = RenderTexture.GetTemporary (src.width, src.height, 0, RenderTextureFormat.ARGB32);
Graphics.Blit (rnd, dst, TransitionMaterial);
//RenderTexture.ReleaseTemporary (rnd);
}
}
And I'm blitting this shader to my 1st Camera:
Shader "Custom/Masking"
{
Properties
{
_MainTex ("Main Texture", 2D) = "white" {}
_MaskTex ("Mask Texture", 2D) = "white" {}
_AmountSlider ("Mask Amount", Range (0,1)) = 0.4
_Color ("Tint", Color) = (1,1,1,1)
_Invert ("Invert", Float) = 0
_ColorMask ("Color Mask", Float) = 15
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
ZWrite Off
Cull Off
Lighting Off
ZTest Less
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "UnityUI.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
half2 screenpos : TEXCOORD2;
};
fixed4 _Color;
fixed4 _TextureSampleAdd;
bool _UseClipRect;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.worldPosition = IN.vertex;
OUT.vertex = mul(UNITY_MATRIX_MVP, OUT.worldPosition);
OUT.texcoord = IN.texcoord;
OUT.screenpos = ComputeScreenPos(OUT.vertex);
#ifdef UNITY_HALF_TEXEL_OFFSET
OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
#endif
OUT.color = IN.color * _Color;
return OUT;
}
sampler2D _MainTex;
sampler2D _MaskTex;
float _AmountSlider;
float _Invert;
fixed4 frag(v2f IN) : SV_Target
{
float4 color = tex2D (_MainTex, IN.texcoord );
float alpha = (abs(_Invert-(tex2D(_MaskTex, IN.screenpos).r)) - _AmountSlider) + 1;
float4 alpha_color = float4(color.r, color.g, color.b, smoothstep(alpha, 1, 0) * color.a);
return alpha_color;
}
ENDCG
}
}
}