Browse Source

Merge pull request #1696 from abow/master

Deprecated Ray.show(), Ray.hide() and added Ray.intersectsMeshes()
David Catuhe 8 years ago
parent
commit
0409acfd34
1 changed files with 44 additions and 34 deletions
  1. 44 34
      src/Culling/babylon.ray.ts

+ 44 - 34
src/Culling/babylon.ray.ts

@@ -6,12 +6,8 @@
         private _tvec: Vector3;
         private _tvec: Vector3;
         private _qvec: Vector3;
         private _qvec: Vector3;
 
 
-        private _renderPoints: Vector3[];
-        private _renderLine: LinesMesh;
-        private _renderFunction: () => void;
-        private _scene: Scene;
-        private _show = false;
         private _tmpRay: Ray;
         private _tmpRay: Ray;
+        private _rayHelper: RayHelper;
 
 
         constructor(public origin: Vector3, public direction: Vector3, public length: number = Number.MAX_VALUE) {
         constructor(public origin: Vector3, public direction: Vector3, public length: number = Number.MAX_VALUE) {
         }
         }
@@ -216,52 +212,66 @@
 
 
         }
         }
 
 
-        public show(scene:Scene, color:Color3): void{
-
-            if(!this._show){
+        public intersectsMeshes(meshes:Array<AbstractMesh>, fastCheck?: boolean, results?:Array<PickingInfo>): Array<PickingInfo> {
 
 
-                this._renderFunction = this._render.bind(this);
-                this._show = true;
-                this._scene = scene;
-                this._renderPoints = [this.origin, this.origin.add(this.direction.scale(this.length))];
-                this._renderLine = Mesh.CreateLines("ray", this._renderPoints, scene, true);
+            if(results){
+                results.length = 0;
+            }else{
+                results = [];
+            }
 
 
-                this._scene.registerBeforeRender(this._renderFunction);
+            for(var i = 0; i < meshes.length; i++){
+                var pickInfo = this.intersectsMesh(meshes[i], fastCheck);
 
 
+                if(pickInfo.hit){
+                    results.push(pickInfo);
+                }
             }
             }
 
 
-            if (color) {
-                this._renderLine.color.copyFrom(color);
-            }
+            results.sort(this._comparePickingInfo);
+
+            return results;
 
 
         }
         }
 
 
-        public hide(): void{
+        private _comparePickingInfo(pickingInfoA:PickingInfo, pickingInfoB:PickingInfo): number{
 
 
-            if(this._show){
-                this._show = false;
-                this._scene.unregisterBeforeRender(this._renderFunction);
-                this._scene = null;
+            if(pickingInfoA.distance < pickingInfoB.distance){
+                return -1;
+            }else if(pickingInfoA.distance > pickingInfoB.distance){
+                return 1;
+            }else{
+                return 0;
             }
             }
 
 
-            if(this._renderLine){
-                this._renderLine.dispose();
-                this._renderLine = null;
-                this._renderPoints = null;
+        }
+
+        /**
+         *  @Deprecated. Use new RayHelper.show() instead.
+         * */
+        public show(scene:Scene, color:Color3): void{
+
+            console.warn('Ray.show() has been deprecated.  Use new RayHelper.show() instead.');
+
+            if(!this._rayHelper){
+                this._rayHelper = new RayHelper(this);
             }
             }
+            
+            this._rayHelper.show(scene, color);
 
 
         }
         }
 
 
-        private _render(): void {
+        /**
+         *  @Deprecated. Use new RayHelper.hide() instead.
+         * */
+        public hide(): void{
 
 
-            var point = this._renderPoints[1];
-            var len = Math.min(this.length, 1000000);
-            
-            point.copyFrom(this.direction);
-            point.scaleInPlace(len);
-            point.addInPlace(this.origin);
+            console.warn('Ray.hide() has been deprecated.  Use new RayHelper.hide() instead.');
 
 
-            Mesh.CreateLines("ray", this._renderPoints, this._scene, true, this._renderLine);
+            if(this._rayHelper){
+                this._rayHelper.hide();
+                this._rayHelper = null;
+            }
 
 
         }
         }