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

135 lines
3.8 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
* @constructor
*/
GameLib.D3.Helper = function(
graphics,
id,
name,
object,
helperType
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_HELPER
);
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.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;
this.instance = this.createInstance();
};
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
* @param update
*/
GameLib.D3.Helper.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_EDGES) {
instance = new THREE.EdgesHelper(this.object.instance, 0x00FF00);
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT) {
instance = new THREE.DirectionalLightHelper(this.object.instance);
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT) {
instance = new THREE.PointLightHelper(this.object.instance, 1);
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT) {
instance = new THREE.SpotLightHelper(this.object.instance);
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_WIREFRAME) {
instance = new THREE.WireframeGeometry(this.object.instance, 0x00FF00);
}
if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_SKELETON) {
instance = new THREE.SkeletonHelper(this.object.instance);
}
return instance;
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Helper.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};