|
@@ -27814,7 +27814,29 @@ var BABYLON;
|
|
|
var tmpQ = new CANNON.Quaternion(-0.7071067811865475, 0, 0, 0.7071067811865475);
|
|
|
body.quaternion = body.quaternion.mult(tmpQ);
|
|
|
}
|
|
|
- //TODO - If the body is heightmap-based, this won't work correctly. find a good fix.
|
|
|
+ if (registeredMesh.heightmap) {
|
|
|
+ //calculate the correct body position:
|
|
|
+ var rotationQuaternion = mesh.rotationQuaternion;
|
|
|
+ mesh.rotationQuaternion = new BABYLON.Quaternion();
|
|
|
+ mesh.computeWorldMatrix(true);
|
|
|
+ //get original center with no rotation
|
|
|
+ var center = mesh.getBoundingInfo().boundingBox.center.clone();
|
|
|
+ var oldPivot = mesh.getPivotMatrix() || BABYLON.Matrix.Translation(0, 0, 0);
|
|
|
+ //rotation is back
|
|
|
+ mesh.rotationQuaternion = rotationQuaternion;
|
|
|
+ //calculate the new center using a pivot (since Cannon.js doesn't center height maps)
|
|
|
+ var p = BABYLON.Matrix.Translation(mesh.getBoundingInfo().boundingBox.extendSize.x, 0, -mesh.getBoundingInfo().boundingBox.extendSize.z);
|
|
|
+ mesh.setPivotMatrix(p);
|
|
|
+ mesh.computeWorldMatrix(true);
|
|
|
+ //calculate the translation
|
|
|
+ var translation = mesh.getBoundingInfo().boundingBox.center.subtract(center).subtract(mesh.position).negate();
|
|
|
+ body.position = new CANNON.Vec3(translation.x, translation.y - mesh.getBoundingInfo().boundingBox.extendSize.y, translation.z);
|
|
|
+ //add it inverted to the delta
|
|
|
+ registeredMesh.delta = mesh.getBoundingInfo().boundingBox.center.subtract(center);
|
|
|
+ registeredMesh.delta.y += mesh.getBoundingInfo().boundingBox.extendSize.y;
|
|
|
+ mesh.setPivotMatrix(oldPivot);
|
|
|
+ mesh.computeWorldMatrix(true);
|
|
|
+ }
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
@@ -27902,9 +27924,10 @@ var BABYLON;
|
|
|
CannonJSPlugin.prototype._createConvexPolyhedron = function (rawVerts, rawFaces, mesh) {
|
|
|
var verts = [], faces = [];
|
|
|
mesh.computeWorldMatrix(true);
|
|
|
+ //reuse this variable
|
|
|
+ var transformed = BABYLON.Vector3.Zero();
|
|
|
// Get vertices
|
|
|
for (var i = 0; i < rawVerts.length; i += 3) {
|
|
|
- var transformed = BABYLON.Vector3.Zero();
|
|
|
BABYLON.Vector3.TransformNormalFromFloatsToRef(rawVerts[i], rawVerts[i + 1], rawVerts[i + 2], mesh.getWorldMatrix(), transformed);
|
|
|
verts.push(new CANNON.Vec3(transformed.x, transformed.y, transformed.z));
|
|
|
}
|
|
@@ -27915,41 +27938,51 @@ var BABYLON;
|
|
|
var shape = new CANNON.ConvexPolyhedron(verts, faces);
|
|
|
return shape;
|
|
|
};
|
|
|
- CannonJSPlugin.prototype._createHeightmap = function (mesh) {
|
|
|
+ CannonJSPlugin.prototype._createHeightmap = function (mesh, pointDepth) {
|
|
|
var pos = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
|
|
|
- if (pos[0] !== pos[pos.length - 1]) {
|
|
|
- console.log("ERROR");
|
|
|
- return;
|
|
|
- }
|
|
|
var matrix = [];
|
|
|
- //dimension
|
|
|
- var dim = -pos[0];
|
|
|
- //array size
|
|
|
- var arraySize = -1;
|
|
|
- for (var i = 0; i < pos.length; i = i + 3) {
|
|
|
- if (pos[i + 2] === pos[2]) {
|
|
|
- arraySize++;
|
|
|
- }
|
|
|
- else {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- //calculate element size
|
|
|
+ //For now pointDepth will not be used and will be automatically calculated.
|
|
|
+ //Future reference - try and find the best place to add a reference to the pointDepth variable.
|
|
|
+ var arraySize = pointDepth || ~~(Math.sqrt(pos.length / 3) - 1);
|
|
|
+ var dim = Math.min(mesh.getBoundingInfo().boundingBox.extendSize.x, mesh.getBoundingInfo().boundingBox.extendSize.z);
|
|
|
var elementSize = dim * 2 / arraySize;
|
|
|
- //calculate the matrix for cannon's heightfield
|
|
|
+ var minY = mesh.getBoundingInfo().boundingBox.extendSize.y;
|
|
|
for (var i = 0; i < pos.length; i = i + 3) {
|
|
|
var x = Math.round((pos[i + 0]) / elementSize + arraySize / 2);
|
|
|
var z = Math.round(((pos[i + 2]) / elementSize - arraySize / 2) * -1);
|
|
|
+ var y = pos[i + 1] + minY;
|
|
|
if (!matrix[x]) {
|
|
|
matrix[x] = [];
|
|
|
}
|
|
|
- matrix[x][z] = pos[i + 1];
|
|
|
+ if (!matrix[x][z]) {
|
|
|
+ matrix[x][z] = y;
|
|
|
+ }
|
|
|
+ matrix[x][z] = Math.max(y, matrix[x][z]);
|
|
|
+ }
|
|
|
+ for (var x = 0; x <= arraySize; ++x) {
|
|
|
+ if (!matrix[x]) {
|
|
|
+ var loc = 1;
|
|
|
+ while (!matrix[(x + loc) % arraySize]) {
|
|
|
+ loc++;
|
|
|
+ }
|
|
|
+ matrix[x] = matrix[(x + loc) % arraySize].slice();
|
|
|
+ }
|
|
|
+ for (var z = 0; z <= arraySize; ++z) {
|
|
|
+ if (!matrix[x][z]) {
|
|
|
+ var loc = 1;
|
|
|
+ var newValue;
|
|
|
+ while (newValue === undefined) {
|
|
|
+ newValue = matrix[x][(z + loc++) % arraySize];
|
|
|
+ }
|
|
|
+ matrix[x][z] = newValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
var shape = new CANNON.Heightfield(matrix, {
|
|
|
elementSize: elementSize
|
|
|
});
|
|
|
//For future reference, needed for body transformation
|
|
|
- shape.dim = dim;
|
|
|
+ shape.minY = minY;
|
|
|
return shape;
|
|
|
};
|
|
|
CannonJSPlugin.prototype._addMaterial = function (friction, restitution) {
|
|
@@ -27990,19 +28023,37 @@ var BABYLON;
|
|
|
//-90 DEG in X, precalculated
|
|
|
var tmpQ = new CANNON.Quaternion(-0.7071067811865475, 0, 0, 0.7071067811865475);
|
|
|
body.quaternion = body.quaternion.mult(tmpQ);
|
|
|
- //Invert!
|
|
|
+ //Invert! (Precalculated, 90 deg in X)
|
|
|
deltaRotation = new BABYLON.Quaternion(0.7071067811865475, 0, 0, 0.7071067811865475);
|
|
|
}
|
|
|
//If it is a heightfield, if should be centered.
|
|
|
if (shape.type === CANNON.Shape.types.HEIGHTFIELD) {
|
|
|
- body.position = new CANNON.Vec3(-shape.dim, 0, shape.dim).vadd(mesh.position);
|
|
|
+ //calculate the correct body position:
|
|
|
+ var rotationQuaternion = mesh.rotationQuaternion;
|
|
|
+ mesh.rotationQuaternion = new BABYLON.Quaternion();
|
|
|
+ mesh.computeWorldMatrix(true);
|
|
|
+ //get original center with no rotation
|
|
|
+ var center = mesh.getBoundingInfo().boundingBox.center.clone();
|
|
|
+ var oldPivot = mesh.getPivotMatrix() || BABYLON.Matrix.Translation(0, 0, 0);
|
|
|
+ //rotation is back
|
|
|
+ mesh.rotationQuaternion = rotationQuaternion;
|
|
|
+ //calculate the new center using a pivot (since Cannon.js doesn't center height maps)
|
|
|
+ var p = BABYLON.Matrix.Translation(mesh.getBoundingInfo().boundingBox.extendSize.x, 0, -mesh.getBoundingInfo().boundingBox.extendSize.z);
|
|
|
+ mesh.setPivotMatrix(p);
|
|
|
+ mesh.computeWorldMatrix(true);
|
|
|
+ //calculate the translation
|
|
|
+ var translation = mesh.getBoundingInfo().boundingBox.center.subtract(center).subtract(mesh.position).negate();
|
|
|
+ body.position = new CANNON.Vec3(translation.x, translation.y - mesh.getBoundingInfo().boundingBox.extendSize.y, translation.z);
|
|
|
//add it inverted to the delta
|
|
|
- deltaPosition = mesh.position.add(new BABYLON.Vector3(shape.dim, 0, -shape.dim));
|
|
|
+ deltaPosition = mesh.getBoundingInfo().boundingBox.center.subtract(center);
|
|
|
+ deltaPosition.y += mesh.getBoundingInfo().boundingBox.extendSize.y;
|
|
|
+ mesh.setPivotMatrix(oldPivot);
|
|
|
+ mesh.computeWorldMatrix(true);
|
|
|
}
|
|
|
//add the shape
|
|
|
body.addShape(shape);
|
|
|
this._world.add(body);
|
|
|
- this._registeredMeshes.push({ mesh: mesh, body: body, material: material, delta: deltaPosition, deltaRotation: deltaRotation });
|
|
|
+ this._registeredMeshes.push({ mesh: mesh, body: body, material: material, delta: deltaPosition, deltaRotation: deltaRotation, heightmap: shape.type === CANNON.Shape.types.HEIGHTFIELD });
|
|
|
return body;
|
|
|
};
|
|
|
CannonJSPlugin.prototype.registerMeshesAsCompound = function (parts, options) {
|
|
@@ -35818,7 +35869,7 @@ var BABYLON;
|
|
|
var BABYLON;
|
|
|
(function (BABYLON) {
|
|
|
var SolidParticleSystem = (function () {
|
|
|
- function SolidParticleSystem(name, scene) {
|
|
|
+ function SolidParticleSystem(name, scene, options) {
|
|
|
// public members
|
|
|
this.particles = new Array();
|
|
|
this.nbParticles = 0;
|
|
@@ -35830,6 +35881,7 @@ var BABYLON;
|
|
|
this._colors = new Array();
|
|
|
this._uvs = new Array();
|
|
|
this._index = 0; // indices index
|
|
|
+ this._updatable = true;
|
|
|
this._shapeCounter = 0;
|
|
|
this._copy = new BABYLON.SolidParticle(null, null, null, null, null);
|
|
|
this._color = new BABYLON.Color4(0, 0, 0, 0);
|
|
@@ -35864,10 +35916,15 @@ var BABYLON;
|
|
|
this.name = name;
|
|
|
this._scene = scene;
|
|
|
this._camera = scene.activeCamera;
|
|
|
+ if (options && options.updatable) {
|
|
|
+ this._updatable = options.updatable;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ this._updatable = true;
|
|
|
+ }
|
|
|
}
|
|
|
// build the SPS mesh : returns the mesh
|
|
|
- SolidParticleSystem.prototype.buildMesh = function (updatable) {
|
|
|
- if (updatable === void 0) { updatable = true; }
|
|
|
+ SolidParticleSystem.prototype.buildMesh = function () {
|
|
|
if (this.nbParticles === 0) {
|
|
|
var triangle = BABYLON.MeshBuilder.CreateDisc("", { radius: 1, tessellation: 3 }, this._scene);
|
|
|
this.addShape(triangle, 1);
|
|
@@ -35890,14 +35947,14 @@ var BABYLON;
|
|
|
vertexData.set(this._colors32, BABYLON.VertexBuffer.ColorKind);
|
|
|
}
|
|
|
var mesh = new BABYLON.Mesh(name, this._scene);
|
|
|
- vertexData.applyToMesh(mesh, updatable);
|
|
|
+ vertexData.applyToMesh(mesh, this._updatable);
|
|
|
this.mesh = mesh;
|
|
|
// free memory
|
|
|
this._positions = null;
|
|
|
this._normals = null;
|
|
|
this._uvs = null;
|
|
|
this._colors = null;
|
|
|
- if (!updatable) {
|
|
|
+ if (!this._updatable) {
|
|
|
this.particles.length = 0;
|
|
|
}
|
|
|
return mesh;
|
|
@@ -36015,7 +36072,9 @@ var BABYLON;
|
|
|
// particles
|
|
|
for (var i = 0; i < nb; i++) {
|
|
|
this._meshBuilder(this._index, shape, this._positions, meshInd, this._indices, meshUV, this._uvs, meshCol, this._colors, this.nbParticles + i, i, options);
|
|
|
- this._addParticle(this.nbParticles + i, this._positions.length, modelShape, this._shapeCounter, i);
|
|
|
+ if (this._updatable) {
|
|
|
+ this._addParticle(this.nbParticles + i, this._positions.length, modelShape, this._shapeCounter, i);
|
|
|
+ }
|
|
|
this._index += shape.length;
|
|
|
}
|
|
|
this.nbParticles += nb;
|
|
@@ -36227,6 +36286,16 @@ var BABYLON;
|
|
|
// dispose the SPS
|
|
|
SolidParticleSystem.prototype.dispose = function () {
|
|
|
this.mesh.dispose();
|
|
|
+ // drop references to internal big arrays for the GC
|
|
|
+ this._positions = null;
|
|
|
+ this._indices = null;
|
|
|
+ this._normals = null;
|
|
|
+ this._uvs = null;
|
|
|
+ this._colors = null;
|
|
|
+ this._positions32 = null;
|
|
|
+ this._normals32 = null;
|
|
|
+ this._uvs32 = null;
|
|
|
+ this._colors32 = null;
|
|
|
};
|
|
|
Object.defineProperty(SolidParticleSystem.prototype, "computeParticleRotation", {
|
|
|
// getters
|