فهرست منبع

Bounding box fixedScale not updating

Trevor Baron 7 سال پیش
والد
کامیت
b4a93556e1
2فایلهای تغییر یافته به همراه51 افزوده شده و 35 حذف شده
  1. 9 2
      src/Gizmos/babylon.boundingBoxGizmo.ts
  2. 42 33
      src/Gizmos/babylon.gizmo.ts

+ 9 - 2
src/Gizmos/babylon.boundingBoxGizmo.ts

@@ -267,6 +267,7 @@ module BABYLON {
          * Updates the bounding box information for the Gizmo
          * Updates the bounding box information for the Gizmo
          */
          */
         public updateBoundingBox(){
         public updateBoundingBox(){
+            this._update();
             if(this.attachedMesh){             
             if(this.attachedMesh){             
                 // Rotate based on axis
                 // Rotate based on axis
                 if(!this.attachedMesh.rotationQuaternion){
                 if(!this.attachedMesh.rotationQuaternion){
@@ -300,7 +301,7 @@ module BABYLON {
                 this.attachedMesh.position.copyFrom(this._tmpVector);
                 this.attachedMesh.position.copyFrom(this._tmpVector);
                 this._recurseComputeWorld(this.attachedMesh);
                 this._recurseComputeWorld(this.attachedMesh);
             }
             }
-
+            
             // Update rotation sphere locations
             // Update rotation sphere locations
             var rotateSpheres = this._rotateSpheresParent.getChildMeshes();
             var rotateSpheres = this._rotateSpheresParent.getChildMeshes();
             for(var i=0;i<3;i++){
             for(var i=0;i<3;i++){
@@ -323,6 +324,9 @@ module BABYLON {
                             rotateSpheres[index].lookAt(Vector3.Cross(Vector3.Forward(), rotateSpheres[index].position.normalizeToNew()).normalizeToNew().add(rotateSpheres[index].position));
                             rotateSpheres[index].lookAt(Vector3.Cross(Vector3.Forward(), rotateSpheres[index].position.normalizeToNew()).normalizeToNew().add(rotateSpheres[index].position));
                         }
                         }
                         if(this.fixedDragMeshScreenSize){
                         if(this.fixedDragMeshScreenSize){
+                            this._rootMesh.computeWorldMatrix();
+                            this._rotateSpheresParent.computeWorldMatrix();
+                            rotateSpheres[index].computeWorldMatrix();
                             rotateSpheres[index].absolutePosition.subtractToRef(this.gizmoLayer.utilityLayerScene.activeCamera!.position, this._tmpVector);
                             rotateSpheres[index].absolutePosition.subtractToRef(this.gizmoLayer.utilityLayerScene.activeCamera!.position, this._tmpVector);
                             var distanceFromCamera = this.rotationSphereSize*this._tmpVector.length()/this.fixedDragMeshScreenSizeDistanceFactor;
                             var distanceFromCamera = this.rotationSphereSize*this._tmpVector.length()/this.fixedDragMeshScreenSizeDistanceFactor;
                             rotateSpheres[index].scaling.set(distanceFromCamera, distanceFromCamera, distanceFromCamera);
                             rotateSpheres[index].scaling.set(distanceFromCamera, distanceFromCamera, distanceFromCamera);
@@ -332,7 +336,7 @@ module BABYLON {
                     }
                     }
                 }
                 }
             }
             }
-
+            
             // Update scale box locations
             // Update scale box locations
             var scaleBoxes = this._scaleBoxesParent.getChildMeshes();
             var scaleBoxes = this._scaleBoxesParent.getChildMeshes();
             for(var i=0;i<2;i++){
             for(var i=0;i<2;i++){
@@ -343,6 +347,9 @@ module BABYLON {
                             scaleBoxes[index].position.set(this._boundingDimensions.x*i,this._boundingDimensions.y*j,this._boundingDimensions.z*k);
                             scaleBoxes[index].position.set(this._boundingDimensions.x*i,this._boundingDimensions.y*j,this._boundingDimensions.z*k);
                             scaleBoxes[index].position.addInPlace(new BABYLON.Vector3(-this._boundingDimensions.x/2,-this._boundingDimensions.y/2,-this._boundingDimensions.z/2));
                             scaleBoxes[index].position.addInPlace(new BABYLON.Vector3(-this._boundingDimensions.x/2,-this._boundingDimensions.y/2,-this._boundingDimensions.z/2));
                             if(this.fixedDragMeshScreenSize){
                             if(this.fixedDragMeshScreenSize){
+                                this._rootMesh.computeWorldMatrix();
+                                this._scaleBoxesParent.computeWorldMatrix();
+                                scaleBoxes[index].computeWorldMatrix();
                                 scaleBoxes[index].absolutePosition.subtractToRef(this.gizmoLayer.utilityLayerScene.activeCamera!.position, this._tmpVector);
                                 scaleBoxes[index].absolutePosition.subtractToRef(this.gizmoLayer.utilityLayerScene.activeCamera!.position, this._tmpVector);
                                 var distanceFromCamera = this.scaleBoxSize*this._tmpVector.length()/this.fixedDragMeshScreenSizeDistanceFactor;
                                 var distanceFromCamera = this.scaleBoxSize*this._tmpVector.length()/this.fixedDragMeshScreenSizeDistanceFactor;
                                 scaleBoxes[index].scaling.set(distanceFromCamera, distanceFromCamera, distanceFromCamera);
                                 scaleBoxes[index].scaling.set(distanceFromCamera, distanceFromCamera, distanceFromCamera);

+ 42 - 33
src/Gizmos/babylon.gizmo.ts

@@ -66,46 +66,55 @@ module BABYLON {
          */
          */
         constructor(/** The utility layer the gizmo will be added to */ public gizmoLayer:UtilityLayerRenderer=UtilityLayerRenderer.DefaultUtilityLayer){
         constructor(/** The utility layer the gizmo will be added to */ public gizmoLayer:UtilityLayerRenderer=UtilityLayerRenderer.DefaultUtilityLayer){
             this._rootMesh = new BABYLON.Mesh("gizmoRootNode",gizmoLayer.utilityLayerScene);
             this._rootMesh = new BABYLON.Mesh("gizmoRootNode",gizmoLayer.utilityLayerScene);
-            var tempVector = new Vector3();
             this._beforeRenderObserver = this.gizmoLayer.utilityLayerScene.onBeforeRenderObservable.add(()=>{
             this._beforeRenderObserver = this.gizmoLayer.utilityLayerScene.onBeforeRenderObservable.add(()=>{
-                if(this.attachedMesh){
-                    if(this.updateGizmoRotationToMatchAttachedMesh){
-                        if(!this._rootMesh.rotationQuaternion){
-                            this._rootMesh.rotationQuaternion = Quaternion.RotationYawPitchRoll(this._rootMesh.rotation.y, this._rootMesh.rotation.x, this._rootMesh.rotation.z);
-                        }
+                this._update();
+            })
+            this.attachedMesh = null;
+        }
+
+        private _tempVector = new Vector3();
+        /**
+         * @hidden
+         * Updates the gizmo to match the attached mesh's position/rotation
+         */
+        protected _update(){
+            if(this.attachedMesh){
+                if(this.updateGizmoRotationToMatchAttachedMesh){
+                    if(!this._rootMesh.rotationQuaternion){
+                        this._rootMesh.rotationQuaternion = Quaternion.RotationYawPitchRoll(this._rootMesh.rotation.y, this._rootMesh.rotation.x, this._rootMesh.rotation.z);
+                    }
 
 
-                        // Remove scaling before getting rotation matrix to get rotation matrix unmodified by scale
-                        tempVector.copyFrom(this.attachedMesh.scaling);
-                        if(this.attachedMesh.scaling.x < 0){
-                            this.attachedMesh.scaling.x *= -1;
-                        }
-                        if(this.attachedMesh.scaling.y < 0){
-                            this.attachedMesh.scaling.y *= -1;
-                        }
-                        if(this.attachedMesh.scaling.z < 0){
-                            this.attachedMesh.scaling.z *= -1;
-                        }
-                        this.attachedMesh.computeWorldMatrix().getRotationMatrixToRef(this._tmpMatrix);
-                        this.attachedMesh.scaling.copyFrom(tempVector);
-                        this.attachedMesh.computeWorldMatrix();
-                        Quaternion.FromRotationMatrixToRef(this._tmpMatrix, this._rootMesh.rotationQuaternion);
+                    // Remove scaling before getting rotation matrix to get rotation matrix unmodified by scale
+                    this._tempVector.copyFrom(this.attachedMesh.scaling);
+                    if(this.attachedMesh.scaling.x < 0){
+                        this.attachedMesh.scaling.x *= -1;
                     }
                     }
-                    if(this.updateGizmoPositionToMatchAttachedMesh){
-                        this._rootMesh.position.copyFrom(this.attachedMesh.absolutePosition);
+                    if(this.attachedMesh.scaling.y < 0){
+                        this.attachedMesh.scaling.y *= -1;
                     }
                     }
-                    if(this._updateScale && this.gizmoLayer.utilityLayerScene.activeCamera && this.attachedMesh){
-                        var cameraPosition = this.gizmoLayer.utilityLayerScene.activeCamera.position;
-                        if((<WebVRFreeCamera>this.gizmoLayer.utilityLayerScene.activeCamera).devicePosition){
-                            cameraPosition = (<WebVRFreeCamera>this.gizmoLayer.utilityLayerScene.activeCamera).devicePosition;
-                        }
-                        this._rootMesh.position.subtractToRef(cameraPosition, tempVector);
-                        var dist = tempVector.length()/this._scaleFactor;
-                        this._rootMesh.scaling.set(dist, dist, dist);
+                    if(this.attachedMesh.scaling.z < 0){
+                        this.attachedMesh.scaling.z *= -1;
                     }
                     }
+                    this.attachedMesh.computeWorldMatrix().getRotationMatrixToRef(this._tmpMatrix);
+                    this.attachedMesh.scaling.copyFrom(this._tempVector);
+                    this.attachedMesh.computeWorldMatrix();
+                    Quaternion.FromRotationMatrixToRef(this._tmpMatrix, this._rootMesh.rotationQuaternion);
                 }
                 }
-            })
-            this.attachedMesh = null;
+                if(this.updateGizmoPositionToMatchAttachedMesh){
+                    this._rootMesh.position.copyFrom(this.attachedMesh.absolutePosition);
+                }
+                if(this._updateScale && this.gizmoLayer.utilityLayerScene.activeCamera && this.attachedMesh){
+                    var cameraPosition = this.gizmoLayer.utilityLayerScene.activeCamera.position;
+                    if((<WebVRFreeCamera>this.gizmoLayer.utilityLayerScene.activeCamera).devicePosition){
+                        cameraPosition = (<WebVRFreeCamera>this.gizmoLayer.utilityLayerScene.activeCamera).devicePosition;
+                    }
+                    this._rootMesh.position.subtractToRef(cameraPosition, this._tempVector);
+                    var dist = this._tempVector.length()/this._scaleFactor;
+                    this._rootMesh.scaling.set(dist, dist, dist);
+                }
+            }
         }
         }
+
         /**
         /**
          * Disposes of the gizmo
          * Disposes of the gizmo
          */
          */