6.2 漫反射-半兰伯特

//漫反射 半兰伯特 diffuse = (c.m)(0.5(n.i)+0.5)
//模型背光面有可以有明暗变化
Shader "Unity shader book/Chapter 6/HalfLambert" {
    Properties{
        _Diffuse("Diffuse",Color) = (1,1,1,1)
    }
        SubShader{
            Pass{
                Tags{"LightMode" = "ForwardBase"} //指定光照模式,只有定义正确的光照模式,才能得到unity内置光照变量 比如——LIghtColor0
                CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #include "Lighting.cginc"

            fixed4 _Diffuse;//定义和属性类型匹配的变量
            //定义顶点着色器输入和输出结构体;float最高精度浮点值,32位;fixed最低浮点值,11位存储,范围-2.0到2.0;
        struct a2v {
            float4 vertext :POSITION;
            float3 normal : NORMAL;
        };
        struct v2f {
            float4 pos : SV_POSITION; //结构体必须包含顶点在剪裁空间的坐标位置信息
            float3 worldNormal:TEXCOORD0;
        };

        v2f vert(a2v i) {
            v2f o;
            o.pos = mul(UNITY_MATRIX_MVP, i.vertext);
            o.worldNormal = UnityObjectToWorldNormal(i.normal);
            return o;
        }

        fixed4 frag(v2f i) :SV_Target{
            fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
            fixed3 worldNormal = normalize(i.worldNormal);
            fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

            fixed3 halfLambert = dot(worldNormal, worldLightDir)*0.5 + 0.5;
            fixed3 diffuse = _LightColor0.rgb*_Diffuse.rgb*halfLambert;

            fixed3 color = ambient + diffuse;

            return fixed4(color, 1.0);
        }

            ENDCG
        }
    }
    FallBack "Diffuse"
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值