Skip to content

Commit

Permalink
Merge pull request #320 from raananw/master
Browse files Browse the repository at this point in the history
Extending the Ray class functionality
  • Loading branch information
deltakosh committed Nov 26, 2014
2 parents 20d5b6a + 5445408 commit 10e9dfc
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
13 changes: 13 additions & 0 deletions Babylon/Loading/Plugins/babylon.babylonFileLoader.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Babylon/Loading/Plugins/babylon.babylonFileLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
35 changes: 29 additions & 6 deletions Babylon/Math/babylon.math.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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;
})();
Expand All @@ -2463,4 +2487,3 @@
BABYLON.Axis = Axis;
;
})(BABYLON || (BABYLON = {}));
//# sourceMappingURL=babylon.math.js.map
27 changes: 24 additions & 3 deletions Babylon/Math/babylon.math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 10e9dfc

Please sign in to comment.