瀏覽代碼

Merge branch 'master' of https://github.com/BabylonJS/Babylon.js

David Catuhe 5 年之前
父節點
當前提交
465c5f5cb1
共有 3 個文件被更改,包括 40 次插入9 次删除
  1. 1 0
      dist/preview release/what's new.md
  2. 3 9
      src/Meshes/mesh.ts
  3. 36 0
      src/Meshes/transformNode.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -312,3 +312,4 @@
 - The glTF2 exporter extension no longer ignores childless empty nodes.([drigax](https://github.com/drigax))
 - Default culling strategy changed to CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY ([Deltakosh](https://github.com/deltakosh/))
 - `MaterialHelper.BindLight` and `MaterialHelper.BindLights` do not need the usePhysicalLight anymore ([Sebavan](https://github.com/sebavan/))
+- `Mesh.bakeTransformIntoVertices` now preserves child world-space transforms([drigax](https://github.com/drigax))

+ 3 - 9
src/Meshes/mesh.ts

@@ -2151,18 +2151,12 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
      * This method returns nothing but really modifies the mesh even if it's originally not set as updatable.
      * Note that, under the hood, this method sets a new VertexBuffer each call.
      * @see http://doc.babylonjs.com/resources/baking_transformations
+     * @param bakeIndependenlyOfChildren indicates whether to preserve all child nodes' World Matrix during baking
      * @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;
     }
 

+ 36 - 0
src/Meshes/transformNode.ts

@@ -1168,6 +1168,42 @@ export class TransformNode extends Node {
         return this._worldMatrix;
     }
 
+    /**
+     * Resets this nodeTransform's local matrix to Matrix.Identity().
+     * @param independentOfChildren indicates if all child nodeTransform's world-space transform should be preserved.
+     */
+    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 {
     }