How to convert a normal map from tangent space to world space #8620
Replies: 2 comments
-
If your model contains normal and tangent attributes,
Does that mean your model does not contain normal and tangent attribute? |
Beta Was this translation helpful? Give feedback.
-
@show50726 The normal map is like below , My code in filament fragment is like below, Just can get black result:
I change the mat file in demo "sample-lit-cube" in android and the demo “sample_full_pbr.cpp” to check result. |
Beta Was this translation helpful? Give feedback.
-
I need convert a PNG normal map from tangent space to world space.
First I used getWorldTangentFrame() * normalMap, but the value is not right.
I try to calculate TBN matrix in vertex shader. I realized that I couldn't get the normal and tangent values.
Many thanks if anyone can give any helpful guidance。
The following is the method in Unity that I referenced:
vertex:
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
float3 worldBinormal = cross(worldNormal,worldTangent)*v.tangent.w;
o.T2W0 = float4(worldTangent.x,worldBinormal.x,worldNormal.x, worldPos.x);
o.T2W1 = float4(worldTangent.y,worldBinormal.y,worldNormal.y, worldPos.y);
o.T2W2 = float4(worldTangent.z,worldBinormal.z,worldNormal.z, worldPos.z);
fragment:
float4 Normaltex = tex2D(_BumpMap, i.uv);
float3 tangentNormal = UnpackNormal(Normaltex);
tangentNormal.xy *= 1.0;
tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
float3x3 T2WMatrix = float3x3(i.T2W0.xyz,i.T2W1.xyz,i.T2W2.xyz);
float3 worldNormal = mul(T2WMatrix,tangentNormal);
Beta Was this translation helpful? Give feedback.
All reactions