소스 검색

removed attachToMesh, detachFromMesh, dispose from Ray and moved them to a new RayHelper class

Adam Bowman 8 년 전
부모
커밋
4fb23a65ce
3개의 변경된 파일159개의 추가작업 그리고 76개의 파일을 삭제
  1. 1 0
      Tools/Gulp/config.json
  2. 0 76
      src/Culling/babylon.ray.ts
  3. 158 0
      src/Culling/babylon.rayHelper.ts

+ 1 - 0
Tools/Gulp/config.json

@@ -34,6 +34,7 @@
       "../../src/Culling/babylon.boundingBox.js",
       "../../src/Culling/babylon.boundingInfo.js",
       "../../src/Culling/babylon.ray.js",
+      "../../src/Culling/babylon.rayHelper.js",
       "../../src/Mesh/babylon.abstractMesh.js",
       "../../src/Lights/babylon.light.js",
       "../../src/Lights/babylon.pointLight.js",

+ 0 - 76
src/Culling/babylon.ray.ts

@@ -13,11 +13,6 @@
         private _show = false;
         private _tmpRay: Ray;
 
-        private _updateToMeshFunction: () => void;
-        private _attachedToMesh: AbstractMesh;
-        private _meshSpaceDirection: Vector3;
-        private _meshSpaceOrigin: Vector3;
-
         constructor(public origin: Vector3, public direction: Vector3, public length: number = Number.MAX_VALUE) {
         }
 
@@ -355,77 +350,6 @@
             return -1;
         }
 
-        public attachToMesh(mesh:AbstractMesh, meshSpaceDirection?:Vector3, meshSpaceOrigin?:Vector3, length?:number): void{
-
-            this._attachedToMesh = mesh;
-
-            if(!this.direction){
-                this.direction = Vector3.Zero();
-            }
-
-            if(!this.origin){
-                this.origin = Vector3.Zero();
-            }
-
-            if(length){
-                this.length = length;
-            }
-
-            if(!meshSpaceOrigin){
-                meshSpaceOrigin = Vector3.Zero();
-            }
-
-            if(!meshSpaceDirection){
-                // -1 so that this will work with Mesh.lookAt
-                meshSpaceDirection = new Vector3(0, 0, -1);
-            }
-
-            if(!this._meshSpaceDirection){
-                this._meshSpaceDirection = meshSpaceDirection.clone();
-                this._meshSpaceOrigin = meshSpaceOrigin.clone();
-            }else{
-                this._meshSpaceDirection.copyFrom(meshSpaceDirection);
-                this._meshSpaceOrigin.copyFrom(meshSpaceOrigin);
-            }
-
-            if(!this._updateToMeshFunction){
-                this._updateToMeshFunction = this._updateToMesh.bind(this);
-                this._attachedToMesh.getScene().registerBeforeRender(this._updateToMeshFunction);
-            }
-
-            this._updateToMesh();
-
-        }
-
-        public detachFromMesh(): void{
-
-            if(this._attachedToMesh){
-                this._attachedToMesh.getScene().unregisterBeforeRender(this._updateToMeshFunction);
-                this._attachedToMesh = null;
-                this._updateToMeshFunction = null;
-            }
-
-        }
-
-        private _updateToMesh(): void{
-
-            if(this._attachedToMesh._isDisposed){
-                this.detachFromMesh();
-                return;
-            }
-
-            this._attachedToMesh.getDirectionToRef(this._meshSpaceDirection, this.direction);
-            Vector3.TransformCoordinatesToRef(this._meshSpaceOrigin, this._attachedToMesh.getWorldMatrix(), this.origin);
-
-        }
-
-        public dispose(): void{
-
-            this.hide();
-            this.detachFromMesh();
-
-        }
-
         // Statics
         public static CreateNew(x: number, y: number, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Ray {
             var start = Vector3.Unproject(new Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);

+ 158 - 0
src/Culling/babylon.rayHelper.ts

@@ -0,0 +1,158 @@
+module BABYLON {
+    export class RayHelper {
+        
+        public ray:Ray;
+
+        private _renderPoints: Vector3[];
+        private _renderLine: LinesMesh;
+        private _renderFunction: () => void;
+        private _scene: Scene;
+        
+        private _updateToMeshFunction: () => void;
+        private _attachedToMesh: AbstractMesh;
+        private _meshSpaceDirection: Vector3;
+        private _meshSpaceOrigin: Vector3;
+
+        constructor(ray:Ray) {
+            this.ray = ray;
+        }
+
+        private _comparePickingInfo(pickingInfoA:PickingInfo, pickingInfoB:PickingInfo): number{
+
+            if(pickingInfoA.distance < pickingInfoB.distance){
+                return -1;
+            }else if(pickingInfoA.distance > pickingInfoB.distance){
+                return 1;
+            }else{
+                return 0;
+            }
+
+        }
+
+        public show(scene:Scene, color:Color3): void{
+
+            if(!this._renderFunction){
+
+                var ray = this.ray;
+
+                this._renderFunction = this._render.bind(this);
+                this._scene = scene;
+                this._renderPoints = [ray.origin, ray.origin.add(ray.direction.scale(ray.length))];
+                this._renderLine = Mesh.CreateLines("ray", this._renderPoints, scene, true);
+
+                this._scene.registerBeforeRender(this._renderFunction);
+
+            }
+
+            if (color) {
+                this._renderLine.color.copyFrom(color);
+            }
+
+        }
+
+        public hide(): void{
+
+            if(this._renderFunction){
+                this._scene.unregisterBeforeRender(this._renderFunction);
+                this._scene = null;
+                this._renderFunction = null;
+                this._renderLine.dispose();
+                this._renderLine = null;
+                this._renderPoints = null;
+            }
+
+        }
+
+        private _render(): void {
+
+            var ray = this.ray;
+
+            var point = this._renderPoints[1];
+            var len = Math.min(ray.length, 1000000);
+            
+            point.copyFrom(ray.direction);
+            point.scaleInPlace(len);
+            point.addInPlace(ray.origin);
+
+            Mesh.CreateLines("ray", this._renderPoints, this._scene, true, this._renderLine);
+
+        }
+
+        public attachToMesh(mesh:AbstractMesh, meshSpaceDirection?:Vector3, meshSpaceOrigin?:Vector3, length?:number): void{
+
+            this._attachedToMesh = mesh;
+
+            var ray = this.ray;
+
+            if(!ray.direction){
+                ray.direction = Vector3.Zero();
+            }
+
+            if(!ray.origin){
+                ray.origin = Vector3.Zero();
+            }
+
+            if(length){
+                ray.length = length;
+            }
+
+            if(!meshSpaceOrigin){
+                meshSpaceOrigin = Vector3.Zero();
+            }
+
+            if(!meshSpaceDirection){
+                // -1 so that this will work with Mesh.lookAt
+                meshSpaceDirection = new Vector3(0, 0, -1);
+            }
+
+            if(!this._meshSpaceDirection){
+                this._meshSpaceDirection = meshSpaceDirection.clone();
+                this._meshSpaceOrigin = meshSpaceOrigin.clone();
+            }else{
+                this._meshSpaceDirection.copyFrom(meshSpaceDirection);
+                this._meshSpaceOrigin.copyFrom(meshSpaceOrigin);
+            }
+
+            if(!this._updateToMeshFunction){
+                this._updateToMeshFunction = this._updateToMesh.bind(this);
+                this._attachedToMesh.getScene().registerBeforeRender(this._updateToMeshFunction);
+            }
+
+            this._updateToMesh();
+
+        }
+
+        public detachFromMesh(): void{
+
+            if(this._attachedToMesh){
+                this._attachedToMesh.getScene().unregisterBeforeRender(this._updateToMeshFunction);
+                this._attachedToMesh = null;
+                this._updateToMeshFunction = null;
+            }
+
+        }
+
+        private _updateToMesh(): void{
+
+            var ray = this.ray;
+
+            if(this._attachedToMesh._isDisposed){
+                this.detachFromMesh();
+                return;
+            }
+
+            this._attachedToMesh.getDirectionToRef(this._meshSpaceDirection, ray.direction);
+            Vector3.TransformCoordinatesToRef(this._meshSpaceOrigin, this._attachedToMesh.getWorldMatrix(), ray.origin);
+
+        }
+
+        public dispose(): void{
+
+            this.hide();
+            this.detachFromMesh();
+            this.ray = null;
+
+        }
+
+    }
+}