Skip to content

Commit 9e9e8bc

Browse files
authored
glTF Exporter: Fix light direction calculation (#16721)
The previous light direction calculation didn't account for when the Babylon light direction matched glTF's. This would result NaN values in RotationAxisToRef, as the rotation axis would be (0,0,0), and its length (0) used as a divisor. I noticed we already have a function to do what we need for the light direction calculation-- `FromUnitVectorsToRef`-- so I've replaced the whole thing with that. Much simpler. (Bonus: I also noticed that the lights in our glTF light export test don't use glTF falloff, so I've earmarked that change in this PR.)
1 parent 7e658d9 commit 9e9e8bc

File tree

4 files changed

+6
-8
lines changed

4 files changed

+6
-8
lines changed

packages/dev/serializers/src/glTF/2.0/Extensions/KHR_lights_punctual.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,15 @@ export class KHR_lights_punctual implements IGLTFExporterExtensionV2 {
107107
node.translation = translation.asArray();
108108
}
109109

110-
// Babylon lights have "constant" rotation and variable direction, while
111-
// glTF lights have variable rotation and constant direction. Therefore,
112-
// compute a quaternion that aligns the Babylon light's direction with glTF's constant one.
110+
// Represent the Babylon light's direction as a quaternion
111+
// relative to glTF lights' forward direction, (0, 0, -1).
113112
if (lightType !== KHRLightsPunctual_LightType.POINT) {
114113
const direction = babylonNode.direction.normalizeToRef(TmpVectors.Vector3[0]);
115114
if (convertToRightHanded) {
116115
ConvertToRightHandedPosition(direction);
117116
}
118-
const angle = Math.acos(Vector3.Dot(LIGHTDIRECTION, direction));
119-
const axis = Vector3.Cross(LIGHTDIRECTION, direction);
120-
const lightRotationQuaternion = Quaternion.RotationAxisToRef(axis, angle, TmpVectors.Quaternion[0]);
117+
118+
const lightRotationQuaternion = Quaternion.FromUnitVectorsToRef(LIGHTDIRECTION, direction, TmpVectors.Quaternion[0]);
121119
if (!Quaternion.IsIdentity(lightRotationQuaternion)) {
122120
node.rotation = lightRotationQuaternion.asArray();
123121
}
-5 Bytes
Loading
7 Bytes
Loading

packages/tools/tests/test/visualization/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,13 +1048,13 @@
10481048
},
10491049
{
10501050
"title": "GLTF Serializer KHR punctual light, left-handed",
1051-
"playgroundId": "#FLXW8B#25",
1051+
"playgroundId": "#FLXW8B#27",
10521052
"replace": "//options//, roundtrip = true; useRightHandedSystem = false;",
10531053
"referenceImage": "glTFSerializerKHRPunctualLightLH.png"
10541054
},
10551055
{
10561056
"title": "GLTF Serializer KHR punctual light, right-handed",
1057-
"playgroundId": "#FLXW8B#25",
1057+
"playgroundId": "#FLXW8B#27",
10581058
"replace": "//options//, roundtrip = true; useRightHandedSystem = true;",
10591059
"referenceImage": "glTFSerializerKHRPunctualLightRH.png"
10601060
},

0 commit comments

Comments
 (0)