Fix the rotation direction of the normals#7041
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughThis change reverses the order of vector-matrix multiplication for rotating 2D normal components in two terrain pixel shaders (Terrain200NormalsPS and Terrain200BNormalsPS). The multiplication order is changed from Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
effects/terrain.fx (1)
1841-1847:sampleNormalhelper has the same missing inverse rotation onnormalRotated— not currently active.
normalRotatedis sampled atmul(position, rotationMatrix)(clockwise UV rotation) but its XY components are not un-rotated before being passed tosplatBlendNormal. This means the rotated sample contributes incorrectly-oriented normals to the blend. This does not affect any currently rendered output because the only callers (TerrainPBRNormalsPS/ techniqueTerrainPBR) are fully commented out, but the bug would reappear if those techniques were re-enabled.🛠️ Proposed fix for when this code is un-commented
float3 sampleNormal(sampler2D s, float2 position, uniform float2 scale, float mask) { // 30° rotation float2x2 rotationMatrix = float2x2(float2(0.866, -0.5), float2(0.5, 0.866)); float3 normal = tex2D(s, position * scale).rgb; float3 normalRotated = tex2D(s, mul(position, rotationMatrix) * scale).rgb; - return splatBlendNormal(normalize(2 * normal - 1), normalize(2 * normalRotated - 1), 0.5, mask, 0.03); + float3 normalRotatedDecoded = normalize(2 * normalRotated - 1); + normalRotatedDecoded.xy = mul(rotationMatrix, normalRotatedDecoded.xy); + return splatBlendNormal(normalize(2 * normal - 1), normalRotatedDecoded, 0.5, mask, 0.03); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@effects/terrain.fx` around lines 1841 - 1847, The sampled rotated normal (normalRotated) is not being un-rotated before blending, so compute the normal vector from that sample (e.g., vec3 nRot = normalize(2 * normalRotated - 1)), apply the inverse rotation to its XY components (since rotationMatrix is a pure rotation the inverse is its transpose, e.g., float2x2 invRot = transpose(rotationMatrix); nRot.xy = mul(nRot.xy, invRot)), and then pass the corrected nRot into splatBlendNormal instead of the raw rotated sample; keep the other call parameters (0.5, mask, 0.03) and the original normal handling unchanged so the function (sampleNormal) uses properly un-rotated normals for splatBlendNormal.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@effects/terrain.fx`:
- Around line 1841-1847: The sampled rotated normal (normalRotated) is not being
un-rotated before blending, so compute the normal vector from that sample (e.g.,
vec3 nRot = normalize(2 * normalRotated - 1)), apply the inverse rotation to its
XY components (since rotationMatrix is a pure rotation the inverse is its
transpose, e.g., float2x2 invRot = transpose(rotationMatrix); nRot.xy =
mul(nRot.xy, invRot)), and then pass the corrected nRot into splatBlendNormal
instead of the raw rotated sample; keep the other call parameters (0.5, mask,
0.03) and the original normal handling unchanged so the function (sampleNormal)
uses properly un-rotated normals for splatBlendNormal.
The normals of the rotated texture layers of the Terrain200 shader need to be rotated to face the right direction. Unfortunately this rotation happened in the wrong direction. This is now fixed. Flipping the arguments of a matrix multiplication flips the rotation direction. The map editor needs the same fix.
Video of the issue:
wronglyrotatedlighting.mp4
Checklist
Summary by CodeRabbit