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

120 lines
2.6 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
*/
2017-01-06 16:53:53 +01:00
GameLib.D3.Spline = function (
2016-11-28 15:05:02 +01:00
graphics,
apiSpline
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiSpline)) {
apiSpline = {};
}
2016-12-22 17:22:19 +01:00
GameLib.D3.API.Spline.call(
this,
apiSpline.id,
apiSpline.name,
2017-01-02 17:05:40 +01:00
apiSpline.vertices,
apiSpline.parentEntity
2016-12-22 17:22:19 +01:00
);
2016-12-23 16:07:10 +01:00
this.vertices = this.vertices.map(
function (vertex) {
return new GameLib.Vector3(
graphics,
vertex,
this
2016-12-23 16:07:10 +01:00
)
}
);
2016-11-28 15:05:02 +01:00
this.instance = this.createInstance();
};
2016-12-22 17:22:19 +01:00
GameLib.D3.Spline.prototype = Object.create(GameLib.D3.API.Spline.prototype);
GameLib.D3.Spline.prototype.constructor = GameLib.D3.Spline;
2016-11-28 15:05:02 +01:00
/**
* Creates an instance spline
* @param update boolean
*/
GameLib.D3.Spline.prototype.createInstance = function(update) {
2016-12-22 17:22:19 +01:00
if (update) {
return this.instance;
2016-11-28 15:05:02 +01:00
}
2016-12-22 17:22:19 +01:00
var vertices = this.vertices.map(
function (vertex) {
return vertex.instance;
}
);
return new THREE.CatmullRomCurve3(vertices);
2016-11-28 15:05:02 +01:00
};
/**
* Updates the instance
*/
GameLib.D3.Spline.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
2016-12-09 20:32:09 +01:00
/**
* Converts a GameLib.D3.Spline to GameLib.D3.API.Spline
* @returns {GameLib.D3.API.Spline}
*/
2016-12-22 17:22:19 +01:00
GameLib.D3.Spline.prototype.toApiComponent = function() {
2016-12-09 20:32:09 +01:00
return new GameLib.D3.API.Spline(
this.id,
this.name,
2016-12-22 17:22:19 +01:00
this.vertices.map(
function (vertex) {
2016-12-23 16:07:10 +01:00
return vertex.toApiVector()
2016-12-22 17:22:19 +01:00
}
2017-01-02 17:05:40 +01:00
),
GameLib.Utils.IdOrNull(this.parentEntity)
2016-12-09 20:32:09 +01:00
);
};
/**
* Returns a GameLib.D3.Spline from a spline Object
* @param graphics GameLib.D3.Graphics
2016-12-22 17:22:19 +01:00
* @param objectComponent Object
2017-01-02 17:05:40 +01:00
* @returns {GameLib.D3.Spline}
2016-12-09 20:32:09 +01:00
* @constructor
*/
2016-12-22 17:22:19 +01:00
GameLib.D3.Spline.FromObjectComponent = function(
2016-12-09 20:32:09 +01:00
graphics,
2016-12-22 17:22:19 +01:00
objectComponent
2016-12-09 20:32:09 +01:00
) {
2017-01-06 16:53:53 +01:00
var apiSpline = GameLib.D3.API.Spline.FromObjectComponent(objectComponent);
2016-12-09 20:32:09 +01:00
return new GameLib.D3.Spline(
graphics,
2016-12-22 17:22:19 +01:00
apiSpline
2016-12-09 20:32:09 +01:00
);
};
2017-01-06 16:53:53 +01:00
/**
* 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,
2017-01-06 16:53:53 +01:00
0.1
);
};