漫反射实现 - UnityShader

漫反射分为两种实现

  • 逐顶点漫反射(主要依靠顶点着色器,计算顶点的光照)
  • 逐片元漫反射(主要依靠片元着色器,计算片元的光照)

使用的公式是兰伯特定律
c d i f f u s e = ( c l i g h t ⋅ m d i f f u s e ) cos ⁡ θ (1) c_{diffuse} = (c_{light} \cdot m_{diffuse})\cos{\theta}\tag{1} cdiffuse=(clightmdiffuse)cosθ(1)
c o s θ = n ^ ⋅ l ^ (2) cos{\theta} = \widehat{n} \cdot \widehat{l}\tag{2} cosθ=n l (2)

在兰伯特的基础上增加亮光(添加两个偏移)
c d i f f u s e = ( c l i g h t ⋅ m d i f f u s e ) ( n ^ ⋅ l ^ ∗ 0.5 + 0.5 ) (3) c_{diffuse} = (c_{light} \cdot m_{diffuse})( \widehat{n} \cdot \widehat{l} * 0.5 + 0.5)\tag{3} cdiffuse=(clightmdiffuse)(n l 0.5+0.5)(3)

c l i g h t c_{light} clight 是光线的颜色, m d i f f u s e m_{diffuse} mdiffuse 是材质的颜色, c o s θ cos{\theta} cosθ是指法线与光线方向的角度

逐顶点漫反射

Shader "Lit/DiffuseLitShader1"
{
    Properties
    {
        _Diffuse ("Diffuse", Color) = (1, 1, 1, 1)
    }
    SubShader
    {
        Pass
        {
            Tags { "LightMode" = "ForwardBase" }
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include <UnityCG.cginc>
            #include <UnityLightingCommon.cginc>
            #include <UnityShaderUtilities.cginc>

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                fixed3 color : COLOR0;
            };

            fixed4 _Diffuse;

            //逐顶点漫反射
            v2f vert(a2v v)
            {
                v2f o;
                //设置裁剪空间
                o.pos = UnityObjectToClipPos(v.vertex.xyz);
                
                //得到环境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                //从物体坐标的法向量变换到全局坐标的法向量
                float3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
                
                //光照方向
                fixed3 litDir = normalize(_WorldSpaceLightPos0.xyz);
                
                //兰伯特定律 c = (c * m) max(0, dot(n.normalize, l.normalize)) = (c * m) * cos
                //fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, litDir));

                //半兰伯特定理 c = (c * m)(0.5 * cos + 0.5), 可实现暗部增强亮光
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * (dot(worldNormal, litDir) * 0.5 + 0.5);
                

                o.color = ambient + diffuse;

                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                return fixed4(i.color, 1.0);
            }
            ENDCG
        }
    }
    
    FallBack "Diffuse"
}

逐片元漫反射

Shader "Lit/DiffuseLitShader2"
{
    Properties
    {
        _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include <UnityCG.cginc>
            #include <UnityShaderUtilities.cginc>
            #include <UnityLightingCommon.cginc>

            struct a2v
            {
                float4 pos : POSITION;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 worldNormal : TEXCOORD0;
            };

            fixed4 _Diffuse;

            v2f vert(a2v v)
            {
                v2f o;
                
                //得到裁剪空间
                o.pos = UnityObjectToClipPos(v.pos.xyz);

                //获得世界空间的法向量,并保存在 uv 中
                o.worldNormal = float4(UnityObjectToWorldNormal(v.normal.xyz), 1.0);

                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                //得到 ambient
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                //gl 的 方向
                float3 litDir = normalize(_WorldSpaceLightPos0.xyz);

                //使用 兰伯特定律
                //fixed3 diffuse = _LightColor0.rgb * _Diffuse * saturate(dot(i.worldNormal.xyz, litDir));

                //使用 半兰伯特定律
                fixed3 diffuse = _LightColor0.rgb * _Diffuse * (dot(i.worldNormal.xyz, litDir) * 0.5 + 0.5);

                return fixed4(diffuse + ambient, 1.0);
            }
            ENDCG
        }
    }
    
    FallBack "Diffuse"
}

来源为 Unity Shader 入门精要这本书 - 漫反射光照的实现

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值