Переглянути джерело

Merge pull request #320 from raananw/master

Extending the Ray class functionality
David Catuhe 10 роки тому
батько
коміт
10e9dfc120

+ 13 - 0
Babylon/Loading/Plugins/babylon.babylonFileLoader.js

@@ -335,6 +335,11 @@
                 light._excludedMeshesIds = parsedLight.excludedMeshesIds;
             }
 
+            // Parent
+            if (parsedLight.parentId) {
+                light._waitingParentId = parsedLight.parentId;
+            }
+
             if (parsedLight.includedOnlyMeshesIds) {
                 light._includedOnlyMeshesIds = parsedLight.includedOnlyMeshesIds;
             }
@@ -1196,6 +1201,14 @@
                     }
                 }
 
+                for (index = 0; index < scene.lights.length; index++) {
+                    var light = scene.lights[index];
+                    if (light._waitingParentId) {
+                        light.parent = scene.getLastEntryByID(light._waitingParentId);
+                        light._waitingParentId = undefined;
+                    }
+                }
+
                 for (index = 0; index < scene.meshes.length; index++) {
                     var mesh = scene.meshes[index];
                     if (mesh._waitingParentId) {

+ 13 - 0
Babylon/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -333,6 +333,11 @@
         if (parsedLight.excludedMeshesIds) {
             light._excludedMeshesIds = parsedLight.excludedMeshesIds;
         }
+		
+		// Parent
+        if (parsedLight.parentId) {
+            light._waitingParentId = parsedLight.parentId;
+        }
 
         if (parsedLight.includedOnlyMeshesIds) {
             light._includedOnlyMeshesIds = parsedLight.includedOnlyMeshesIds;
@@ -1212,6 +1217,14 @@
                     camera._waitingParentId = undefined;
                 }
             }
+			
+			for (index = 0; index < scene.lights.length; index++) {
+                var light = scene.lights[index];
+                if (light._waitingParentId) {
+                    light.parent = scene.getLastEntryByID(light._waitingParentId);
+                    light._waitingParentId = undefined;
+                }
+            }
 
             for (index = 0; index < scene.meshes.length; index++) {
                 var mesh = scene.meshes[index];

+ 29 - 6
Babylon/Math/babylon.math.js

@@ -575,7 +575,7 @@
         };
 
         Vector3.prototype.equalsWithEpsilon = function (otherVector) {
-            return Math.abs(this.x - otherVector.x) < BABYLON.Engine.Epsilon && Math.abs(this.y - otherVector.y) < BABYLON.Engine.Epsilon && Math.abs(this.z - otherVector.z) < BABYLON.Engine.Epsilon;
+            return Math.abs(this.x - otherVector.x) < Engine.Epsilon && Math.abs(this.y - otherVector.y) < Engine.Epsilon && Math.abs(this.z - otherVector.z) < Engine.Epsilon;
         };
 
         Vector3.prototype.equalsToFloats = function (x, y, z) {
@@ -1005,7 +1005,7 @@
         };
 
         Vector4.prototype.equalsWithEpsilon = function (otherVector) {
-            return Math.abs(this.x - otherVector.x) < BABYLON.Engine.Epsilon && Math.abs(this.y - otherVector.y) < BABYLON.Engine.Epsilon && Math.abs(this.z - otherVector.z) < BABYLON.Engine.Epsilon && Math.abs(this.w - otherVector.w) < BABYLON.Engine.Epsilon;
+            return Math.abs(this.x - otherVector.x) < Engine.Epsilon && Math.abs(this.y - otherVector.y) < Engine.Epsilon && Math.abs(this.z - otherVector.z) < Engine.Epsilon && Math.abs(this.w - otherVector.w) < Engine.Epsilon;
         };
 
         Vector4.prototype.equalsToFloats = function (x, y, z, w) {
@@ -2281,9 +2281,11 @@
     BABYLON.Frustum = Frustum;
 
     var Ray = (function () {
-        function Ray(origin, direction) {
+        function Ray(origin, direction, length) {
+            if (typeof length === "undefined") { length = Number.MAX_VALUE; }
             this.origin = origin;
             this.direction = direction;
+            this.length = length;
         }
         // Methods
         Ray.prototype.intersectsBoxMinMax = function (minimum, maximum) {
@@ -2422,7 +2424,13 @@
                 return null;
             }
 
-            return new BABYLON.IntersectionInfo(bu, bv, Vector3.Dot(this._edge2, this._qvec) * invdet);
+            //check if the distance is longer than the predefined length.
+            var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
+            if (distance > this.length) {
+                return null;
+            }
+
+            return new IntersectionInfo(bu, bv, distance);
         };
 
         // Statics
@@ -2436,11 +2444,27 @@
             return new Ray(start, direction);
         };
 
+        /**
+        * Function will create a new transformed ray starting from origin and ending at the end point. Ray's length will be set, and ray will be
+        * transformed to the given world matrix.
+        * @param origin The origin point
+        * @param end The end point
+        * @param world a matrix to transform the ray to. Default is the identity matrix.
+        */
+        Ray.CreateNewFromTo = function (origin, end, world) {
+            if (typeof world === "undefined") { world = BABYLON.Matrix.Identity(); }
+            var direction = end.subtract(origin);
+            var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
+            direction.normalize();
+
+            return Ray.Transform(new Ray(origin, direction, length), world);
+        };
+
         Ray.Transform = function (ray, matrix) {
             var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
             var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
 
-            return new Ray(newOrigin, newDirection);
+            return new Ray(newOrigin, newDirection, ray.length);
         };
         return Ray;
     })();
@@ -2463,4 +2487,3 @@
     BABYLON.Axis = Axis;
     ;
 })(BABYLON || (BABYLON = {}));
-//# sourceMappingURL=babylon.math.js.map

+ 24 - 3
Babylon/Math/babylon.math.ts

@@ -2280,7 +2280,7 @@
         private _tvec: Vector3;
         private _qvec: Vector3;
 
-        constructor(public origin: Vector3, public direction: Vector3) {
+        constructor(public origin: Vector3, public direction: Vector3, public length: number = Number.MAX_VALUE) {
         }
 
         // Methods
@@ -2422,8 +2422,14 @@
             if (bv < 0 || bu + bv > 1.0) {
                 return null;
             }
+			
+			//check if the distance is longer than the predefined length.
+			var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
+			if(distance > this.length) {
+				return null;
+			}
 
-            return new IntersectionInfo(bu, bv, Vector3.Dot(this._edge2, this._qvec) * invdet);
+            return new IntersectionInfo(bu, bv, distance);
         }
 
         // Statics
@@ -2436,12 +2442,27 @@
 
             return new Ray(start, direction);
         }
+		
+		/**
+		* Function will create a new transformed ray starting from origin and ending at the end point. Ray's length will be set, and ray will be 
+		* transformed to the given world matrix.
+		* @param origin The origin point
+		* @param end The end point
+		* @param world a matrix to transform the ray to. Default is the identity matrix.
+		*/
+		public static CreateNewFromTo(origin : BABYLON.Vector3, end : BABYLON.Vector3, world: Matrix = BABYLON.Matrix.Identity()): Ray {
+			var direction = end.subtract(origin);
+			var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
+            direction.normalize();
+			
+			return Ray.Transform(new Ray(origin, direction, length), world);
+		}
 
         public static Transform(ray: Ray, matrix: Matrix): Ray {
             var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
             var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
 
-            return new Ray(newOrigin, newDirection);
+            return new Ray(newOrigin, newDirection, ray.length);
         }
     }