Browse Source

calculate light rotation for spot and directional light

Kacey Coley 6 years ago
parent
commit
f03cebfa3b
1 changed files with 5 additions and 2 deletions
  1. 5 2
      serializers/src/glTF/2.0/Extensions/KHR_lights_punctual.ts

+ 5 - 2
serializers/src/glTF/2.0/Extensions/KHR_lights_punctual.ts

@@ -119,8 +119,11 @@ export class KHR_lights_punctual implements IGLTFExporterExtensionV2 {
                         node.translation = lightPosition.asArray();
                     }
                     if (lightType !== LightType.POINT) {
-                        const lightRotation = babylonLight.getRotation();
-                        let lightRotationQuaternion = Quaternion.RotationYawPitchRoll(lightRotation.y, lightRotation.x, lightRotation.z);
+                        const localAxis = babylonLight.direction;
+                        const yaw = -Math.atan2(localAxis.z, localAxis.x) + Math.PI / 2;
+                        const len = Math.sqrt(localAxis.x * localAxis.x + localAxis.z * localAxis.z);
+                        const pitch = -Math.atan2(localAxis.y, len);
+                        const lightRotationQuaternion = Quaternion.RotationYawPitchRoll(yaw, pitch, 0);
                         if (this._exporter._convertToRightHandedSystem) {
                             _GLTFUtilities._GetRightHandedQuaternionFromRef(lightRotationQuaternion);
                         }