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

153 lines
4.4 KiB
JavaScript

/**
* Helpers for displaying outlines or making 'invisible' scene objects visible
* @param graphics GameLib.D3.Graphics
* @param id
* @param name
* @param object
* @param helperType
* @param parentEntity
* @constructor
*/
GameLib.D3.Helper = function(
graphics,
id,
name,
object,
helperType,
parentEntity
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'Helper (' + id + ')';
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(object)) {
console.warn('Cannot create a helper for an Object which does not exist');
throw new Error('Cannot create a helper for an Object which does not exist');
}
this.object = object;
if (GameLib.Utils.UndefinedOrNull(helperType)) {
helperType = GameLib.D3.Helper.HELPER_TYPE_NONE;
if (
object instanceof GameLib.D3.Mesh &&
object.meshType !== GameLib.D3.Mesh.MESH_TYPE_CURVE
) {
helperType = GameLib.D3.Helper.HELPER_TYPE_EDGES;
}
if (object instanceof GameLib.D3.Light) {
if (object.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) {
helperType = GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT;
}
if (object.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT) {
helperType = GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT;
}
if (object.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT) {
helperType = GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT;
}
}
if (object instanceof GameLib.D3.Skeleton) {
helperType = GameLib.D3.Helper.HELPER_TYPE_SKELETON;
}
}
this.helperType = helperType;
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
}
this.parentEntity = parentEntity;
this.createInstance();
/**
* A helper as a component - does this make sense at all?
*/
// GameLib.Component.call(
// this,
// GameLib.Component.COMPONENT_HELPER
// );
};
GameLib.D3.Helper.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.Helper.prototype.constructor = GameLib.D3.Helper;
/**
* Helper types
* @type {string}
*/
GameLib.D3.Helper.HELPER_TYPE_NONE = 0x0;
GameLib.D3.Helper.HELPER_TYPE_EDGES = 0x1;
GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT = 0x2;
GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT = 0x3;
GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT = 0x4;
GameLib.D3.Helper.HELPER_TYPE_WIREFRAME = 0x5;
GameLib.D3.Helper.HELPER_TYPE_SKELETON = 0x6;
/**
* Creates a helper instance
*/
GameLib.D3.Helper.prototype.createInstance = function() {
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_EDGES) {
this.instance = new THREE.LineSegments(
new THREE.EdgesGeometry(
this.object.instance.geometry
),
new THREE.LineBasicMaterial(
{
color:0x00ff00,
linewidth:2
}
)
)
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT) {
this.instance = new THREE.DirectionalLightHelper(this.object.instance);
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT) {
this.instance = new THREE.PointLightHelper(this.object.instance, 1);
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT) {
this.instance = new THREE.SpotLightHelper(this.object.instance);
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_WIREFRAME) {
this.instance = new THREE.WireframeGeometry(this.object.instance, 0x00FF00);
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_SKELETON) {
this.instance = new THREE.SkeletonHelper(this.object.instance);
}
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Helper.prototype.updateInstance = function() {
this.instance.position.copy(this.object.instance.position);
if (this.object.instance.parentMesh && this.object.instance.parentMesh.instance) {
this.instance.position.add(this.object.instance.parentMesh.instance.position);
}
this.instance.scale.copy(this.object.instance.scale);
this.instance.quaternion.copy(this.object.instance.quaternion);
};