2017-06-24 02:42:28 +02:00
|
|
|
/**
|
2017-09-03 15:49:27 +02:00
|
|
|
*
|
2017-06-24 02:42:28 +02:00
|
|
|
* @param physics
|
2017-09-03 15:49:27 +02:00
|
|
|
* @param apiShape
|
2017-09-05 13:18:10 +02:00
|
|
|
* @param heightData
|
2017-06-24 02:42:28 +02:00
|
|
|
* @param minValue
|
|
|
|
* @param maxValue
|
|
|
|
* @param elementSize
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Shape.HeightMap = function (
|
|
|
|
physics,
|
|
|
|
apiShape,
|
2017-09-05 13:18:10 +02:00
|
|
|
heightData,
|
2017-06-24 02:42:28 +02:00
|
|
|
minValue,
|
|
|
|
maxValue,
|
|
|
|
elementSize
|
|
|
|
) {
|
|
|
|
this.physics = physics;
|
|
|
|
this.physics.isNotCannonThrow();
|
|
|
|
|
2017-09-05 13:18:10 +02:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(heightData)) {
|
|
|
|
heightData = [[10, 10, 10], [10, 10, 10], [10, 10, 10]];
|
2017-06-24 02:42:28 +02:00
|
|
|
}
|
2017-09-05 13:18:10 +02:00
|
|
|
this.heightData = heightData;
|
2017-06-24 02:42:28 +02:00
|
|
|
|
2017-09-03 15:49:27 +02:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(minValue)) {
|
|
|
|
minValue = 0;
|
|
|
|
}
|
|
|
|
this.minValue = minValue;
|
2017-06-24 02:42:28 +02:00
|
|
|
|
2017-09-03 15:49:27 +02:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(maxValue)) {
|
|
|
|
maxValue = 10;
|
|
|
|
}
|
|
|
|
this.maxValue = maxValue;
|
2017-06-24 02:42:28 +02:00
|
|
|
|
2017-09-03 15:49:27 +02:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(elementSize)) {
|
|
|
|
elementSize = 1;
|
|
|
|
}
|
|
|
|
this.elementSize = elementSize;
|
2017-06-24 02:42:28 +02:00
|
|
|
|
2017-09-03 15:49:27 +02:00
|
|
|
GameLib.D3.Shape.call(
|
2017-06-24 02:42:28 +02:00
|
|
|
this,
|
|
|
|
this.physics,
|
|
|
|
apiShape
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Shape.HeightMap.prototype = Object.create(GameLib.D3.Shape.prototype);
|
|
|
|
GameLib.D3.Shape.HeightMap.prototype.constructor = GameLib.D3.Shape.HeightMap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create instance
|
|
|
|
* @returns {GameLib.D3.Shape.HeightMap}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Shape.HeightMap.prototype.createInstance = function() {
|
|
|
|
|
|
|
|
var instance = new CANNON.Heightfield(
|
2017-09-05 13:18:10 +02:00
|
|
|
this.heightData,
|
2017-06-24 02:42:28 +02:00
|
|
|
{
|
|
|
|
elemSize : this.elementSize
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update instance
|
|
|
|
*/
|
|
|
|
GameLib.D3.Shape.HeightMap.prototype.updateInstance = function() {
|
2017-09-05 13:18:10 +02:00
|
|
|
this.instance.data = this.heightData;
|
|
|
|
// this.instance.minValue = this.minValue;
|
|
|
|
// this.instance.maxValue = this.maxValue;
|
2017-06-24 02:42:28 +02:00
|
|
|
this.instance.elemSize = this.elemSize;
|
|
|
|
this.instance.update();
|
|
|
|
// this.instance.updateBoundingSphereRadius();
|
|
|
|
// this.instance.updateMaxValue();
|
|
|
|
// this.instance.updateMinValue();
|
2017-09-03 15:49:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
GameLib.D3.Shape.HeightMap.prototype.toApiObject = function() {
|
|
|
|
var apiShape = GameLib.D3.Shape.prototype.toApiObject.call(this);
|
2017-09-05 13:18:10 +02:00
|
|
|
apiShape.heightData = this.heightData;
|
2017-09-03 15:49:27 +02:00
|
|
|
apiShape.minValue = this.minValue;
|
|
|
|
apiShape.maxValue = this.maxValue;
|
|
|
|
apiShape.elemSize = this.elemSize;
|
|
|
|
return apiShape;
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Shape.HeightMap.prototype.setFromMesh = function() {
|
|
|
|
|
|
|
|
if (this.parentMesh === null) {
|
|
|
|
console.log('select a mesh first');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.parentMesh.isHeightMap) {
|
|
|
|
console.log('not a heightmap mesh');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-05 05:22:52 +02:00
|
|
|
var dim1Array = Array.prototype.slice.call(this.parentMesh.getHeightData());
|
|
|
|
|
2017-09-05 13:18:10 +02:00
|
|
|
// var w = this.parentMesh.widthSegments + 1;
|
|
|
|
//
|
|
|
|
// var h = 0;
|
2017-09-05 05:22:52 +02:00
|
|
|
|
2017-09-05 13:18:10 +02:00
|
|
|
// var offset = 0;
|
2017-09-05 05:22:52 +02:00
|
|
|
|
2017-09-05 13:18:10 +02:00
|
|
|
this.heightData = [];
|
2017-09-05 05:22:52 +02:00
|
|
|
|
2017-09-05 13:18:10 +02:00
|
|
|
for (var x = 0; x <= this.parentMesh.widthSegments; x++) {
|
2017-09-05 05:22:52 +02:00
|
|
|
|
2017-09-05 13:18:10 +02:00
|
|
|
this.heightData[x] = [];
|
2017-09-05 05:22:52 +02:00
|
|
|
|
2017-09-05 13:18:10 +02:00
|
|
|
for (var y = 0; y <= this.parentMesh.heightSegments; y++) {
|
2017-09-05 05:22:52 +02:00
|
|
|
|
2017-09-05 13:18:10 +02:00
|
|
|
this.heightData[x][y] = dim1Array[((x * (this.parentMesh.widthSegments + 1)) + y)];
|
2017-09-05 05:22:52 +02:00
|
|
|
|
2017-09-05 13:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-09-05 05:22:52 +02:00
|
|
|
|
|
|
|
|
2017-09-03 15:49:27 +02:00
|
|
|
|
2017-09-05 13:18:10 +02:00
|
|
|
// this.heightData = dim1Array.reduce(
|
|
|
|
// function(result, value) {
|
|
|
|
//
|
|
|
|
// result[h].push(value);
|
|
|
|
//
|
|
|
|
// w--;
|
|
|
|
//
|
|
|
|
// if (w === 0) {
|
|
|
|
// w = this.parentMesh.widthSegments;
|
|
|
|
//
|
|
|
|
// if (h < this.parentMesh.heightSegments) {
|
|
|
|
// h++;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return result;
|
|
|
|
// }.bind(this),
|
|
|
|
// result
|
|
|
|
// );
|
2017-09-03 15:49:27 +02:00
|
|
|
|
|
|
|
this.updateInstance();
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Shape.HeightMap.FromObject = function(physics, objectShape) {
|
|
|
|
|
|
|
|
var apiShape = GameLib.D3.API.Shape.FromObject(objectShape);
|
|
|
|
|
|
|
|
return new GameLib.D3.Shape.HeightMap(
|
|
|
|
physics,
|
|
|
|
apiShape,
|
2017-09-05 13:18:10 +02:00
|
|
|
objectShape.heightData,
|
2017-09-03 15:49:27 +02:00
|
|
|
objectShape.minValue,
|
|
|
|
objectShape.maxValue,
|
|
|
|
objectShape.elemSize
|
|
|
|
);
|
2017-06-24 02:42:28 +02:00
|
|
|
};
|