heightfield shape

beta.r3js.org
polygonboutique 2016-10-13 15:16:28 +02:00
parent 1528637994
commit 7f4f7f0a2d
1 changed files with 19 additions and 11 deletions

View File

@ -1225,6 +1225,7 @@ GameLib.D3.Physics.Shape.TYPE_SPHERE = 1;
GameLib.D3.Physics.Shape.TYPE_BOX = 2;
GameLib.D3.Physics.Shape.TYPE_TRIMESH = 3;
GameLib.D3.Physics.Shape.TYPE_CYLINDER = 4;
GameLib.D3.Physics.Shape.TYPE_HEIGHTFIELD = 5;
/**
* Physics Convex Hull Shape Superset
@ -2874,6 +2875,15 @@ GameLib.D3.Physics.World.prototype.CreateCylinderShape = function(
}
};
GameLib.D3.Physics.World.prototype.CreateHeightfieldShape = function (
heightmapData,
elementSize
) {
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
return new GameLib.D3.Physics.Shape(new CANNON.Heightfield(heightmapData.matrix, { elementSize: elementSize }), GameLib.D3.Physics.Shape.TYPE_HEIGHTFIELD);
}
};
GameLib.D3.Physics.World.prototype.AddRigidBody = function(
rigidBody // Physics.RigidBody
) {
@ -3265,20 +3275,17 @@ GameLib.D3.HeightmapData = function (
this.matrix = matrix || [];
};
// Note: this currently only works for cannon!
GameLib.D3.GenerateThreeMeshFromHeightField = function (
heightFieldShape
// Physics type.....
) {
var geometry = new THREE.Geometry();
var v0 = new CANNON.Vec3();
var v1 = new CANNON.Vec3();
var v2 = new CANNON.Vec3();
var v0 = new THREE.Vector3();
var v1 = new THREE.Vector3();
var v2 = new THREE.Vector3();
var shape = heightFieldShape;
var shape = heightFieldShape.shapeObject;
for (var xi = 0; xi < shape.data.length - 1; xi++) {
for (var yi = 0; yi < shape.data[xi].length - 1; yi++) {
for (var k = 0; k < 2; k++) {
@ -3286,9 +3293,9 @@ GameLib.D3.GenerateThreeMeshFromHeightField = function (
v0.copy(shape.pillarConvex.vertices[0]);
v1.copy(shape.pillarConvex.vertices[1]);
v2.copy(shape.pillarConvex.vertices[2]);
v0.vadd(shape.pillarOffset, v0);
v1.vadd(shape.pillarOffset, v1);
v2.vadd(shape.pillarOffset, v2);
v0.add(shape.pillarOffset);
v1.add(shape.pillarOffset);
v2.add(shape.pillarOffset);
geometry.vertices.push(
new THREE.Vector3(v0.x, v0.y, v0.z),
new THREE.Vector3(v1.x, v1.y, v1.z),
@ -3307,6 +3314,7 @@ GameLib.D3.GenerateThreeMeshFromHeightField = function (
GameLib.D3.GenerateHeightmapDataFromImage = function (
imagePath,
heightScale,
callback // receives HeightmapData instance as the first argument
) {
var img = new Image();
@ -3335,7 +3343,7 @@ GameLib.D3.GenerateHeightmapDataFromImage = function (
for (var i = 0; i < sizeX; i++) {
matrix.push([]);
for (var j = 0; j < sizeY; j++) {
var height = (heightList[(sizeX - i) + j * sizeY] / 255) * 15;
var height = (heightList[(sizeX - i) + j * sizeY] / 255) * heightScale;
matrix[i].push(height);
}
}