fucking cool linking now

beta.r3js.org
Theunis J. Botha 2017-06-28 17:09:06 +02:00
parent 9b89113ed9
commit 3ee447bf4d
34 changed files with 1059 additions and 1090 deletions

View File

@ -42,6 +42,7 @@ GameLib.Event.MESH_INSTANCE_UPDATED = 0x1a;
GameLib.Event.LIGHT_INSTANCE_CREATED = 0x1b;
GameLib.Event.LIGHT_INSTANCE_UPDATED = 0x1c;
GameLib.Event.DELETE_COMPONENT = 0x1d;
GameLib.Event.COMPONENT_DOWNLOAD_COMPLETE = 0x1e;
/**
* Subscribe to some events
@ -76,6 +77,75 @@ GameLib.Event.prototype.subscribe = function(
}
};
GameLib.Event.EmitInstanceEvents = function(component) {
if (
component instanceof GameLib.D3.Mesh
) {
GameLib.Event.Emit(
GameLib.Event.MESH_INSTANCE_CREATED,
{
mesh: component
}
)
}
if (
component instanceof GameLib.D3.Light
) {
GameLib.Event.Emit(
GameLib.Event.LIGHT_INSTANCE_CREATED,
{
light: component
}
)
}
if (
component instanceof GameLib.D3.Scene
) {
GameLib.Event.Emit(
GameLib.Event.SCENE_INSTANCE_CREATED,
{
scene: component
}
);
}
if (
component instanceof GameLib.D3.Material
) {
GameLib.Event.Emit(
GameLib.Event.MATERIAL_INSTANCE_CREATED,
{
material: component
}
);
}
if (
component instanceof GameLib.D3.Texture
) {
GameLib.Event.Emit(
GameLib.Event.TEXTURE_INSTANCE_CREATED,
{
texture: component
}
);
}
if (
component instanceof GameLib.D3.Image
) {
GameLib.Event.Emit(
GameLib.Event.IMAGE_INSTANCE_CREATED,
{
image: component
}
)
}
};
/**
* Stop listening for this event after the callback returns true
* @param eventName

View File

@ -592,4 +592,40 @@ GameLib.Utils.Intersect = function (a, b) {
return c.indexOf(e) === i;
}
);
};
GameLib.Utils.Difference = function (a, b) {
var t;
/**
* Loop over shortest array
*/
if (b.length > a.length) {
t = b;
b = a;
a = t;
}
return a.filter(
/**
* Check if exists
* @param e
* @returns {boolean}
*/
function (e) {
return (b.indexOf(e) === -1);
}
).filter(
/**
* Remove Duplicates
* @param e
* @param i
* @param c
* @returns {boolean}
*/
function (e, i, c) {
return c.indexOf(e) === i;
}
);
};

View File

@ -41,8 +41,13 @@ GameLib.Component = function(
if (this.dependencies.length === 0) {
delete this.dependencies;
this.loaded = true;
this.instance = this.createInstance();
if (this.instance) {
this.loaded = true;
GameLib.Event.EmitInstanceEvents(this);
}
}
};

View File

@ -64,17 +64,11 @@ GameLib.D3.Bone.prototype.constructor = GameLib.D3.Bone;
/**
* Creates an instance bone
* @param update boolean
*/
GameLib.D3.Bone.prototype.createInstance = function(update) {
GameLib.D3.Bone.prototype.createInstance = function() {
var instance = null;
if (update) {
instance = this.instance;
} else {
instance = new THREE.Bone();
}
var instance = new THREE.Bone();
instance.name = this.name;
@ -102,7 +96,24 @@ GameLib.D3.Bone.prototype.createInstance = function(update) {
* Updates the instance
*/
GameLib.D3.Bone.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
this.instance.name = this.name;
this.instance.position.x = this.position.x;
this.instance.position.y = this.position.y;
this.instance.position.z = this.position.z;
this.instance.quaternion.x = this.quaternion.x;
this.instance.quaternion.y = this.quaternion.y;
this.instance.quaternion.z = this.quaternion.z;
this.instance.quaternion.w = this.quaternion.w;
this.instance.scale.x = this.scale.x;
this.instance.scale.y = this.scale.y;
this.instance.scale.z = this.scale.z;
this.instance.up.x = this.up.x;
this.instance.up.y = this.up.y;
this.instance.up.z = this.up.z;
};
/**

View File

@ -95,67 +95,28 @@ GameLib.D3.Camera.CAMERA_TYPE_STEREO = 0x3;
* Creates a camera instance of 'graphics' type (only THREE for now)
* @returns {THREE.Camera}
*/
GameLib.D3.Camera.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
GameLib.D3.Camera.prototype.createInstance = function() {
var instance = null;
if (update) {
instance = this.instance;
}
if (!instance) {
if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ) {
instance = new THREE.PerspectiveCamera(
this.fov,
this.aspect,
this.near,
this.far
);
} else if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
instance = new THREE.OrthographicCamera(
this.minX,
this.maxX,
this.maxY,
this.minY,
this.minZ,
this.maxZ
);
} else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
instance = new THREE.StereoCamera();
}
}
if (update) {
if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
instance.left = this.minX;
instance.right = this.maxX;
instance.bottom = this.minY;
instance.top = this.maxY;
instance.near = this.minZ;
instance.far = this.maxZ;
}
if (
this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ||
this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO
) {
instance.fov = this.fov;
instance.aspect = this.aspect;
instance.near = this.near;
instance.far = this.far;
}
if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
instance.eyeSeparation = this.eyeSeparation;
instance.focalLength = this.focalLength;
instance.update(instance);
}
if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ) {
instance = new THREE.PerspectiveCamera(
this.fov,
this.aspect,
this.near,
this.far
);
} else if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
instance = new THREE.OrthographicCamera(
this.minX,
this.maxX,
this.maxY,
this.minY,
this.minZ,
this.maxZ
);
} else if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
instance = new THREE.StereoCamera();
}
if (!instance) {
@ -183,7 +144,43 @@ GameLib.D3.Camera.prototype.createInstance = function(update) {
* Updates the instance with the current state
*/
GameLib.D3.Camera.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) {
this.instance.left = this.minX;
this.instance.right = this.maxX;
this.instance.bottom = this.minY;
this.instance.top = this.maxY;
this.instance.near = this.minZ;
this.instance.far = this.maxZ;
}
if (
this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ||
this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_STEREO
) {
this.instance.fov = this.fov;
this.instance.aspect = this.aspect;
this.instance.near = this.near;
this.instance.far = this.far;
}
if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_STEREO) {
this.instance.eyeSeparation = this.eyeSeparation;
this.instance.focalLength = this.focalLength;
this.instance.update(this.instance);
}
this.instance.position.x = this.position.x;
this.instance.position.y = this.position.y;
this.instance.position.z = this.position.z;
this.instance.quaternion.x = this.quaternion.x;
this.instance.quaternion.y = this.quaternion.y;
this.instance.quaternion.z = this.quaternion.z;
this.instance.quaternion.w = this.quaternion.w;
this.instance.lookAt(this.lookAt.instance);
this.instance.updateProjectionMatrix();
};
/**

View File

@ -43,11 +43,6 @@ GameLib.D3.CustomCode.prototype.constructor = GameLib.D3.CustomCode;
*/
GameLib.D3.CustomCode.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = function(deltaTime) {
this.args['deltaTime'] = deltaTime;
var f = new Function(this.code).apply(this.parentEntity, this.args);

View File

@ -94,18 +94,8 @@ GameLib.D3.Helper.HELPER_TYPE_SKELETON = 0x6;
/**
* Creates a helper instance
* @param update
*/
GameLib.D3.Helper.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
if (update) {
return;
}
GameLib.D3.Helper.prototype.createInstance = function() {
var instance = null;
@ -140,5 +130,4 @@ GameLib.D3.Helper.prototype.createInstance = function(update) {
* Updates the instance with the current state
*/
GameLib.D3.Helper.prototype.updateInstance = function() {
this.createInstance(true);
};

View File

@ -23,7 +23,6 @@ GameLib.D3.Image = function(
apiImage.path,
apiImage.contentType,
apiImage.size,
apiImage.data,
apiImage.parentEntity
);
@ -42,7 +41,6 @@ GameLib.D3.Image.prototype.constructor = GameLib.D3.Image;
* @returns {*}
*/
GameLib.D3.Image.prototype.createInstance = function() {
console.log('create image instance');
return null;
};
@ -65,7 +63,6 @@ GameLib.D3.Image.prototype.toApiObject = function() {
this.path,
this.contentType,
this.size,
this.data,
GameLib.Utils.IdOrNull(this.parentEntity)
);

View File

@ -67,11 +67,6 @@ GameLib.D3.Input.Drive.prototype.constructor = GameLib.D3.Input.Drive;
GameLib.D3.Input.Drive.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
if (update) {
return this.instance;
}

View File

@ -80,24 +80,11 @@ GameLib.D3.Input.Editor = function (
GameLib.D3.Input.Editor.prototype = Object.create(GameLib.D3.API.Input.Editor.prototype);
GameLib.D3.Input.Editor.prototype.constructor = GameLib.D3.Input.Editor;
GameLib.D3.Input.Editor.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = null;
if (update) {
instance = this.instance;
}
return instance;
GameLib.D3.Input.Editor.prototype.createInstance = function() {
return true;
};
GameLib.D3.Input.Editor.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
/**

View File

@ -100,188 +100,178 @@ GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4;
* Creates a light instance
* @returns {*}
*/
GameLib.D3.Light.prototype.createInstance = function(update) {
GameLib.D3.Light.prototype.createInstance = function() {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = null;
if (update) {
if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT ||
this.lightType === 'AmbientLight'
) {
instance = new THREE.AmbientLight(
this.color.instance,
this.intensity
);
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL ||
this.lightType === 'DirectionalLight'
) {
instance = new THREE.DirectionalLight(
this.color.instance,
this.intensity
);
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT ||
this.lightType === 'PointLight'
) {
instance = new THREE.PointLight(
this.color.instance,
this.intensity
);
instance.distance = this.distance;
instance.decay = this.decay;
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT ||
this.lightType === 'SpotLight'
) {
instance = new THREE.SpotLight(
this.color.instance,
this.intensity
);
instance.distance = this.distance;
instance.angle = this.angle;
instance.penumbra = this.penumbra;
instance.decay = this.decay;
} else {
console.warn('unsupported light type: ' + this.lightType);
return null;
}
var oldInstance = null;
instance.name = this.name;
if (GameLib.Utils.UndefinedOrNull(this.instance)) {
console.warn('cannot update an non-existent light');
return
}
instance.position.x = this.position.x;
instance.position.y = this.position.y;
instance.position.z = this.position.z;
if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT &&
!(this.instance instanceof THREE.AmbientLight)
) {
oldInstance = this.instance;
instance.scale.x = this.scale.x;
instance.scale.y = this.scale.y;
instance.scale.z = this.scale.z;
this.instance = new THREE.AmbientLight(
this.color.instance,
this.intensity
);
if (instance.target) {
instance.target.position.x = this.targetPosition.x;
instance.target.position.y = this.targetPosition.y;
instance.target.position.z = this.targetPosition.z;
}
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL &&
!(this.instance instanceof THREE.DirectionalLight)
) {
oldInstance = this.instance;
instance.quaternion.x = this.quaternion.x;
instance.quaternion.y = this.quaternion.y;
instance.quaternion.z = this.quaternion.z;
instance.quaternion.w = this.quaternion.w;
this.instance = new THREE.DirectionalLight(
this.color.instance,
this.intensity
);
instance.intensity = this.intensity;
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT &&
!(this.instance instanceof THREE.PointLight)
) {
oldInstance = this.instance;
instance.color.set(this.color.toHex());
this.instance = new THREE.PointLight(
this.color.instance,
this.intensity
);
this.instance.distance = this.distance;
this.instance.decay = this.decay;
return instance;
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT &&
!(this.instance instanceof THREE.SpotLight)
) {
oldInstance = this.instance;
this.instance = new THREE.SpotLight(
this.color.instance,
this.intensity
);
this.instance.distance = this.distance;
this.instance.angle = this.angle;
this.instance.penumbra = this.penumbra;
this.instance.decay = this.decay;
} else {
/**
* Light type not supported or light types match
*/
}
if (oldInstance && this.parentScene) {
this.parentScene.instance.remove(oldInstance);
this.parentScene.instance.add(this.instance);
}
this.instance.name = this.name;
this.instance.position.x = this.position.x;
this.instance.position.y = this.position.y;
this.instance.position.z = this.position.z;
this.instance.scale.x = this.scale.x;
this.instance.scale.y = this.scale.y;
this.instance.scale.z = this.scale.z;
if (this.instance.target) {
this.instance.target.position.x = this.targetPosition.x;
this.instance.target.position.y = this.targetPosition.y;
this.instance.target.position.z = this.targetPosition.z;
}
this.instance.quaternion.x = this.quaternion.x;
this.instance.quaternion.y = this.quaternion.y;
this.instance.quaternion.z = this.quaternion.z;
this.instance.quaternion.w = this.quaternion.w;
this.instance.intensity = this.intensity;
this.instance.color.set(this.color.toHex());
} else {
var instance = null;
if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT ||
this.lightType === 'AmbientLight'
) {
instance = new THREE.AmbientLight(
this.color.instance,
this.intensity
);
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL ||
this.lightType === 'DirectionalLight'
) {
instance = new THREE.DirectionalLight(
this.color.instance,
this.intensity
);
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT ||
this.lightType === 'PointLight'
) {
instance = new THREE.PointLight(
this.color.instance,
this.intensity
);
instance.distance = this.distance;
instance.decay = this.decay;
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT ||
this.lightType === 'SpotLight'
) {
instance = new THREE.SpotLight(
this.color.instance,
this.intensity
);
instance.distance = this.distance;
instance.angle = this.angle;
instance.penumbra = this.penumbra;
instance.decay = this.decay;
} else {
console.warn('unsupported light type: ' + this.lightType);
return null;
}
instance.name = this.name;
instance.position.x = this.position.x;
instance.position.y = this.position.y;
instance.position.z = this.position.z;
instance.scale.x = this.scale.x;
instance.scale.y = this.scale.y;
instance.scale.z = this.scale.z;
if (instance.target) {
instance.target.position.x = this.targetPosition.x;
instance.target.position.y = this.targetPosition.y;
instance.target.position.z = this.targetPosition.z;
}
instance.quaternion.x = this.quaternion.x;
instance.quaternion.y = this.quaternion.y;
instance.quaternion.z = this.quaternion.z;
instance.quaternion.w = this.quaternion.w;
instance.intensity = this.intensity;
instance.color.set(this.color.toHex());
return instance;
}
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Light.prototype.updateInstance = function() {
this.createInstance(true);
var oldInstance = null;
if (GameLib.Utils.UndefinedOrNull(this.instance)) {
console.warn('cannot update an non-existent light');
return
}
if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT &&
!(this.instance instanceof THREE.AmbientLight)
) {
oldInstance = this.instance;
this.instance = new THREE.AmbientLight(
this.color.instance,
this.intensity
);
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL &&
!(this.instance instanceof THREE.DirectionalLight)
) {
oldInstance = this.instance;
this.instance = new THREE.DirectionalLight(
this.color.instance,
this.intensity
);
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT &&
!(this.instance instanceof THREE.PointLight)
) {
oldInstance = this.instance;
this.instance = new THREE.PointLight(
this.color.instance,
this.intensity
);
this.instance.distance = this.distance;
this.instance.decay = this.decay;
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT &&
!(this.instance instanceof THREE.SpotLight)
) {
oldInstance = this.instance;
this.instance = new THREE.SpotLight(
this.color.instance,
this.intensity
);
this.instance.distance = this.distance;
this.instance.angle = this.angle;
this.instance.penumbra = this.penumbra;
this.instance.decay = this.decay;
} else {
/**
* Light type not supported or light types match
*/
}
if (oldInstance && this.parentScene) {
this.parentScene.instance.remove(oldInstance);
this.parentScene.instance.add(this.instance);
}
this.instance.name = this.name;
this.instance.position.x = this.position.x;
this.instance.position.y = this.position.y;
this.instance.position.z = this.position.z;
this.instance.scale.x = this.scale.x;
this.instance.scale.y = this.scale.y;
this.instance.scale.z = this.scale.z;
if (this.instance.target) {
this.instance.target.position.x = this.targetPosition.x;
this.instance.target.position.y = this.targetPosition.y;
this.instance.target.position.z = this.targetPosition.z;
}
this.instance.quaternion.x = this.quaternion.x;
this.instance.quaternion.y = this.quaternion.y;
this.instance.quaternion.z = this.quaternion.z;
this.instance.quaternion.w = this.quaternion.w;
this.instance.intensity = this.intensity;
this.instance.color.set(this.color.toHex());
};
/**

View File

@ -75,10 +75,7 @@ GameLib.D3.LookAt.prototype.constructor = GameLib.D3.LookAt;
GameLib.D3.LookAt.prototype.createInstance = function() {
console.log('GameLib.D3.LookAt.prototype.createInstance()');
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
return true;
};
GameLib.D3.LookAt.prototype.toApiObject = function() {

View File

@ -698,12 +698,7 @@ GameLib.D3.Material.prototype.updateMeshBasicMaterialInstance = function() {
* Material instance
* @returns {*}
*/
GameLib.D3.Material.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
GameLib.D3.Material.prototype.createInstance = function() {
var instance = null;

View File

@ -424,11 +424,6 @@ GameLib.D3.Mesh.prototype.createInstanceDefaults = function(instance) {
*/
GameLib.D3.Mesh.prototype.createInstance = function() {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var geometry = new THREE.Geometry();
/**

View File

@ -50,7 +50,7 @@ GameLib.D3.Mesh.Plane.prototype = Object.create(GameLib.D3.Mesh.prototype);
GameLib.D3.Mesh.Plane.prototype.constructor = GameLib.D3.Mesh.Plane;
GameLib.D3.Mesh.Plane.prototype.createInstance = function(update) {
GameLib.D3.Mesh.Plane.prototype.createInstance = function() {
var geometry = new THREE.PlaneGeometry(
this.width,

View File

@ -42,7 +42,7 @@ GameLib.D3.Mesh.Sphere = function (
GameLib.D3.Mesh.Sphere.prototype = Object.create(GameLib.D3.Mesh.prototype);
GameLib.D3.Mesh.Sphere.prototype.constructor = GameLib.D3.Mesh.Sphere;
GameLib.D3.Mesh.Sphere.prototype.createInstance = function(update) {
GameLib.D3.Mesh.Sphere.prototype.createInstance = function() {
var geometry = null;

View File

@ -54,11 +54,6 @@ GameLib.D3.Pass.PASS_TYPE_COPY_SHADER = 0x2;
*/
GameLib.D3.Pass.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = null;
if (update) {
@ -79,7 +74,7 @@ GameLib.D3.Pass.prototype.createInstance = function(update) {
);
}
} else if (this.passType == GameLib.D3.Pass.PASS_TYPE_COPY_SHADER) {
} else if (this.passType === GameLib.D3.Pass.PASS_TYPE_COPY_SHADER) {
if (!THREE.CopyShader) {
console.warn('No THREE.CopyShader');

View File

@ -127,10 +127,6 @@ GameLib.D3.PathFollowing.prototype.createInstance = function() {
console.log('GameLib.D3.PathFollowing.prototype.createInstance()');
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
};

View File

@ -51,38 +51,26 @@ GameLib.D3.Raycaster.prototype.constructor = GameLib.D3.Raycaster;
/**
* Creates or updates a raycaster instance
* @param update
*/
GameLib.D3.Raycaster.prototype.createInstance = function(update) {
GameLib.D3.Raycaster.prototype.createInstance = function() {
var instance = new THREE.Raycaster();
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
instance.set(
this.position.instance,
this.direction.instance
);
if (update) {
this.instance.set(
this.position.instance,
this.direction.instance
);
return null;
} else {
var instance = new THREE.Raycaster();
instance.set(
this.position.instance,
this.direction.instance
);
return instance;
}
return instance;
};
GameLib.D3.Raycaster.prototype.updateInstance = function() {
this.createInstance(true);
this.instance.set(
this.position.instance,
this.direction.instance
);
return null;
};
GameLib.D3.Raycaster.prototype.toApiObject = function() {

View File

@ -55,43 +55,23 @@ GameLib.D3.RenderTarget.RGBA_FORMAT = 1023;
/**
* Creates a Render Target instance
* @param update
* @returns {*}
*/
GameLib.D3.RenderTarget.prototype.createInstance = function(update) {
GameLib.D3.RenderTarget.prototype.createInstance = function() {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = null;
if (update) {
instance = this.instance;
instance.width = this.width;
instance.height = this.height;
instance.minFilter = this.minFilter;
instance.magFilter = this.magFilter;
instance.format = this.format;
instance.stencilBuffer = this.stencilBuffer;
instance.texture = this.texture.instance;
instance.texture.needsUpdate = true;
} else {
instance = new THREE.WebGLRenderTarget(
this.width,
this.height,
{
minFilter : this.minFilter,
magFilter : this.magFilter,
format : this.format,
stencilBuffer : this.stencilBuffer
}
);
if (this.texture instanceof GameLib.D3.Texture && this.texture.instance) {
instance.texture = this.texture.instance;
var instance = new THREE.WebGLRenderTarget(
this.width,
this.height,
{
minFilter : this.minFilter,
magFilter : this.magFilter,
format : this.format,
stencilBuffer : this.stencilBuffer
}
);
if (this.texture instanceof GameLib.D3.Texture && this.texture.instance) {
instance.texture = this.texture.instance;
}
return instance;
@ -101,7 +81,14 @@ GameLib.D3.RenderTarget.prototype.createInstance = function(update) {
* updates instance
*/
GameLib.D3.RenderTarget.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
this.instance.width = this.width;
this.instance.height = this.height;
this.instance.minFilter = this.minFilter;
this.instance.magFilter = this.magFilter;
this.instance.format = this.format;
this.instance.stencilBuffer = this.stencilBuffer;
this.instance.texture = this.texture.instance;
this.instance.texture.needsUpdate = true;
};
/**

View File

@ -61,75 +61,61 @@ GameLib.D3.Renderer.prototype.constructor = GameLib.D3.Renderer;
/**
* Create Renderer Instance
* @param update
* @returns {*}
*/
GameLib.D3.Renderer.prototype.createInstance = function(update) {
GameLib.D3.Renderer.prototype.createInstance = function() {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = new THREE.WebGLRenderer({
canvas : this.domElement.instance
});
if (update) {
instance.localClippingEnabled = this.localClipping;
this.instance.localClippingEnabled = this.localClipping;
instance.setSize(
this.width,
this.height
);
this.instance.setSize(
this.width,
this.height
);
instance.setClearColor(
new THREE.Color(
this.clearColor.r,
this.clearColor.g,
this.clearColor.b
),
1 - this.clearColor.a
);
this.instance.setClearColor(
new THREE.Color(
this.clearColor.r,
this.clearColor.g,
this.clearColor.b
),
1 - this.clearColor.a
);
this.instance.domElement.width = this.width;
this.instance.domElement.height = this.height;
this.instance.autoClear = this.autoClear;
this.instance.preserveDrawingBuffer = this.preserveDrawingBuffer;
} else {
var instance = new THREE.WebGLRenderer({
canvas : this.domElement.instance
});
instance.localClippingEnabled = this.localClipping;
instance.setSize(
this.width,
this.height
);
instance.setClearColor(
new THREE.Color(
this.clearColor.r,
this.clearColor.g,
this.clearColor.b
),
1 - this.clearColor.a
);
instance.domElement.width = this.width;
instance.domElement.height = this.height;
instance.autoClear = this.autoClear;
instance.preserveDrawingBuffer = this.preserveDrawingBuffer;
return instance;
}
instance.domElement.width = this.width;
instance.domElement.height = this.height;
instance.autoClear = this.autoClear;
instance.preserveDrawingBuffer = this.preserveDrawingBuffer;
return instance;
};
GameLib.D3.Renderer.prototype.updateInstance = function() {
this.createInstance(true);
this.instance.localClippingEnabled = this.localClipping;
this.instance.setSize(
this.width,
this.height
);
this.instance.setClearColor(
new THREE.Color(
this.clearColor.r,
this.clearColor.g,
this.clearColor.b
),
1 - this.clearColor.a
);
this.instance.domElement.width = this.width;
this.instance.domElement.height = this.height;
this.instance.autoClear = this.autoClear;
this.instance.preserveDrawingBuffer = this.preserveDrawingBuffer;
};
GameLib.D3.Renderer.prototype.toApiObject = function() {

View File

@ -153,9 +153,6 @@ GameLib.D3.Scene = function (
);
}
this.sceneInstanceSubscriptions = [];
this.objectInstanceSubscriptions = [];
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_SCENE,
@ -179,11 +176,6 @@ GameLib.D3.Scene.prototype.constructor = GameLib.D3.Scene;
*/
GameLib.D3.Scene.prototype.createInstance = function() {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = new THREE.Scene();
instance.name = this.name;

View File

@ -102,11 +102,6 @@ GameLib.D3.Skeleton.prototype.constructor = GameLib.D3.Skeleton;
*/
GameLib.D3.Skeleton.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = null;
if (update) {

View File

@ -50,16 +50,7 @@ GameLib.D3.Spline.prototype.constructor = GameLib.D3.Spline;
* Creates an instance spline
* @param update boolean
*/
GameLib.D3.Spline.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
if (update) {
return this.instance;
}
GameLib.D3.Spline.prototype.createInstance = function() {
var vertices = this.vertices.map(
function (vertex) {
@ -74,7 +65,14 @@ GameLib.D3.Spline.prototype.createInstance = function(update) {
* Updates the instance
*/
GameLib.D3.Spline.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
var vertices = this.vertices.map(
function (vertex) {
return vertex.instance;
}
);
this.instance = new THREE.CatmullRomCurve3(vertices);
};
/**

View File

@ -58,30 +58,14 @@ GameLib.D3.Stats.prototype.resize = function() {
/**
* Creates a helper instance
* @param update
*/
GameLib.D3.Stats.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = null;
if (update) {
this.instance = new this.stats();
instance = this.instance;
} else {
instance = new this.stats();
}
return instance;
GameLib.D3.Stats.prototype.createInstance = function() {
return new this.stats();
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Stats.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
this.instance = new this.stats();
};

View File

@ -155,67 +155,62 @@ GameLib.D3.Texture.TEXTURE_TYPE_CUBE = 0x2;
*/
GameLib.D3.Texture.prototype.createInstance = function() {
if (!(this.image instanceof GameLib.D3.Image)) {
console.warn('The image associated with this texture has not been setup properly - objects linked?');
var instance = null;
if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_CUBE) {
if (this.image.instance) {
instance = new THREE.CubeTexture(
[
this.image.instance,
this.image.instance,
this.image.instance,
this.image.instance,
this.image.instance,
this.image.instance
]
);
instance.needsUpdate = true;
} else {
return null;
}
var instance = null;
if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_CUBE) {
if (this.image.instance) {
instance = new THREE.CubeTexture(
[
this.image.instance,
this.image.instance,
this.image.instance,
this.image.instance,
this.image.instance,
this.image.instance
]
);
instance.needsUpdate = true;
} else {
return null;
}
if (this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING &&
this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFRACTION_MAPPING
) {
this.mapping = GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING;
}
} else {
if (this.image.instance) {
instance = new THREE.Texture(
this.image.instance
);
instance.needsUpdate = true;
} else {
return null;
}
if (this.mapping !== GameLib.D3.Texture.TYPE_UV_MAPPING) {
this.mapping = GameLib.D3.Texture.TYPE_UV_MAPPING;
}
if (this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING &&
this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFRACTION_MAPPING
) {
this.mapping = GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING;
}
instance.name = this.name;
instance.flipY = this.flipY;
instance.encoding = this.encoding;
instance.offset.x = this.offset.x;
instance.offset.y = this.offset.y;
instance.repeat.x = this.repeat.x;
instance.repeat.y = this.repeat.y;
instance.mapping = this.mapping;
instance.format = this.format;
instance.wrapS = this.wrapS;
instance.wrapT = this.wrapT;
} else {
return instance;
if (this.image.instance) {
instance = new THREE.Texture(
this.image.instance
);
instance.needsUpdate = true;
} else {
return null;
}
if (this.mapping !== GameLib.D3.Texture.TYPE_UV_MAPPING) {
this.mapping = GameLib.D3.Texture.TYPE_UV_MAPPING;
}
}
instance.name = this.name;
instance.flipY = this.flipY;
instance.encoding = this.encoding;
instance.offset.x = this.offset.x;
instance.offset.y = this.offset.y;
instance.repeat.x = this.repeat.x;
instance.repeat.y = this.repeat.y;
instance.mapping = this.mapping;
instance.format = this.format;
instance.wrapS = this.wrapS;
instance.wrapT = this.wrapT;
return instance;
};
/**

View File

@ -51,27 +51,15 @@ GameLib.D3.Viewport.prototype.constructor = GameLib.D3.Viewport;
* @param update
* @returns {*}
*/
GameLib.D3.Viewport.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = null;
if (update) {
instance = this.instance;
}
return instance;
GameLib.D3.Viewport.prototype.createInstance = function() {
return true;
};
/**
*
*/
GameLib.D3.Viewport.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
return true;
};
/**

View File

@ -28,30 +28,17 @@ GameLib.DomElement.prototype.constructor = GameLib.DomElement;
/**
* Creates an instance domElement
* @param update
* @returns {*}
*/
GameLib.DomElement.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
if (update) {
return null;
} else {
var instance = document.getElementById(this.domElementId);
return instance;
}
GameLib.DomElement.prototype.createInstance = function() {
return document.getElementById(this.domElementId);
};
/**
* Updates the instance vector, calls updateInstance on the parent object
*/
GameLib.DomElement.prototype.updateInstance = function() {
this.createInstance(true);
this.instance = document.getElementById(this.domElementId);
};
/**

View File

@ -45,7 +45,7 @@ GameLib.Entity.prototype.createInstance = function() {
/**
* FUCK ecsjs and tiny-ecs - no client-side support and shitty code (only takes constructors as args)
*/
return null;
return true;
};
/**

View File

@ -101,26 +101,16 @@ GameLib.GUI.prototype.resize = function() {
/**
* Creates a helper instance
* @param update
*/
GameLib.GUI.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
} else {
instance = new dat.GUI( { autoPlace: false } );
}
return instance;
GameLib.GUI.prototype.createInstance = function() {
return new dat.GUI( { autoPlace: false } );
};
/**
* Updates the instance with the current state
*/
GameLib.GUI.prototype.updateInstance = function() {
this.createInstance(true);
this.instance = new dat.GUI( { autoPlace: false } );
};
GameLib.GUI.prototype.addObject = function(object) {

View File

@ -41,26 +41,13 @@ GameLib.Mouse.prototype.constructor = GameLib.Mouse;
* @returns {*}
*/
GameLib.Mouse.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
var instance = null;
if (update) {
instance = this.instance;
}
return instance;
return true;
};
/**
* Updates the instance vector, calls updateInstance on the parent object
*/
GameLib.Mouse.prototype.updateInstance = function() {
this.createInstance(true);
};
/**

View File

@ -45,7 +45,7 @@ GameLib.System.SYSTEM_TYPE_LINKING = 0x40;
GameLib.System.SYSTEM_TYPE_ALL = 0xFFFF;
GameLib.System.prototype.createInstance = function() {
console.log('GameLib.System.prototype.createInstance();');
//console.log('GameLib.System.prototype.createInstance();');
};
/**

View File

@ -48,7 +48,7 @@ GameLib.System.Linking.prototype.start = function() {
this.componentCreatedSubscription = this.subscribe(
GameLib.Event.COMPONENT_CREATED,
this.componentCreated().bind(this)
this.componentCreated.bind(this)
);
this.parentSceneChangeSubscription = this.subscribe(
@ -93,64 +93,6 @@ GameLib.System.Linking.prototype.start = function() {
};
GameLib.EntityManager.prototype.emitInstanceEvents = function (component) {
if (
component instanceof GameLib.D3.Mesh
) {
GameLib.Event.Emit(
GameLib.Event.MESH_INSTANCE_CREATED,
{
mesh: component
}
)
}
if (
component instanceof GameLib.D3.Light
) {
GameLib.Event.Emit(
GameLib.Event.LIGHT_INSTANCE_CREATED,
{
light: component
}
)
}
if (
component instanceof GameLib.D3.Scene
) {
GameLib.Event.Emit(
GameLib.Event.SCENE_INSTANCE_CREATED,
{
scene: component
}
);
}
if (
component instanceof GameLib.D3.Material
) {
GameLib.Event.Emit(
GameLib.Event.MATERIAL_INSTANCE_CREATED,
{
material: component
}
);
}
if (
component instanceof GameLib.D3.Texture
) {
GameLib.Event.Emit(
GameLib.Event.TEXTURE_INSTANCE_CREATED,
{
texture: component
}
);
}
};
GameLib.System.Linking.prototype.link = function(component, data) {
for (var property in component.linkedObjects) {
if (component.linkedObjects.hasOwnProperty(property)) {
@ -173,13 +115,68 @@ GameLib.System.Linking.prototype.link = function(component, data) {
}
};
GameLib.System.Linking.prototype.componentCreated = function(data) {
GameLib.System.Linking.prototype.resolveDependencies = function(component) {
if (!component.loaded) {
return;
}
var parentComponents = this.dependencies[component.id];
/**
* Shorthand
* Now find all the components which depend on this component
*/
var component = data.component;
if (GameLib.Utils.UndefinedOrNull(parentComponents)) {
/**
* We don't know about components which depend on this component - but it could still load.
* However, it is stored in the register and dependency list for later use
*/
} else {
parentComponents.map(
function(parentComponent) {
/**
* Link the parent component to this component
*/
this.link(parentComponent, {component: component});
/**
* Remove the actual dependency
*/
var index = parentComponent.dependencies.indexOf(component.id);
if (index === -1) {
console.warn('dependency mismatch');
} else {
parentComponent.dependencies.splice(index, 1);
}
/**
* If we now managed to link the objects, and this object has no more dependencies
*/
if (parentComponent.dependencies.length === 0) {
delete parentComponent.dependencies;
parentComponent.instance = parentComponent.createInstance();
if (parentComponent.instance) {
parentComponent.loaded = true;
GameLib.Event.EmitInstanceEvents(parentComponent);
}
}
}.bind(this)
);
delete this.dependencies[component.id];
}
};
GameLib.System.Linking.prototype.registerDependencies = function(component) {
/**
* Register this component immediately
*/
@ -189,7 +186,8 @@ GameLib.System.Linking.prototype.componentCreated = function(data) {
* We only care about components with unloaded dependencies -
* other components will have already had their instance objects created
*/
if (component.dependencies.length > 0) {
if (component.dependencies &&
component.dependencies.length > 0) {
component.dependencies = component.dependencies.reduce(
function(result, id) {
@ -242,62 +240,53 @@ GameLib.System.Linking.prototype.componentCreated = function(data) {
);
if (component.dependencies.length === 0) {
component.loaded = true;
component.instance = component.createInstance();
this.emitInstanceEvents(component);
delete component.dependencies;
component.instance = component.createInstance();
if (component.instance) {
component.loaded = true;
GameLib.Event.EmitInstanceEvents(component);
}
}
}
};
var parentComponents = this.dependencies[component.id];
GameLib.System.Linking.prototype.componentCreated = function(data) {
/**
* Now find all the components which depend on this component
* Shorthand
*/
if (GameLib.Utils.UndefinedOrNull(parentComponents)) {
var component = data.component;
/**
* We don't know about components which depend on this component - but it could still load.
* However, it is stored in the register for later use
*/
/**
* Register any dependencies of this component
*/
this.registerDependencies(component);
} else {
parentComponents.map(
function(parentComponent) {
this.link(parentComponent, {component: component});
/**
* Remove the actual dependency
*/
var index = parentComponent.dependencies.indexOf(component.id);
if (index === -1) {
console.warn('dependency mismatch');
} else {
parentComponent.dependencies.splice(index, 1);
}
/**
* If we now managed to link the objects, and this object has no more dependencies
*/
if (parentComponent.dependencies.length === 0) {
parentComponent.loaded = true;
parentComponent.instance = parentComponent.createInstance();
this.emitInstanceEvents(parentComponent);
delete parentComponent.dependencies;
}
}.bind(this)
);
delete this.dependencies[component.id];
}
/**
* Resolve any dependencies to this component
*/
this.resolveDependencies(component);
};
GameLib.System.Linking.prototype.queryRegister = function(constructor) {
return this.register.reduce(
function(result, component) {
if (component instanceof constructor) {
result.push(component);
}
return result;
},
[]
);
};
GameLib.System.Linking.prototype.meshInstanceCreated = function(data) {
var scenes = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Scene]);
this.resolveDependencies(data.mesh);
var scenes = this.queryRegister(GameLib.D3.Scene);
scenes.map(function(scene){
if (data.mesh.parentScene === scene) {
@ -309,7 +298,9 @@ GameLib.System.Linking.prototype.meshInstanceCreated = function(data) {
GameLib.System.Linking.prototype.lightInstanceCreated = function(data) {
var scenes = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Scene]);
this.resolveDependencies(data.light);
var scenes = this.queryRegister(GameLib.D3.Scene);
scenes.map(function(scene){
if (data.light.parentScene === scene) {
@ -321,41 +312,49 @@ GameLib.System.Linking.prototype.lightInstanceCreated = function(data) {
GameLib.System.Linking.prototype.sceneInstanceCreated = function(data) {
var scene = data.scene;
this.resolveDependencies(data.scene);
scene.images.map(
function(image){
GameLib.Event.Emit(
GameLib.Event.LOAD_IMAGE,
{
onLoaded : function(image) {
if (this.onImageLoaded) {
this.onImageLoaded(image);
}
},
onProgress : function(image, progress) {
if (this.onImageProgress) {
this.onImageProgress(image, progress);
}
},
onError : function(image, error) {
if (this.onImageError) {
this.onImageError(image, error);
}
},
image : image
}
);
}.bind(this)
);
// var scene = data.scene;
//
// scene.images.map(
// function(image){
// GameLib.Event.Emit(
// GameLib.Event.LOAD_IMAGE,
// {
// onLoaded : function(image) {
// if (this.onImageLoaded) {
// this.onImageLoaded(image);
// }
// },
// onProgress : function(image, progress) {
// if (this.onImageProgress) {
// this.onImageProgress(image, progress);
// }
// },
// onError : function(image, error) {
// if (this.onImageError) {
// this.onImageError(image, error);
// }
// },
// image : image
// }
// );
// }.bind(this)
// );
/**
* Add all meshes and lights
*/
var object = GameLib.EntityManager.Instance.queryComponents([
GameLib.D3.Mesh,
GameLib.D3.Light
]);
var object = this.queryRegister(GameLib.D3.Mesh);
object.map(function(object){
if (
object.parentScene === scene
) {
scene.addObject(object);
}
});
object = this.queryRegister(GameLib.D3.Light);
object.map(function(object){
if (
object.parentScene === scene
@ -368,217 +367,230 @@ GameLib.System.Linking.prototype.sceneInstanceCreated = function(data) {
GameLib.System.Linking.prototype.imageInstanceCreated = function(data) {
var textures = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Texture]);
this.resolveDependencies(data.image);
textures.map(
function(texture) {
/**
* Only work with images that belong to this texture
*/
if (
texture.image === data.image
) {
/**
* Update instance, if already an instance
*/
if (texture.instance) {
texture.updateInstance();
GameLib.Event.Emit(
GameLib.Event.TEXTURE_INSTANCE_UPDATED,
{
texture : texture
}
)
} else {
/**
* Create a new instance
*/
texture.instance = texture.createInstance();
GameLib.Event.Emit(
GameLib.Event.TEXTURE_INSTANCE_CREATED,
{
texture : texture
}
)
}
}
}
);
// var textures = this.queryRegister(GameLib.D3.Texture);
//
// textures.map(
//
// function(texture) {
// /**
// * Only work with images that belong to this texture
// */
// if (
// texture.image === data.image
// ) {
// /**
// * Update instance, if already an instance
// */
// if (texture.instance) {
// texture.updateInstance();
// GameLib.Event.Emit(
// GameLib.Event.TEXTURE_INSTANCE_UPDATED,
// {
// texture : texture
// }
// )
// } else {
// /**
// * Create a new instance
// */
// texture.instance = texture.createInstance();
// GameLib.Event.Emit(
// GameLib.Event.TEXTURE_INSTANCE_CREATED,
// {
// texture : texture
// }
// )
// }
// }
// }
// );
};
GameLib.System.Linking.prototype.textureInstanceCreated = function(data) {
var materials = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Material]);
this.resolveDependencies(data.texture);
materials.map(
// var materials = this.queryRegister(GameLib.D3.Material);
//
// materials.map(
//
// function(material) {
//
// if (!material.loaded) {
// return;
// }
//
// if (!data.texture.loaded || !data.texture.instance) {
// console.warn('texture indicated it is loaded however it is not');
// return;
// }
//
// console.log('texture instance created');
function(material) {
// var modified = false;
if (!material.instance) {
console.log('No material instance for ' + material.name);
return;
}
var modified = false;
/**
* We also need to check if the image of the texture is assigned -
* if not we should disable the map
*/
if (material.alphaMap === data.texture) {
if (data.texture.image) {
material.instance.alphaMap = data.texture.instance;
} else {
material.instance.alphaMap = null;
}
modified = true;
}
if (material.aoMap === data.texture) {
if (data.texture.image) {
material.instance.aoMap = data.texture.instance;
} else {
material.instance.aoMap = null;
}
modified = true;
}
if (material.bumpMap === data.texture) {
if (data.texture.image) {
material.instance.bumpMap = data.texture.instance;
} else {
material.instance.bumpMap = null;
}
modified = true;
}
if (material.diffuseMap === data.texture) {
if (data.texture.image) {
material.instance.map = data.texture.instance;
} else {
material.instance.map = null;
}
modified = true;
}
if (material.displacementMap === data.texture) {
if (data.texture.image) {
material.instance.displacementMap = data.texture.instance;
} else {
material.instance.displacementMap = null;
}
modified = true;
}
if (material.emissiveMap === data.texture) {
if (data.texture.image) {
material.instance.emissiveMap = data.texture.instance;
} else {
material.instance.emissiveMap = null;
}
modified = true;
}
if (material.environmentMap === data.texture) {
if (data.texture.image) {
material.instance.envMap = data.texture.instance;
} else {
material.instance.envMap = null;
}
modified = true;
}
if (material.lightMap === data.texture) {
if (data.texture.image) {
material.instance.lightMap = data.texture.instance;
} else {
material.instance.lightMap = null;
}
modified = true;
}
if (material.metalnessMap === data.texture) {
if (data.texture.image) {
material.instance.metalnessMap = data.texture.instance;
} else {
material.instance.metalnessMap = null;
}
modified = true;
}
if (material.normalMap === data.texture) {
if (data.texture.image) {
material.instance.normalMap = data.texture.instance;
} else {
material.instance.normalMap = null;
}
modified = true;
}
if (material.roughnessMap === data.texture) {
if (data.texture.image) {
material.instance.roughnessMap = data.texture.instance;
} else {
material.instance.roughnessMap = null;
}
modified = true;
}
if (material.specularMap === data.texture) {
if (data.texture.image) {
material.instance.specularMap = data.texture.instance;
} else {
material.instance.specularMap = null;
}
modified = true;
}
if (modified) {
material.updateInstance();
GameLib.Event.Emit(
GameLib.Event.MATERIAL_INSTANCE_UPDATED,
{
material : material
}
)
}
}
);
// /**
// * We also need to check if the image of the texture is assigned -
// * if not we should disable the map
// */
// if (material.alphaMap === data.texture) {
// if (material.instance.alphaMap !== data.texture.instance) {
// material.instance.alphaMap = data.texture.instance;
// }
// modified = true;
// } else {
// if (material.instance.alphaMap)
// material.instance.alphaMap = null;
// }
//
// if (material.aoMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.aoMap = data.texture.instance;
// } else {
// material.instance.aoMap = null;
// }
// modified = true;
// }
// if (material.bumpMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.bumpMap = data.texture.instance;
// } else {
// material.instance.bumpMap = null;
// }
// modified = true;
// }
// if (material.diffuseMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.map = data.texture.instance;
// } else {
// material.instance.map = null;
// }
// modified = true;
// }
// if (material.displacementMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.displacementMap = data.texture.instance;
// } else {
// material.instance.displacementMap = null;
// }
// modified = true;
// }
// if (material.emissiveMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.emissiveMap = data.texture.instance;
// } else {
// material.instance.emissiveMap = null;
// }
// modified = true;
// }
// if (material.environmentMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.envMap = data.texture.instance;
// } else {
// material.instance.envMap = null;
// }
// modified = true;
// }
// if (material.lightMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.lightMap = data.texture.instance;
// } else {
// material.instance.lightMap = null;
// }
// modified = true;
// }
// if (material.metalnessMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.metalnessMap = data.texture.instance;
// } else {
// material.instance.metalnessMap = null;
// }
// modified = true;
// }
// if (material.normalMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.normalMap = data.texture.instance;
// } else {
// material.instance.normalMap = null;
// }
// modified = true;
// }
// if (material.roughnessMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.roughnessMap = data.texture.instance;
// } else {
// material.instance.roughnessMap = null;
// }
// modified = true;
// }
// if (material.specularMap === data.texture) {
//
// if (data.texture.image) {
// material.instance.specularMap = data.texture.instance;
// } else {
// material.instance.specularMap = null;
// }
// modified = true;
// }
//
// if (modified) {
// material.updateInstance();
// GameLib.Event.Emit(
// GameLib.Event.MATERIAL_INSTANCE_UPDATED,
// {
// material : material
// }
// )
// }
// }
// );
};
GameLib.System.Linking.prototype.materialInstanceCreated = function(data) {
var meshes = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Mesh]);
this.resolveDependencies(data.material);
meshes.map(function(mesh){
if (!mesh.instance) {
return;
}
/**
* Only work with materials assigned to us
*/
if (mesh.materials[0] !== data.material) {
return;
}
if (mesh.instance.material === data.material.instance) {
//mesh.instance.geometry.uvsNeedUpdate = true;
return;
}
if (mesh.materials[0] === data.material) {
if (mesh.instance.material !== data.material.instance) {
mesh.instance.material = data.material.instance;
}
//mesh.instance.geometry.uvsNeedUpdate = true;
}
});
// var meshes = this.queryRegister(GameLib.D3.Mesh);
//
// meshes.map(function(mesh){
//
// if (!mesh.instance) {
// return;
// }
//
// /**
// * Only work with materials assigned to us
// */
// if (mesh.materials[0] !== data.material) {
// return;
// }
//
// if (mesh.instance.material === data.material.instance) {
// //mesh.instance.geometry.uvsNeedUpdate = true;
// return;
// }
//
// if (mesh.materials[0] === data.material) {
//
// if (mesh.instance.material !== data.material.instance) {
// mesh.instance.material = data.material.instance;
// }
//
// //mesh.instance.geometry.uvsNeedUpdate = true;
// }
// });
};

View File

@ -1,5 +1,6 @@
/**
* Storage System takes care loading and linking components and dependencies
* @param graphics
* @param apiSystem GameLib.API.System
* @param apiUrl
* @param token
@ -13,6 +14,7 @@
* @constructor
*/
GameLib.System.Storage = function(
graphics,
apiSystem,
apiUrl,
token,
@ -24,6 +26,9 @@ GameLib.System.Storage = function(
onComponentProgress,
onComponentError
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
GameLib.System.call(
this,
apiSystem
@ -168,206 +173,213 @@ GameLib.System.Storage.prototype.save = function(data) {
};
/**
* 'Loads' data from a url
*/
GameLib.System.Storage.prototype.load = function(data) {
if (typeof XMLHttpRequest === 'undefined') {
console.log('Implement server side load here');
return;
}
/**
* Load all the ids into our 'loading' list
*/
var loading = data.ids.reduce(
function(result, id) {
if (result.indexOf(id) === -1) {
result.push(id);
}
return result;
},
false
);
GameLib.System.Storage.prototype.loadComponent = function(toProcess, includeDependencies) {
var loaded = [];
var includeDependencies = data.includeDependencies;
var loading = [];
var onComponentLoaded = this.onComponentLoaded;
toProcess.map(function(id) {
if (loading.indexOf(id) === -1) {
loading.push(id);
}
});
var onComponentProgress = this.onComponentProgress;
return function download(id) {
var onComponentError = this.onComponentError;
var onComponentLoaded = this.onComponentLoaded;
while (loading.length > 0) {
var onComponentProgress = this.onComponentProgress;
var id = loading.pop();
var onComponentError = this.onComponentError;
var xhr = new XMLHttpRequest();
xhr.onload = function () {
xhr.onload = function(__system) {
try {
var object = JSON.parse(this.responseText);
} catch (error) {
return function () {
if (onComponentError) {
onComponentError(error);
try {
var object = JSON.parse(this.responseText);
} catch (error) {
if (onComponentError) {
onComponentError(error);
}
GameLib.Event.Emit(
GameLib.Event.LOAD_COMPONENT_ERROR,
{
error: error
}
);
return;
}
GameLib.Event.Emit(
GameLib.Event.LOAD_COMPONENT_ERROR,
{
error: error
if (object.result !== 'success') {
if (onComponentError) {
onComponentError(error);
}
);
return;
}
GameLib.Event.Emit(
GameLib.Event.LOAD_COMPONENT_ERROR,
{
error: error
}
);
if (object.result !== 'success') {
if (onComponentError) {
onComponentError(error);
return;
}
GameLib.Event.Emit(
GameLib.Event.LOAD_COMPONENT_ERROR,
{
error: error
}
);
return;
}
/**
* Now we need to create the runtime component - this happens systematically.
* First, we create an API object from the Object, then a Runtime object from the API object
* Each component has a function 'FromObject' which essentially does this for you
*/
var componentName = GameLib.Component.GetComponentName(object.componentType);
var componentClass = eval(componentName);
var fn = componentClass['FromObject'];
var runtimeComponent = null;
if (object.componentType === GameLib.Component.COMPONENT_ENTITY) {
runtimeComponent = fn(object, GameLib.EntityManager.Instance);
} else {
runtimeComponent = fn(this.graphics, object);
}
loaded.push(runtimeComponent.id);
if (includeDependencies) {
/**
* Before we announce the creation of this component, we should get
* a list of all dependencies of this object, because once we announce
* the creation of this object - the linking system will attempt to resolve
* all dependencies
* Now we need to create the runtime component - this happens systematically.
* First, we create an API object from the Object, then a Runtime object from the API object
* Each component has a function 'FromObject' which essentially does this for you
*/
var dependencies = runtimeComponent.dependencies.map(function (dependency) {
return dependency;
object.component.map(function (component) {
var componentName = GameLib.Component.GetComponentName(component.componentType);
var componentClass = eval(componentName);
var fn = componentClass['FromObject'];
var runtimeComponent = null;
if (component.componentType === GameLib.Component.COMPONENT_ENTITY) {
runtimeComponent = fn(component, GameLib.EntityManager.Instance);
} else {
runtimeComponent = fn(__system.graphics, component);
}
if (runtimeComponent instanceof GameLib.D3.Image) {
GameLib.Event.Emit(
GameLib.Event.LOAD_IMAGE,
{
image : runtimeComponent
}
)
}
loaded.push(runtimeComponent.id);
if (includeDependencies) {
/**
* Before we announce the creation of this component, we should get
* a list of all dependencies of this component, because once we announce
* the creation of this component - the linking system will attempt to resolve
* all dependencies
*/
var dependencies = runtimeComponent.getDependencies();
/**
* Now - we should systematically check if we have the dependency already
* loaded (in our runtime environment) - if we have - we just ignore loading this dependency (for now)
* TODO: decide what to do with runtime versions of 'stale' dependencies
*/
var components = GameLib.EntityManager.Instance.queryComponents(componentClass);
dependencies = dependencies.reduce(
function (result, dependency) {
var found = components.reduce(
function (result, component) {
if (component.id === dependency) {
found = true;
}
return result;
},
false
);
if (!found) {
result.push(dependency);
}
return result;
},
[]
);
/**
* Also check if we did not already load this component quite recently
*/
dependencies = dependencies.reduce(
function (result, dependency) {
var found = loaded.reduce(
function (result, id) {
if (id === dependency) {
result = true;
}
return result;
},
false
);
if (!found) {
result.push(dependency);
}
return result;
},
[]
);
/**
* We should now check our 'loading' list and add all dependencies which are not already in there
*/
dependencies.map(
function (dependency) {
if (loading.indexOf(dependency) === -1) {
loading.push(dependency);
}
}
)
}
/**
* Ok - now we have a super good idea of which components still need to load -
* they live in the 'loading' list.
*
* At this point - the runtime components are created, but they are not ready
* to be used. They may have dependencies to other components, which still need
* to load, or may never be loaded.
*
* It is however safe, to announce, that we created the
* runtime version of it, however it could still have some dependencies.
*
* The Linking system will then kick in and try to resolve all dependencies
*/
if (onComponentLoaded) {
onComponentLoaded(runtimeComponent);
}
GameLib.Event.Emit(
GameLib.Event.COMPONENT_CREATED,
{
component: runtimeComponent
}
);
var toProcess = GameLib.Utils.Difference(loaded, loading);
if (toProcess.length === 0) {
GameLib.Event.Emit(
GameLib.Event.COMPONENT_DOWNLOAD_COMPLETE,
{
loaded: loaded
}
)
} else {
download.bind(__system)(toProcess.pop());
}
});
/**
* Now - we should systematically check if we have the dependency already
* loaded (in our runtime environment) - if we have - we just ignore loading this dependency (for now)
* TODO: decide what to do with runtime versions of 'stale' dependencies
*/
var components = GameLib.EntityManager.Instance.queryComponents(componentClass);
dependencies = dependencies.reduce(
function (result, dependency) {
var found = components.reduce(
function (result, component) {
if (component.id === dependency) {
found = true;
}
return result;
},
false
);
if (!found) {
result.push(dependency);
}
return result;
},
[]
);
/**
* Also check if we did not already load this component quite recently
*/
dependencies = dependencies.reduce(
function (result, dependency) {
var found = loaded.reduce(
function (result, id) {
if (id === dependency) {
result = true;
}
return result;
},
false
);
if (!found) {
result.push(dependency);
}
return result;
},
[]
);
/**
* We should now check our 'loading' list and add all dependencies which are not already in there
*/
dependencies.map(
function (dependency) {
if (loading.indexOf(dependency) === -1) {
loading.push(dependency);
}
}
)
}
/**
* Ok - now we have a super good idea of which components still need to load -
* they live in the 'loading' list.
*
* At this point - the runtime components are created, but they are not ready
* to be used. They may have dependencies to other objects, which still need
* to load, or may never be loaded.
*
* It is however safe, to announce, that we created the
* runtime version of it, however it could still have some dependencies.
*
* The Linking system will then kick in and try to resolve all dependencies
*/
if (onComponentLoaded) {
onComponentLoaded(runtimeComponent);
}
GameLib.Event.Emit(
GameLib.Event.COMPONENT_CREATED,
{
component: runtimeComponent
}
)
};
};
}(this);
xhr.onprogress = function(__id) {
return function (progressEvent) {
@ -400,8 +412,25 @@ GameLib.System.Storage.prototype.load = function(data) {
);
xhr.send();
}.bind(this);
};
/**
* 'Loads' data from a url
*/
GameLib.System.Storage.prototype.load = function(data) {
if (typeof XMLHttpRequest === 'undefined') {
console.log('Implement server side load here');
return;
}
if (data.ids && data.ids.length > 0) {
this.loadComponent(data.ids, data.includeDependencies)(data.ids[0]);
} else {
console.log('No components selected');
}
};
@ -466,6 +495,7 @@ GameLib.System.Storage.prototype.loadImage = function(data) {
window.URL.revokeObjectURL(url);
image.instance = img;
image.loaded = true;
image.publish(
GameLib.Event.IMAGE_INSTANCE_CREATED,
{