/** * Spline constructor * @param graphics GameLib.D3.Graphics * @param apiSpline GameLib.D3.API.Spline * @constructor */ GameLib.D3.Spline = function Spline( 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( 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); return new GameLib.Vector3( this.graphics, this, new GameLib.API.Vector3(point.x, point.y, point.z), 0.1 ); }; /** * 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 ) ); };