Dissolve Shader

                    Dissolve Shader

There are several ways to implement::

  • Alpha
  • CutOff
  • Grab Texture

As a result, we get 3 shaders. Two of them use only alpha and who can fly to the mobile device and one with Alpha Test that looks prettier but more voracious. Thanks AlphaTest we can disable clipping invisible polygons and not getting layering. But will have to pay a shader model 2.0 and 3.0 use because of what can not be used on mobile devices.

Frame

Algorithm
     Take the brightness of a pixel with a mask or destruction directly from the main texture.  Compare this brightness with N.  If the brightness is greater than N then the alpha pixel set to zero.

and we get this:

We lack the normal maps. And lines.
Algorithm Lines is as follows:

     Take the brightness of a pixel with a mask of destruction, but offset by UV + LineSize. And another but already offset UV - LineSize
     If at least one of the pixels is less than N, then we set the color of the pixel of the texture for the lines
     Otherwise, set the alpha to zero (This is as a replacement for a similar operation in the first algorithm)

To sum up the above, we get a clipping mask and then cut off if the pixels do not have check whether right next to him not be cut off if they have the edge and we are set ourselves a certain color.

If we add to the above normal mapping and displacement texture line more sinusoidal time getting here so that's canvas.

The main shader

Shader "HolyMonkey/Dissolve/Bumped" {

    Properties {
        _MainColor ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _Mask("Mask To Dissolve", 2D) = "white" {}
        _LineTexture("Line Texture", 2D) = "white" {}
        _Range ("Range", Range(0,3)) = 0
        _LineSize ("LineSize", Float) = 0.001
        _Color ("Line Color", Color) = (1,1,1,1)
        _BumpMap ("Normalmap", 2D) = "bump" {}
        _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    }
    
    SubShader {
        Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
        LOD 300
        ZWrite On
        Cull Off
        CGPROGRAM 
        #pragma target 3.0
        #include "UnityCG.cginc"
        #pragma surface surf Lambert alphatest:_Cutoff
        

        sampler2D _MainTex;
        sampler2D _LineTexture;
        sampler2D _BumpMap;
        sampler2D _Mask;
        half4 _Color;
        half4 _MainColor;
        float _Range;
        float _LineSize;
                   
        struct Input {
            float2 uv_MainTex;
            float2 uv_BumpMap;
                        float2 uv_Detail;
        };
            
        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            half4 m = tex2D (_Mask, IN.uv_MainTex);
                half4 lc =  tex2D (_Mask, IN.uv_MainTex - _LineSize);
                half4 lc2 = tex2D (_Mask, IN.uv_MainTex + _LineSize);
            half4 lc3 = tex2D(_LineTexture, IN.uv_MainTex + _SinTime) * _Color;    
             
                o.Albedo = c *  _MainColor;
                o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
                o.Alpha = 1;
            
            float factor = m.rgb.x + m.rgb.y + m.rgb.z;
            if(factor >= _Range)
            {
               float factor2 = lc.rgb.x + lc.rgb.y + lc.rgb.z;
               float factor3 = lc2.rgb.x + lc2.rgb.y + lc2.rgb.z;
               if(factor2 < _Range || factor3 < _Range)
               {
                  o.Albedo = lc3;
               }
               else
               {
                  o.Alpha = 0.0;
               }
            }
        }
        ENDCG
    } 
    Fallback "Diffuse"
}

You can also play with some aspects and obtain the following

With the included COOLING

Shader "HolyMonkey/Dissolve/Culling-Mobile" {

    Properties {
        _MainColor ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _Mask("Mask To Dissolve", 2D) = "white" {}
        _LineTexture("Line Texture", 2D) = "white" {}
        _Range ("Range", Range(0,3)) = 0
        _LineSize ("LineSize", Float) = 0.001
        _Color ("Line Color", Color) = (1,1,1,1)
        _BumpMap ("Normalmap", 2D) = "bump" {}
    }
    
    SubShader {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        LOD 300
        CGPROGRAM 
        #pragma target 2.0
        #include "UnityCG.cginc"
        #pragma surface surf Lambert alpha 
        

        sampler2D _MainTex;
        sampler2D _LineTexture;
        sampler2D _BumpMap;
        sampler2D _Mask;
        half4 _Color;
        half4 _MainColor;
        float _Range;
        float _LineSize;
                   
        struct Input {
            float2 uv_MainTex;
            float2 uv_BumpMap;
            float2 uv_Detail;
        };
            
        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            half4 m = tex2D (_Mask, IN.uv_MainTex);
            half4 lc =  tex2D (_Mask, IN.uv_MainTex - _LineSize);
            half4 lc2 = tex2D (_Mask, IN.uv_MainTex + _LineSize);
            half4 lc3 = tex2D(_LineTexture, IN.uv_MainTex + _SinTime) * _Color;    
             
            o.Albedo = c *  _MainColor;
            o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
            o.Alpha = 1;
            
            float factor = m.rgb.x + m.rgb.y + m.rgb.z;
            if(factor >= _Range)
            {
               float factor2 = lc.rgb.x + lc.rgb.y + lc.rgb.z;
               float factor3 = lc2.rgb.x + lc2.rgb.y + lc2.rgb.z;
               if(factor2 < _Range || factor3 < _Range)
               {
                  o.Albedo = lc3;
               }
               else
               {
                  o.Alpha = 0.0;
               }
            }
        }
        ENDCG
    } 
    Fallback "Diffuse"
}

With one more texture instead of transparency

Shader "HolyMonkey/Dissolve/NotTransparent" {

    Properties {
        _MainColor ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _BackTexture ("Back Texture", 2D) = "white" {}
        _Mask("Mask To Dissolve", 2D) = "white" {}
        _LineTexture("Line Texture", 2D) = "white" {}
        _Range ("Range", Range(0,3)) = 0
        _LineSize ("LineSize", Float) = 0.001
        _Color ("Line Color", Color) = (1,1,1,1)
        _BumpMap ("Normalmap", 2D) = "bump" {}
    }
    
    SubShader {
        LOD 300
        ZWrite On
        Cull Off
        
        CGPROGRAM 
        #pragma target 2.0
        #include "UnityCG.cginc"
        #pragma surface surf Lambert
        

        sampler2D _MainTex;
        sampler2D _LineTexture;
        sampler2D _BumpMap;
        sampler2D _Mask;
        sampler2D _BackTexture;
        half4 _Color;
        half4 _MainColor;
        float _Range;
        float _LineSize;
                   
        struct Input {
            float2 uv_MainTex;
            float2 uv_BumpMap;
            float2 uv_Detail;
        };
            
        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            half4 m = tex2D (_Mask, IN.uv_MainTex);
            half4 lc =  tex2D (_Mask, IN.uv_MainTex - _LineSize);
            half4 lc2 = tex2D (_Mask, IN.uv_MainTex + _LineSize);
            half4 lc3 = tex2D(_LineTexture, IN.uv_MainTex + _SinTime) * _Color;    
            half4 bc  = tex2D(_BackTexture, IN.uv_MainTex);
            o.Albedo = c *  _MainColor;
            o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
            
            float factor = m.rgb.x + m.rgb.y + m.rgb.z;
            if(factor >= _Range)
            {
               float factor2 = lc.rgb.x + lc.rgb.y + lc.rgb.z;
               float factor3 = lc2.rgb.x + lc2.rgb.y + lc2.rgb.z;
               if(factor2 < _Range || factor3 < _Range)
               {
                  o.Albedo = lc3;
               }
               else
               {
                  o.Albedo = bc;
                  o.Normal = float3(1,1,1);
               }
            }
        }
        ENDCG
    } 
    Fallback "Diffuse"
}

 

All data posted on the site represents accessible information that can be browsed and downloaded for free from the web.

http://habrahabr.ru/post/208532/

 

 

User replies

No replies yet