Transform normals tangent to world

Hi,

I’m struggling to convert a normal map from tangent space to world space in HLSL in URP.

I have this that works for the rotation, but when the mesh gets away from the origin it stops working properly.

// Data from vertex shader in world space
half3 binormal = i.binormal;
half3 tangent = i.tangent;
half3 worldVertexNormal = i.normal;

float3x3 tangentTransform = float3x3(tangent, binormal, i.normal);

// Normal Map
half4 normalMap = _NormalMap.Sample(sampler_linear_repeat, i.texcoord);
half3 unpackedNormal = UnpackNormal(normalMap);

// Tangent To World
half3 worldNormal = TransformTangentToWorld(unpackedNormal, tangentTransform, true);

 
Light main = ComputeMainLight(i.worldPos);
half3 lightDir = main.direction;

return saturate(dot(worldNormal, lightDir));

view at the origin:

away from the origin:

What am I missing?
Thanks!

Just realized that I was using TransformObjectToWorld in the vertex shader to transform the tangent and binormal. Turns out this is for positions and they have TransformObjectToWorldDir for directions :man_facepalming:t2: