Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Adding edge glow feature to dissolve shader

Discussion in 'Shaders' started by Megaboy, Nov 11, 2013.

  1. Megaboy

    Megaboy

    Joined:
    Sep 22, 2013
    Posts:
    7
    I'm using pretty much standard shader for 2 texture dissolving effect. It works fine for me, but I really want to add some kind of emission or glowing to dissolving edges. I have read a bunch of articles, but still got no idea how to apply it in my 2 texture case, because I'm beginner in this process, indeed. I would be glad of any help, please forward me on the way. Thank you in advance.

    This looks very close to what I need:
    $dissolve.png
    $205ca3a.jpg

    Code (csharp):
    1.  Properties {
    2.    
    3.       _Color ("Main Color", Color) = (1,1,1,1)
    4.       _MainTex ("Texture (RGB)", 2D) = "white" {}
    5.       _MainBumpMap ("Normalmap", 2D) = "bump" {}
    6.       _InnerTex("Inner Texture (RGB)", 2D) = "white" {}
    7.       _InnerBumpMap ("Normalmap", 2D) = "bump" {}
    8.       _SliceGuide ("Slice Guide (RGB)", 2D) = "white" {}
    9.       _SliceAmount ("Slice Amount", Range(0.0, 1.0)) = 0.5
    10.  
    11.  
    12.     }
    13.  
    14.     SubShader {
    15.  
    16.    Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
    17.  
    18.       LOD 200
    19.      
    20.      
    21.       CGPROGRAM
    22. #pragma surface surf Lambert alpha
    23.  
    24. sampler2D _InnerTex;
    25. sampler2D _InnerBumpMap;
    26. fixed4 _Color;
    27.  
    28. struct Input {
    29.     float2 uv_InnerTex;
    30.     float2 uv_InnerBumpMap;
    31. };
    32.  
    33. void surf (Input IN, inout SurfaceOutput o) {
    34.     fixed4 c = tex2D(_InnerTex, IN.uv_InnerTex) * _Color;
    35.     o.Albedo = c.rgba;
    36.     o.Alpha = c.a;
    37.     o.Normal = UnpackNormal(tex2D(_InnerBumpMap, IN.uv_InnerBumpMap));
    38. }
    39. ENDCG
    40.  
    41.       CGPROGRAM
    42.       #pragma surface surf Lambert alpha
    43.      
    44.      
    45.       struct Input {
    46.           float2 uv_MainTex;
    47.           float2 uv_MainBumpMap;
    48.           float2 uv_SliceGuide;
    49.           float _SliceAmount;
    50.  
    51.       };
    52.       sampler2D _MainTex;
    53.       sampler2D _SliceGuide;
    54.       sampler2D _MainBumpMap;
    55.       float _SliceAmount;
    56.      
    57.                
    58.      
    59.       void surf (Input IN, inout SurfaceOutput o) {
    60.    
    61.           clip(tex2D (_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
    62.           fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    63.           o.Albedo = c.rgb;
    64.           o.Alpha = c.a;
    65.           o.Normal = UnpackNormal(tex2D(_MainBumpMap, IN.uv_MainBumpMap));
    66.                      
    67.       }
    68.       ENDCG
    69.     }
    70.     Fallback "Diffuse"
    71.  
    72.   }
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Try to change the color to the texture.
    Code (csharp):
    1.  
    2.  
    3. Shader "Custom/Burn"
    4. {
    5.         Properties
    6.         {
    7.                 _MainTex ("Base (RGB)", 2D) = "white" {}
    8.                 _Cutoff("Cutoff", Range(0, 1)) = 0
    9.                 _BurnShift("BurnShift", Range(0, 0.1)) = 0.01
    10.                 _BurnColor("Cutoff", Color) = (1, 1, 0, 1)
    11.         }
    12.         SubShader
    13.         {
    14.                 Tags { "RenderType"="Opaque" }
    15.                 LOD 200
    16.                
    17.                 CGPROGRAM
    18.                 #pragma surface surf Lambert
    19.                 //#pragma only_renderers d3d9
    20.  
    21.                 sampler2D _MainTex;
    22.                 fixed _Cutoff;
    23.                 fixed _BurnShift;
    24.                 fixed4 _BurnColor;
    25.  
    26.                 struct Input
    27.                 {
    28.                         float2 uv_MainTex;
    29.                 };
    30.  
    31.                 void surf (Input IN, inout SurfaceOutput o)
    32.                 {
    33.                         half4 c = tex2D (_MainTex, IN.uv_MainTex);
    34.  
    35.                         fixed pos = c.a - _Cutoff;
    36.                         fixed bPos = pos - _BurnShift;
    37.                         fixed isBurn = 1-step(-bPos, 0);
    38.  
    39.                         o.Albedo = lerp(c.rgb, _BurnColor.rgb, isBurn);
    40.                         o.Emission = lerp(fixed3(0,0,0), _BurnColor.rgb, isBurn);
    41.  
    42.                         fixed a = c.a-_Cutoff;
    43.                         o.Alpha = a;
    44.                         clip(a);
    45.                 }
    46.                 ENDCG
    47.         }
    48.         FallBack "Diffuse"
    49. }
    50.  
    51.  
    52.  
    P.S. Why "clip" is search in documentation hyperlink, broken parser?
     
    Last edited: Nov 11, 2013
  3. Megaboy

    Megaboy

    Joined:
    Sep 22, 2013
    Posts:
    7
    I feel embarrassed but I really can't apply it to my shader, please explain me the
    principle of signifying edges. Anyway, I appreciate any help
     
  4. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Code (csharp):
    1.  
    2. void surf (Input IN, inout SurfaceOutput o)
    3.                     {
    4.                             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    5.      
    6.                             fixed pos = c.a - _Cutoff;//Decrease the value from the alpha channel.
    7.                             fixed bPos = pos - _BurnShift;//Decrease dimmed value- it's create shifted zone of burn
    8.  
    9.                             //fixed isBurn = 1-step(-bPos, 0);//just way to do without "if"
    10.                             fixed isBurn = 0;//at start fragment is normal one
    11.                             if(bPos<0)//if they in burn zone
    12.                             {
    13.                                 isBurn=1;//mark it
    14.                             }
    15.      
    16.                             //o.Albedo = lerp(c.rgb, _BurnColor.rgb, isBurn);//just way to do without "if"
    17.                             if(isBurn)
    18.                             {
    19.                                 o.Albedo = _BurnColor.rgb;
    20.                                 o.Emission = _BurnColor.rgb;
    21.                             }
    22.                             else
    23.                             {
    24.                                 o.Albedo = c.rgb;
    25.                                 o.Emission = fixed3(0,0,0);
    26.                             }
    27.                             //o.Emission = lerp(fixed3(0,0,0), _BurnColor.rgb, isBurn);//just way to do without "if"
    28.      
    29.                             fixed a = c.a-_Cutoff;
    30.                             o.Alpha = a;
    31.                             //clip(a);//just way to do without "if"
    32.                             if(a<0)
    33.                             {
    34.                                 discard;
    35.                             }
    36.                     }
    37.  
     
  5. Megaboy

    Megaboy

    Joined:
    Sep 22, 2013
    Posts:
    7
    The way I use your code might be seem absolutely illogical, but it does nothing though. I know probably I'm doing it in a wrong way but could you please help me to make it work from this point.
    Code (csharp):
    1.  Properties {
    2.    
    3.       _Color ("Main Color", Color) = (1,1,1,1)
    4.       _MainTex ("Texture (RGB)", 2D) = "white" {}
    5.       _MainBumpMap ("Normalmap", 2D) = "bump" {}
    6.       _InnerTex("Inner Texture (RGB)", 2D) = "white" {}
    7.       _InnerBumpMap ("Normalmap", 2D) = "bump" {}
    8.       _SliceGuide ("Slice Guide (RGB)", 2D) = "white" {}
    9.       _SliceAmount ("Slice Amount", Range(0.0, 1.0)) = 0.5
    10.       _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    11.       _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    12.       _Cutoff("Cutoff", Range(0, 1)) = 0
    13.       _BurnShift("BurnShift", Range(0, 0.1)) = 0.01
    14.       _BurnColor("Cutoff", Color) = (1, 1, 0, 1)
    15.  
    16.  
    17.  
    18.     }
    19.  
    20.     SubShader {
    21.  
    22.    Tags {"Queue" = "Transparent" "RenderType"="Transparent"}
    23.  
    24.       LOD 200
    25.      
    26.      
    27.       CGPROGRAM
    28. #pragma surface surf BlinnPhong
    29.  
    30. sampler2D _InnerTex;
    31. sampler2D _InnerBumpMap;
    32. fixed4 _Color;
    33. half _Shininess;
    34.  
    35. struct Input {
    36.     float2 uv_InnerTex;
    37.     float2 uv_InnerBumpMap;
    38. };
    39.  
    40. void surf (Input IN, inout SurfaceOutput o) {
    41.     fixed4 c = tex2D(_InnerTex, IN.uv_InnerTex) * _Color;
    42.     o.Albedo = c.rgba;
    43.     o.Gloss = c.a;
    44.     o.Alpha = c.a;
    45.     o.Specular = _Shininess;
    46.     o.Normal = UnpackNormal(tex2D(_InnerBumpMap, IN.uv_InnerBumpMap));
    47. }
    48. ENDCG
    49.  
    50.  
    51.       CGPROGRAM
    52.       #pragma surface surf BlinnPhong alpha
    53.      
    54.      
    55.       struct Input {
    56.           float2 uv_MainTex;
    57.           float2 uv_MainBumpMap;
    58.           float2 uv_SliceGuide;
    59.           float _SliceAmount;
    60.  
    61.       };
    62.       sampler2D _MainTex;
    63.       sampler2D _SliceGuide;
    64.       sampler2D _MainBumpMap;
    65.       float _SliceAmount;
    66.       half _Shininess;
    67.       fixed4 _Color;
    68.        fixed _Cutoff;
    69.        fixed _BurnShift;
    70.        fixed4 _BurnColor;
    71.      
    72.                
    73.      
    74.       void surf (Input IN, inout SurfaceOutput o) {
    75.    
    76.          clip(tex2D (_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
    77.          half4 c = tex2D(_MainTex, IN.uv_MainTex);
    78.          fixed pos = c.a - _Cutoff;
    79.          fixed bPos = pos - _BurnShift;
    80.          fixed isBurn = 1-step(-bPos, 0);
    81.                        
    82.          o.Albedo = lerp(c.rgb,_BurnColor.rgb, isBurn);
    83.          o.Emission = lerp(fixed3(0,0,0), _BurnColor.rgb, isBurn);
    84.            
    85.          fixed a = c.a-_Cutoff;
    86.          o.Alpha = a;
    87.          clip(a);
    88.          o.Gloss = c.a;
    89.  
    90.          o.Specular = _Shininess;
    91.          o.Normal = UnpackNormal(tex2D(_MainBumpMap, IN.uv_MainBumpMap));
    92.                      
    93.       }
    94.       ENDCG
    95.     }
    96.     Fallback "Diffuse"
    97.  
    98.   }
     
  6. avadlamani

    avadlamani

    Joined:
    Sep 29, 2013
    Posts:
    104
    Try using the step() function as a mask.
     
  7. Megaboy

    Megaboy

    Joined:
    Sep 22, 2013
    Posts:
    7
    replacing "clip()" with "step()", everything goes pink and "Shader error in 'Custom Shaders/Dissolve Shader": Program 'SurfShaderInternalFunc', unable to find compatible overloaded function "step(float3)" at line 81"
     
  8. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
  9. Rispat-Momit

    Rispat-Momit

    Joined:
    Feb 14, 2013
    Posts:
    265