فهرست منبع

Bounding info tools dependencies

sebavan 6 سال پیش
والد
کامیت
52a4dff968

+ 1 - 7
Tools/Config/tempCircularValidation/core.json

@@ -1,7 +1,4 @@
 {
-    "../../src/Behaviors/Meshes/pointerDragBehavior.ts": [
-        "../../src/Gizmos/boundingBoxGizmo.ts"
-    ],
     "../../src/Culling/Octrees/octree.ts": [
         "../../src/Culling/Octrees/octreeBlock.ts"
     ],
@@ -11,9 +8,6 @@
     "../../src/Engines/engine.ts": [
         "../../src/Materials/Textures/internalTexture.ts"
     ],
-    "../../src/Gizmos/boundingBoxGizmo.ts": [
-        "../../src/Behaviors/Meshes/pointerDragBehavior.ts"
-    ],
     "../../src/Layers/effectLayer.ts": [
         "../../src/Layers/effectLayerSceneComponent.ts"
     ],
@@ -87,5 +81,5 @@
     "../../src/scene.ts": [
         "../../src/Materials/standardMaterial.ts"
     ],
-    "errorCount": 30
+    "errorCount": 28
 }

+ 5 - 5
src/Behaviors/Meshes/pointerDragBehavior.ts

@@ -6,8 +6,8 @@ import { Nullable } from "../../types";
 import { Observer, Observable } from "../../Misc/observable";
 import { Vector3 } from "../../Maths/math";
 import { PointerInfo, PointerEventTypes } from "../../Events/pointerEvents";
-import { BoundingBoxGizmo } from "../../Gizmos/boundingBoxGizmo";
 import { Ray } from "../../Culling/ray";
+import { PivotTools } from '../../Misc/pivotTools';
 
     /**
      * A behavior that when attached to a mesh will allow the mesh to be dragged around the screen based on pointer events
@@ -202,7 +202,7 @@ import { Ray } from "../../Culling/ray";
 
             this._beforeRenderObserver = this._scene.onBeforeRenderObservable.add(() => {
                 if (this._moving && this.moveAttached) {
-                    BoundingBoxGizmo._RemoveAndStorePivotPoint(this._attachedNode);
+                    PivotTools._RemoveAndStorePivotPoint(this._attachedNode);
                     // Slowly move mesh to avoid jitter
                     this._targetPosition.subtractToRef((this._attachedNode).absolutePosition, this._tmpVector);
                     this._tmpVector.scaleInPlace(this.dragDeltaRatio);
@@ -210,7 +210,7 @@ import { Ray } from "../../Culling/ray";
                     if (this.validateDrag(this._tmpVector)) {
                         (this._attachedNode).setAbsolutePosition(this._tmpVector);
                     }
-                    BoundingBoxGizmo._RestorePivotPoint(this._attachedNode);
+                    PivotTools._RestorePivotPoint(this._attachedNode);
                 }
             });
         }
@@ -257,7 +257,7 @@ import { Ray } from "../../Culling/ray";
                 return;
             }
 
-            BoundingBoxGizmo._RemoveAndStorePivotPoint(this._attachedNode);
+            PivotTools._RemoveAndStorePivotPoint(this._attachedNode);
             // Create start ray from the camera to the object
             if (fromRay) {
                 this._startDragRay.direction.copyFrom(fromRay.direction);
@@ -288,7 +288,7 @@ import { Ray } from "../../Culling/ray";
                     }
                 }
             }
-            BoundingBoxGizmo._RestorePivotPoint(this._attachedNode);
+            PivotTools._RestorePivotPoint(this._attachedNode);
         }
 
         private _dragDelta = new Vector3();

+ 9 - 42
src/Gizmos/boundingBoxGizmo.ts

@@ -13,6 +13,7 @@ import { _DepthCullingState, _StencilState, _AlphaState } from "../States/index"
 import { Gizmo } from "./gizmo";
 import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer";
 import { StandardMaterial } from "../Materials/standardMaterial";
+import { PivotTools } from "../Misc/pivotTools";
     /**
      * Bounding box gizmo
      */
@@ -85,40 +86,6 @@ import { StandardMaterial } from "../Materials/standardMaterial";
         private _dragMesh: Nullable<Mesh> = null;
         private pointerDragBehavior = new PointerDragBehavior();
 
-        // Stores the state of the pivot cache (_oldPivotPoint, _pivotTranslation)
-        // store/remove pivot point should only be applied during their outermost calls
-        private static _PivotCached = 0;
-        private static _OldPivotPoint = new Vector3();
-        private static _PivotTranslation = new Vector3();
-        private static _PivotTmpVector = new Vector3();
-        /** @hidden */
-        public static _RemoveAndStorePivotPoint(mesh: AbstractMesh) {
-            if (mesh && BoundingBoxGizmo._PivotCached === 0) {
-                // Save old pivot and set pivot to 0,0,0
-                mesh.getPivotPointToRef(BoundingBoxGizmo._OldPivotPoint);
-                if (!BoundingBoxGizmo._OldPivotPoint.equalsToFloats(0, 0, 0)) {
-                    mesh.setPivotMatrix(Matrix.IdentityReadOnly);
-                    BoundingBoxGizmo._OldPivotPoint.subtractToRef(mesh.getPivotPoint(), BoundingBoxGizmo._PivotTranslation);
-                    BoundingBoxGizmo._PivotTmpVector.copyFromFloats(1, 1, 1);
-                    BoundingBoxGizmo._PivotTmpVector.subtractInPlace(mesh.scaling);
-                    BoundingBoxGizmo._PivotTmpVector.multiplyInPlace(BoundingBoxGizmo._PivotTranslation);
-                    mesh.position.addInPlace(BoundingBoxGizmo._PivotTmpVector);
-                }
-            }
-            BoundingBoxGizmo._PivotCached++;
-        }
-        /** @hidden */
-        public static _RestorePivotPoint(mesh: AbstractMesh) {
-            if (mesh && !BoundingBoxGizmo._OldPivotPoint.equalsToFloats(0, 0, 0) && BoundingBoxGizmo._PivotCached === 1) {
-                mesh.setPivotPoint(BoundingBoxGizmo._OldPivotPoint);
-                BoundingBoxGizmo._PivotTmpVector.copyFromFloats(1, 1, 1);
-                BoundingBoxGizmo._PivotTmpVector.subtractInPlace(mesh.scaling);
-                BoundingBoxGizmo._PivotTmpVector.multiplyInPlace(BoundingBoxGizmo._PivotTranslation);
-                mesh.position.subtractInPlace(BoundingBoxGizmo._PivotTmpVector);
-            }
-            this._PivotCached--;
-        }
-
         /**
          * Creates an BoundingBoxGizmo
          * @param gizmoLayer The utility layer the gizmo will be added to
@@ -185,7 +152,7 @@ import { StandardMaterial } from "../Materials/standardMaterial";
                 _dragBehavior.onDragObservable.add((event) => {
                     this.onRotationSphereDragObservable.notifyObservers({});
                     if (this.attachedMesh) {
-                        BoundingBoxGizmo._RemoveAndStorePivotPoint(this.attachedMesh);
+                        PivotTools._RemoveAndStorePivotPoint(this.attachedMesh);
 
                         var worldDragDirection = startingTurnDirection;
 
@@ -225,7 +192,7 @@ import { StandardMaterial } from "../Materials/standardMaterial";
                         }
                         this.updateBoundingBox();
 
-                        BoundingBoxGizmo._RestorePivotPoint(this.attachedMesh);
+                        PivotTools._RestorePivotPoint(this.attachedMesh);
                     }
                     this._updateDummy();
                 });
@@ -262,7 +229,7 @@ import { StandardMaterial } from "../Materials/standardMaterial";
                         _dragBehavior.onDragObservable.add((event) => {
                             this.onScaleBoxDragObservable.notifyObservers({});
                             if (this.attachedMesh) {
-                                BoundingBoxGizmo._RemoveAndStorePivotPoint(this.attachedMesh);
+                                PivotTools._RemoveAndStorePivotPoint(this.attachedMesh);
                                 var relativeDragDistance = (event.dragDistance / this._boundingDimensions.length()) * this._anchorMesh.scaling.length();
                                 var deltaScale = new Vector3(relativeDragDistance, relativeDragDistance, relativeDragDistance);
                                 deltaScale.scaleInPlace(this._scaleDragSpeed);
@@ -289,7 +256,7 @@ import { StandardMaterial } from "../Materials/standardMaterial";
                                 }
                                 this._anchorMesh.removeChild(this.attachedMesh);
 
-                                BoundingBoxGizmo._RestorePivotPoint(this.attachedMesh);
+                                PivotTools._RestorePivotPoint(this.attachedMesh);
                             }
                             this._updateDummy();
                         });
@@ -352,10 +319,10 @@ import { StandardMaterial } from "../Materials/standardMaterial";
             if (value) {
                 // Reset anchor mesh to match attached mesh's scale
                 // This is needed to avoid invalid box/sphere position on first drag
-                BoundingBoxGizmo._RemoveAndStorePivotPoint(value);
+                PivotTools._RemoveAndStorePivotPoint(value);
                 this._anchorMesh.addChild(value);
                 this._anchorMesh.removeChild(value);
-                BoundingBoxGizmo._RestorePivotPoint(value);
+                PivotTools._RestorePivotPoint(value);
                 this.updateBoundingBox();
 
                 this.gizmoLayer.utilityLayerScene.onAfterRenderObservable.addOnce(() => {
@@ -376,7 +343,7 @@ import { StandardMaterial } from "../Materials/standardMaterial";
          */
         public updateBoundingBox() {
             if (this.attachedMesh) {
-                BoundingBoxGizmo._RemoveAndStorePivotPoint(this.attachedMesh);
+                PivotTools._RemoveAndStorePivotPoint(this.attachedMesh);
                 this._update();
                 // Rotate based on axis
                 if (!this.attachedMesh.rotationQuaternion) {
@@ -415,7 +382,7 @@ import { StandardMaterial } from "../Materials/standardMaterial";
 
             if (this.attachedMesh) {
                 this._existingMeshScale.copyFrom(this.attachedMesh.scaling);
-                BoundingBoxGizmo._RestorePivotPoint(this.attachedMesh);
+                PivotTools._RestorePivotPoint(this.attachedMesh);
             }
         }
 

+ 42 - 0
src/Misc/pivotTools.ts

@@ -0,0 +1,42 @@
+import { Vector3, Matrix } from '../Maths/math';
+import { AbstractMesh } from '../Meshes/abstractMesh';
+
+/**
+ * Class containing a set of static utilities functions for managing Pivots
+ * @hidden
+ */
+export class PivotTools {
+    // Stores the state of the pivot cache (_oldPivotPoint, _pivotTranslation)
+    // store/remove pivot point should only be applied during their outermost calls
+    private static _PivotCached = 0;
+    private static _OldPivotPoint = new Vector3();
+    private static _PivotTranslation = new Vector3();
+    private static _PivotTmpVector = new Vector3();
+    /** @hidden */
+    public static _RemoveAndStorePivotPoint(mesh: AbstractMesh) {
+        if (mesh && PivotTools._PivotCached === 0) {
+            // Save old pivot and set pivot to 0,0,0
+            mesh.getPivotPointToRef(PivotTools._OldPivotPoint);
+            if (!PivotTools._OldPivotPoint.equalsToFloats(0, 0, 0)) {
+                mesh.setPivotMatrix(Matrix.IdentityReadOnly);
+                PivotTools._OldPivotPoint.subtractToRef(mesh.getPivotPoint(), PivotTools._PivotTranslation);
+                PivotTools._PivotTmpVector.copyFromFloats(1, 1, 1);
+                PivotTools._PivotTmpVector.subtractInPlace(mesh.scaling);
+                PivotTools._PivotTmpVector.multiplyInPlace(PivotTools._PivotTranslation);
+                mesh.position.addInPlace(PivotTools._PivotTmpVector);
+            }
+        }
+        PivotTools._PivotCached++;
+    }
+    /** @hidden */
+    public static _RestorePivotPoint(mesh: AbstractMesh) {
+        if (mesh && !PivotTools._OldPivotPoint.equalsToFloats(0, 0, 0) && PivotTools._PivotCached === 1) {
+            mesh.setPivotPoint(PivotTools._OldPivotPoint);
+            PivotTools._PivotTmpVector.copyFromFloats(1, 1, 1);
+            PivotTools._PivotTmpVector.subtractInPlace(mesh.scaling);
+            PivotTools._PivotTmpVector.multiplyInPlace(PivotTools._PivotTranslation);
+            mesh.position.subtractInPlace(PivotTools._PivotTmpVector);
+        }
+        this._PivotCached--;
+    }
+}