Unity 漫反射

diffuse=Kd*_LightColor0*max(N·L,0)
Kd–环境光颜色、顶点颜色、点光源的距离衰减
_LightColor0–光源颜色
N·L–入射光向量 · 顶点法向量


Shader "Custom/MyDiffuse_Vertex"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        _Bright("Bright",Float)=2
    }

    SubShader
    {
        Tags{ "RenderType" = "Opaque" }

        Pass
        {
            Tags{ "LightMode" = "ForwardBase" }
            //处理平行光
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            uniform sampler2D _MainTex;
            uniform float _Bright;
            struct appdata
            {
                float4 vertex:POSITION;
                float2 texcoord:TEXCOORD0;
                float3 normal:NORMAL;
            };


            struct v2f
            {
                float4 pos:SV_POSITION;
                float2 uv:TEXCOORD0;
                fixed4 color : COLOR;
            };


            v2f vert(in appdata v)
            {
                //_LightColor0 :光的颜色
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex); //  顶点变换到裁剪空间
                o.uv = v.texcoord;
                float3 N = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));//法线
                float3 L;//入射光方向
                float atten;//点光源的距离衰减系数
                if (_WorldSpaceLightPos0.w == 0)//_WorldSpaceLightPos0:平行光: (world space direction, 0);
                {
                    L = normalize(_WorldSpaceLightPos0);
                    atten = 1;
                }
                else //_WorldSpaceLightPos0: 其他光: (world space position, 1).
                {
                    float3 light2Vertex = _WorldSpaceLightPos0 - mul(unity_ObjectToWorld, v.vertex);
                    L = normalize(light2Vertex);
                    float Length = length(light2Vertex);
                    atten = 1 / Length;
                }
                float Kd = UNITY_LIGHTMODEL_AMBIENT*atten*_Bright;
                //计算入射光和法线的夹角的余弦值,并剔除小于零的值(小于0的值相当于从物体的背面向物体表面照射,没有物理意义)
                float cos = max(dot(N, L),0);
                fixed4 difColor = Kd*_LightColor0 *cos;//漫反射光的颜色
                o.color = difColor;
                return o;
            }

            fixed4 frag(v2f i) :SV_Target 
            {
                fixed4 col;
                col = tex2D(_MainTex, i.uv);
                col *= i.color;
                return col;
            }
            ENDCG
        }

        Pass
        {
            Tags{ "LightMode" = "ForwardAdd" }
            //处理点光源
            Blend one one   //**着重注意
            //与前一个Pass 的颜色1:1混合 
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            uniform sampler2D _MainTex;
            uniform float _Bright;
            struct appdata
            {
                float4 vertex:POSITION;
                float2 texcoord:TEXCOORD0;
                float3 normal:NORMAL;
            };


            struct v2f
            {
                float4 pos:SV_POSITION;
                float2 uv:TEXCOORD0;
                fixed4 color : COLOR;
            };


            v2f vert(in appdata v)
            {
                //_LightColor0 :光的颜色
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex); //  顶点变换到裁剪空间
                o.uv = v.texcoord;
                float3 N = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));//法线
                float3 L;//入射光方向
                float atten;//点光源的距离衰减系数
                if (_WorldSpaceLightPos0.w == 0)//_WorldSpaceLightPos0:平行光: (world space direction, 0);
                {
                    L = normalize(_WorldSpaceLightPos0);
                    atten = 1;
                }
                else //_WorldSpaceLightPos0: 其他光: (world space position, 1).
                {
                    float3 light2Vertex = _WorldSpaceLightPos0 - mul(unity_ObjectToWorld, v.vertex);
                    L = normalize(light2Vertex);
                    float Length = length(light2Vertex);
                    atten = 1 / Length;
                }
                float Kd = UNITY_LIGHTMODEL_AMBIENT*atten*_Bright;
                //计算入射光和法线的夹角的余弦值,并剔除小于零的值(小于0的值相当于从物体的背面向物体表面照射,没有物理意义)
                float cos = max(dot(N, L),0);
                fixed4 difColor = Kd*_LightColor0 *cos;//漫反射光的颜色
                o.color = difColor;
                return o;
            }

            fixed4 frag(v2f i) :SV_Target
            {
                fixed4 col;
                col = tex2D(_MainTex, i.uv);
                col *= i.color;
                return col;
            }
            ENDCG
        }

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值