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

144 lines
3.0 KiB
JavaScript

/**
* Spline constructor
* @param graphics R3.Runtime.Graphics
* @param apiSpline R3.D3.API.Spline
* @constructor
*/
R3.D3.Spline = function(
graphics,
apiSpline
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (R3.Utils.UndefinedOrNull(apiSpline)) {
apiSpline = {};
}
R3.D3.API.Spline.call(
this,
apiSpline.id,
apiSpline.name,
apiSpline.vertices,
apiSpline.parent
);
this.vertices = this.vertices.map(
function(vertex) {
return new R3.Vector3(
graphics,
vertex,
this
)
}
);
R3.Component.call(this);
};
R3.D3.Spline.prototype = Object.create(R3.Component.prototype);
R3.D3.Spline.prototype.constructor = R3.D3.Spline;
/**
* Creates an instance spline
*/
R3.D3.Spline.prototype.createInstance = function() {
var vertices = this.vertices.map(
function(vertex) {
if (R3.Utils.UndefinedOrNull(vertex)) {
throw new Error('no vertex')
}
if (R3.Utils.UndefinedOrNull(vertex.instance)) {
throw new Error('no vertex instance')
}
return vertex.instance;
}
);
this.instance = THREE.CatmullRomCurve3(vertices);
__CREATE_INSTANCE__;
};
/**
* Updates the instance
*/
R3.D3.Spline.prototype.updateInstance = function(property) {
var vertices = this.vertices.map(
function(vertex) {
if (R3.Utils.UndefinedOrNull(vertex)) {
throw new Error('no vertex')
}
if (R3.Utils.UndefinedOrNull(vertex.instance)) {
throw new Error('no vertex instance')
}
return vertex.instance;
}
);
this.instance = new THREE.CatmullRomCurve3(vertices);
__UPDATE_INSTANCE__;
};
/**
* Converts a R3.D3.Spline to R3.D3.API.Spline
* @returns {R3.D3.API.Spline}
*/
R3.D3.Spline.prototype.toApiObject = function() {
return new R3.D3.API.Spline(
this.id,
this.name,
this.vertices.map(
function(vertex) {
return vertex.toApiObject()
}
),
R3.Utils.IdOrNull(this.parent)
);
};
/**
* Returns a R3.D3.Spline from a spline Object
* @param graphics R3.Runtime.Graphics
* @param objectComponent Object
* @returns {R3.D3.Spline}
* @constructor
*/
R3.D3.Spline.FromObject = function(
graphics,
objectComponent
) {
var apiSpline = R3.D3.API.Spline.FromObject(objectComponent);
return new R3.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 {*}
*/
R3.D3.Spline.prototype.getPointAt = function(proper) {
var point = this.instance.getPointAt(proper);
return new R3.Vector3(
this.graphics,
new R3.API.Vector3(point.x, point.y, point.z),
this,
0.1
);
};