r3-legacy/src/r3-vector3.js

124 lines
2.3 KiB
JavaScript

/**
* R3.Vector3
* @param apiComponent
*
* @property x
* @property y
* @property z
*
* @constructor
*/
R3.Vector3 = function(
apiComponent
) {
__RUNTIME_COMPONENT__;
__UPGRADE_TO_RUNTIME__;
};
R3.Vector3.prototype = Object.create(R3.Component.prototype);
R3.Vector3.prototype.constructor = R3.Vector3;
/**
* Creates an instance vector3
* @returns {*}
*/
R3.Vector3.prototype.createInstance = function() {
var runtime = R3.Component.GetComponentRuntime(this.parent);
switch (runtime) {
case R3.Runtime.GRAPHICS :
this.instance = this.graphics.Vector3(
this.x,
this.y,
this.z
);
break;
case R3.Runtime.PHYSICS :
this.instance = this.physics.Vector3(
this.x,
this.y,
this.z
);
break;
default:
throw new Error('unhandled component runtime: ' + runtime);
}
__CREATE_INSTANCE__;
};
/**
* Updates the instance vector, calls updateInstance on the parent object
*/
R3.Vector3.prototype.updateInstance = function(property) {
if (property === 'x') {
this.instance.x = this.x;
return;
}
if (property === 'y') {
this.instance.y = this.y;
return;
}
if (property === 'z') {
this.instance.z = this.z;
return;
}
__UPDATE_INSTANCE__;
};
/**
* Clones this vector
* @returns {R3.Vector3|R3.Vector3|R3.Vector3}
*/
R3.Vector3.prototype.clone = function() {
return new R3.Vector3(this);
};
/**
* Create a negative version of this vector
* @returns {R3.Vector3}
*/
R3.Vector3.prototype.negativeCopy = function() {
var vector3 = new R3.Vector3(this);
vector3.x *= -1;
vector3.y *= -1;
vector3.z *= -1;
return vector3;
};
/**
* Applies rotation specified by axis / angle to this vector - its a wrapper for three..
* @param axis
* @param angle
*/
R3.Vector3.prototype.applyAxisAngle = function(axis, angle) {
console.warn('todo: do nor rely on graphics instance');
return;
this.instance.applyAxisAngle(
new THREE.Vector3(
axis.x,
axis.y,
axis.z
),
angle
);
this.x = this.instance.x;
this.y = this.instance.y;
this.z = this.instance.z;
return this;
};