Skip to content

第七章关于法线贴图反映射的计算 #341

@shixianming

Description

@shixianming

在源码中片元着色其是这样的:
fixed4 frag(v2f i) : SV_Target {
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);

			// Get the texel in the normal map
			fixed4 packedNormal = tex2D(_BumpMap, i.uv.zw);
			fixed3 tangentNormal;
			// If the texture is not marked as "Normal map"

// tangentNormal.xy = (packedNormal.xy * 2 - 1) * _BumpScale;
// tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));

			// Or mark the texture as "Normal map", and use the built-in funciton
			tangentNormal = UnpackNormal(packedNormal);
			tangentNormal.xy *= _BumpScale;
			tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
			
			fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
			
			fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
			
			fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentLightDir));

			fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
			fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(tangentNormal, halfDir)), _Gloss);
			
			return fixed4(ambient + diffuse + specular, 1.0);
		}

示例中贴图的Texture Type已选择为Normal map
有如下疑惑
1.以上代码中手动反映射法线和调用UnpackNormal反映射似乎没什么区别
2.在UnpackNormal中已根据压缩格式对反映射做了处理,包括z分量,为什么在调用UnpackNormal后还要再计算一次z分量
我尝试把这一行注释掉,但是高光部分似乎变暗了些,我不知道什么才是正确,希望大佬们解答一下,十分感谢

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions