Explorar o código

Merge pull request #1542 from abow/master

added Ray.intersectsMesh and Ray.render
David Catuhe %!s(int64=8) %!d(string=hai) anos
pai
achega
d85eb433a5
Modificáronse 2 ficheiros con 63 adicións e 2 borrados
  1. 2 2
      dist/preview release/what's new.md
  2. 61 0
      src/Culling/babylon.ray.ts

+ 2 - 2
dist/preview release/what's new.md

@@ -6,8 +6,8 @@
 - Babylon.js now supports right handed system with ```scene.useRightHandedSystem = true``` ([deltakosh](https://github.com/deltakosh))
 - Babylon.js is now compiled with [optimize-js](https://github.com/nolanlawson/optimize-js) to get faster initial load ([deltakosh](https://github.com/deltakosh))
 - Canvas2D moved to a separate folder in main repo. Now you need to also include babylon.cavans2d.js to get Canvas@D feature ([deltakosh](https://github.com/deltakosh))
-- New BoneIKController [Demo](http://www.babylonjs-playground.com/#1EVNNB#6) - ([abow](https://github.com/abow))
-- New BoneLookController [Demo](http://www.babylonjs-playground.com/#1B1PUZ#13) - ([abow](https://github.com/abow))
+- New BoneIKController [Demo](http://www.babylonjs-playground.com/#1EVNNB#15) - ([abow](https://github.com/abow))
+- New BoneLookController [Demo](http://www.babylonjs-playground.com/#1B1PUZ#15) - ([abow](https://github.com/abow))
 
 ### Updates
 - Added `node.doNotSerialize` to prevent specific nodes to be serialized by `SceneSerializer` ([deltakosh](https://github.com/deltakosh))

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

@@ -6,6 +6,12 @@
         private _tvec: Vector3;
         private _qvec: Vector3;
 
+        private _renderPoints: Vector3[];
+        private _renderLine: LinesMesh;
+        private _renderFunction: () => void;
+        private _scene: Scene;
+        private _show = false;
+
         constructor(public origin: Vector3, public direction: Vector3, public length: number = Number.MAX_VALUE) {
         }
 
@@ -193,6 +199,61 @@
             }
         }
 
+        public intersectsMesh(mesh:AbstractMesh, fastCheck?: boolean): PickingInfo {
+
+            var tm = Tmp.Matrix[0];
+
+            mesh.getWorldMatrix().invertToRef(tm);
+
+            var ray = Ray.Transform(this, tm);
+
+            return mesh.intersects(ray, 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 (color) {
+                this._renderLine.color.copyFrom(color);
+            }
+
+            this._scene.registerBeforeRender(this._renderFunction);
+
+        }
+
+        public hide(): void{
+
+            if(this._show){
+                this._show = false;
+                this._scene.unregisterBeforeRender(this._renderFunction);
+            }
+
+            if(this._renderLine){
+                this._renderLine.dispose();
+                this._renderLine = null;
+                this._renderPoints = null;
+            }
+
+        }
+
+        private _render(): void {
+
+            var point = this._renderPoints[1];
+
+            point.copyFrom(this.direction);
+            point.scaleInPlace(this.length);
+            point.addInPlace(this.origin);
+
+            Mesh.CreateLines("ray", this._renderPoints, this._scene, true, this._renderLine);
+
+        }
+
         private static smallnum = 0.00000001;
         private static rayl = 10e8;