Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

AlphaBlend Shader w/ Mask

Discussion in 'Shaders' started by Ralkarin_, May 15, 2014.

  1. Ralkarin_

    Ralkarin_

    Joined:
    Nov 15, 2010
    Posts:
    17
    Hi all! First time in the ShaderLab forum, so bear with me please, and thank you in advance for any help!

    Goal: I need to make a shader that supports 2 textures with an alpha cutoff to fade the texture over time. The shader should use the Mask texture's alpha value to determine which pixels should be culled based on a cutoff threshold, BUT I do not want the alpha value from the mask applied to the Main Texture, which so far has eliminated a simple "AlphaTest" check for me.

    Main Texture: RGBA (supporting alpha blending)
    Mask/Cutout Texture: Alpha channel for cutoff mask
    Cutoff Threshold: 0 - 1 for alpha testing/culling.

    I want to be able to fade parts of the texture based on the Mask over time, based on the cutoff threshold, but otherwise use the main texture with all of it's anti-aliased transparent goodness.

    So, now that the goal is out of the way. I've got this working on my PC based desktop, but I can't get it to work on my Android tablet. It shows me a solid pink texture. (the shader is referenced directly by the material, I'm not using Shader.Find()). I cannot see any compile time warnings or errors in my Editor log (unless I'm missing it)... so my thought is that I'm using something that is not supported by OpenGL on the Android device.

    I've tried several shaders found on the internet, but none of them seem to be doing what I'm looking for.

    So, I have 2 questions:
    1) Is there a more trivial way to accomplish what I'm looking for and I'm over complicating this because I'm a Shader Programming noob?
    2) What feature in the shader below am I using that would make it incompatible with Android/iOS mobile devices?

    Regards,
    Josh

    Here's the shader (free for anyone else to use):

    Code (csharp):
    1. Shader "MyTransparentCutoutShader" {
    2. Properties {
    3.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    4.     _CutTex ("Cutout (A)", 2D) = "white" {}
    5.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    6. }
    7.  
    8. SubShader {
    9.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.     LOD 200
    11.  
    12.     Lighting Off
    13.     ZWrite Off
    14.     Blend SrcAlpha OneMinusSrcAlpha
    15.    
    16.      Pass {
    17.         CGPROGRAM
    18.         #pragma vertex vert
    19.         #pragma fragment frag
    20.        
    21.         #include "UnityCG.cginc"
    22.        
    23.         struct appdata_t {
    24.             float4 vertex : POSITION;
    25.             float2 texcoord : TEXCOORD0;
    26.         };
    27.                
    28.         struct v2f {
    29.             float4 vertex : SV_POSITION;
    30.             half2 texcoord : TEXCOORD0;
    31.         };
    32.        
    33.         sampler2D _MainTex;
    34.         sampler2D _CutTex;
    35.         float _Cutoff;
    36.         float4 _MainTex_ST;
    37.          
    38.         float4 frag(float2 uv_MainTex : TEXCOORD) : COLOR
    39.         {
    40.             fixed4 c = tex2D(_MainTex, uv_MainTex);
    41.             float ca = tex2D(_CutTex, uv_MainTex).a;
    42.            
    43.             if (ca < _Cutoff)
    44.               discard;
    45.          
    46.             return c;
    47.         }
    48.                    
    49.         v2f vert (appdata_t v)
    50.         {
    51.             v2f o;
    52.             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    53.             o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    54.             return o;
    55.         }
    56.        
    57.         ENDCG
    58.         }
    59.     }
    60. }
     
  2. Ralkarin_

    Ralkarin_

    Joined:
    Nov 15, 2010
    Posts:
    17
    Based on some very preliminary evidence, the change that seems to have fixed this on the Android running OpenGL is changing the semantic name attached to the fragment shader parameter from TEXCOORD to TEXCOORD0. No more pink textures, with the desired affect!

    Will post more updates if any are necessary. Thanks for hanging out with me. :-D