U3D Shader 案例

一:使用shader实现在小球上渲染出旋转地球的效果【顶点片段着色器】,并实现多层渲染,渲染出在地球的外面有一层云【大气层】

Shader "Practice/Earth"
{
    //属性
    Properties{
        //地球纹理
        _EarthTex("EarthTex",2D)="white"{}
        //云层纹理
        _CloudTex("CloudTex",2D)="white"{}
    }
    SubShader{
        Tags{"RenderType"="Transparent"}
        LOD 200
        Pass{
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            //声明纹理对应地球和云层
            sampler2D _EarthTex;
            sampler2D _CloudTex;
            struct v2f{
                float4 vertex:POSITION;
                fixed4 texcoord:TEXCOORD0;
            };
            //实现顶点函数
            void vert(inout v2f v){
                v.vertex=mul(UNITY_MATRIX_MVP,v.vertex);
            }
            fixed4 frag(v2f v):COLOR
            {
                //渲染出地球
                fixed u= v.texcoord.x+_Time*-0.1;
                float2 uv=float2(u,v.texcoord.y);
                fixed4 earthColor=tex2D(_EarthTex,uv);
                //渲染云层
                fixed uc = v.texcoord.x+_Time*-0.3;
                float2 uvc = float2(uc,v.texcoord.y);
                fixed4 cloudColor=tex2D(_CloudTex,uvc);
                cloudColor=cloudColor.r*fixed4(1,1,1,1);

                //取出地球和云的颜色的差值
                return lerp(earthColor,cloudColor,0.5);
            }
            ENDCG
        }
    }
}

二:shader实现河水流动的效果。【表面着色器】

                                                        注:河水是流动的

Shader "Practice/River"{
    //属性
    Properties{
        _Color("Color",Color)=(1,1,1,1)
        _MainTex("Albedo(RGB)",2D)="white"{}
        _RiverSpeedX("X_Speed",Range(2,10))=7
        _RiverSpeedY("Y_Speed",Range(2,10))=7
    }
    SubShader{
        Tags{"RenderType"="Opaque"}
        LOD 200
        CGPROGRAM
        //表面着色器
        #pragma surface surf Lambert
        //定义属性
        sampler2D _MainTex;
        fixed4 _Color;
        fixed _RiverSpeedX;
        fixed _RiverSpeedY;
        //输入结构体
        struct Input{
            float2 uv_MainTex;
        };
        //实现渲染效果
        void surf(Input IN,inout SurfaceOutput o){
            //设置速度
            fixed xSpeed=_RiverSpeedX*_Time;
            fixed ySpeed=_RiverSpeedY*_Time;
            //获取随时间改变的uv坐标
            fixed2 uv=IN.uv_MainTex+fixed2(xSpeed,ySpeed);
            //根据新的uv坐标取出图片上的采样信息
            fixed4 c=tex2D(_MainTex,uv)*_Color;
            o.Albedo=c.rgb;
            o.Alpha=c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

三:使用shader为人物模型添加材质{顶点着色器}

//该着色器,用来实现发现贴图的效果,让纹理看起来更加逼真
Shader "David/David01_Bump"
{
    //属性
    Properties
    {
        _MainTex("MainTex",2D)="white"{}
    }
    SubShader{
        Tags{"RanderType"="Opaque"}
        LOD 200
        Pass{
            Name "Surface"
            CGPROGRAM
            //顶点方法
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            sampler2D _MainTex;
            struct v2f{
                float4 vertex:POSITION;
                fixed4 texcoord:TEXCOORD;
            };
            void vert(inout v2f v)
            {
                v.vertex=mul(UNITY_MATRIX_MVP,v.vertex);
            }
            fixed4 frag(v2f v):COLOR
            {
                return tex2D(_MainTex,v.    texcoord);
            }
             ENDCG
        }
    }
    FallBack "Diffuse"
}

shader渲染实现类似于鬼谷子隐身效果【有轮廓】

Shader "David/David02_Transparent"
{
    //属性
    Properties{
        _MainTex("MainTex",2D)="white"{}
        _BlendTex("BlendTex",2D)="white"{}
    }
    SubShader{
        Tags{"RenderType"="Transparent"}
        LOD 200
        //透明设置
        Blend SrcAlpha OneMinusSrcAlpha
        Pass{
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            sampler2D _MainTex;
            sampler2D _BlendTex;

            void vert(inout appdata_base v){
                v.vertex=mul(UNITY_MATRIX_MVP,v.vertex);
            }

            fixed4 frag(appdata_base v):COLOR
            {
                fixed4 col=tex2D(_MainTex,v.texcoord);
                fixed4 blendCol=tex2D(_BlendTex,v.texcoord);
                return col*blendCol;
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

Shader渲染人物模型卡通化

Shader "David/David03_Carton"
{
    Properties{
        _MainTex("MainTex",2D)="white"{}
        _BumpTex("BumpTex",2D)="white"{}
        _EmissionColor("EmissionColor",Color)=(1,1,1,1)
        //卡通程度
        _Cartoon("Carton",Range(-0.05,0.05))=0

        IsEnter("IsEnter",int)=0

    }
    SubShader{
        Tags{"RenderType"="Opaque"}
        LOD 200
        CGPROGRAM
        #pragma surface surf CustomLighting vertex:vert
        //获取主纹理
        sampler2D _MainTex;
        //法线纹理
        sampler2D _BumpTex;
        //高光颜色
        fixed4 _EmissionColor;
        //卡通程度
        float _Cartoon;
        //判断条件
        fixed IsEnter;

        struct Input{
            fixed2 uv_MainTex;
            fixed2 uv_BumpTex;
            //视觉方向
            float3 viewDir;
        };

        //实现自定义光照模型方法 注意:自定义光照模型名字前一定要加上Lighting
        half4 LightingCustomLighting(SurfaceOutput o,half3 lightDir,half atten)
        {
            half4 result;
            half4 nDot=dot(lightDir,o.Normal);
            nDot=nDot*0.5+0.5f;
            //_LightColor0:表示当前光照颜色,有系统填充
            result.rgb=o.Albedo*_LightColor0.rgb;
            result.a=o.Alpha;
            return result;
        }

        void vert(inout appdata_full v){
            v.vertex.xyz+=v.normal.xyz*_Cartoon;
        }
        void surf(Input IN,inout SurfaceOutput o)
        {
            //采样基本纹理
            fixed4 bump=tex2D(_BumpTex,IN.uv_BumpTex);
            fixed4 albedo=tex2D(_MainTex,IN.uv_MainTex);
            o.Albedo=albedo;
            o.Normal=UnpackNormal(bump);

//人物变红【红色高亮光】
            if    (IsEnter>0){
            //求视觉方向的单位向量
            float3 view=normalize(IN.viewDir);
            //计算法线与视觉防线的点积
            float value = dot (view,o.Normal);
            //限制Value的范围
            value=1-saturate(value);
            o.Emission=_EmissionColor*value*IsEnter;
            }
        }

        ENDCG
    }
    Fallback "Diffuse"
}
原文地址:https://www.cnblogs.com/zpy1993-09/p/13180646.html