[Shader] UnityShader系列---灰白效果

原创 2015年11月18日 18:07:57

支持 NGUI的 UIPanel 的SoftClip,支持主流机型(小米,锤子,三星,iPhone等手机)

无图无真相

这里写图片描述

修改

Unlit - Transparent Colored.shader

如下:

Shader "Unlit/Transparent Colored"
{
    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
            Fog { Mode Off }
            Offset -1, -1
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag           
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;

            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;   

            };

            v2f o;

            v2f vert (appdata_t v)
            {
                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 IN) : COLOR
//          {
//              return tex2D(_MainTex, IN.texcoord) * IN.color;
//          }
            fixed4 frag (v2f i) : COLOR  
            {  
                fixed4 col;  
                col = tex2D(_MainTex, i.texcoord);  

                //if (i.gray < float(0.00000000001))
                //if (i.gray == 0)&&i.color.g <0.001&&i.color.b <0.001
                if(i.color.r <0.001&&i.color.g <0.001&&i.color.b <0.001)
                {  
                    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
            }
        }
    }
}


修改

Unlit - Transparent Colored 1.shader

如下:

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 o;

            v2f vert (appdata_t v)
            {
                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) * IN.color;
//              col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);
//              return col;
//          }
            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)
                //if (dot(IN.color, fixed4(1,1,1,0)) < float(0.00000000001))
                if (IN.color.r <0.001&&IN.color.g <0.001&&IN.color.b <0.001)
                {  
                  col = tex2D(_MainTex, IN.texcoord);  
                  float grey = dot(col.rgb, fixed3(0.222, 0.707, 0.071));
                  col.rgb = float3(grey, grey, grey);
                }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
            }
        }
    }
}


修改

Unlit - Transparent Colored 2.shader

如下:

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 o;

            v2f vert (appdata_t v)
            {
                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));
//
//              // Sample the texture
//              half4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
//              
//              col.a *= clamp(f, 0.0, 1.0);
//              return col;
//          }

            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)) < float(0.00000000001))
                //if (dot(IN.color, fixed4(1,1,1,0)) ==0)  
                if (IN.color.r <0.001&&IN.color.g <0.001&&IN.color.b <0.001)
                {
                  float grey = dot(col.rgb, fixed3(0.222, 0.707, 0.071));
                  col.rgb = float3(grey, grey, grey);
                }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
            }
        }
    }
}


修改

Unlit - Transparent Colored 3.shader

如下:

 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 o;

            v2f vert (appdata_t v)
            {
                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)* IN.color;
//              col.a *= clamp(f, 0.0, 1.0);
//              return col;
//          }

            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)) < float(0.00000000001))
                //if (dot(IN.color, fixed4(1,1,1,0)) ==0)  
                if (IN.color.r <0.001&&IN.color.g <0.001&&IN.color.b <0.001)
                {
                  float grey = dot(col.rgb, fixed3(0.222, 0.707, 0.071));
                  col.rgb = float3(grey, grey, grey);
                }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
            }
        }
    }
}

显示黑白效果:

UITexture texture;
texture.color = Color.black;

去除黑白效果:

UITexture texture;
texture.color = Color.white;

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[UnityShader]shader书写技巧之制作新手指导

参考链接:http://www.unitymanual.com/thread-35475-1-1.html 由于原作者太厉害了,所以第一次看他的代码表示看不懂,但经过一番琢磨,终于想通了,所以想分...
  • lyh916
  • lyh916
  • 2015年03月18日 23:33
  • 1115

[UnityShader]Shader上的优化

原文链接:http://game.ceeger.com/Components/SL-ShaderPerformance.html Compute only things that y...
  • lyh916
  • lyh916
  • 2015年06月16日 21:24
  • 1012

UnityShader实例07:在shader调用unity内置lightmap和light Probes

在shader调用unity内置lightmap和Light Probe 在编写unity shader的时候,有时候会有需求,希望写的shader能支持unity内置lightmap或者ligh...

UnityShader入门 积雪Shader

积雪Shader 如果你是一个shader编程的新手,并且你想学到下面这些酷炫的技术,我觉得你可以看看这篇教程: 实现一个积雪效果的shader创建一个具有凹凸纹理的shade...

UnityShader——初探Compute Shader

Compute Shader是基于DX11(SM4.5+)的在GPU上运行的程序,通过Compute Shader我们可以将大量可以并行的计算放到GPU中计算从而节省CPU资源,Unity 5.6版本...
  • NotMz
  • NotMz
  • 2017年07月20日 16:43
  • 119

[UnityShader3]热扭曲效果

参考链接:http://blog.sina.com.cn/s/blog_89d90b7c0102vaqy.html 1.首先回顾一下透明度混合的实现。在片段着色器输出源颜色后,经过Blend...
  • lyh916
  • lyh916
  • 2016年07月11日 11:29
  • 4065

[UnityShader3]序列帧遮罩效果

参考链接:http://liweizhaolili.blog.163.com/blog/static/1623074420154412826120/ 效果图: 上面的图有意思吧?呵呵。。其...
  • lyh916
  • lyh916
  • 2016年07月04日 18:08
  • 973

[UnityShader3]溶解与重现效果

参考链接:http://www.cnblogs.com/Esfog/p/DissolveShader.html 效果图: 从颜色变化来说,有三种,一种是纹理颜色,一种是纹理与黑边的混合颜色...
  • lyh916
  • lyh916
  • 2015年06月10日 13:14
  • 3294

[UnityShader3]局部动态效果

参考链接:http://blog.csdn.net/stalendp/article/details/30989295 效果图: Shader "Custom/Lightn...
  • lyh916
  • lyh916
  • 2016年07月14日 16:04
  • 1692

[UnityShader3]2D描边效果

参考链接: http://inpla.net/thread-6502-1-1.html http://www.jianshu.com/p/1d9d439c28fa 1. ...
  • lyh916
  • lyh916
  • 2016年05月24日 10:08
  • 3168
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Shader] UnityShader系列---灰白效果
举报原因:
原因补充:

(最多只允许输入30个字)