Skip to content

Commit

Permalink
v1.8.0
Browse files Browse the repository at this point in the history
Former-commit-id: f714c52
  • Loading branch information
deltakosh committed Dec 18, 2013
1 parent 3de835d commit 8495199
Show file tree
Hide file tree
Showing 49 changed files with 15,098 additions and 75 deletions.
26 changes: 20 additions & 6 deletions Babylon/Animations/babylon.animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ var BABYLON = BABYLON || {};
};

// Methods
BABYLON.Animation.prototype.floatInterpolateFunction = function (startValue, endValue, gradient) {
return startValue + (endValue - startValue) * gradient;
};

BABYLON.Animation.prototype.quaternionInterpolateFunction = function (startValue, endValue, gradient) {
return BABYLON.Quaternion.Slerp(startValue, endValue, gradient);
};

BABYLON.Animation.prototype.vector3InterpolateFunction = function (startValue, endValue, gradient) {
return BABYLON.Vector3.Lerp(startValue, endValue, gradient);
};

BABYLON.Animation.prototype.clone = function() {
var clone = new BABYLON.Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);

Expand All @@ -37,6 +49,8 @@ var BABYLON = BABYLON || {};
if (loopMode === BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT && repeatCount > 0) {
return highLimitValue.clone ? highLimitValue.clone() : highLimitValue;
}

this.currentFrame = currentFrame;

for (var key = 0; key < this._keys.length; key++) {
if (this._keys[key + 1].frame >= currentFrame) {
Expand All @@ -50,9 +64,9 @@ var BABYLON = BABYLON || {};
switch (loopMode) {
case BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE:
case BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT:
return startValue + (endValue - startValue) * gradient;
return this.floatInterpolateFunction(startValue, endValue, gradient);
case BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE:
return offsetValue * repeatCount + (startValue + (endValue - startValue) * gradient);
return offsetValue * repeatCount + this.floatInterpolateFunction(startValue, endValue, gradient);
}
break;
// Quaternion
Expand All @@ -61,10 +75,10 @@ var BABYLON = BABYLON || {};
switch (loopMode) {
case BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE:
case BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT:
quaternion = BABYLON.Quaternion.Slerp(startValue, endValue, gradient);
quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient);
break;
case BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE:
quaternion = BABYLON.Quaternion.Slerp(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
quaternion = this.quaternionInterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
break;
}

Expand All @@ -74,9 +88,9 @@ var BABYLON = BABYLON || {};
switch (loopMode) {
case BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE:
case BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT:
return BABYLON.Vector3.Lerp(startValue, endValue, gradient);
return this.vector3InterpolateFunction(startValue, endValue, gradient);
case BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE:
return BABYLON.Vector3.Lerp(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
return this.vector3InterpolateFunction(startValue, endValue, gradient).add(offsetValue.scale(repeatCount));
}
// Matrix
case BABYLON.Animation.ANIMATIONTYPE_MATRIX:
Expand Down
1 change: 0 additions & 1 deletion Babylon/Cameras/babylon.camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ var BABYLON = BABYLON || {};
BABYLON.Camera.ORTHOGRAPHIC_CAMERA = 1;

// Members
BABYLON.Camera.prototype.fov = 0.8;
BABYLON.Camera.prototype.orthoLeft = null;
BABYLON.Camera.prototype.orthoRight = null;
BABYLON.Camera.prototype.orthoBottom = null;
Expand Down
5 changes: 4 additions & 1 deletion Babylon/Cameras/babylon.freeCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var BABYLON = BABYLON || {};
BABYLON.FreeCamera.prototype.noRotationConstraint = false;
BABYLON.FreeCamera.prototype.angularSensibility = 2000.0;
BABYLON.FreeCamera.prototype.lockedTarget = null;

BABYLON.FreeCamera.prototype.onCollide = null;

// Methods
BABYLON.FreeCamera.prototype._computeLocalCameraSpeed = function () {
Expand Down Expand Up @@ -228,6 +228,9 @@ var BABYLON = BABYLON || {};

if (this._diffPosition.length() > BABYLON.Engine.collisionsEpsilon) {
this.position.addInPlace(this._diffPosition);
if (this.onCollide) {
this.onCollide(this._collider.collidedMesh);
}
}
};

Expand Down
1 change: 1 addition & 0 deletions Babylon/Collisions/babylon.collider.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ var BABYLON = BABYLON || {};
}
this.nearestDistance = distToCollision;
this.collisionFound = true;
this.collidedMesh = subMesh.getMesh();
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion Babylon/Lights/babylon.pointLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var BABYLON = BABYLON || {};
BABYLON.PointLight.prototype = Object.create(BABYLON.Light.prototype);

// Methods
BABYLON.Light.prototype.transferToEffect = function (effect, positionUniformName) {
BABYLON.PointLight.prototype.transferToEffect = function (effect, positionUniformName) {
if (this.parent && this.parent.getWorldMatrix) {
if (!this._transformedPosition) {
this._transformedPosition = BABYLON.Vector3.Zero();
Expand Down
1 change: 0 additions & 1 deletion Babylon/Materials/textures/babylon.baseTexture.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var BABYLON = BABYLON || {};
// Members
BABYLON.BaseTexture.prototype.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
BABYLON.BaseTexture.prototype.hasAlpha = false;
BABYLON.BaseTexture.prototype.hasAlpha = false;
BABYLON.BaseTexture.prototype.level = 1;
BABYLON.BaseTexture.prototype._texture = null;

Expand Down
72 changes: 72 additions & 0 deletions Babylon/Mesh/babylon.mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,78 @@ var BABYLON = BABYLON || {};
this.onDispose();
}
};

// Physics
BABYLON.Mesh.prototype.setPhysicsState = function(options) {
if (!this._scene._physicsEngine) {
return;
}

options.impostor = options.impostor || BABYLON.PhysicsEngine.NoImpostor;
options.mass = options.mass || 0;
options.friction = options.friction || 0.0;
options.restitution = options.restitution || 0.9;

this._physicImpostor = options.impostor;
this._physicsMass = options.mass;
this._physicsFriction = options.friction;
this._physicRestitution = options.restitution;

if (options.impostor === BABYLON.PhysicsEngine.NoImpostor) {
this._scene._physicsEngine._unregisterMesh(this);
return;
}

this._scene._physicsEngine._registerMesh(this, options);
};

BABYLON.Mesh.prototype.getPhysicsImpostor = function() {
if (!this._physicImpostor) {
return BABYLON.PhysicsEngine.NoImpostor;
}

return this._physicImpostor;
};

BABYLON.Mesh.prototype.getPhysicsMass = function() {
if (!this._physicsMass) {
return 0;
}

return this._physicsMass;
};

BABYLON.Mesh.prototype.getPhysicsFriction = function () {
if (!this._physicsFriction) {
return 0;
}

return this._physicsFriction;
};

BABYLON.Mesh.prototype.getPhysicsRestitution = function () {
if (!this._physicRestitution) {
return 0;
}

return this._physicRestitution;
};

BABYLON.Mesh.prototype.applyImpulse = function(force, contactPoint) {
if (!this._physicImpostor) {
return;
}

this._scene._physicsEngine._applyImpulse(this, force, contactPoint);
};

BABYLON.Mesh.prototype.setPhysicsLinkWith = function(otherMesh, pivot1, pivot2) {
if (!this._physicImpostor) {
return;
}

this._scene._physicsEngine._createLink(this, otherMesh, pivot1, pivot2);
};

// Statics
BABYLON.Mesh.CreateBox = function (name, size, scene, updatable) {
Expand Down
172 changes: 172 additions & 0 deletions Babylon/Physics/babylon.physicsEngine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
"use strict";

var BABYLON = BABYLON || {};

(function () {
BABYLON.PhysicsEngine = function (gravity) {
this.gravity = gravity || new BABYLON.Vector3(0, 0, -9.82);

this._world = new CANNON.World();
this._world.broadphase = new CANNON.NaiveBroadphase();
this._world.gravity.set(this.gravity.x, this.gravity.y, this.gravity.z);

this._registeredMeshes = [];
this._physicsMaterials = [];
};

BABYLON.PhysicsEngine.prototype._runOneStep = function (delta) {
if (delta > 1.0) {
delta = 1.0;
}

this._world.step(delta);

for (var index = 0; index < this._registeredMeshes.length; index++) {
var registeredMesh = this._registeredMeshes[index];

registeredMesh.mesh.position.x = registeredMesh.body.position.x;
registeredMesh.mesh.position.y = registeredMesh.body.position.z;
registeredMesh.mesh.position.z = registeredMesh.body.position.y;

if (!registeredMesh.mesh.rotationQuaternion) {
registeredMesh.mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
}

registeredMesh.mesh.rotationQuaternion.x = registeredMesh.body.quaternion.x;
registeredMesh.mesh.rotationQuaternion.y = registeredMesh.body.quaternion.z;
registeredMesh.mesh.rotationQuaternion.z = registeredMesh.body.quaternion.y;
registeredMesh.mesh.rotationQuaternion.w = -registeredMesh.body.quaternion.w;
}
};

BABYLON.PhysicsEngine.prototype._addMaterial = function (friction, restitution) {
var index;
var mat;

for (index = 0; index < this._physicsMaterials.length; index++) {
mat = this._physicsMaterials[index];

if (mat.friction === friction && mat.restitution === restitution) {
return mat;
}
}

var currentMat = new CANNON.Material();
currentMat.friction = friction;
currentMat.restitution = restitution;
this._physicsMaterials.push(currentMat);

for (index = 0; index < this._physicsMaterials.length; index++) {
mat = this._physicsMaterials[index];

var contactMaterial = new CANNON.ContactMaterial(mat, currentMat, Math.max(mat.friction, currentMat.friction), mat.restitution * currentMat.restitution);
this._world.addContactMaterial(contactMaterial);
}

return currentMat;
};

BABYLON.PhysicsEngine.prototype._setGravity = function (gravity) {
this._world.gravity.set(this.gravity.x, this.gravity.y, this.gravity.z);
};

BABYLON.PhysicsEngine.prototype._registerMesh = function (mesh, options) {
var shape = null;

this._unregisterMesh(mesh);

mesh.computeWorldMatrix(true);

switch (options.impostor) {
case BABYLON.PhysicsEngine.SphereImpostor:
var bbox = mesh.getBoundingInfo().boundingBox;
var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;

shape = new CANNON.Sphere(Math.max(radiusX, radiusY, radiusZ) / 2);
break;
case BABYLON.PhysicsEngine.BoxImpostor:
var bbox = mesh.getBoundingInfo().boundingBox;
var min = bbox.minimumWorld;
var max = bbox.maximumWorld;
var box = max.subtract(min).scale(0.5);
shape = new CANNON.Box(new CANNON.Vec3(box.x, box.z, box.y));
break;
case BABYLON.PhysicsEngine.PlaneImpostor:
shape = new CANNON.Plane();
break;
}

var material = this._addMaterial(options.friction, options.restitution);
var body = new CANNON.RigidBody(options.mass, shape, material);

body.position.set(mesh.position.x, mesh.position.z, mesh.position.y);
this._world.add(body);

this._registeredMeshes.push({ mesh: mesh, body: body, material:material});
};

BABYLON.PhysicsEngine.prototype._unregisterMesh = function (mesh) {
for (var index = 0; index < this._registeredMeshes.length; index++) {
var registeredMesh = this._registeredMeshes[index];

if (registeredMesh.mesh === mesh) {
// Remove
this._world.remove(registeredMesh.body);
this._registeredMeshes.splice(index, 1);
return;
}
}
};

BABYLON.PhysicsEngine.prototype._applyImpulse = function (mesh, force, contactPoint) {
var worldPoint = new CANNON.Vec3(contactPoint.x, contactPoint.z, contactPoint.y);
var impulse = new CANNON.Vec3(force.x, force.z, force.y);

for (var index = 0; index < this._registeredMeshes.length; index++) {
var registeredMesh = this._registeredMeshes[index];

if (registeredMesh.mesh === mesh) {
registeredMesh.body.applyImpulse(impulse, worldPoint);
return;
}
}
};

BABYLON.PhysicsEngine.prototype._createLink = function (mesh1, mesh2, pivot1, pivot2) {
var body1, body2;
for (var index = 0; index < this._registeredMeshes.length; index++) {
var registeredMesh = this._registeredMeshes[index];

if (registeredMesh.mesh === mesh1) {
body1 = registeredMesh.body;
} else if (registeredMesh.mesh === mesh2) {
body2 = registeredMesh.body;
}
}

if (!body1 || !body2) {
return;
}

var constraint = new CANNON.PointToPointConstraint(body1, new CANNON.Vec3(pivot1.x, pivot1.z, pivot1.y), body2, new CANNON.Vec3(pivot2.x, pivot2.z, pivot2.y));
this._world.addConstraint(constraint);
};

BABYLON.PhysicsEngine.prototype.dispose = function () {
while (this._registeredMeshes.length) {
this._unregisterMesh(this._registeredMeshes[0].mesh);
}
};

// Statics
BABYLON.PhysicsEngine.IsSupported = function() {
return CANNON !== undefined;
};

BABYLON.PhysicsEngine.NoImpostor = 0;
BABYLON.PhysicsEngine.SphereImpostor = 1;
BABYLON.PhysicsEngine.BoxImpostor = 2;
BABYLON.PhysicsEngine.PlaneImpostor = 3;
})();
7 changes: 5 additions & 2 deletions Babylon/Shaders/shadowMap.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ vec2 packHalf(float depth)
return color - (color.yy * bitOffset);
}

#ifndef VSM
varying vec4 vPosition;
#endif

void main(void)
{
#ifdef VSM
float moment1 = gl_FragCoord.z / gl_FragCoord.w;
float moment1 = gl_FragCoord.z / gl_FragCoord.w;
float moment2 = moment1 * moment1;
gl_FragColor = vec4(packHalf(moment1), packHalf(moment2));
#else
gl_FragColor = pack(gl_FragCoord.z / gl_FragCoord.w);
gl_FragColor = pack(vPosition.z / vPosition.w);
#endif
}
Loading

0 comments on commit 8495199

Please sign in to comment.