Complete Implementation Scheme of NGUI Picture Ashing Effect

Posted by PowersWithin on Mon, 17 Jun 2019 20:19:04 +0200

Links to the original text: http://blog.csdn.net/kakashi8841/article/details/45478433




1. Demand for ashing

Often, when we play games, we encounter a situation. For example, a skill icon is normal color when it can be clicked, and grey when it cannot be clicked. Another example is a function that is normally colored when it is open and grey when it is not. More often, your QQ friends, for example, will turn gray if they are not online.

Then there is a need to turn a picture grey.


2. Talking about Ashing and Gray Level

First of all, you can't say let art produce two sets of pictures, one set of colors and one set of gray ones. This will increase resource usage.

Then we can only find a way to deal with it through procedures.

So let's first figure out what a grey picture looks like.

(colour)

(after ashing)

It's gray to see the words "Team Upgrade" and their background.

How to make a picture grey? First, the color is composed of RGB (you want to say CMYK, HSB, even index color, I won't argue with you about this, after all, I'm a accomplished person). What we see is so-called grey. That is, R, G and B all have the same values. That is to say, we need to calculate the original RGB value of the picture into a new RGB, the new color of his R=G=B.


Well, how can RGB be calculated as a gray value?

This involves an ashing formula, which is an empirical formula, that is to say, the formula is not fixed. As long as you figure it out, the effect is grey, that's OK.

For example, in the simplest case, you can use formulas:

k = (r + g + b) / 3

This is to average RGB.

There is also a psychological formula:

k = r*.222 + g*.707 + b*.071;

What does this formula mean? As you can see, 0.222 + 0.707 + 0.071 = 1

In fact, this formula means that the proportion of RGB is different, for example, R accounts for 0.222. Then he multiplied the RGB values by the corresponding proportions and got a new value. Formula 1, in fact, holds that RGB accounts for the same proportion.

These formulas end up with gray pictures (because you R=G=B=k), but look different. You can also try other weights yourself.


3. Practice of ashing NGUI

So much, let's start with a few lines of code.

The idea is to modify the Transparent Colored shader of NGUI. We deal with it by losing a color. Take (0, 0, 0) as the switch to enable gray level.

The Transparent Colored code is as follows:

  1. Shader "Unlit/Transparent Colored"  
  2. {  
  3.     Properties  
  4.     {  
  5.         _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}  
  6.     }  
  7.       
  8.     SubShader  
  9.     {  
  10.         LOD 100  
  11.   
  12.         Tags  
  13.         {  
  14.             "Queue" = "Transparent"  
  15.             "IgnoreProjector" = "True"  
  16.             "RenderType" = "Transparent"  
  17.         }  
  18.           
  19.         Cull Off  
  20.         Lighting Off  
  21.         ZWrite Off  
  22.         Fog { Mode Off }  
  23.         Offset -1, -1  
  24.         Blend SrcAlpha OneMinusSrcAlpha  
  25.   
  26.         Pass  
  27.         {  
  28.             CGPROGRAM  
  29.             #pragma vertex vert  
  30.             #pragma fragment frag  
  31.                   
  32.             #include "UnityCG.cginc"  
  33.       
  34.             struct appdata_t  
  35.             {  
  36.                 float4 vertex : POSITION;  
  37.                 float2 texcoord : TEXCOORD0;  
  38.                 fixed4 color : COLOR;  
  39.             };  
  40.       
  41.             struct v2f  
  42.             {  
  43.                 float4 vertex : SV_POSITION;  
  44.                 half2 texcoord : TEXCOORD0;  
  45.                 fixed4 color : COLOR;  
  46.                 fixed gray : TEXCOORD1;   
  47.             };  
  48.       
  49.             sampler2D _MainTex;  
  50.             float4 _MainTex_ST;  
  51.                   
  52.             v2f vert (appdata_t v)  
  53.             {  
  54.                 v2f o;  
  55.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);  
  56.                 o.texcoord = v.texcoord;  
  57.                 o.color = v.color;  
  58.                 o.gray = dot(v.color, fixed4(1,1,1,0));  
  59.                 return o;  
  60.             }  
  61.                   
  62.             fixed4 frag (v2f i) : COLOR  
  63.             {  
  64.                 fixed4 col;  
  65.                 col = tex2D(_MainTex, i.texcoord);  
  66.                   
  67.                 if(i.gray == 0)  
  68.                 {  
  69.                     float grey = dot(col.rgb, float3(0.299, 0.587, 0.114));  
  70.                     col.rgb = float3(grey, grey, grey);  
  71.                 }  
  72.                 else  
  73.                 {  
  74.                   col = col * i.color;  
  75.                 }  
  76.                 return col;  
  77.             }  
  78.             ENDCG  
  79.         }  
  80.     }  
  81.   
  82.     SubShader  
  83.     {  
  84.         LOD 100  
  85.   
  86.         Tags  
  87.         {  
  88.             "Queue" = "Transparent"  
  89.             "IgnoreProjector" = "True"  
  90.             "RenderType" = "Transparent"  
  91.         }  
  92.           
  93.         Pass  
  94.         {  
  95.             Cull Off  
  96.             Lighting Off  
  97.             ZWrite Off  
  98.             Fog { Mode Off }  
  99.             Offset -1, -1  
  100.             ColorMask RGB  
  101.             AlphaTest Greater .01  
  102.             Blend SrcAlpha OneMinusSrcAlpha  
  103.             ColorMaterial AmbientAndDiffuse  
  104.               
  105.             SetTexture [_MainTex]  
  106.             {  
  107.                 Combine Texture * Primary  
  108.             }  
  109.         }  
  110.     }  
  111. }  
Shader "Unlit/Transparent Colored"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
    }

    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        Fog { Mode Off }
        Offset -1, -1
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                half2 texcoord : TEXCOORD0;
                fixed4 color : COLOR;
                fixed gray : TEXCOORD1; 
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.texcoord = v.texcoord;
                o.color = v.color;
                o.gray = dot(v.color, fixed4(1,1,1,0));
                return o;
            }

            fixed4 frag (v2f i) : COLOR
            {
                fixed4 col;
                col = tex2D(_MainTex, i.texcoord);

                if(i.gray == 0)
                {
                    float grey = dot(col.rgb, float3(0.299, 0.587, 0.114));
                    col.rgb = float3(grey, grey, grey);
                }
                else
                {
                  col = col * i.color;
                }
                return col;
            }
            ENDCG
        }
    }

    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            Offset -1, -1
            ColorMask RGB
            AlphaTest Greater .01
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMaterial AmbientAndDiffuse

            SetTexture [_MainTex]
            {
                Combine Texture * Primary
            }
        }
    }
}
We calculate in vert whether the set color (the place shown below) is all zero.

If all is 0, the ashing formula is applied to frag.

So the modification was completed.


4. Supporting softclip

To work with softclip, please modify it together

Transparent Colored 1.shader, Transparent Colored 2.shader, Transparent Colored 3.shader are the following codes:

Many articles on the Internet may not mention the above three shader modifications, resulting in many developers using ashing in softclip ineffective.

  1. Shader "HIDDEN/Unlit/Transparent Colored 1"  
  2. {  
  3.     Properties  
  4.     {  
  5.         _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}  
  6.     }  
  7.   
  8.     SubShader  
  9.     {  
  10.         LOD 200  
  11.   
  12.         Tags  
  13.         {  
  14.             "Queue" = "Transparent"  
  15.             "IgnoreProjector" = "True"  
  16.             "RenderType" = "Transparent"  
  17.         }  
  18.           
  19.         Pass  
  20.         {  
  21.             Cull Off  
  22.             Lighting Off  
  23.             ZWrite Off  
  24.             Offset -1, -1  
  25.             Fog { Mode Off }  
  26.             ColorMask RGB  
  27.             AlphaTest Greater .01  
  28.             Blend SrcAlpha OneMinusSrcAlpha  
  29.   
  30.             CGPROGRAM  
  31.             #pragma vertex vert  
  32.             #pragma fragment frag  
  33.   
  34.             #include "UnityCG.cginc"  
  35.   
  36.             sampler2D _MainTex;  
  37.             float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);  
  38.             float2 _ClipArgs0 = float2(1000.0, 1000.0);  
  39.   
  40.             struct appdata_t  
  41.             {  
  42.                 float4 vertex : POSITION;  
  43.                 half4 color : COLOR;  
  44.                 float2 texcoord : TEXCOORD0;  
  45.             };  
  46.   
  47.             struct v2f  
  48.             {  
  49.                 float4 vertex : POSITION;  
  50.                 half4 color : COLOR;  
  51.                 float2 texcoord : TEXCOORD0;  
  52.                 float2 worldPos : TEXCOORD1;  
  53.             };  
  54.   
  55.             v2f vert (appdata_t v)  
  56.             {  
  57.                 v2f o;  
  58.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);  
  59.                 o.color = v.color;  
  60.                 o.texcoord = v.texcoord;  
  61.                 o.worldPos = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;  
  62.                 return o;  
  63.             }  
  64.   
  65.             half4 frag (v2f IN) : COLOR  
  66.             {  
  67.                 // Softness factor  
  68.                 float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipArgs0;  
  69.               
  70.                 // Sample the texture  
  71.                 half4 col = tex2D(_MainTex, IN.texcoord);  
  72.   
  73.                   
  74.                 if (dot(IN.color, fixed4(1,1,1,0)) == 0)  
  75.                 {  
  76.                   col = tex2D(_MainTex, IN.texcoord);  
  77.                   col.rgb = dot(col.rgb, fixed3(.222,.707,.071));  
  78.                 }else{  
  79.                   col = col * IN.color;  
  80.                 }  
  81.                    
  82.                 col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);  
  83.                 return col;  
  84.             }  
  85.             ENDCG  
  86.         }  
  87.     }  
  88.       
  89.     SubShader  
  90.     {  
  91.         LOD 100  
  92.   
  93.         Tags  
  94.         {  
  95.             "Queue" = "Transparent"  
  96.             "IgnoreProjector" = "True"  
  97.             "RenderType" = "Transparent"  
  98.         }  
  99.           
  100.         Pass  
  101.         {  
  102.             Cull Off  
  103.             Lighting Off  
  104.             ZWrite Off  
  105.             Fog { Mode Off }  
  106.             ColorMask RGB  
  107.             AlphaTest Greater .01  
  108.             Blend SrcAlpha OneMinusSrcAlpha  
  109.             ColorMaterial AmbientAndDiffuse  
  110.               
  111.             SetTexture [_MainTex]  
  112.             {  
  113.                 Combine Texture * Primary  
  114.             }  
  115.         }  
  116.     }  
  117. }  
Shader "HIDDEN/Unlit/Transparent Colored 1"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
    }

    SubShader
    {
        LOD 200

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Offset -1, -1
            Fog { Mode Off }
            ColorMask RGB
            AlphaTest Greater .01
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);
            float2 _ClipArgs0 = float2(1000.0, 1000.0);

            struct appdata_t
            {
                float4 vertex : POSITION;
                half4 color : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : POSITION;
                half4 color : COLOR;
                float2 texcoord : TEXCOORD0;
                float2 worldPos : TEXCOORD1;
            };

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.color = v.color;
                o.texcoord = v.texcoord;
                o.worldPos = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;
                return o;
            }

            half4 frag (v2f IN) : COLOR
            {
                // Softness factor
                float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipArgs0;

                // Sample the texture
                half4 col = tex2D(_MainTex, IN.texcoord);


                if (dot(IN.color, fixed4(1,1,1,0)) == 0)
                {
                  col = tex2D(_MainTex, IN.texcoord);
                  col.rgb = dot(col.rgb, fixed3(.222,.707,.071));
                }else{
                  col = col * IN.color;
                }

                col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);
                return col;
            }
            ENDCG
        }
    }

    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            ColorMask RGB
            AlphaTest Greater .01
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMaterial AmbientAndDiffuse

            SetTexture [_MainTex]
            {
                Combine Texture * Primary
            }
        }
    }
}

  1. Shader "HIDDEN/Unlit/Transparent Colored 2"  
  2. {  
  3.     Properties  
  4.     {  
  5.         _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}  
  6.     }  
  7.   
  8.     SubShader  
  9.     {  
  10.         LOD 200  
  11.   
  12.         Tags  
  13.         {  
  14.             "Queue" = "Transparent"  
  15.             "IgnoreProjector" = "True"  
  16.             "RenderType" = "Transparent"  
  17.         }  
  18.           
  19.         Pass  
  20.         {  
  21.             Cull Off  
  22.             Lighting Off  
  23.             ZWrite Off  
  24.             Offset -1, -1  
  25.             Fog { Mode Off }  
  26.             ColorMask RGB  
  27.             AlphaTest Greater .01  
  28.             Blend SrcAlpha OneMinusSrcAlpha  
  29.   
  30.             CGPROGRAM  
  31.             #pragma vertex vert  
  32.             #pragma fragment frag  
  33.   
  34.             #include "UnityCG.cginc"  
  35.   
  36.             sampler2D _MainTex;  
  37.             float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);  
  38.             float4 _ClipArgs0 = float4(1000.0, 1000.0, 0.0, 1.0);  
  39.             float4 _ClipRange1 = float4(0.0, 0.0, 1.0, 1.0);  
  40.             float4 _ClipArgs1 = float4(1000.0, 1000.0, 0.0, 1.0);  
  41.   
  42.             struct appdata_t  
  43.             {  
  44.                 float4 vertex : POSITION;  
  45.                 half4 color : COLOR;  
  46.                 float2 texcoord : TEXCOORD0;  
  47.             };  
  48.   
  49.             struct v2f  
  50.             {  
  51.                 float4 vertex : POSITION;  
  52.                 half4 color : COLOR;  
  53.                 float2 texcoord : TEXCOORD0;  
  54.                 float4 worldPos : TEXCOORD1;  
  55.             };  
  56.   
  57.             float2 Rotate (float2 v, float2 rot)  
  58.             {  
  59.                 float2 ret;  
  60.                 ret.x = v.x * rot.y - v.y * rot.x;  
  61.                 ret.y = v.x * rot.x + v.y * rot.y;  
  62.                 return ret;  
  63.             }  
  64.   
  65.             v2f vert (appdata_t v)  
  66.             {  
  67.                 v2f o;  
  68.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);  
  69.                 o.color = v.color;  
  70.                 o.texcoord = v.texcoord;  
  71.                 o.worldPos.xy = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;  
  72.                 o.worldPos.zw = Rotate(v.vertex.xy, _ClipArgs1.zw) * _ClipRange1.zw + _ClipRange1.xy;  
  73.                 return o;  
  74.             }  
  75.   
  76.             half4 frag (v2f IN) : COLOR  
  77.             {  
  78.                 // First clip region  
  79.                 float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos.xy)) * _ClipArgs0.xy;  
  80.                 float f = min(factor.x, factor.y);  
  81.   
  82.                 // Second clip region  
  83.                 factor = (float2(1.0, 1.0) - abs(IN.worldPos.zw)) * _ClipArgs1.xy;  
  84.                 f = min(f, min(factor.x, factor.y));  
  85.   
  86.                 half4 col;  
  87.                 col = tex2D(_MainTex, IN.texcoord);  
  88.                 if (dot(IN.color, fixed4(1,1,1,0)) == 0)  
  89.                 {  
  90.                   col.rgb = dot(col.rgb, fixed3(.222,.707,.071));  
  91.                 }else{  
  92.                   col = col * IN.color;  
  93.                 }  
  94.   
  95.                 col.a *= clamp(f, 0.0, 1.0);  
  96.                   
  97.                 return col;  
  98.             }  
  99.             ENDCG  
  100.         }  
  101.     }  
  102.       
  103.     SubShader  
  104.     {  
  105.         LOD 100  
  106.   
  107.         Tags  
  108.         {  
  109.             "Queue" = "Transparent"  
  110.             "IgnoreProjector" = "True"  
  111.             "RenderType" = "Transparent"  
  112.         }  
  113.           
  114.         Pass  
  115.         {  
  116.             Cull Off  
  117.             Lighting Off  
  118.             ZWrite Off  
  119.             Fog { Mode Off }  
  120.             ColorMask RGB  
  121.             AlphaTest Greater .01  
  122.             Blend SrcAlpha OneMinusSrcAlpha  
  123.             ColorMaterial AmbientAndDiffuse  
  124.               
  125.             SetTexture [_MainTex]  
  126.             {  
  127.                 Combine Texture * Primary  
  128.             }  
  129.         }  
  130.     }  
  131. }  
Shader "HIDDEN/Unlit/Transparent Colored 2"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
    }

    SubShader
    {
        LOD 200

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Offset -1, -1
            Fog { Mode Off }
            ColorMask RGB
            AlphaTest Greater .01
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);
            float4 _ClipArgs0 = float4(1000.0, 1000.0, 0.0, 1.0);
            float4 _ClipRange1 = float4(0.0, 0.0, 1.0, 1.0);
            float4 _ClipArgs1 = float4(1000.0, 1000.0, 0.0, 1.0);

            struct appdata_t
            {
                float4 vertex : POSITION;
                half4 color : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : POSITION;
                half4 color : COLOR;
                float2 texcoord : TEXCOORD0;
                float4 worldPos : TEXCOORD1;
            };

            float2 Rotate (float2 v, float2 rot)
            {
                float2 ret;
                ret.x = v.x * rot.y - v.y * rot.x;
                ret.y = v.x * rot.x + v.y * rot.y;
                return ret;
            }

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.color = v.color;
                o.texcoord = v.texcoord;
                o.worldPos.xy = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;
                o.worldPos.zw = Rotate(v.vertex.xy, _ClipArgs1.zw) * _ClipRange1.zw + _ClipRange1.xy;
                return o;
            }

            half4 frag (v2f IN) : COLOR
            {
                // First clip region
                float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos.xy)) * _ClipArgs0.xy;
                float f = min(factor.x, factor.y);

                // Second clip region
                factor = (float2(1.0, 1.0) - abs(IN.worldPos.zw)) * _ClipArgs1.xy;
                f = min(f, min(factor.x, factor.y));

                half4 col;
                col = tex2D(_MainTex, IN.texcoord);
                if (dot(IN.color, fixed4(1,1,1,0)) == 0)
                {
                  col.rgb = dot(col.rgb, fixed3(.222,.707,.071));
                }else{
                  col = col * IN.color;
                }

                col.a *= clamp(f, 0.0, 1.0);

                return col;
            }
            ENDCG
        }
    }

    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            ColorMask RGB
            AlphaTest Greater .01
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMaterial AmbientAndDiffuse

            SetTexture [_MainTex]
            {
                Combine Texture * Primary
            }
        }
    }
}
  1. Shader "HIDDEN/Unlit/Transparent Colored 3"  
  2. {  
  3.     Properties  
  4.     {  
  5.         _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}  
  6.     }  
  7.   
  8.     SubShader  
  9.     {  
  10.         LOD 200  
  11.   
  12.         Tags  
  13.         {  
  14.             "Queue" = "Transparent"  
  15.             "IgnoreProjector" = "True"  
  16.             "RenderType" = "Transparent"  
  17.         }  
  18.           
  19.         Pass  
  20.         {  
  21.             Cull Off  
  22.             Lighting Off  
  23.             ZWrite Off  
  24.             Offset -1, -1  
  25.             Fog { Mode Off }  
  26.             ColorMask RGB  
  27.             AlphaTest Greater .01  
  28.             Blend SrcAlpha OneMinusSrcAlpha  
  29.   
  30.             CGPROGRAM  
  31.             #pragma vertex vert  
  32.             #pragma fragment frag  
  33.   
  34.             #include "UnityCG.cginc"  
  35.   
  36.             sampler2D _MainTex;  
  37.             float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);  
  38.             float4 _ClipArgs0 = float4(1000.0, 1000.0, 0.0, 1.0);  
  39.             float4 _ClipRange1 = float4(0.0, 0.0, 1.0, 1.0);  
  40.             float4 _ClipArgs1 = float4(1000.0, 1000.0, 0.0, 1.0);  
  41.             float4 _ClipRange2 = float4(0.0, 0.0, 1.0, 1.0);  
  42.             float4 _ClipArgs2 = float4(1000.0, 1000.0, 0.0, 1.0);  
  43.   
  44.             struct appdata_t  
  45.             {  
  46.                 float4 vertex : POSITION;  
  47.                 half4 color : COLOR;  
  48.                 float2 texcoord : TEXCOORD0;  
  49.             };  
  50.   
  51.             struct v2f  
  52.             {  
  53.                 float4 vertex : POSITION;  
  54.                 half4 color : COLOR;  
  55.                 float2 texcoord : TEXCOORD0;  
  56.                 float4 worldPos : TEXCOORD1;  
  57.                 float2 worldPos2 : TEXCOORD2;  
  58.             };  
  59.   
  60.             float2 Rotate (float2 v, float2 rot)  
  61.             {  
  62.                 float2 ret;  
  63.                 ret.x = v.x * rot.y - v.y * rot.x;  
  64.                 ret.y = v.x * rot.x + v.y * rot.y;  
  65.                 return ret;  
  66.             }  
  67.   
  68.             v2f vert (appdata_t v)  
  69.             {  
  70.                 v2f o;  
  71.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);  
  72.                 o.color = v.color;  
  73.                 o.texcoord = v.texcoord;  
  74.                 o.worldPos.xy = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;  
  75.                 o.worldPos.zw = Rotate(v.vertex.xy, _ClipArgs1.zw) * _ClipRange1.zw + _ClipRange1.xy;  
  76.                 o.worldPos2 = Rotate(v.vertex.xy, _ClipArgs2.zw) * _ClipRange2.zw + _ClipRange2.xy;  
  77.                 return o;  
  78.             }  
  79.   
  80.             half4 frag (v2f IN) : COLOR  
  81.             {  
  82.                 // First clip region  
  83.                 float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos.xy)) * _ClipArgs0.xy;  
  84.                 float f = min(factor.x, factor.y);  
  85.   
  86.                 // Second clip region  
  87.                 factor = (float2(1.0, 1.0) - abs(IN.worldPos.zw)) * _ClipArgs1.xy;  
  88.                 f = min(f, min(factor.x, factor.y));  
  89.   
  90.                 // Third clip region  
  91.                 factor = (float2(1.0, 1.0) - abs(IN.worldPos2)) * _ClipArgs2.xy;  
  92.                 f = min(f, min(factor.x, factor.y));  
  93.   
  94.                 // Sample the texture  
  95.                 half4 col = tex2D(_MainTex, IN.texcoord);  
  96.                 if (dot(IN.color, fixed4(1,1,1,0)) == 0)  
  97.                 {  
  98.                   col.rgb = dot(col.rgb, fixed3(.222,.707,.071));  
  99.                 }else{  
  100.                   col = col * IN.color;  
  101.                 }  
  102.   
  103.                 col.a *= clamp(f, 0.0, 1.0);  
  104.                 return col;  
  105.             }  
  106.             ENDCG  
  107.         }  
  108.     }  
  109.       
  110.     SubShader  
  111.     {  
  112.         LOD 100  
  113.   
  114.         Tags  
  115.         {  
  116.             "Queue" = "Transparent"  
  117.             "IgnoreProjector" = "True"  
  118.             "RenderType" = "Transparent"  
  119.         }  
  120.           
  121.         Pass  
  122.         {  
  123.             Cull Off  
  124.             Lighting Off  
  125.             ZWrite Off  
  126.             Fog { Mode Off }  
  127.             ColorMask RGB  
  128.             AlphaTest Greater .01  
  129.             Blend SrcAlpha OneMinusSrcAlpha  
  130.             ColorMaterial AmbientAndDiffuse  
  131.               
  132.             SetTexture [_MainTex]  
  133.             {  
  134.                 Combine Texture * Primary  
  135.             }  
  136.         }  
  137.     }  
  138. }  
Shader "HIDDEN/Unlit/Transparent Colored 3"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
    }

    SubShader
    {
        LOD 200

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Offset -1, -1
            Fog { Mode Off }
            ColorMask RGB
            AlphaTest Greater .01
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);
            float4 _ClipArgs0 = float4(1000.0, 1000.0, 0.0, 1.0);
            float4 _ClipRange1 = float4(0.0, 0.0, 1.0, 1.0);
            float4 _ClipArgs1 = float4(1000.0, 1000.0, 0.0, 1.0);
            float4 _ClipRange2 = float4(0.0, 0.0, 1.0, 1.0);
            float4 _ClipArgs2 = float4(1000.0, 1000.0, 0.0, 1.0);

            struct appdata_t
            {
                float4 vertex : POSITION;
                half4 color : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : POSITION;
                half4 color : COLOR;
                float2 texcoord : TEXCOORD0;
                float4 worldPos : TEXCOORD1;
                float2 worldPos2 : TEXCOORD2;
            };

            float2 Rotate (float2 v, float2 rot)
            {
                float2 ret;
                ret.x = v.x * rot.y - v.y * rot.x;
                ret.y = v.x * rot.x + v.y * rot.y;
                return ret;
            }

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.color = v.color;
                o.texcoord = v.texcoord;
                o.worldPos.xy = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;
                o.worldPos.zw = Rotate(v.vertex.xy, _ClipArgs1.zw) * _ClipRange1.zw + _ClipRange1.xy;
                o.worldPos2 = Rotate(v.vertex.xy, _ClipArgs2.zw) * _ClipRange2.zw + _ClipRange2.xy;
                return o;
            }

            half4 frag (v2f IN) : COLOR
            {
                // First clip region
                float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos.xy)) * _ClipArgs0.xy;
                float f = min(factor.x, factor.y);

                // Second clip region
                factor = (float2(1.0, 1.0) - abs(IN.worldPos.zw)) * _ClipArgs1.xy;
                f = min(f, min(factor.x, factor.y));

                // Third clip region
                factor = (float2(1.0, 1.0) - abs(IN.worldPos2)) * _ClipArgs2.xy;
                f = min(f, min(factor.x, factor.y));

                // Sample the texture
                half4 col = tex2D(_MainTex, IN.texcoord);
                if (dot(IN.color, fixed4(1,1,1,0)) == 0)
                {
                  col.rgb = dot(col.rgb, fixed3(.222,.707,.071));
                }else{
                  col = col * IN.color;
                }

                col.a *= clamp(f, 0.0, 1.0);
                return col;
            }
            ENDCG
        }
    }

    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            ColorMask RGB
            AlphaTest Greater .01
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMaterial AmbientAndDiffuse

            SetTexture [_MainTex]
            {
                Combine Texture * Primary
            }
        }
    }
}

OK, that's all. The test project will be uploaded later.


Topics: Fragment