Pārlūkot izejas kodu

Fix for mesh bakeCurrentTransformIntoVertices() where child transforms are not properly preserved. Added NodeTransform.resetLocalMatrix() method to allow for safe reset of transform independent of child transforms

Nicholas Barlow 5 gadi atpakaļ
vecāks
revīzija
d813d68519
2 mainītis faili ar 34 papildinājumiem un 9 dzēšanām
  1. 2 9
      src/Meshes/mesh.ts
  2. 32 0
      src/Meshes/transformNode.ts

+ 2 - 9
src/Meshes/mesh.ts

@@ -2153,16 +2153,9 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
      * @see http://doc.babylonjs.com/resources/baking_transformations
      * @returns the current mesh
      */
-    public bakeCurrentTransformIntoVertices(): Mesh {
+    public bakeCurrentTransformIntoVertices(bakeIndependenlyOfChildren : boolean = true): Mesh {
         this.bakeTransformIntoVertices(this.computeWorldMatrix(true));
-        this.scaling.copyFromFloats(1, 1, 1);
-        this.position.copyFromFloats(0, 0, 0);
-        this.rotation.copyFromFloats(0, 0, 0);
-        //only if quaternion is already set
-        if (this.rotationQuaternion) {
-            this.rotationQuaternion = Quaternion.Identity();
-        }
-        this._worldMatrix = Matrix.Identity();
+        this.resetLocalMatrix(bakeIndependenlyOfChildren);
         return this;
     }
 

+ 32 - 0
src/Meshes/transformNode.ts

@@ -1168,6 +1168,38 @@ export class TransformNode extends Node {
         return this._worldMatrix;
     }
 
+    public resetLocalMatrix(independentOfChildren : boolean = true): void
+    {
+        this.computeWorldMatrix();
+        if (independentOfChildren){
+            let children = this.getChildren();
+            for(let i = 0; i < children.length; ++i){
+                let child = children[i] as TransformNode;
+                if (child){
+                    child.computeWorldMatrix();
+                    let bakedMatrix = TmpVectors.Matrix[0];
+                    child._localMatrix.multiplyToRef(this._localMatrix, bakedMatrix);
+                    let tmpRotationQuaternion = TmpVectors.Quaternion[0];
+                    bakedMatrix.decompose(child.scaling, tmpRotationQuaternion, child.position);
+                    if (child.rotationQuaternion){
+                        child.rotationQuaternion = tmpRotationQuaternion;
+                    } else {
+                        tmpRotationQuaternion.toEulerAnglesToRef(child.rotation);
+                    }
+                }
+            }
+        }
+        this.scaling.copyFromFloats(1, 1, 1);
+        this.position.copyFromFloats(0, 0, 0);
+        this.rotation.copyFromFloats(0, 0, 0);
+
+        //only if quaternion is already set
+        if (this.rotationQuaternion) {
+            this.rotationQuaternion = Quaternion.Identity();
+        }
+        this._worldMatrix = Matrix.Identity();
+    }
+
     protected _afterComputeWorldMatrix(): void {
     }