r3-legacy/src/game-lib-d3-spline.js

120 lines
2.6 KiB
JavaScript

/**
* Spline constructor
* @param graphics GameLib.D3.Graphics
* @param apiSpline GameLib.D3.API.Spline
* @constructor
*/
GameLib.D3.Spline = function (
graphics,
apiSpline
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiSpline)) {
apiSpline = {};
}
GameLib.D3.API.Spline.call(
this,
apiSpline.id,
apiSpline.name,
apiSpline.vertices,
apiSpline.parentEntity
);
this.vertices = this.vertices.map(
function (vertex) {
return new GameLib.Vector3(
graphics,
vertex,
this
)
}
);
this.instance = this.createInstance();
};
GameLib.D3.Spline.prototype = Object.create(GameLib.D3.API.Spline.prototype);
GameLib.D3.Spline.prototype.constructor = GameLib.D3.Spline;
/**
* Creates an instance spline
* @param update boolean
*/
GameLib.D3.Spline.prototype.createInstance = function(update) {
if (update) {
return this.instance;
}
var vertices = this.vertices.map(
function (vertex) {
return vertex.instance;
}
);
return new THREE.CatmullRomCurve3(vertices);
};
/**
* Updates the instance
*/
GameLib.D3.Spline.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
/**
* Converts a GameLib.D3.Spline to GameLib.D3.API.Spline
* @returns {GameLib.D3.API.Spline}
*/
GameLib.D3.Spline.prototype.toApiComponent = function() {
return new GameLib.D3.API.Spline(
this.id,
this.name,
this.vertices.map(
function (vertex) {
return vertex.toApiVector()
}
),
GameLib.Utils.IdOrNull(this.parentEntity)
);
};
/**
* Returns a GameLib.D3.Spline from a spline Object
* @param graphics GameLib.D3.Graphics
* @param objectComponent Object
* @returns {GameLib.D3.Spline}
* @constructor
*/
GameLib.D3.Spline.FromObjectComponent = function(
graphics,
objectComponent
) {
var apiSpline = GameLib.D3.API.Spline.FromObjectComponent(objectComponent);
return new GameLib.D3.Spline(
graphics,
apiSpline
);
};
/**
* Gets the current point from the spline at the proper value
* @param proper Number (fraction between 0 and 1 indicating position on spline)
* @returns {*}
*/
GameLib.D3.Spline.prototype.getPointAt = function(proper) {
var point = this.instance.getPointAt(proper);
return new GameLib.Vector3(
this.graphics,
new GameLib.API.Vector3(point.x, point.y, point.z),
this,
0.1
);
};