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

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

【Unity Shaders】学习笔记——Shader和渲染管线

Shader和渲染管线
  • qq_27831145
  • qq_27831145
  • 2016年06月17日 21:16
  • 731

Unity Shader-后处理:径向模糊效果

简介 游戏都追求画面感,个人感觉后处理是最能表现画面感的方法之一(虽然比较费)。之前玩了一阵子模糊效果,今天再来看一个模糊效果,径向模糊。这种效果对于画面感的提升非常有帮助,尤其是在快速运动突然加...
  • puppet_master
  • puppet_master
  • 2017年02月24日 23:21
  • 3703

Unity Shader学习笔记:凹凸映射

凹凸映射的目的是使用一张纹理来修改模型表面的法线,以便为模型提供更多的细节。...
  • SHIYUEDYX
  • SHIYUEDYX
  • 2017年08月24日 20:15
  • 206

Unity喷墨效果Shader实现

笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等...
  • jxw167
  • jxw167
  • 2017年07月07日 23:00
  • 3594

Unity-Shader Sublime 自制shader提示插件

Unity-Shader 编写shader,最好还是有一个好的IDE或者编辑器,支持语法高亮和智能提示的. 最初使用mac ,找了找插件,没找到,只能依靠mono的高亮进行编写,对于习惯了IDE的...
  • w88219003
  • w88219003
  • 2015年06月29日 15:00
  • 6125

Unity Shader学习笔记(五)

ShaderLab中属性的类型和CG中变量的类型之间的匹配关系如图:     uniform关键字: uniform fixed4 _Color;     uniform 关键字是CG中修饰变...
  • zjw1349547081
  • zjw1349547081
  • 2016年12月29日 11:17
  • 528

shader实例溶解,燃烧效果

游戏中物体腐化消失,燃烧消失时,会有从局部慢慢消失的效果,然后配合一些粒子特效,就能达到非常好的美术效果。 类似效果如下: 注: _DissColor为溶解主色,_AddColor为叠加色,...
  • m0_37283423
  • m0_37283423
  • 2017年04月25日 13:35
  • 787

【Unity3D Shader编程】之八 Unity5新版Shader模板源码解析&径向模糊屏幕特效的实现

概要:本文对Unity5中全新的三种Shader模板的源码进行了解析,然后还讲解了运动模糊屏幕特效的实现方法。 Unity5和之前的书写模式有了一定的改变。Unity5时代的Shader Refere...
  • zhmxy555
  • zhmxy555
  • 2015年10月25日 20:26
  • 29483

[UnityShader3]网格渐现效果

参考链接:http://liweizhaolili.blog.163.com/blog/static/1623074420140591864/ 效果图: 1.首先就是简单的渐现效果,同时要...
  • lyh916
  • lyh916
  • 2016年07月05日 08:48
  • 2563

【Shader】彩虹色效果+模型卡通描边效果

Shader "Custom/VertexFragment" { Properties { _myTexture("Texture", 2D) = "white" {} _myColor(...
  • sinat_20559947
  • sinat_20559947
  • 2015年09月18日 16:39
  • 2751
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Shader] UnityShader系列---灰白效果
举报原因:
原因补充:

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