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

101 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-11-28 15:05:02 +01:00
/**
* Spline constructor
* @param graphics GameLib.D3.Graphics
2016-11-29 12:54:25 +01:00
* @param apiSpline GameLib.D3.API.Spline
2016-11-28 15:05:02 +01:00
* @constructor
*/
GameLib.D3.Spline = function Spline(
2016-11-28 15:05:02 +01:00
graphics,
apiSpline
) {
for (var property in apiSpline) {
if (apiSpline.hasOwnProperty(property)) {
this[property] = apiSpline[property];
}
}
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.instance = this.createInstance();
};
/**
* Creates an instance spline
* @param update boolean
*/
GameLib.D3.Spline.prototype.createInstance = function(update) {
var vertices = [];
for (var v = 0; v < this.vertices.length; v++) {
vertices.push(new THREE.Vector3(
2016-11-28 15:05:02 +01:00
this.vertices[v].x,
this.vertices[v].y,
this.vertices[v].z
));
}
return new this.graphics.instance.CatmullRomCurve3(vertices);
};
/**
* Updates the instance
*/
GameLib.D3.Spline.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
/**
* 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);
2016-12-15 14:53:39 +01:00
return new GameLib.Vector3(
this.graphics,
this,
2016-12-15 14:53:39 +01:00
new GameLib.API.Vector3(point.x, point.y, point.z),
0.1
);
};
2016-12-09 20:32:09 +01:00
/**
* Converts a GameLib.D3.Spline to GameLib.D3.API.Spline
* @returns {GameLib.D3.API.Spline}
*/
GameLib.D3.Spline.prototype.toApiSpline = function() {
return new GameLib.D3.API.Spline(
this.id,
this.name,
this.vertices
);
};
/**
* Returns a GameLib.D3.Spline from a spline Object
* @param graphics GameLib.D3.Graphics
* @param objectSpline Object
* @returns {GameLib.D3.Spline}
* @constructor
*/
GameLib.D3.Spline.FromObjectSpline = function(
graphics,
objectSpline
) {
return new GameLib.D3.Spline(
graphics,
new GameLib.D3.API.Spline(
objectSpline.id,
objectSpline.name,
objectSpline.vertices
)
);
2016-11-28 15:05:02 +01:00
2016-12-09 20:32:09 +01:00
};