Sfoglia il codice sorgente

Merge pull request #8896 from CedricGuillemet/gizmoParenting

Gizmo parenting
sebavan 5 anni fa
parent
commit
91f79bdd65
3 ha cambiato i file con 17 aggiunte e 28 eliminazioni
  1. 9 14
      src/Gizmos/axisDragGizmo.ts
  2. 1 1
      src/Gizmos/gizmo.ts
  3. 7 13
      src/Gizmos/planeDragGizmo.ts

+ 9 - 14
src/Gizmos/axisDragGizmo.ts

@@ -1,7 +1,7 @@
 import { Observer, Observable } from "../Misc/observable";
 import { Nullable } from "../types";
 import { PointerInfo } from "../Events/pointerEvents";
-import { Vector3, Matrix } from "../Maths/math.vector";
+import { Vector3 } from "../Maths/math.vector";
 import { TransformNode } from "../Meshes/transformNode";
 import { Node } from "../node";
 import { Mesh } from "../Meshes/mesh";
@@ -101,32 +101,27 @@ export class AxisDragGizmo extends Gizmo {
         this.dragBehavior.moveAttached = false;
         this._rootMesh.addBehavior(this.dragBehavior);
 
-        var localDelta = new Vector3();
-        var tmpMatrix = new Matrix();
         this.dragBehavior.onDragObservable.add((event) => {
             if (this.attachedNode) {
-                // Convert delta to local translation if it has a parent
-                if (this.attachedNode.parent) {
-                    this.attachedNode.parent.getWorldMatrix().invertToRef(tmpMatrix);
-                    tmpMatrix.setTranslationFromFloats(0, 0, 0);
-                    Vector3.TransformCoordinatesToRef(event.delta, tmpMatrix, localDelta);
-                } else {
-                    localDelta.copyFrom(event.delta);
-                }
+                // Keep world translation and use it to update world transform
+                // if the node has parent, the local transform properties (position, rotation, scale)
+                // will be recomputed in _matrixChanged function
+
                 // Snapping logic
                 if (this.snapDistance == 0) {
                     if ((this.attachedNode as any).position) { // Required for nodes like lights
-                        (this.attachedNode as any).position.addInPlaceFromFloats(localDelta.x, localDelta.y, localDelta.z);
+                        (this.attachedNode as any).position.addInPlaceFromFloats(event.delta.x, event.delta.y, event.delta.z);
                     }
+
                     // use _worldMatrix to not force a matrix update when calling GetWorldMatrix especialy with Cameras
-                    this.attachedNode._worldMatrix.addTranslationFromFloats(localDelta.x, localDelta.y, localDelta.z);
+                    this.attachedNode._worldMatrix.addTranslationFromFloats(event.delta.x, event.delta.y, event.delta.z);
                     this.attachedNode.updateCache();
                 } else {
                     currentSnapDragDistance += event.dragDistance;
                     if (Math.abs(currentSnapDragDistance) > this.snapDistance) {
                         var dragSteps = Math.floor(Math.abs(currentSnapDragDistance) / this.snapDistance);
                         currentSnapDragDistance = currentSnapDragDistance % this.snapDistance;
-                        localDelta.normalizeToRef(tmpVector);
+                        event.delta.normalizeToRef(tmpVector);
                         tmpVector.scaleInPlace(this.snapDistance * dragSteps);
                         this.attachedNode._worldMatrix.addTranslationFromFloats(tmpVector.x, tmpVector.y, tmpVector.z);
                         this.attachedNode.updateCache();

+ 1 - 1
src/Gizmos/gizmo.ts

@@ -234,7 +234,7 @@ export class Gizmo implements IDisposable {
                 var parentInv = this._tempMatrix1;
                 var localMat = this._tempMatrix2;
                 transform.parent.getWorldMatrix().invertToRef(parentInv);
-                this._attachedNode._worldMatrix.multiplyToRef(parentInv, localMat);
+                this._attachedNode.getWorldMatrix().multiplyToRef(parentInv, localMat);
                 localMat.decompose(transform.scaling, this._tempQuaternion, transform.position);
             } else {
                 this._attachedNode._worldMatrix.decompose(transform.scaling, this._tempQuaternion, transform.position);

+ 7 - 13
src/Gizmos/planeDragGizmo.ts

@@ -1,7 +1,7 @@
 import { Observer, Observable } from "../Misc/observable";
 import { Nullable } from "../types";
 import { PointerInfo } from "../Events/pointerEvents";
-import { Vector3, Matrix } from "../Maths/math.vector";
+import { Vector3 } from "../Maths/math.vector";
 import { Color3 } from '../Maths/math.color';
 import { TransformNode } from "../Meshes/transformNode";
 import { Node } from "../node";
@@ -92,27 +92,21 @@ export class PlaneDragGizmo extends Gizmo {
         this.dragBehavior.moveAttached = false;
         this._rootMesh.addBehavior(this.dragBehavior);
 
-        var localDelta = new Vector3();
-        var tmpMatrix = new Matrix();
         this.dragBehavior.onDragObservable.add((event) => {
             if (this.attachedNode) {
-                // Convert delta to local translation if it has a parent
-                if (this.attachedNode.parent) {
-                    this.attachedNode.parent.computeWorldMatrix().invertToRef(tmpMatrix);
-                    tmpMatrix.setTranslationFromFloats(0, 0, 0);
-                    Vector3.TransformCoordinatesToRef(event.delta, tmpMatrix, localDelta);
-                } else {
-                    localDelta.copyFrom(event.delta);
-                }
+                // Keep world translation and use it to update world transform
+                // if the node has parent, the local transform properties (position, rotation, scale)
+                // will be recomputed in _matrixChanged function
+
                 // Snapping logic
                 if (this.snapDistance == 0) {
-                    this.attachedNode.getWorldMatrix().addTranslationFromFloats(localDelta.x, localDelta.y, localDelta.z);
+                    this.attachedNode.getWorldMatrix().addTranslationFromFloats(event.delta.x, event.delta.y, event.delta.z);
                 } else {
                     currentSnapDragDistance += event.dragDistance;
                     if (Math.abs(currentSnapDragDistance) > this.snapDistance) {
                         var dragSteps = Math.floor(Math.abs(currentSnapDragDistance) / this.snapDistance);
                         currentSnapDragDistance = currentSnapDragDistance % this.snapDistance;
-                        localDelta.normalizeToRef(tmpVector);
+                        event.delta.normalizeToRef(tmpVector);
                         tmpVector.scaleInPlace(this.snapDistance * dragSteps);
                         this.attachedNode.getWorldMatrix().addTranslationFromFloats(tmpVector.x, tmpVector.y, tmpVector.z);
                         tmpSnapEvent.snapDistance = this.snapDistance * dragSteps;