Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Multiple Images When Alpha Blending Masking

Discussion in 'Shaders' started by emmy21, May 11, 2024.

  1. emmy21

    emmy21

    Joined:
    Nov 16, 2023
    Posts:
    19
    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?

    Code (CSharp):
    1. Shader "Unlit/MaskShader"
    2. {
    3.     Properties
    4.     {
    5.         _ForegroundTex ("Foreground", 2D) = "white" {}
    6.         _MaskTex ("Mask", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Transparent" "Queue"="Transparent" }
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.         LOD 100
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float2 uv : TEXCOORD0;
    25.             };
    26.  
    27.             struct v2f
    28.             {
    29.                 float2 uv : TEXCOORD0;
    30.                 float4 vertex : SV_POSITION;
    31.             };
    32.  
    33.             sampler2D _ForegroundTex;
    34.             sampler2D _MaskTex;
    35.  
    36.             v2f vert (appdata v)
    37.             {
    38.                 v2f o;
    39.                 o.vertex = UnityObjectToClipPos(v.vertex);
    40.                 o.uv = v.uv;
    41.                 return o;
    42.             }
    43.  
    44.             fixed4 frag (v2f i) : SV_Target
    45.             {
    46.                 // Sample the grayscale value from the mask texture
    47.                 fixed4 maskColor = tex2D(_MaskTex, i.uv);
    48.  
    49.                 // Sample the color from the foreground texture
    50.                 fixed4 color = tex2D(_ForegroundTex, i.uv);
    51.  
    52.                 // Blend the foreground color with the background color based on the mask alpha
    53.                 // color = lerp(fixed4(0, 1, 0, 0), color, maskColor.r); // i see a lot of weird visuals
    54.                 // color = lerp(fixed4(0, 1, 0, 0.5), color, maskColor.r); // i see like 5 instances with weird visuals
    55.                 // color = lerp(fixed4(0, 1, 0, 0.99), color, maskColor.r); // i see the very beginnings of the second instance
    56.                color = lerp(fixed4(0, 1, 0, 1), color, maskColor.r); // i see 1 instance and the background is green
    57.  
    58.                 return color;
    59.             }
    60.             ENDCG
    61.         }
    62.     }
    63. }
     
  2. emmy21

    emmy21

    Joined:
    Nov 16, 2023
    Posts:
    19
    Ive also tried this which i thought was the obvious answer:
    color.a = maskColor.r; // i see like 5 instances with weird visuals
    and this which i found online:
    color.a *= maskColor.r; // very similar to the one above