Shader自学笔记 3.3.1 采样渐变纹理应用于BlinnPhong光照模型

Shader "Custom/texture/test1" {

//  定义纹理属性
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Main Tex", 2D) = "white" {}
        _Specular("Specular",Color)=(1,1,1)
        _Gloss ("Gloss", Range(8.0,256)) = 20
        }
    SubShader {
    Pass{
        Tags { "LightMode"="ForwardBase" }

        CGPROGRAM

        #pragma vertex vert

        #pragma fragment frag

        #include "Lighting.cginc"

        fixed4 _Color;
        sampler2D _MainTex;
//      纹理缩放平移属性
        float4 _MainTex_ST;
        fixed4 _Specular;
        float _Gloss;

        struct a2v{
        float4 vertex: POSITION;
        float3 normal:NORMAL;
//      语义指定第一个纹理
        float4 texcoord:TEXCOORD0;

        };

        struct v2f{
        float4 pos:SV_POSITION;
        float3 worldNormal:TEXCOORD0;
        float3 worldPos:TEXCOORD1;
        float2 uv:TEXCOORD2;
        };

        v2f vert(a2v v){
        v2f o;
        o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
        o.worldNormal=UnityObjectToWorldNormal(v.normal);
        o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
//      _MainTex_ST xy为缩放,zw为偏移
        o.uv=v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;
//      也可以直接用内建函数
//      o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
        return o;
        }

        fixed4 frag(v2f i):SV_Target{
        fixed3 worldNormal=normalize(i.worldNormal);
        fixed3 worldLightDir=normalize(UnityWorldSpaceLightDir(i.worldPos));
//      使用纹理采样漫反射颜色
        fixed halfLambert=0.5*dot(worldNormal,worldLightDir)+0.5;
        fixed3 diffuseColor=tex2D(_MainTex,fixed2(halfLambert,halfLambert)).rgb*_Color.rgb;
        fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;
//      fixed3 diffuse=_LightColor0.rgb*diffuseColor;
//      fixed3 albedo=tex2D(_MainTex,i.uv).rgb*_Color.rgb;
//      fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
//      fixed3 diffuse=_LightColor0.rgb*albedo*max(0,dot(worldNormal,worldLightDir));
        fixed3 viewDir=normalize(UnityWorldSpaceViewDir(i.worldPos));
        fixed3 halfDir=normalize(worldLightDir+viewDir);
//      BlinnPhon光照模型
        fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(max(0,dot(worldNormal,halfDir)),_Gloss);
        return fixed4(ambient+diffuseColor+specular,1.0);
        }
        ENDCG
        }

    }
    FallBack "Specular"
}

Shader自学笔记 3.3.1 采样渐变纹理应用于BlinnPhong光照模型_第1张图片

你可能感兴趣的:(Shader)