Skip to content

Commit

Permalink
Fix for issue with tangents rotating the wrong direction with mirrore…
Browse files Browse the repository at this point in the history
…d uv coordinates.
  • Loading branch information
Lehm2000 committed Aug 2, 2024
1 parent c8637a2 commit abd2483
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion RT/Renderer/Backend/DX12/assets/shaders/include/common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit abd2483

Please sign in to comment.