Search Unity

Question Dissolve Surface Shader on Texture

Discussion in 'Shaders' started by Senathorius, May 11, 2021.

  1. Senathorius

    Senathorius

    Joined:
    Oct 7, 2020
    Posts:
    2
    Hello there, I am a newbie when it comes to Shaders, so I'm trying to create a Dissolve effect that basically dissolves from nothing to showing a 2D Texture.
    The problem I have is that the material renders as black, then it dissolves to show the texture.
    This is my current Shader code:

    Code (CSharp):
    1. Shader "Dissolve" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _DissolveMap ("Dissolve Map", 2D) = "white" {}
    6.         _DissolveAmount ("DissolveAmount", Range(0,1)) = 0
    7.         _DissolveColor ("DissolveColor", Color) = (1,1,1,1)
    8.         _DissolveEmission ("DissolveEmission", Range(0,1)) = 1
    9.         _DissolveWidth ("DissolveWidth", Range(0,0.1)) = 0.05
    10.     }
    11.     SubShader {
    12.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    13.         Lighting Off
    14.         ZWrite Off
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.         Cull Off
    17.         LOD 200
    18.  
    19.          Pass{
    20.              SetTexture [_MainTex] {combine texture}
    21.         }
    22.        
    23.         CGPROGRAM
    24.         #pragma surface surf Standard shadowsforwardshadow
    25.         #pragma target 3.0
    26.  
    27.         sampler2D _MainTex;
    28.         sampler2D _DissolveMap;
    29.  
    30.         struct Input {
    31.             float2 uv_MainTex;
    32.             float2 uv_DissolveMap;
    33.         };
    34.  
    35.         half _DissolveAmount;
    36.         half _DissolveEmission;
    37.         half _DissolveWidth;
    38.         fixed4 _Color;
    39.         fixed4 _DissolveColor;
    40.  
    41.         void surf (Input IN, inout SurfaceOutputStandard o) {
    42.  
    43.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;          
    44.             fixed4 mask = tex2D (_DissolveMap, IN.uv_DissolveMap);
    45.  
    46.             if(mask.r < _DissolveAmount)
    47.                 discard;
    48.  
    49.             o.Albedo = c.rgb;
    50.  
    51.             if(mask.r < _DissolveAmount + _DissolveWidth) {
    52.                 o.Albedo = _DissolveColor;
    53.                 o.Emission = _DissolveColor * _DissolveEmission;
    54.             }
    55.  
    56.             o.Albedo = c.rgb;
    57.             o.Alpha = _DissolveAmount;
    58.  
    59.            
    60.            
    61.         }
    62.     }
    63.     FallBack "Diffuse"
    64. }
    65.