Преглед изворни кода

Gizmo Mesh Upgrade - PR Revisions

Dave Gould пре 4 година
родитељ
комит
c8e2edfe4a

+ 10 - 1
src/Gizmos/gizmo.ts

@@ -11,14 +11,23 @@ import { Node } from "../node";
 import { Bone } from "../Bones/bone";
 import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer";
 import { TransformNode } from '../Meshes/transformNode';
-import { StandardMaterial } from 'Materials';
+import { StandardMaterial } from '../Materials/standardMaterial';
 
+/**
+ * Cache built by each axis. Used for managing state between all elements of gizmo for enhanced UI
+ */
 export interface GizmoAxisCache {
+    /** Mesh used to runder the Gizmo */
     gizmoMeshes: Mesh[];
+    /** Mesh used to detect user interaction with Gizmo */
     colliderMeshes: Mesh[];
+    /** Material used to inicate color of gizmo mesh */
     material: StandardMaterial;
+    /** Material used to inicate hover state of the Gizmo */
     hoverMaterial: StandardMaterial;
+    /** Material used to inicate disabled state of the Gizmo */
     disableMaterial: StandardMaterial;
+    /** Used to indicate Active state of the Gizmo */
     active: boolean;
 }
 /**

+ 4 - 38
src/Gizmos/planeRotationGizmo.ts

@@ -3,6 +3,7 @@ import { Nullable } from "../types";
 import { PointerInfo } from "../Events/pointerEvents";
 import { Quaternion, Matrix, Vector3 } from "../Maths/math.vector";
 import { Color3 } from '../Maths/math.color';
+import "../Meshes/Builders/linesBuilder";
 import { AbstractMesh } from "../Meshes/abstractMesh";
 import { LinesMesh } from '../Meshes/linesMesh';
 import { Mesh } from "../Meshes/mesh";
@@ -10,10 +11,8 @@ import { Node } from "../node";
 import { PointerDragBehavior } from "../Behaviors/Meshes/pointerDragBehavior";
 import { Gizmo, GizmoAxisCache } from "./gizmo";
 import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer";
-import "../Meshes/Builders/linesBuilder";   // Why
 import { StandardMaterial } from "../Materials/standardMaterial";
 import { RotationGizmo } from "./rotationGizmo";
-import { Angle } from '../Maths/math.path';
 
 /**
  * Single plane rotation gizmo
@@ -104,7 +103,6 @@ export class PlaneRotationGizmo extends Gizmo {
                 lastDragPosition.copyFrom(e.dragPlanePoint);
 
                 // This is for instantiation location of rotation circle
-                // Rotation Circle Forward Vector
                 const forward = new Vector3(0, 0, 1);
                 const direction = this._rotationCircle.getDirection(forward);
                 direction.normalize();
@@ -115,13 +113,9 @@ export class PlaneRotationGizmo extends Gizmo {
                 lastDragPosition.copyFrom(e.dragPlanePoint);
                 dragPlanePoint = e.dragPlanePoint;
                 const origin = this._rotationCircle.getAbsolutePosition().clone();
-                const originalRotationVector = this._rotationCircle.getAbsolutePosition().clone().addInPlace(direction);
-                const dragStartVector = e.dragPlanePoint;
-                let angle = this.angleBetween3DCoords(origin, originalRotationVector, dragStartVector);
-
-                if (Vector3.Dot(this._rotationCircle.up, Vector3.Down()) > 0) {
-                    angle = -angle;
-                }
+                const originalRotationPoint = this._rotationCircle.getAbsolutePosition().clone().addInPlace(direction);
+                const dragStartPoint = e.dragPlanePoint;
+                let angle = Vector3.GetAngleBetweenVectors(originalRotationPoint.subtract(origin), dragStartPoint.subtract(origin), this._rotationCircle.up);
 
                 this._rotationCircle.addRotation(0, angle, 0);
             }
@@ -273,34 +267,6 @@ export class PlaneRotationGizmo extends Gizmo {
         }
     }
 
-    private angleBetween3DCoords(origin: Vector3, coord1: Vector3, coord2: Vector3): number {
-        // The dot product of vectors v1 & v2 is a function of the cosine of the angle between them scaled by the product of their magnitudes.
-        const v1 = new Vector3(coord1.x - origin.x, coord1.y - origin.y, coord1.z - origin.z);
-        const v2 = new Vector3(coord2.x - origin.x, coord2.y - origin.y, coord2.z - origin.z);
-
-        // Normalize v1
-        const v1mag = Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
-        const v1norm = new Vector3(v1.x / v1mag, v1.y / v1mag, v1.z / v1mag);
-
-        // Normalize v2
-        const v2mag = Math.sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z);
-        const v2norm = new Vector3(v2.x / v2mag, v2.y / v2mag, v2.z / v2mag);
-
-        // Calculate the dot products of vectors v1 and v2
-        const dotProducts = v1norm.x * v2norm.x + v1norm.y * v2norm.y + v1norm.z * v2norm.z;
-        const cross = Vector3.Cross(v1norm as any, v2norm as any);
-
-        // Extract the angle from the dot products
-        let angle = (Math.acos(dotProducts) * 180.0) / Math.PI;
-        angle = Math.round(angle * 1000) / 1000;
-        angle = Angle.FromDegrees(angle).radians();
-
-        // Flip if its cross has negitive y orientation
-        if (cross.y < 0) { angle = -angle; }
-
-        return angle;
-    }
-
     private setupRotationCircle(paths: Vector3[][], parentMesh: AbstractMesh): Mesh {
         const fillRadians = 0;
         const step = PlaneRotationGizmo._CircleConstants.pi2 / PlaneRotationGizmo._CircleConstants.tessellation;

+ 2 - 2
src/Gizmos/positionGizmo.ts

@@ -47,7 +47,7 @@ export class PositionGizmo extends Gizmo {
     private _meshAttached: Nullable<AbstractMesh> = null;
     private _nodeAttached: Nullable<Node> = null;
     private _snapDistance: number;
-    private _observables: Nullable<Observer<PointerInfo>>[] = [];
+    private _observables: Observer<PointerInfo>[] = [];
 
     /** Gizmo state variables used for UI behavior */
     private _dragging = false;
@@ -269,7 +269,7 @@ export class PositionGizmo extends Gizmo {
             }
         });
 
-        this._observables = [pointerObserver];
+        this._observables = [pointerObserver!];
     }
 
     /**

+ 2 - 2
src/Gizmos/rotationGizmo.ts

@@ -36,7 +36,7 @@ export class RotationGizmo extends Gizmo {
 
     private _meshAttached: Nullable<AbstractMesh>;
     private _nodeAttached: Nullable<Node>;
-    private _observables: Nullable<Observer<PointerInfo>>[] = [];
+    private _observables: Observer<PointerInfo>[] = [];
 
     /** Gizmo state variables used for UI behavior */
     private _dragging = false;
@@ -228,7 +228,7 @@ export class RotationGizmo extends Gizmo {
             }
         });
 
-        this._observables = [pointerObserver];
+        this._observables = [pointerObserver!];
     }
 
     /**

+ 2 - 3
src/Gizmos/scaleGizmo.ts

@@ -44,7 +44,7 @@ export class ScaleGizmo extends Gizmo {
     private _coloredMaterial: StandardMaterial;
     private _hoverMaterial: StandardMaterial;
     private _disableMaterial: StandardMaterial;
-    private _observables: Nullable<Observer<PointerInfo>>[] = [];
+    private _observables: Observer<PointerInfo>[] = [];
 
     /** Gizmo state variables used for UI behavior */
     private _dragging = false;
@@ -294,8 +294,7 @@ export class ScaleGizmo extends Gizmo {
                 }
             }
         });
-
-        this._observables = [pointerObserver];
+        this._observables = [pointerObserver!];
     }
 
     /**