瀏覽代碼

added Ray.TransformToRef for intersectsMesh and checked to make sure show doesn't create more than one mesh

Adam Bowman 8 年之前
父節點
當前提交
eeb1f48cc3
共有 1 個文件被更改,包括 27 次插入9 次删除
  1. 27 9
      src/Culling/babylon.ray.ts

+ 27 - 9
src/Culling/babylon.ray.ts

@@ -11,6 +11,7 @@
         private _renderFunction: () => void;
         private _scene: Scene;
         private _show = false;
+        private _tmpRay: Ray;
 
         constructor(public origin: Vector3, public direction: Vector3, public length: number = Number.MAX_VALUE) {
         }
@@ -205,26 +206,34 @@
 
             mesh.getWorldMatrix().invertToRef(tm);
 
-            var ray = Ray.Transform(this, tm);
+            if(this._tmpRay){
+                Ray.TransformToRef(this, tm, this._tmpRay);
+            }else{
+                this._tmpRay = Ray.Transform(this, tm);
+            }
 
-            return mesh.intersects(ray, fastCheck);
+            return mesh.intersects(this._tmpRay, fastCheck);
 
         }
 
         public show(scene:Scene, color:Color3): void{
 
-            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(!this._show){
+
+                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);
+
+                this._scene.registerBeforeRender(this._renderFunction);
+
+            }
 
             if (color) {
                 this._renderLine.color.copyFrom(color);
             }
 
-            this._scene.registerBeforeRender(this._renderFunction);
-
         }
 
         public hide(): void{
@@ -373,5 +382,14 @@
 
             return new Ray(newOrigin, newDirection, ray.length);
         }
+
+        public static TransformToRef(ray: Ray, matrix: Matrix, result:Ray): void {
+            
+            Vector3.TransformCoordinatesToRef(ray.origin, matrix, result.origin);
+            Vector3.TransformNormalToRef(ray.direction, matrix, result.direction);
+
+            ray.direction.normalize();
+            
+        }
     }
 }