Search Unity

Simplifying Shader for Texture Transition with Alpha

Discussion in 'Shaders' started by Doddler, May 31, 2013.

  1. Doddler

    Doddler

    Joined:
    Jul 12, 2011
    Posts:
    269
    So I'm learning how to do shaders, and it's a little tricky because there's not a lot of documentation. What I want to do is perform a transition on an unlit texture from completely visible to invisible, transitioning between the two based on a separate mask texture. I've actually got a working shader, but at the moment it requires supporting pixel shader 2.0. Unfortunately, our target isn't guaranteed to support shader 2.0, so I'm trying to figure out how to scale down the shader without losing the primary functionality.

    You can view what the shader does here: http://doddlercon.com/unity/transitiontest/transitiontest.html

    The following is how it looks in the editor. The cutoff is the current state of the transition, 0 being image is fully visible, 1 being image is fully hidden. Cutoff range represents how smooth the transition is. 0 acts basically as an alpha cutoff, and 1 would offer the smoothest possible transition. The mask is a greyscale image, but it could be changed to an alpha texture if necessary. Here's how it looks in editor:

    Cutoff is at 0:
    $1a7e6be587.jpg

    Cutoff is at 0.5:
    $f4c28a650a.jpg

    Cutoff is at 1:
    $0d1c6c9174.jpg

    Here's the current code for the shader. It's a bit of a mess since making it go unlit was kind of tricky.

    Code (csharp):
    1. Shader "Custom/TransitionShader" {
    2.     Properties {
    3.         _Cutoff ("Cutoff", Range (0,1)) = 0.5
    4.         _CutoffRange ("Cutoff Range", Range (0,1)) = 0.1
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _AlphaTex ("Mask", 2D) = "white" {}
    7.     }
    8.     SubShader {
    9.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.         Blend SrcAlpha OneMinusSrcAlpha
    11.         LOD 200
    12.         Lighting OFF
    13.        
    14.         CGPROGRAM
    15.         #pragma surface surf Unlit alpha noambient novertexlights nodirlightmap nolightmap noforwardadd
    16.  
    17.         //Is this really required to make an unlit surface...?
    18.         half4 LightingUnlit (SurfaceOutput s, half3 dir, half atten) {
    19.             fixed4 c;
    20.             c.rgb = s.Albedo;
    21.             c.a = s.Alpha;
    22.             return c;
    23.         }
    24.  
    25.         sampler2D _MainTex;
    26.         sampler2D _AlphaTex;
    27.         half _Cutoff;
    28.         half _CutoffRange;
    29.  
    30.         struct Input {
    31.             float2 uv_MainTex;
    32.         };
    33.  
    34.         void surf (Input IN, inout SurfaceOutput o) {
    35.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    36.             half d = tex2D (_AlphaTex, IN.uv_MainTex).r;
    37.             o.Albedo = c.rgb;
    38.  
    39.             //Fancy math to combine the alpha with the cutoff  cutoff range.
    40.             half r = _Cutoff * (1 + _CutoffRange * 2) - _CutoffRange;
    41.             o.Alpha = (d - r) * (1 / (_CutoffRange));
    42.         }
    43.         ENDCG
    44.     }
    45.     Fallback "Diffuse"
    46. }
    47.  
    I have no idea how that shader code stacks up. As I said, it does work, but it has higher shader requirements than I'd want it to have for my project. Does anyone know how I could simplify that into something using a fixed function shader?
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Code (csharp):
    1.  
    2. Shader "Custom/MaskedPhoto"
    3. {
    4.     Properties
    5.     {
    6.             _MainTex ("Base (RGB)", 2D) = "white" {}
    7.             _MaskTex ("Mask (A)", 2D) = "white" {}
    8.             _Opacity ("Opacity", Color)= (0, 0, 0, 1)
    9.     }
    10.     SubShader
    11.     {
    12.     Tags { "Queue" = "Transparent" }
    13.     ZWrite Off
    14.     Pass
    15.         {
    16.             Blend SrcAlpha OneMinusSrcAlpha
    17.             SetTexture [_MainTex] { combine texture }
    18.             SetTexture [_MaskTex]
    19.             {
    20.                 ConstantColor [_Opacity]
    21.                 combine previous * texture, texture * constant
    22.             }
    23.         }
    24.  
    25.     }
    26. }
    27.  
    ------------------
    ------------------
    Sorry, this not work properly.
     
    Last edited: Jun 1, 2013