From abd2483f52e594128796148ea792e089721acbc3 Mon Sep 17 00:00:00 2001 From: Jeff Adams Date: Thu, 1 Aug 2024 20:02:07 -0400 Subject: [PATCH] Fix for issue with tangents rotating the wrong direction with mirrored uv coordinates. --- .../DX12/assets/shaders/include/common.hlsl | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/RT/Renderer/Backend/DX12/assets/shaders/include/common.hlsl b/RT/Renderer/Backend/DX12/assets/shaders/include/common.hlsl index b21d62a..b188e21 100644 --- a/RT/Renderer/Backend/DX12/assets/shaders/include/common.hlsl +++ b/RT/Renderer/Backend/DX12/assets/shaders/include/common.hlsl @@ -714,9 +714,24 @@ void GetHitMaterialAndUVs(InstanceData instance_data, RT_Triangle hit_triangle, if (albedo2.a > 0.0) { material_index = material_index2; - if(orient != 0) + if (orient != 0) { uv = uv_rotated; + + // Calculate UV direction vectors + float2 uvEdge1 = hit_triangle.uv1 - hit_triangle.uv0; + float2 uvEdge2 = hit_triangle.uv2 - hit_triangle.uv0; + + // Compute the determinant + float det = uvEdge1.x * uvEdge2.y - uvEdge1.y * uvEdge2.x; + + if (det < 0.0 && orient % 2 == 1) + { + // if the uv coords are mirrored and the orientation is 1 or 3 (90 and 270 degress) add 180 degress + // this is because 90 and 270 need to be swapped when uvs are mirrored and its easier to just add 180 degress. + orient += 2; + } + tangent = GetRotatedTangent(orient, normal, tangent); } }