Search Unity

Dissolve Shader with Alpha channel

Discussion in 'Shaders' started by Aram-Azhari, May 21, 2012.

  1. Aram-Azhari

    Aram-Azhari

    Joined:
    Nov 18, 2009
    Posts:
    142
    Hi everyone.

    I'm trying to use a texture with alpha on the dissolve shader from the wiki community Dissolve With Texture .

    Here's the code I've modified:

    Code (csharp):
    1.  
    2. // simple "dissolving" shader by genericuser (radware.wordpress.com)
    3. // clips materials, using an image as guidance.
    4. // use clouds or random noise as the slice guide for best results.
    5.   Shader "Custom Shaders/Dissolving" {
    6.     Properties {
    7.       _MainTex ("Texture (RGB)", 2D) = "white" {}
    8.       _AlphaTex ("Alpha (A)", 2D) = "white" {}
    9.       _BumpMap ("Normalmap", 2D) = "bump" {}
    10.       _SliceGuide ("Slice Guide (RGB)", 2D) = "white" {}
    11.       _SliceAmount ("Slice Amount", Range(0.0, 1.0)) = 0.5
    12.     }
    13.     SubShader {
    14.       Tags { "RenderType" = "TransparentCutout" }
    15.       Cull Off
    16.       CGPROGRAM
    17.       //if you're not planning on using shadows, remove "addshadow" for better performance
    18.       #pragma surface surf Lambert addshadow
    19.       struct Input {
    20.           float2 uv_MainTex;
    21.           float2 uv_BumpMap;
    22.           float2 uv_AlphaTex;
    23.           float2 uv_SliceGuide;
    24.           float _SliceAmount;
    25.       };
    26.       sampler2D _MainTex;
    27.       sampler2D _SliceGuide;
    28.       sampler2D _BumpMap;
    29.       sampler2D _AlphaTex;
    30.       float _SliceAmount;
    31.       void surf (Input IN, inout SurfaceOutput o) {
    32.           clip(tex2D (_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
    33.           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    34.          [B] o.Alpha = tex2D (_AlphaTex, IN.uv_AlphaTex).a;[/B]
    35.           o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    36.       }
    37.       ENDCG
    38.     }
    39.     Fallback "Diffuse"
    40.   }
    41.  
    But no matter what texture I pass as Alpha, I don't get the alpha channel applied.

    I'm very new to shaders.

    What should I change to make this work?

    Thank you.
     
  2. Banksy

    Banksy

    Joined:
    Mar 31, 2013
    Posts:
    376
    Did you find a solution to your problem ?
     
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    It works for me. Perhaps you are expecting transparency from the alpha? This shader only writes alpha to the frame buffer alpha channel, like all opaque built-in shaders.
     
  4. mr.pitz

    mr.pitz

    Joined:
    Oct 16, 2012
    Posts:
    1
    Hey, I know it is actually a very old thread, but just to answer the question: add the line Blend SrcAlpha OneMinusSrcAlpha in the SubShader and it should work.

    Cheers
     
  5. Doomgriever

    Doomgriever

    Joined:
    May 19, 2013
    Posts:
    5
    After fiddling around with this myself (total newbie when it comes to shaders) I managed to get alpha cutout (as transparency) to work with the Dissolve shader.

    Code (CSharp):
    1.   Shader "Custom Shaders/DissolvingAlpha" {
    2.     Properties {
    3.       _MainTex ("Texture (RGB)", 2D) = "white" {}
    4.       _SliceGuide ("Slice Guide (RGB)", 2D) = "white" {}
    5.       _SliceAmount ("Slice Amount", Range(0.001, 1.0)) = 0.001
    6.     }
    7.     SubShader {
    8.       Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "IgnoreProjector"="True" }
    9.       Cull Off
    10.       CGPROGRAM
    11.       //if you're not planning on using shadows, remove "addshadow" for better performance
    12.       #pragma surface surf Lambert addshadow alpha  alphatest:_AlphaCut
    13.       struct Input {
    14.           float2 uv_MainTex;
    15.           float2 uv_SliceGuide;
    16.           float _SliceAmount;
    17.       };
    18.       sampler2D _MainTex;
    19.       sampler2D _SliceGuide;
    20.       float _SliceAmount;
    21.       void surf (Input IN, inout SurfaceOutput o) {
    22.           clip(tex2D (_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
    23.           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    24.           o.Alpha = tex2D (_MainTex, IN.uv_MainTex).a - 0.1;
    25.       }
    26.       ENDCG
    27.     }
    28.     Fallback "Diffuse"
    29.   }
     
    Last edited: Sep 22, 2016