Selaa lähdekoodia

Merge pull request #1062 from nockawa/master

Added Ray.intersectsPlane() method
David Catuhe 9 vuotta sitten
vanhempi
commit
c429360198
2 muutettua tiedostoa jossa 53 lisäystä ja 7 poistoa
  1. 21 0
      src/Culling/babylon.ray.ts
  2. 32 7
      src/Tools/babylon.observable.ts

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

@@ -172,6 +172,27 @@
             return new IntersectionInfo(bu, bv, distance);
         }
 
+        public intersectsPlane(plane: Plane): IntersectionInfo {
+            var distance: number;
+            var result1 = Vector3.Dot(plane.normal, this.direction);
+            if (Math.abs(result1) < 9.99999997475243E-07) {
+                return null;
+            }
+            else {
+                var result2 = Vector3.Dot(plane.normal, this.origin);
+                distance = (-plane.d - result2) / result1;
+                if (distance < 0.0) {
+                    if (distance < -9.99999997475243E-07) {
+                        return null;
+                    } else {
+                        distance = 0;
+                    }
+                }
+
+                return new IntersectionInfo(undefined, undefined, distance);
+            }
+        }
+        
         // 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);

+ 32 - 7
src/Tools/babylon.observable.ts

@@ -1,6 +1,21 @@
 module BABYLON {
+
+    /**
+     * A class serves as a medium between the observable and its observers
+     */
+    export class EventState {
+        constructor() {
+            this.skipNextObervers = false;
+        }
+
+        /**
+         * If the callback of a given Observer set this member to true the following observers will be ignored
+         */
+        skipNextObervers: boolean;
+    }
+
     export class Observer<T> {
-        constructor(public callback: (eventData: T) => void) {
+        constructor(public callback: (eventData: T, eventState: EventState) => void) {
         }
     }
 
@@ -10,15 +25,20 @@
         /**
          * Create a new Observer with the specified callback
          * @param callback the callback that will be executed for that Observer
+         * @param insertFirst if true the callback will be inserted at the first position, hence executed before the others ones. If false (default behavior) the callback will be inserted at the last position, executed after all the others already present.
          */
-        public add(callback: (eventData: T) => void): Observer<T> {
+        public add(callback: (eventData: T, eventState: EventState) => void, insertFirst = false): Observer<T> {
             if (!callback) {
                 return null;
             }
 
             var observer = new Observer(callback);
 
-            this._observers.push(observer);
+            if (insertFirst) {
+                this._observers.unshift(observer);
+            } else {
+                this._observers.push(observer);
+            }
 
             return observer;
         }
@@ -44,7 +64,7 @@
          * Remove a callback from the Observable object
          * @param callback the callback to remove. If it doesn't belong to this Observable, false will be returned.
         */
-        public removeCallback(callback: (eventData: T) => void): boolean {
+        public removeCallback(callback: (eventData: T, eventState: EventState) => void): boolean {
 
             for (var index = 0; index < this._observers.length; index++) {
                 if (this._observers[index].callback === callback) {
@@ -61,9 +81,14 @@
          * @param eventData
          */
         public notifyObservers(eventData: T): void {
-            this._observers.forEach((observer: Observer<T>) => {
-                observer.callback(eventData);
-            });
+            var state = new EventState();
+
+            for (var obs of this._observers) {
+                obs.callback(eventData, state);
+                if (state.skipNextObervers) {
+                    break;
+                }
+            }
         }
 
         /**