r3-legacy/src/game-lib-d3-shape-height-ma...

165 lines
3.7 KiB
JavaScript

/**
*
* @param physics
* @param apiShape
* @param heightData
* @param minValue
* @param maxValue
* @param elementSize
* @constructor
*/
GameLib.D3.Shape.HeightMap = function (
physics,
apiShape,
heightData,
minValue,
maxValue,
elementSize
) {
this.physics = physics;
this.physics.isNotCannonThrow();
if (GameLib.Utils.UndefinedOrNull(heightData)) {
heightData = [[10, 10, 10], [10, 10, 10], [10, 10, 10]];
}
this.heightData = heightData;
if (GameLib.Utils.UndefinedOrNull(minValue)) {
minValue = 0;
}
this.minValue = minValue;
if (GameLib.Utils.UndefinedOrNull(maxValue)) {
maxValue = 10;
}
this.maxValue = maxValue;
if (GameLib.Utils.UndefinedOrNull(elementSize)) {
elementSize = 1;
}
this.elementSize = elementSize;
GameLib.D3.Shape.call(
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() {
//TODO: initialize properly and throw when errors
this.instance = new CANNON.Heightfield(
this.heightData,
{
elemSize : this.elementSize
}
);
GameLib.D3.Shape.prototype.createInstance.call(this);
};
/**
* Update instance
*/
GameLib.D3.Shape.HeightMap.prototype.updateInstance = function() {
this.instance.data = this.heightData;
// 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();
};
GameLib.D3.Shape.HeightMap.prototype.toApiObject = function() {
var apiShape = GameLib.D3.Shape.prototype.toApiObject.call(this);
apiShape.heightData = this.heightData;
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;
}
var dim1Array = Array.prototype.slice.call(this.parentMesh.getHeightData());
// var w = this.parentMesh.widthSegments + 1;
//
// var h = 0;
// var offset = 0;
this.heightData = [];
for (var x = 0; x <= this.parentMesh.widthSegments; x++) {
this.heightData[x] = [];
for (var y = 0; y <= this.parentMesh.heightSegments; y++) {
this.heightData[x][y] = dim1Array[((x * (this.parentMesh.widthSegments + 1)) + y)];
}
}
// 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
// );
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.heightData,
objectShape.minValue,
objectShape.maxValue,
objectShape.elemSize
);
};