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

126 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-06-24 02:42:28 +02:00
/**
*
2017-06-24 02:42:28 +02:00
* @param physics
* @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,
zValues,
2017-06-24 02:42:28 +02:00
minValue,
maxValue,
elementSize
) {
this.physics = physics;
this.physics.isNotCannonThrow();
if (GameLib.Utils.UndefinedOrNull(zValues)) {
zValues = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10];
2017-06-24 02:42:28 +02:00
}
this.zValues = zValues;
2017-06-24 02:42:28 +02:00
if (GameLib.Utils.UndefinedOrNull(minValue)) {
minValue = 0;
}
this.minValue = minValue;
2017-06-24 02:42:28 +02:00
if (GameLib.Utils.UndefinedOrNull(maxValue)) {
maxValue = 10;
}
this.maxValue = maxValue;
2017-06-24 02:42:28 +02:00
if (GameLib.Utils.UndefinedOrNull(elementSize)) {
elementSize = 1;
}
this.elementSize = elementSize;
2017-06-24 02:42:28 +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(
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() {
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();
};
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;
}
this.zValues = this.parentMesh.getHeightData();
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
};