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
|
|
|
|
* @param zValues
|
2017-06-24 02:42:28 +02:00
|
|
|
* @param minValue
|
|
|
|
* @param maxValue
|
|
|
|
* @param elementSize
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Shape.HeightMap = function (
|
|
|
|
physics,
|
|
|
|
apiShape,
|
2017-09-03 15:49:27 +02:00
|
|
|
zValues,
|
2017-06-24 02:42:28 +02:00
|
|
|
minValue,
|
|
|
|
maxValue,
|
|
|
|
elementSize
|
|
|
|
) {
|
|
|
|
this.physics = physics;
|
|
|
|
this.physics.isNotCannonThrow();
|
|
|
|
|
2017-09-03 15:49:27 +02:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(zValues)) {
|
|
|
|
zValues = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10];
|
2017-06-24 02:42:28 +02:00
|
|
|
}
|
2017-09-03 15:49:27 +02:00
|
|
|
this.zValues = zValues;
|
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-03 15:49:27 +02:00
|
|
|
this.zValues,
|
2017-06-24 02:42:28 +02:00
|
|
|
{
|
|
|
|
minValue : this.minValue,
|
|
|
|
maxValue : this.maxValue,
|
|
|
|
elemSize : this.elementSize
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update instance
|
|
|
|
*/
|
|
|
|
GameLib.D3.Shape.HeightMap.prototype.updateInstance = function() {
|
2017-09-03 15:49:27 +02:00
|
|
|
this.instance.data = this.zValues;
|
2017-06-24 02:42:28 +02:00
|
|
|
this.instance.minValue = this.minValue;
|
|
|
|
this.instance.maxValue = this.maxValue;
|
|
|
|
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);
|
|
|
|
apiShape.zValues = this.zValues;
|
|
|
|
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());
|
|
|
|
|
|
|
|
var w = 0;
|
|
|
|
|
|
|
|
var h = 0;
|
|
|
|
|
|
|
|
var width = this.parentMesh.widthSegments + 1;
|
|
|
|
|
|
|
|
var height = this.parentMesh.heightSegments + 1;
|
|
|
|
|
|
|
|
var dim2Array = dim1Array.reduce(
|
|
|
|
function(result, value) {
|
|
|
|
|
|
|
|
result[h].push(value);
|
|
|
|
|
|
|
|
w++;
|
|
|
|
|
|
|
|
if (w === this.parentMesh.widthSegments) {
|
|
|
|
w = 0;
|
|
|
|
h++;
|
|
|
|
result[h] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}.bind(this),
|
|
|
|
[[]]
|
|
|
|
);
|
|
|
|
|
|
|
|
this.zValues = dim2Array;
|
2017-09-03 15:49:27 +02:00
|
|
|
|
|
|
|
this.minValue = 0;
|
|
|
|
this.maxValue = (this.parentMesh.widthSegments + 1) * (this.parentMesh.heightSegments + 1);
|
|
|
|
|
|
|
|
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,
|
|
|
|
objectShape.zValues,
|
|
|
|
objectShape.minValue,
|
|
|
|
objectShape.maxValue,
|
|
|
|
objectShape.elemSize
|
|
|
|
);
|
2017-06-24 02:42:28 +02:00
|
|
|
};
|