《Unity Shader入门精要》学习1:Phong 模型中法向量归一化的正确位置

学习《Unity Shader入门精要》的Phong模型时,按自己的理解写出了这样的代码:

复制代码
Shader "CH6/SpecularPixelLevel"
{
    Properties
    {
        _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
        _Specular("Specular", Color) = (1, 1, 1, 1)
        _Gloss("Gloss", Range(8.0, 256)) = 20
    }
    SubShader
    {
        Pass
        {
            Tags { "RenderType"="ForwardBase" }
    
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"

            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;
            struct appdata_t
            {
                float4 pos: POSITION;
                float3 normal: NORMAL;
            };
            
            struct v2f
            {
                float4 pos: SV_POSITION;
                fixed4 worldPos: TEXCOORD0;
                float3 worldNormal: TEXCOORD1;
            };
    
            v2f vert(appdata_t v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.pos);
                o.worldPos = mul(unity_ObjectToWorld, v.pos);
                o.worldNormal = normalize(mul(v.normal, (fixed3x3)unity_WorldToObject));

                return o;
            }

            float4 frag(v2f i): SV_Target
            {
				// ambient
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                
                // diffuse
                fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                fixed3 diffuse = _Diffuse.rgb * _LightColor0.rgb * saturate(dot(i.worldNormal, worldLightDir));

                // specular
                fixed3 reflectDif = reflect(-worldLightDir, i.worldNormal);
                fixed3 viewDir = normalize(_WorldSpaceCameraPos - i.worldPos);
                fixed3 specular = _Specular.rgb * _LightColor0.rgb * pow(saturate(dot(reflectDif, viewDir)), _Gloss);

                fixed3 color = ambient + diffuse + specular;
                
                return fixed4(color, 1);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

结果代码和书上的不一样,书上在vertex shader没有将worldNormal归一化,归一化放在了fragment shader。我的代码是将顶点数据的worldNormal归一化了,所以片元数据插值后也是归一化的嘛?于是就想验证一下自己的想法,比较代码也挺简单:

复制代码
Shader "CH6/SpecularPixelLevel"
{
    Properties
    {
        _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
        _Specular("Specular", Color) = (1, 1, 1, 1)
        _Gloss("Gloss", Range(8.0, 256)) = 20
    }
    SubShader
    {
        Pass
        {
            Tags { "RenderType"="ForwardBase" }
    
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"

            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;
            struct appdata_t
            {
                float4 pos: POSITION;
                float3 normal: NORMAL;
            };
            
            struct v2f
            {
                float4 pos: SV_POSITION;
                fixed4 worldPos: TEXCOORD0;
                float3 worldNormal: TEXCOORD1;
            };
    
            v2f vert(appdata_t v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.pos);
                o.worldPos = mul(unity_ObjectToWorld, v.pos);
                o.worldNormal = normalize(mul(v.normal, (fixed3x3)unity_WorldToObject));

                return o;
            }

            float4 frag(v2f i): SV_Target
            {

				float worldNormalLength = length(i.worldNormal);
                float diff = abs(worldNormalLength - 1.0);
                fixed3 color = lerp(fixed3(1, 0, 0), fixed3(0, 1, 0), step(0.001, diff));
                return fixed4(color, 1);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

代码通过length(i.worldNormal)计算法向量长度

,abs(normalLength - 1.0)获取与 1 的偏差,

当偏差小于 0.001(近似为 1)时显示绿色,否则显示红色。结果显示

调整scale比例,长度不是1的法向量更多,但计算光照还是需要长度是1的法向量的:

书上的代码是没有问题的,vertex shader顶点数据不用归一化直接插值,插值后的片元数据再进行归一化。附上正确的代码:

复制代码
Shader "CH6/SpecularPixelLevel"
{
    Properties
    {
        _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
        _Specular("Specular", Color) = (1, 1, 1, 1)
        _Gloss("Gloss", Range(8.0, 256)) = 20
    }
    SubShader
    {
        Pass
        {
            Tags { "RenderType"="ForwardBase" }
    
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"

            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;
            struct appdata_t
            {
                float4 pos: POSITION;
                float3 normal: NORMAL;
            };
            
            struct v2f
            {
                float4 pos: SV_POSITION;
                fixed4 worldPos: TEXCOORD0;
                float3 worldNormal: TEXCOORD1;
            };
    
            v2f vert(appdata_t v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.pos);
                o.worldPos = mul(unity_ObjectToWorld, v.pos);
                o.worldNormal = mul(v.normal, (fixed3x3)unity_WorldToObject);

                return o;
            }

            float4 frag(v2f i): SV_Target
            {

                // ambient
                fixed3 worldNormal = normalize(i.worldNormal);
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                
                // diffuse
                fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                fixed3 diffuse = _Diffuse.rgb * _LightColor0.rgb * saturate(dot(worldNormal, worldLightDir));

                // specular
                fixed3 reflectDif = reflect(-worldLightDir, worldNormal);
                fixed3 viewDir = normalize(_WorldSpaceCameraPos - i.worldPos);
                fixed3 specular = _Specular.rgb * _LightColor0.rgb * pow(saturate(dot(reflectDif, viewDir)), _Gloss);

                fixed3 color = ambient + diffuse + specular;
                
                return fixed4(color, 1);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}
相关推荐
Lojarro2 小时前
GO学习2:基本数据类型 与 转换
后端·学习·golang
come112343 小时前
冀教版三年级上册英语-学习思路和引导方法
学习
派森先生3 小时前
sk07.【scikit-learn基础】--『监督学习』之支持向量机
学习·支持向量机·scikit-learn
wdfk_prog4 小时前
`git rm --cached`:如何让文件“脱离”版本控制
大数据·linux·c语言·笔记·git·学习·elasticsearch
lingliang4 小时前
机器学习之三大学习范式:监督学习、无监督学习、强化学习
人工智能·学习·机器学习
派森先生4 小时前
sk06.【scikit-learn基础】--『监督学习』之决策树
学习·决策树·scikit-learn
stolentime5 小时前
二维凸包——Andrew 算法学习笔记
c++·笔记·学习·算法·计算几何·凸包
【上下求索】5 小时前
学习笔记092——Windows如何将 jar 包启动设置成系统服务
java·windows·笔记·学习·jar
没头脑的男大5 小时前
jupyter notebook继续学习
ide·学习·jupyter