/** * Spline constructor * @param graphics GameLib.D3.Graphics * @param apiSpline GameLib.D3.API.Spline * @constructor */ GameLib.D3.Spline = function RuntimeSpline( graphics, apiSpline ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); GameLib.D3.API.Spline.call( this, apiSpline.id, apiSpline.name, apiSpline.vertices ); 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); }; /** * 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.toApiComponent = function() { return new GameLib.D3.API.Spline( this.id, this.name, this.vertices.map( function (vertex) { return vertex.toApiVertex() } ) ); }; /** * 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 = new GameLib.D3.API.Spline( objectComponent.id, objectComponent.name, objectComponent.vertices.map( function (objectVertex) { return GameLib.D3.Vertex.FromObjectVertex(graphics, objectVertex); } ) ); return new GameLib.D3.Spline( graphics, apiSpline ); };