Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shixianming opened this issue Dec 19, 2023 · 0 comments
Open

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

shixianming opened this issue Dec 19, 2023 · 0 comments

Comments

@shixianming
Copy link

在源码中片元着色其是这样的:
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分量
我尝试把这一行注释掉,但是高光部分似乎变暗了些,我不知道什么才是正确,希望大佬们解答一下,十分感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant