/** * * @param physics * @param apiShape * @param zValues * @param minValue * @param maxValue * @param elementSize * @constructor */ GameLib.D3.Shape.HeightMap = function ( physics, apiShape, zValues, 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]; } this.zValues = zValues; 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() { var instance = new CANNON.Heightfield( this.zValues, { 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; 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 ); };