Im trying to make shader for applying a mask to a texture. The mask is greyscale where black is transparent and white is viewable. Ive tried so many things, but this is the closest ive gotten. Why is it that whenever i try to set the alpha value to anything but 1 I keep seeing multiple instances of the image in the output texture along with other weird visuals?
Shader "Unlit/MaskShader"
{
Properties
{
_ForegroundTex ("Foreground", 2D) = "white" {}
_MaskTex ("Mask", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _ForegroundTex;
sampler2D _MaskTex;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// Sample the grayscale value from the mask texture
fixed4 maskColor = tex2D(_MaskTex, i.uv);
// Sample the color from the foreground texture
fixed4 color = tex2D(_ForegroundTex, i.uv);
// Blend the foreground color with the background color based on the mask alpha
// color = lerp(fixed4(0, 1, 0, 0), color, maskColor.r); // i see a lot of weird visuals
// color = lerp(fixed4(0, 1, 0, 0.5), color, maskColor.r); // i see like 5 instances with weird visuals
// color = lerp(fixed4(0, 1, 0, 0.99), color, maskColor.r); // i see the very beginnings of the second instance
color = lerp(fixed4(0, 1, 0, 1), color, maskColor.r); // i see 1 instance and the background is green
return color;
}
ENDCG
}
}
}