no shortcuts - images to api

beta.r3js.org
Theunis J. Botha 2017-06-08 18:17:03 +02:00
parent f6f1c8a628
commit 9a12e4e3c3
17 changed files with 842 additions and 729 deletions

View File

@ -17,6 +17,7 @@ GameLib.Event.Subscriptions = {};
GameLib.Event.WINDOW_RESIZE = 0x1; GameLib.Event.WINDOW_RESIZE = 0x1;
GameLib.Event.PARENT_SCENE_CHANGE = 0x2; GameLib.Event.PARENT_SCENE_CHANGE = 0x2;
GameLib.Event.PARENT_ENTITY_CHANGE = 0x3; GameLib.Event.PARENT_ENTITY_CHANGE = 0x3;
GameLib.Event.TEXTURE_LOADED = 0x4;
/** /**
* Subscribe to some events * Subscribe to some events

View File

@ -76,6 +76,7 @@ GameLib.Component.COMPONENT_DOM_ELEMENT = 0x1c;
GameLib.Component.COMPONENT_IMAGE_FACTORY = 0x1d; GameLib.Component.COMPONENT_IMAGE_FACTORY = 0x1d;
GameLib.Component.COMPONENT_STATS = 0x1e; GameLib.Component.COMPONENT_STATS = 0x1e;
GameLib.Component.COMPONENT_GUI = 0x1f; GameLib.Component.COMPONENT_GUI = 0x1f;
GameLib.Component.COMPONENT_IMAGE = 0x20;
/** /**
* Components are linked at runtime - for storing, we just store the ID * Components are linked at runtime - for storing, we just store the ID

View File

@ -0,0 +1,77 @@
/**
* Image
* @param id
* @param name
* @param path
* @param contentType
* @param parentEntity GameLib.Entity
* @constructor
*/
GameLib.D3.API.Image = function(
id,
name,
path,
contentType,
parentEntity
) {
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_IMAGE,
null,
null,
parentEntity
);
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'Image ' + GameLib.Utils.RandomId() + '.png';
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(path)) {
path = '/' + this.name.toLowerCase().replace(' ', '_');
}
this.path = path;
if (GameLib.Utils.UndefinedOrNull(contentType)) {
contentType = 'application/octet-stream';
if (this.name.match(/(png)$/i)) {
contentType = 'image/png';
}
if (this.name.match(/(jpg|jpeg)$/i)) {
contentType = 'image/jpeg';
}
if (this.name.match(/(gif)$/i)) {
contentType = 'image/gif';
}
}
this.contentType = contentType;
};
GameLib.D3.API.Image.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.Image.prototype.constructor = GameLib.D3.API.Image;
/**
* Returns an API light from an Object light
* @constructor
* @param objectImage
*/
GameLib.D3.API.Image.FromObject = function(objectImage) {
return new GameLib.D3.API.Image(
objectImage.id,
objectImage.name,
objectImage.path,
objectImage.contentType,
objectImage.parentEntity
);
};

View File

@ -511,6 +511,8 @@ GameLib.D3.API.Material = function(
selected = false; selected = false;
} }
this.selected = selected; this.selected = selected;
this.needsUpdate = false;
}; };
GameLib.D3.API.Material.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.API.Material.prototype = Object.create(GameLib.Component.prototype);

View File

@ -76,7 +76,7 @@ GameLib.D3.API.Renderer = function (
this.domElement = domElement; this.domElement = domElement;
if (GameLib.Utils.UndefinedOrNull(clearColor)) { if (GameLib.Utils.UndefinedOrNull(clearColor)) {
clearColor = new GameLib.API.Color(); clearColor = new GameLib.API.Color(0.58, 0.58, 0.58);
} }
this.clearColor = clearColor; this.clearColor = clearColor;
}; };

View File

@ -2,7 +2,6 @@
* Raw Scene API object - should always correspond with the Scene Schema * Raw Scene API object - should always correspond with the Scene Schema
* @param id String * @param id String
* @param name String * @param name String
* @param imageFactory [GameLib.D3.API.ImageFactory]
* @param meshes [GameLib.D3.API.Mesh] * @param meshes [GameLib.D3.API.Mesh]
* @param position GameLib.API.Vector3 * @param position GameLib.API.Vector3
* @param quaternion GameLib.API.Quaternion * @param quaternion GameLib.API.Quaternion
@ -18,7 +17,6 @@
GameLib.D3.API.Scene = function( GameLib.D3.API.Scene = function(
id, id,
name, name,
imageFactory,
meshes, meshes,
position, position,
quaternion, quaternion,
@ -34,7 +32,6 @@ GameLib.D3.API.Scene = function(
this, this,
GameLib.Component.COMPONENT_SCENE, GameLib.Component.COMPONENT_SCENE,
{ {
'imageFactory' : GameLib.D3.ImageFactory,
'meshes' : [GameLib.D3.Mesh], 'meshes' : [GameLib.D3.Mesh],
'lights' : [GameLib.D3.Light], 'lights' : [GameLib.D3.Light],
'textures' : [GameLib.D3.Texture], 'textures' : [GameLib.D3.Texture],
@ -55,12 +52,6 @@ GameLib.D3.API.Scene = function(
} }
this.name = name; this.name = name;
if (GameLib.Utils.UndefinedOrNull(imageFactory)) {
imageFactory = null;
console.warn('Constructing an API Scene with no Image Factory')
}
this.imageFactory = imageFactory;
if (GameLib.Utils.UndefinedOrNull(meshes)) { if (GameLib.Utils.UndefinedOrNull(meshes)) {
meshes = []; meshes = [];
} }
@ -114,12 +105,10 @@ GameLib.D3.API.Scene.prototype.constructor = GameLib.D3.API.Scene;
/** /**
* Returns an API scene from an Object scene * Returns an API scene from an Object scene
* @param objectScene * @param objectScene
* @param imageFactory GameLib.D3.ImageFactory
* @constructor * @constructor
*/ */
GameLib.D3.API.Scene.FromObjectScene = function(objectScene, imageFactory) { GameLib.D3.API.Scene.FromObjectScene = function(objectScene) {
var apiImageFactory = null;
var apiMeshes = []; var apiMeshes = [];
var apiLights = []; var apiLights = [];
var apiTextures = []; var apiTextures = [];
@ -131,15 +120,6 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene, imageFactory) {
var apiActiveCamera = null; var apiActiveCamera = null;
/**
* Passed in ImageFactory overrides API image factory
*/
if (imageFactory) {
apiImageFactory = imageFactory;
} else if (objectScene.imageFactory) {
apiImageFactory = GameLib.D3.API.ImageFactory.FromObjectImageFactory(objectScene.imageFactory);
}
if (objectScene.meshes) { if (objectScene.meshes) {
apiMeshes = objectScene.meshes.map( apiMeshes = objectScene.meshes.map(
function(objectMesh) { function(objectMesh) {
@ -191,7 +171,6 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene, imageFactory) {
return new GameLib.D3.API.Scene( return new GameLib.D3.API.Scene(
objectScene.id, objectScene.id,
objectScene.name, objectScene.name,
apiImageFactory,
apiMeshes, apiMeshes,
apiPosition, apiPosition,
apiQuaternion, apiQuaternion,

View File

@ -3,7 +3,7 @@
* @param id * @param id
* @param typeId * @param typeId
* @param name * @param name
* @param imagePath * @param image
* @param wrapS * @param wrapS
* @param wrapT * @param wrapT
* @param repeat * @param repeat
@ -28,7 +28,7 @@ GameLib.D3.API.Texture = function(
id, id,
typeId, typeId,
name, name,
imagePath, image,
wrapS, wrapS,
wrapT, wrapT,
repeat, repeat,
@ -76,10 +76,10 @@ GameLib.D3.API.Texture = function(
} }
this.name = name; this.name = name;
if (GameLib.Utils.UndefinedOrNull(imagePath)) { if (GameLib.Utils.UndefinedOrNull(image)) {
imagePath = null; image = null;
} }
this.imagePath = imagePath; this.image = image;
if (GameLib.Utils.UndefinedOrNull(wrapS)) { if (GameLib.Utils.UndefinedOrNull(wrapS)) {
wrapS = GameLib.D3.Texture.TYPE_REPEAT_WRAPPING; wrapS = GameLib.D3.Texture.TYPE_REPEAT_WRAPPING;
@ -182,7 +182,7 @@ GameLib.D3.API.Texture.FromObjectTexture = function(objectTexture) {
objectTexture.id, objectTexture.id,
objectTexture.textureType, objectTexture.textureType,
objectTexture.name, objectTexture.name,
objectTexture.imagePath, objectTexture.image,
objectTexture.wrapS, objectTexture.wrapS,
objectTexture.wrapT, objectTexture.wrapT,
GameLib.API.Vector2.FromObjectVector(objectTexture.repeat), GameLib.API.Vector2.FromObjectVector(objectTexture.repeat),

View File

@ -97,6 +97,15 @@ GameLib.D3.ImageFactory.prototype.loadImage = function(
this.instance.load( this.instance.load(
this.baseUrl + imagePath + '?ts=' + Date.now(), this.baseUrl + imagePath + '?ts=' + Date.now(),
function (image) { function (image) {
GameLib.Event.Emit(
GameLib.Event.TEXTURE_LOADED,
{
imagePath : imagePath,
imageInstance : image
}
);
defer.resolve(image); defer.resolve(image);
}.bind(this), }.bind(this),
function onProgress(xhr) { function onProgress(xhr) {

View File

@ -1,48 +1,89 @@
/** /**
* Image * Image
* @param id
* @param filename
* @param size
* @param contentType
* @param textureLink
* @constructor * @constructor
* @param graphics
* @param apiImage
*/ */
GameLib.D3.Image = function( GameLib.D3.Image = function(
id, graphics,
filename, apiImage
size,
contentType,
textureLink
) { ) {
if (GameLib.Utils.UndefinedOrNull(id)) { if (GameLib.Utils.UndefinedOrNull(apiImage)) {
id = GameLib.Utils.RandomId(); apiImage = {};
} }
this.id = id;
this.filename = filename; if (apiImage instanceof GameLib.D3.Image) {
return apiImage;
if (GameLib.Utils.UndefinedOrNull(size)) {
size = 0;
} }
this.size = size;
if (GameLib.Utils.UndefinedOrNull(contentType)) { GameLib.D3.API.Image.call(
this,
apiImage.id,
apiImage.name,
apiImage.path,
apiImage.contentType,
apiImage.parentEntity
);
contentType = 'application/octet-stream'; this.instance = this.createInstance();
};
if (this.filename.match(/(png)$/i)) {
contentType = 'image/png'; GameLib.D3.Image.prototype = Object.create(GameLib.D3.API.Image.prototype);
} GameLib.D3.Image.prototype.constructor = GameLib.D3.Image;
if (this.filename.match(/(jpg|jpeg)$/i)) { /**
contentType = 'image/jpeg'; * Creates a light instance
} * @returns {*}
*/
if (this.filename.match(/(gif)$/i)) { GameLib.D3.Image.prototype.createInstance = function(update) {
contentType = 'image/gif';
} if (update) {
} console.log('why the fuck are you updating an image instance?');
this.contentType = contentType; this.instance.id = this.id;
this.instance.name = this.name;
this.textureLink = textureLink; this.instance.path = this.path;
this.instance.contentType = this.contentType;
} else {
var instance = new THREE.TextureLoader().load(this.path);
return instance;
}
return instance;
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Image.prototype.updateInstance = function() {
this.createInstance(true);
};
/**
*
* @returns {GameLib.D3.API.Image}
*/
GameLib.D3.Image.prototype.toApiObject = function() {
var apiImage = new GameLib.D3.API.Image(
this.id,
this.name,
this.path,
this.contentType,
GameLib.Utils.IdOrNull(this.parentEntity)
);
return apiImage;
};
/**
* @param graphics
* @param objectLight
* @returns {GameLib.D3.Image}
* @constructor
*/
GameLib.D3.Image.FromObject = function(graphics, objectLight) {
return new GameLib.D3.Image(
graphics,
GameLib.D3.API.Image.FromObject(objectLight)
);
}; };

View File

@ -3,14 +3,12 @@
* created * created
* @param graphics GameLib.D3.Graphics * @param graphics GameLib.D3.Graphics
* @param apiMaterial GameLib.D3.API.Material * @param apiMaterial GameLib.D3.API.Material
* @param imageFactory GameLib.D3.ImageFactory
* @constructor * @constructor
* @returns {GameLib.D3.Material | GameLib.D3.API.Material} * @returns {GameLib.D3.Material | GameLib.D3.API.Material}
*/ */
GameLib.D3.Material = function( GameLib.D3.Material = function(
graphics, graphics,
apiMaterial, apiMaterial
imageFactory
) { ) {
this.graphics = graphics; this.graphics = graphics;
@ -24,12 +22,6 @@ GameLib.D3.Material = function(
return apiMaterial; return apiMaterial;
} }
if (GameLib.Utils.UndefinedOrNull(imageFactory)) {
console.warn('Cannot create a Material fully without specifying an ImageFactory');
imageFactory = null;
}
this.imageFactory = imageFactory;
GameLib.D3.API.Material.call( GameLib.D3.API.Material.call(
this, this,
apiMaterial.id, apiMaterial.id,
@ -125,12 +117,8 @@ GameLib.D3.Material = function(
if (this.alphaMap instanceof GameLib.D3.API.Texture) { if (this.alphaMap instanceof GameLib.D3.API.Texture) {
this.alphaMap = new GameLib.D3.Texture( this.alphaMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.alphaMap, this.alphaMap
this.imageFactory
); );
} else {
console.warn('this.alphaMap is not an instance of API.Texture');
// throw new Error('this.alphaMap is not an instance of API.Texture');
} }
} }
@ -138,12 +126,8 @@ GameLib.D3.Material = function(
if (this.aoMap instanceof GameLib.D3.API.Texture) { if (this.aoMap instanceof GameLib.D3.API.Texture) {
this.aoMap = new GameLib.D3.Texture( this.aoMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.aoMap, this.aoMap
this.imageFactory
); );
} else {
console.warn('this.aoMap is not an instance of API.Texture');
// throw new Error('this.aoMap is not an instance of API.Texture');
} }
} }
@ -151,12 +135,8 @@ GameLib.D3.Material = function(
if (this.bumpMap instanceof GameLib.D3.API.Texture) { if (this.bumpMap instanceof GameLib.D3.API.Texture) {
this.bumpMap = new GameLib.D3.Texture( this.bumpMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.bumpMap, this.bumpMap
this.imageFactory
); );
} else {
console.warn('this.bumpMap is not an instance of API.Texture');
// throw new Error('this.bumpMap is not an instance of API.Texture');
} }
} }
@ -164,12 +144,8 @@ GameLib.D3.Material = function(
if (this.diffuseMap instanceof GameLib.D3.API.Texture) { if (this.diffuseMap instanceof GameLib.D3.API.Texture) {
this.diffuseMap = new GameLib.D3.Texture( this.diffuseMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.diffuseMap, this.diffuseMap
this.imageFactory
); );
} else {
console.warn('this.diffuseMap is not an instance of API.Texture');
// throw new Error('this.diffuseMap is not an instance of API.Texture');
} }
} }
@ -177,12 +153,8 @@ GameLib.D3.Material = function(
if (this.displacementMap instanceof GameLib.D3.API.Texture) { if (this.displacementMap instanceof GameLib.D3.API.Texture) {
this.displacementMap = new GameLib.D3.Texture( this.displacementMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.displacementMap, this.displacementMap
this.imageFactory
); );
} else {
console.warn('this.displacementMap is not an instance of API.Texture');
// throw new Error('this.displacementMap is not an instance of API.Texture');
} }
} }
@ -190,12 +162,8 @@ GameLib.D3.Material = function(
if (this.emissiveMap instanceof GameLib.D3.API.Texture) { if (this.emissiveMap instanceof GameLib.D3.API.Texture) {
this.emissiveMap = new GameLib.D3.Texture( this.emissiveMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.emissiveMap, this.emissiveMap
this.imageFactory
); );
} else {
console.warn('this.emissiveMap is not an instance of API.Texture');
// throw new Error('this.emissiveMap is not an instance of API.Texture');
} }
} }
@ -203,12 +171,8 @@ GameLib.D3.Material = function(
if (this.environmentMap instanceof GameLib.D3.API.Texture) { if (this.environmentMap instanceof GameLib.D3.API.Texture) {
this.environmentMap = new GameLib.D3.Texture( this.environmentMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.environmentMap, this.environmentMap
this.imageFactory
); );
} else {
console.warn('this.environmentMap is not an instance of API.Texture');
// throw new Error('this.environmentMap is not an instance of API.Texture');
} }
} }
@ -216,12 +180,8 @@ GameLib.D3.Material = function(
if (this.lightMap instanceof GameLib.D3.API.Texture) { if (this.lightMap instanceof GameLib.D3.API.Texture) {
this.lightMap = new GameLib.D3.Texture( this.lightMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.lightMap, this.lightMap
this.imageFactory
); );
} else {
console.warn('this.lightMap is not an instance of API.Texture');
// throw new Error('this.lightMap is not an instance of API.Texture');
} }
} }
@ -229,12 +189,8 @@ GameLib.D3.Material = function(
if (this.metalnessMap instanceof GameLib.D3.API.Texture) { if (this.metalnessMap instanceof GameLib.D3.API.Texture) {
this.metalnessMap = new GameLib.D3.Texture( this.metalnessMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.metalnessMap, this.metalnessMap
this.imageFactory
); );
} else {
console.warn('this.metalnessMap is not an instance of API.Texture');
// throw new Error('this.metalnessMap is not an instance of API.Texture');
} }
} }
@ -242,12 +198,8 @@ GameLib.D3.Material = function(
if (this.normalMap instanceof GameLib.D3.API.Texture) { if (this.normalMap instanceof GameLib.D3.API.Texture) {
this.normalMap = new GameLib.D3.Texture( this.normalMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.normalMap, this.normalMap
this.imageFactory
); );
} else {
console.warn('this.normalMap is not an instance of API.Texture');
// throw new Error('this.normalMap is not an instance of API.Texture');
} }
} }
@ -255,12 +207,8 @@ GameLib.D3.Material = function(
if (this.roughnessMap instanceof GameLib.D3.API.Texture) { if (this.roughnessMap instanceof GameLib.D3.API.Texture) {
this.roughnessMap = new GameLib.D3.Texture( this.roughnessMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.roughnessMap, this.roughnessMap
this.imageFactory
); );
} else {
console.warn('this.roughnessMap is not an instance of API.Texture');
// throw new Error('this.roughnessMap is not an instance of API.Texture');
} }
} }
@ -268,17 +216,11 @@ GameLib.D3.Material = function(
if (this.specularMap instanceof GameLib.D3.API.Texture) { if (this.specularMap instanceof GameLib.D3.API.Texture) {
this.specularMap = new GameLib.D3.Texture( this.specularMap = new GameLib.D3.Texture(
this.graphics, this.graphics,
this.specularMap, this.specularMap
this.imageFactory
); );
} else {
console.warn('this.specularMap is not an instance of API.Texture');
// throw new Error('this.specularMap is not an instance of API.Texture');
} }
} }
this.needsUpdate = false;
this.buildIdToObject(); this.buildIdToObject();
this.instance = this.createInstance(); this.instance = this.createInstance();
@ -384,289 +326,437 @@ GameLib.D3.Material.MATERIAL_TYPE_POINTS = "PointsMaterial";
GameLib.D3.Material.MATERIAL_TYPE_SPRITE = "SpriteMaterial"; GameLib.D3.Material.MATERIAL_TYPE_SPRITE = "SpriteMaterial";
GameLib.D3.Material.MATERIAL_TYPE_MULTIPLE = "MultiMaterial"; GameLib.D3.Material.MATERIAL_TYPE_MULTIPLE = "MultiMaterial";
GameLib.D3.Material.prototype.createStandardMaterialInstance = function() {
return new THREE.MeshStandardMaterial({
name: this.name,
opacity: this.opacity,
transparent: this.transparent,
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: this.color.instance,
roughness: this.roughness,
metalness: this.metalness,
lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: this.aoMapIntensity,
emissive: this.emissive.instance,
emissiveIntensity: this.emissiveIntensity,
bumpScale: this.bumpScale,
normalScale: this.normalScale,
displacementScale: this.displacementScale,
refractionRatio: this.refractionRatio,
fog: this.fog,
shading: this.shading,
wireframe: this.wireframe,
wireframeLinewidth: this.wireframeLineWidth,
wireframeLinecap: this.wireframeLineCap,
wireframeLinejoin: this.wireframeLineJoin,
vertexColors: this.vertexColors,
skinning: this.skinning,
morphTargets: this.morphTargets,
morphNormals: this.morphNormals
});
};
GameLib.D3.Material.prototype.createPointsMaterialInstance = function() {
return new THREE.PointsMaterial({
name: this.name,
opacity: this.opacity,
transparent: this.transparent,
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: this.color.instance,
size: this.pointSize,
sizeAttenuation: this.pointSizeAttenuation,
vertexColors: GameLib.D3.Material.TYPE_NO_COLORS,
fog: this.fog
});
};
GameLib.D3.Material.prototype.createPhongMaterialInstance = function() {
return new THREE.MeshPhongMaterial({
name: this.name,
opacity: this.opacity,
transparent: this.transparent,
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: this.color.instance,
specular: this.specular.instance,
shininess: this.shininess,
lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: this.aoMapIntensity,
emissive: this.emissive.instance,
emissiveIntensity: this.emissiveIntensity,
bumpScale: this.bumpScale,
normalScale: this.normalScale,
displacementScale: this.displacementScale,
combine: this.combine,
refractionRatio: this.refractionRatio,
fog: this.fog,
shading: this.shading,
wireframe: this.wireframe,
wireframeLinewidth: this.wireframeLineWidth,
wireframeLinecap: this.wireframeLineCap,
wireframeLinejoin: this.wireframeLineJoin,
vertexColors: this.vertexColors,
skinning: this.skinning,
morphTargets: this.morphTargets,
morphNormals: this.morphNormals
});
};
GameLib.D3.Material.prototype.createMeshBasicMaterialInstance = function() {
return new THREE.MeshBasicMaterial({
name: this.name,
opacity: this.opacity,
transparent: this.transparent,
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: this.color.instance,
vertexColors: GameLib.D3.Material.TYPE_NO_COLORS,
fog: this.fog
});
};
/**
* updates textures
*/
GameLib.D3.Material.prototype.updateTextures = function() {
if (this.alphaMap) {
this.instance.alphaMap = this.alphaMap.instance;
} else {
this.instance.alphaMap = null;
}
if (this.aoMap) {
this.instance.aoMap = this.aoMap.instance;
} else {
this.instance.aoMap = null;
}
if (this.bumpMap) {
this.instance.bumpMap = this.bumpMap.instance;
} else {
this.instance.bumpMap = null;
}
if (this.diffuseMap) {
this.instance.map = this.diffuseMap.instance;
} else {
this.instance.map = null;
}
if (this.displacementMap) {
this.instance.displacementMap = this.displacementMap.instance;
} else {
this.instance.displacementMap = null;
}
if (this.emissiveMap) {
this.instance.emissiveMap = this.emissiveMap.instance;
} else {
this.instance.emissiveMap = null;
}
if (this.environmentMap) {
this.instance.envMap = this.environmentMap.instance;
} else {
this.instance.envMap = null;
}
if (this.lightMap) {
this.instance.lightMap = this.lightMap.instance;
} else {
this.instance.lightMap = null;
}
if (this.metalnessMap) {
this.instance.metalnessMap = this.metalnessMap.instance;
} else {
this.instance.metalnessMap = null;
}
if (this.normalMap) {
this.instance.normalMap = this.normalMap.instance;
} else {
this.instance.normalMap = null;
}
if (this.roughnessMap) {
this.instance.roughnessMap = this.roughnessMap.instance;
} else {
this.instance.roughnessMap = null;
}
if (this.specularMap) {
this.instance.specularMap = this.specularMap.instance;
} else {
this.instance.specularMap = null;
}
};
GameLib.D3.Material.prototype.updateStandardMaterialInstance = function() {
this.instance.name = this.name;
this.instance.opacity = this.opacity;
this.instance.transparent = this.transparent;
this.instance.blending = this.blending;
this.instance.blendSrc = this.blendSrc;
this.instance.blendDst = this.blendDst;
this.instance.blendEquation = this.blendEquation;
this.instance.depthTest = this.depthTest;
this.instance.depthFunc = this.depthFunc;
this.instance.depthWrite = this.depthWrite;
this.instance.polygonOffset = this.polygonOffset;
this.instance.polygonOffsetFactor = this.polygonOffsetFactor;
this.instance.polygonOffsetUnits = this.polygonOffsetUnits;
this.instance.alphaTest = this.alphaTest;
this.instance.clippingPlanes = this.clippingPlanes;
this.instance.clipShadows = this.clipShadows;
this.instance.overdraw = this.overdraw;
this.instance.visible = this.visible;
this.instance.side = this.side;
this.instance.color = this.color.instance;
this.instance.roughness = this.roughness;
this.instance.metalness = this.metalness;
this.instance.lightMapIntensity = this.lightMapIntensity;
this.instance.aoMapIntensity = this.aoMapIntensity;
this.instance.emissive = this.emissive.instance;
this.instance.emissiveIntensity = this.emissiveIntensity;
this.instance.bumpScale = this.bumpScale;
this.instance.normalScale = this.normalScale;
this.instance.displacementScale = this.displacementScale;
this.instance.refractionRatio = this.refractionRatio;
this.instance.fog = this.fog;
this.instance.shading = this.shading;
this.instance.wireframe = this.wireframe;
this.instance.wireframeLinewidth = this.wireframeLineWidth;
this.instance.wireframeLinecap = this.wireframeLineCap;
this.instance.wireframeLinejoin = this.wireframeLineJoin;
this.instance.vertexColors = this.vertexColors;
this.instance.skinning = this.skinning;
this.instance.morphTargets = this.morphTargets;
this.instance.morphNormals = this.morphNormals;
this.updateTextures();
};
GameLib.D3.Material.prototype.updatePointsMaterialInstance = function() {
this.instance.name = this.name;
this.instance.opacity = this.opacity;
this.instance.transparent = this.transparent;
this.instance.blending = this.blending;
this.instance.blendSrc = this.blendSrc;
this.instance.blendDst = this.blendDst;
this.instance.blendEquation = this.blendEquation;
this.instance.depthTest = this.depthTest;
this.instance.depthFunc = this.depthFunc;
this.instance.depthWrite = this.depthWrite;
this.instance.polygonOffset = this.polygonOffset;
this.instance.polygonOffsetFactor = this.polygonOffsetFactor;
this.instance.polygonOffsetUnits = this.polygonOffsetUnits;
this.instance.alphaTest = this.alphaTest;
this.instance.clippingPlanes = this.clippingPlanes;
this.instance.clipShadows = this.clipShadows;
this.instance.overdraw = this.overdraw;
this.instance.visible = this.visible;
this.instance.side = this.side;
this.instance.color = this.color.instance;
this.instance.size = this.pointSize;
this.instance.sizeAttenuation = this.pointSizeAttenuation;
this.instance.vertexColors = GameLib.D3.Material.TYPE_NO_COLORS;
this.instance.fog = this.fog;
this.updateTextures();
};
GameLib.D3.Material.prototype.updatePhongMaterialInstance = function() {
this.instance.name = this.name;
this.instance.opacity = this.opacity;
this.instance.transparent = this.transparent;
this.instance.blending = this.blending;
this.instance.blendSrc = this.blendSrc;
this.instance.blendDst = this.blendDst;
this.instance.blendEquation = this.blendEquation;
this.instance.depthTest = this.depthTest;
this.instance.depthFunc = this.depthFunc;
this.instance.depthWrite = this.depthWrite;
this.instance.polygonOffset = this.polygonOffset;
this.instance.polygonOffsetFactor = this.polygonOffsetFactor;
this.instance.polygonOffsetUnits = this.polygonOffsetUnits;
this.instance.alphaTest = this.alphaTest;
this.instance.clippingPlanes = this.clippingPlanes;
this.instance.clipShadows = this.clipShadows;
this.instance.overdraw = this.overdraw;
this.instance.visible = this.visible;
this.instance.side = this.side;
this.instance.color = this.color.instance;
this.instance.specular = this.specular.instance;
this.instance.shininess = this.shininess;
this.instance.lightMapIntensity = this.lightMapIntensity;
this.instance.aoMapIntensity = this.aoMapIntensity;
this.instance.emissive = this.emissive.instance;
this.instance.emissiveIntensity = this.emissiveIntensity;
this.instance.bumpScale = this.bumpScale;
this.instance.normalScale = this.normalScale;
this.instance.displacementScale = this.displacementScale;
this.instance.combine = this.combine;
this.instance.refractionRatio = this.refractionRatio;
this.instance.fog = this.fog;
this.instance.shading = this.shading;
this.instance.wireframe = this.wireframe;
this.instance.wireframeLinewidth = this.wireframeLineWidth;
this.instance.wireframeLinecap = this.wireframeLineCap;
this.instance.wireframeLinejoin = this.wireframeLineJoin;
this.instance.vertexColors = this.vertexColors;
this.instance.skinning = this.skinning;
this.instance.morphTargets = this.morphTargets;
this.instance.morphNormals = this.morphNormals;
this.updateTextures();
};
GameLib.D3.Material.prototype.updateMeshBasicMaterialInstance = function() {
this.instance.name = this.name;
this.instance.opacity = this.opacity;
this.instance.transparent = this.transparent;
this.instance.blending = this.blending;
this.instance.blendSrc = this.blendSrc;
this.instance.blendDst = this.blendDst;
this.instance.blendEquation = this.blendEquation;
this.instance.depthTest = this.depthTest;
this.instance.depthFunc = this.depthFunc;
this.instance.depthWrite = this.depthWrite;
this.instance.polygonOffset = this.polygonOffset;
this.instance.polygonOffsetFactor = this.polygonOffsetFactor;
this.instance.polygonOffsetUnits = this.polygonOffsetUnits;
this.instance.alphaTest = this.alphaTest;
this.instance.clippingPlanes = this.clippingPlanes;
this.instance.clipShadows = this.clipShadows;
this.instance.overdraw = this.overdraw;
this.instance.visible = this.visible;
this.instance.side = this.side;
this.instance.color = this.color.instance;
this.instance.vertexColors = GameLib.D3.Material.TYPE_NO_COLORS;
this.instance.fog = this.fog;
this.updateTextures();
};
/** /**
* Material instance * Material instance
* @returns {*} * @returns {*}
*/ */
GameLib.D3.Material.prototype.createInstance = function(update) { GameLib.D3.Material.prototype.createInstance = function(update) {
var instance = null;
if (update) { if (update) {
instance = this.instance;
}
if (!instance) { if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_STANDARD) {
if (!(this.instance instanceof THREE.MeshStandardMaterial)) {
this.instance = this.createStandardMaterialInstance();
} else {
this.updateStandardMaterialInstance();
}
} else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_POINTS) {
if (!(this.instance instanceof THREE.PointsMaterial)) {
this.instance = this.createPointsMaterialInstance();
} else {
this.updatePointsMaterialInstance();
}
} else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_PHONG) {
if (!(this.instance instanceof THREE.MeshPhongMaterial)) {
this.instance = this.createPhongMaterialInstance();
} else {
this.updatePhongMaterialInstance();
}
} else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_BASIC) {
if (!(this.instance instanceof THREE.MeshBasicMaterial)) {
this.instance = this.createMeshBasicMaterialInstance();
} else {
this.updateMeshBasicMaterialInstance();
}
} else {
console.warn('not yet implemented (material type = ' + this.materialType + ')');
}
if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { this.instance.needsUpdate = true;
instance = new THREE.MeshStandardMaterial({ } else {
name: this.name,
opacity: this.opacity,
transparent: this.transparent,
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: this.color.instance,
roughness: this.roughness,
metalness: this.metalness,
lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: this.aoMapIntensity,
emissive: this.emissive.instance,
emissiveIntensity: this.emissiveIntensity,
bumpScale: this.bumpScale,
normalScale: this.normalScale,
displacementScale: this.displacementScale,
refractionRatio: this.refractionRatio,
fog: this.fog,
shading: this.shading,
wireframe: this.wireframe,
wireframeLinewidth: this.wireframeLineWidth,
wireframeLinecap: this.wireframeLineCap,
wireframeLinejoin: this.wireframeLineJoin,
vertexColors: this.vertexColors,
skinning: this.skinning,
morphTargets: this.morphTargets,
morphNormals: this.morphNormals
});
} else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_PHONG) { var instance = null;
instance = new THREE.MeshPhongMaterial({ if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_STANDARD) {
name: this.name,
opacity: this.opacity,
transparent: this.transparent,
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: this.color.instance,
specular: this.specular.instance,
shininess: this.shininess,
lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: this.aoMapIntensity,
emissive: this.emissive.instance,
emissiveIntensity: this.emissiveIntensity,
bumpScale: this.bumpScale,
normalScale: this.normalScale,
displacementScale: this.displacementScale,
combine: this.combine,
refractionRatio: this.refractionRatio,
fog: this.fog,
shading: this.shading,
wireframe: this.wireframe,
wireframeLinewidth: this.wireframeLineWidth,
wireframeLinecap: this.wireframeLineCap,
wireframeLinejoin: this.wireframeLineJoin,
vertexColors: this.vertexColors,
skinning: this.skinning,
morphTargets: this.morphTargets,
morphNormals: this.morphNormals
});
} else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_POINTS) {
instance = new this.graphics.instance.PointsMaterial({ instance = this.createStandardMaterialInstance();
name: this.name,
opacity: this.opacity,
transparent: this.transparent,
blending: this.blending,
blendSrc: this.blendSrc,
blendDst: this.blendDst,
blendEquation: this.blendEquation,
depthTest: this.depthTest,
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: this.color.instance,
size: this.pointSize,
sizeAttenuation: this.pointSizeAttenuation,
vertexColors: GameLib.D3.Material.TYPE_NO_COLORS,
fog: this.fog
});
} else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_BASIC) { } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_POINTS) {
instance = new THREE.MeshBasicMaterial({ instance = this.createPointsMaterialInstance();
name: this.name,
opacity: this.opacity, } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_PHONG) {
transparent: this.transparent,
blending: this.blending, instance = this.createPhongMaterialInstance();
blendSrc: this.blendSrc,
blendDst: this.blendDst, } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_BASIC) {
blendEquation: this.blendEquation,
depthTest: this.depthTest, instance = this.createMeshBasicMaterialInstance();
depthFunc: this.depthFunc,
depthWrite: this.depthWrite,
polygonOffset: this.polygonOffset,
polygonOffsetFactor: this.polygonOffsetFactor,
polygonOffsetUnits: this.polygonOffsetUnits,
alphaTest: this.alphaTest,
clippingPlanes: this.clippingPlanes,
clipShadows: this.clipShadows,
overdraw: this.overdraw,
visible: this.visible,
side: this.side,
color: this.color.instance,
vertexColors: GameLib.D3.Material.TYPE_NO_COLORS,
fog: this.fog
});
} else { } else {
console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up"); console.warn("material type is not implemented yet: " + this.materialType);
}
}
if (update) {
for (var property in instance) {
if (instance.hasOwnProperty(property)) {
if (property == 'alphaMap') {
if (this.alphaMap) {
instance.alphaMap = this.alphaMap.instance;
} else {
instance.alphaMap = null;
}
}
else if (property == 'aoMap') {
if (this.aoMap) {
instance.aoMap = this.aoMap.instance;
} else {
instance.aoMap = null;
}
}
else if (property == 'bumpMap') {
if (this.bumpMap) {
instance.bumpMap = this.bumpMap.instance;
} else {
instance.bumpMap = null;
}
}
else if (property == 'map') {
if (this.diffuseMap) {
instance.map = this.diffuseMap.instance;
} else {
instance.map = null;
}
}
else if (property == 'displacementMap') {
if (this.displacementMap) {
instance.displacementMap = this.displacementMap.instance;
} else {
instance.displacementMap = null;
}
}
else if (property == 'emissiveMap') {
if (this.emissiveMap) {
instance.emissiveMap = this.emissiveMap.instance;
} else {
instance.emissiveMap = null;
}
}
else if (property == 'envMap') {
if (this.environmentMap) {
instance.envMap = this.environmentMap.instance;
} else {
instance.envMap = null;
}
}
else if (property == 'lightMap') {
if (this.lightMap) {
instance.lightMap = this.lightMap.instance;
} else {
instance.lightMap = null;
}
}
else if (property == 'metalnessMap') {
if (this.metalnessMap) {
instance.metalnessMap = this.metalnessMap.instance;
} else {
instance.metalnessMap = null;
}
}
else if (property == 'normalMap') {
if (this.normalMap) {
instance.normalMap = this.normalMap.instance;
} else {
instance.normalMap = null;
}
}
else if (property == 'roughnessMap') {
if (this.roughnessMap) {
instance.roughnessMap = this.roughnessMap.instance;
} else {
instance.roughnessMap = null;
}
}
else if (property == 'specularMap') {
if (this.specularMap) {
instance.specularMap = this.specularMap.instance;
} else {
instance.specularMap = null;
}
}
else if (property == 'size') {
instance.size = this.pointSize;
}
else if (property == 'sizeAttenuation') {
instance.sizeAttenuation = this.pointSizeAttenuation;
}
else if (instance[property] instanceof THREE.Color) {
instance[property].copy(this[property])
} else if (this.hasOwnProperty(property)) {
instance[property] = this[property];
}
}
} }
instance.needsUpdate = true; instance.needsUpdate = true;
return instance;
} }
return instance;
}; };
/** /**
* Updates the instance with the current state * Updates the instance with the current state
*/ */
GameLib.D3.Material.prototype.updateInstance = function() { GameLib.D3.Material.prototype.updateInstance = function() {
this.instance = this.createInstance(true); this.createInstance(true);
};
GameLib.D3.Material.prototype.clone = function() {
return _.cloneDeep(this);
}; };
/** /**
@ -812,15 +902,13 @@ GameLib.D3.Material.prototype.toApiObject = function() {
* Creates a GameLib.D3.Material from a material Object * Creates a GameLib.D3.Material from a material Object
* @param graphics GameLib.D3.Graphics * @param graphics GameLib.D3.Graphics
* @param objectMaterial Object * @param objectMaterial Object
* @param imageFactory GameLib.D3.ImageFactory
* @constructor * @constructor
*/ */
GameLib.D3.Material.FromObjectMaterial = function(graphics, objectMaterial, imageFactory) { GameLib.D3.Material.FromObjectMaterial = function(graphics, objectMaterial) {
var gameLibMaterial = new GameLib.D3.Material( var gameLibMaterial = new GameLib.D3.Material(
graphics, graphics,
GameLib.D3.API.Material.FromObjectMaterial(objectMaterial), GameLib.D3.API.Material.FromObjectMaterial(objectMaterial)
imageFactory
); );
return gameLibMaterial; return gameLibMaterial;

View File

@ -2,15 +2,11 @@
* Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created * Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created
* @param graphics GameLib.D3.Graphics * @param graphics GameLib.D3.Graphics
* @param apiMesh GameLib.D3.API.Mesh * @param apiMesh GameLib.D3.API.Mesh
* @param imageFactory GameLib.D3.ImageFactory
* @param computeNormals Boolean
* @constructor * @constructor
*/ */
GameLib.D3.Mesh = function ( GameLib.D3.Mesh = function (
graphics, graphics,
apiMesh, apiMesh
imageFactory,
computeNormals
) { ) {
this.graphics = graphics; this.graphics = graphics;
this.graphics.isNotThreeThrow(); this.graphics.isNotThreeThrow();
@ -23,17 +19,6 @@ GameLib.D3.Mesh = function (
return apiMesh; return apiMesh;
} }
if (GameLib.Utils.UndefinedOrNull(computeNormals)) {
computeNormals = true;
}
this.computeNormals = computeNormals;
if (GameLib.Utils.UndefinedOrNull(imageFactory)) {
console.warn('Cannot create Meshes fully without specifying an ImageFactory for downloading Textures');
imageFactory = null;
}
this.imageFactory = imageFactory;
GameLib.D3.API.Mesh.call( GameLib.D3.API.Mesh.call(
this, this,
apiMesh.id, apiMesh.id,
@ -60,23 +45,27 @@ GameLib.D3.Mesh = function (
apiMesh.renderOrder apiMesh.renderOrder
); );
this.materials = this.materials.map( apiMesh.materials = apiMesh.materials.map(function(apiMaterial) {
function (apiMaterial) { if (apiMaterial instanceof GameLib.D3.API.Material) {
/**
if (apiMaterial instanceof GameLib.D3.API.Material) { * Do Nothing
*/
if (apiMaterial instanceof GameLib.D3.Material) {
return apiMaterial;
} else {
return new GameLib.D3.Material( return new GameLib.D3.Material(
this.graphics, this.graphics,
apiMaterial, apiMaterial
this.imageFactory );
)
} else {
console.warn('API material not of instance API.Material - should be linked at runtime');
return apiMaterial;
// throw new Error('API material not of instance API.Material');
} }
} else {
/**
* Do Nothing
*/
}
}.bind(this));
}.bind(this) this.materials = apiMesh.materials;
);
if (this.skeleton) { if (this.skeleton) {
this.skeleton = new GameLib.D3.Skeleton( this.skeleton = new GameLib.D3.Skeleton(
@ -315,15 +304,8 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) {
geometry = this.customGeometry(); geometry = this.customGeometry();
} }
/** geometry.computeFaceNormals();
* Re-calculate normals (if we have to) geometry.computeVertexNormals();
* @type {Array}
*/
if (this.computeNormals) {
geometry.computeFaceNormals();
geometry.computeVertexNormals();
this.computeNormals = false;
}
if (this.meshType === GameLib.D3.Mesh.TYPE_NORMAL) { if (this.meshType === GameLib.D3.Mesh.TYPE_NORMAL) {
instance = new THREE.Mesh(geometry); instance = new THREE.Mesh(geometry);
@ -432,7 +414,7 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) {
instance.rotateZ(this.localRotation.z); instance.rotateZ(this.localRotation.z);
} }
if (this.materials.length == 1 && this.materials[0].instance) { if (this.materials.length === 1 && this.materials[0].instance) {
instance.material = this.materials[0].instance; instance.material = this.materials[0].instance;
} }
@ -502,19 +484,15 @@ GameLib.D3.Mesh.prototype.toApiObject = function() {
* Converts a standard object mesh to a GameLib.D3.Mesh * Converts a standard object mesh to a GameLib.D3.Mesh
* @param graphics GameLib.D3.Graphics * @param graphics GameLib.D3.Graphics
* @param objectMesh {Object} * @param objectMesh {Object}
* @param computeNormals boolean to indicate whether or not to recalculate normals
* @param imageFactory GameLib.D3.ImageFactory
* @constructor * @constructor
*/ */
GameLib.D3.Mesh.FromObjectMesh = function(graphics, objectMesh, computeNormals, imageFactory) { GameLib.D3.Mesh.FromObjectMesh = function(graphics, objectMesh) {
var apiMesh = GameLib.D3.API.Mesh.FromObjectMesh(objectMesh); var apiMesh = GameLib.D3.API.Mesh.FromObjectMesh(objectMesh);
return new GameLib.D3.Mesh( return new GameLib.D3.Mesh(
graphics, graphics,
apiMesh, apiMesh
computeNormals,
imageFactory
); );
}; };

View File

@ -3,13 +3,11 @@
* created * created
* @param graphics * @param graphics
* @param apiScene GameLib.D3.API.Scene * @param apiScene GameLib.D3.API.Scene
* @param computeNormals
* @constructor * @constructor
*/ */
GameLib.D3.Scene = function ( GameLib.D3.Scene = function (
graphics, graphics,
apiScene, apiScene
computeNormals
) { ) {
this.graphics = graphics; this.graphics = graphics;
this.graphics.isNotThreeThrow(); this.graphics.isNotThreeThrow();
@ -22,16 +20,10 @@ GameLib.D3.Scene = function (
return apiScene; return apiScene;
} }
if (GameLib.Utils.UndefinedOrNull(computeNormals)) {
computeNormals = true;
}
this.computeNormals = computeNormals;
GameLib.D3.API.Scene.call( GameLib.D3.API.Scene.call(
this, this,
apiScene.id, apiScene.id,
apiScene.name, apiScene.name,
apiScene.imageFactory,
apiScene.meshes, apiScene.meshes,
apiScene.position, apiScene.position,
apiScene.quaternion, apiScene.quaternion,
@ -44,13 +36,6 @@ GameLib.D3.Scene = function (
apiScene.parentEntity apiScene.parentEntity
); );
if (this.imageFactory instanceof GameLib.D3.API.ImageFactory) {
this.imageFactory = new GameLib.D3.ImageFactory(
this.graphics,
this.imageFactory
);
}
this.meshes = this.meshes.map( this.meshes = this.meshes.map(
function(apiMesh) { function(apiMesh) {
@ -58,9 +43,7 @@ GameLib.D3.Scene = function (
return new GameLib.D3.Mesh( return new GameLib.D3.Mesh(
this.graphics, this.graphics,
apiMesh, apiMesh
this.imageFactory,
this.computeNormals
); );
} else { } else {
@ -107,49 +90,49 @@ GameLib.D3.Scene = function (
}.bind(this) }.bind(this)
); );
this.textures = this.textures.map( // this.textures = this.textures.map(
function(apiTexture) { // function(apiTexture) {
//
if (apiTexture instanceof GameLib.D3.API.Texture) { // if (apiTexture instanceof GameLib.D3.API.Texture) {
var texture = new GameLib.D3.Texture( // var texture = new GameLib.D3.Texture(
this.graphics, // this.graphics,
apiTexture, // apiTexture,
this.imageFactory // this.imageFactory
); // );
//
this.idToObject[texture.id] = texture; // this.idToObject[texture.id] = texture;
//
return texture; // return texture;
} else { // } else {
console.warn('apiTexture not an instance of API.Texture'); // console.warn('apiTexture not an instance of API.Texture');
throw new Error('apiTexture not an instance of API.Texture'); // throw new Error('apiTexture not an instance of API.Texture');
} // }
//
}.bind(this) // }.bind(this)
); // );
//
this.materials = this.materials.map( // this.materials = this.materials.map(
function(apiMaterial) { // function(apiMaterial) {
//
if (apiMaterial instanceof GameLib.D3.API.Material) { // if (apiMaterial instanceof GameLib.D3.API.Material) {
//
var material = new GameLib.D3.Material( // var material = new GameLib.D3.Material(
this.graphics, // this.graphics,
apiMaterial, // apiMaterial,
this.imageFactory // this.imageFactory
); // );
//
this.idToObject[material.id] = material; // this.idToObject[material.id] = material;
//
return material; // return material;
//
} else { // } else {
console.warn('apiMaterial not an instance of API.Material'); // console.warn('apiMaterial not an instance of API.Material');
throw new Error('apiMaterial not an instance of API.Material'); // throw new Error('apiMaterial not an instance of API.Material');
} // }
//
}.bind(this) // }.bind(this)
); // );
if (this.activeCamera instanceof GameLib.D3.API.Camera) { if (this.activeCamera instanceof GameLib.D3.API.Camera) {
this.activeCamera = new GameLib.D3.Camera( this.activeCamera = new GameLib.D3.Camera(
@ -162,17 +145,6 @@ GameLib.D3.Scene = function (
this.linkObjects(this.idToObject); this.linkObjects(this.idToObject);
this.meshes.map(
function(mesh) {
mesh.updateInstance();
mesh.materials.map(
function(material) {
material.updateInstance();
}
)
}
);
this.buildIdToObject(); this.buildIdToObject();
this.instance = this.createInstance(); this.instance = this.createInstance();
@ -214,11 +186,6 @@ GameLib.D3.Scene.prototype.createInstance = function() {
*/ */
GameLib.D3.Scene.prototype.toApiObject = function() { GameLib.D3.Scene.prototype.toApiObject = function() {
var apiImageFactory = null;
if (this.imageFactory instanceof GameLib.D3.ImageFactory) {
apiImageFactory = this.imageFactory.toApiObject();
}
var apiMeshes = this.meshes.map( var apiMeshes = this.meshes.map(
function(mesh) { function(mesh) {
return mesh.toApiObject(); return mesh.toApiObject();
@ -246,7 +213,6 @@ GameLib.D3.Scene.prototype.toApiObject = function() {
return new GameLib.D3.API.Scene( return new GameLib.D3.API.Scene(
this.id, this.id,
this.name, this.name,
apiImageFactory,
apiMeshes, apiMeshes,
this.position.toApiObject(), this.position.toApiObject(),
this.quaternion.toApiObject(), this.quaternion.toApiObject(),
@ -264,23 +230,18 @@ GameLib.D3.Scene.prototype.toApiObject = function() {
* Converts a scene Object to a GameLib.D3.Scene object * Converts a scene Object to a GameLib.D3.Scene object
* @param graphics GameLib.D3.Graphics * @param graphics GameLib.D3.Graphics
* @param objectScene Object * @param objectScene Object
* @param computeNormals boolean to indicate whether or not to recalculate normals
* @param imageFactory GameLib.D3.ImageFactory
* @returns {GameLib.D3.Scene} * @returns {GameLib.D3.Scene}
* @constructor * @constructor
*/ */
GameLib.D3.Scene.FromObjectScene = function( GameLib.D3.Scene.FromObjectScene = function(
graphics, graphics,
objectScene, objectScene
computeNormals,
imageFactory
) { ) {
var apiScene = GameLib.D3.API.Scene.FromObjectScene(objectScene, imageFactory); var apiScene = GameLib.D3.API.Scene.FromObjectScene(objectScene);
return new GameLib.D3.Scene( return new GameLib.D3.Scene(
graphics, graphics,
apiScene, apiScene
computeNormals
); );
}; };
@ -296,9 +257,7 @@ GameLib.D3.Scene.prototype.addMesh = function(mesh) {
} else if (mesh instanceof GameLib.D3.API.Mesh) { } else if (mesh instanceof GameLib.D3.API.Mesh) {
mesh = new GameLib.D3.Mesh( mesh = new GameLib.D3.Mesh(
this.graphics, this.graphics,
mesh, mesh
mesh.imageFactory,
mesh.computeNormals
) )
} }

View File

@ -3,13 +3,11 @@
* created * created
* @param apiTexture * @param apiTexture
* @param graphics GameLib.D3.Graphics * @param graphics GameLib.D3.Graphics
* @param imageFactory GameLib.D3.ImageFactory
* @constructor * @constructor
*/ */
GameLib.D3.Texture = function( GameLib.D3.Texture = function(
graphics, graphics,
apiTexture, apiTexture
imageFactory
) { ) {
this.graphics = graphics; this.graphics = graphics;
this.graphics.isNotThreeThrow(); this.graphics.isNotThreeThrow();
@ -22,18 +20,12 @@ GameLib.D3.Texture = function(
return apiTexture; return apiTexture;
} }
if (GameLib.Utils.UndefinedOrNull(imageFactory)) {
console.warn('Cannot create a Texture without specifying an ImageFactory');
imageFactory = null;
}
this.imageFactory = imageFactory;
GameLib.D3.API.Texture.call( GameLib.D3.API.Texture.call(
this, this,
apiTexture.id, apiTexture.id,
apiTexture.typeId, apiTexture.typeId,
apiTexture.name, apiTexture.name,
apiTexture.imagePath, apiTexture.image,
apiTexture.wrapS, apiTexture.wrapS,
apiTexture.wrapT, apiTexture.wrapT,
apiTexture.repeat, apiTexture.repeat,
@ -55,59 +47,28 @@ GameLib.D3.Texture = function(
); );
this.offset = new GameLib.Vector2( this.offset = new GameLib.Vector2(
graphics, this.graphics,
this.offset, this.offset,
this this
); );
this.repeat = new GameLib.Vector2( this.repeat = new GameLib.Vector2(
graphics, this.graphics,
this.repeat, this.repeat,
this this
); );
this.imageInstance = null; this.image = new GameLib.D3.Image(
this.graphics,
this.image
);
this.instance = null; this.instance = this.createInstance();
this.loadTexture();
}; };
GameLib.D3.Texture.prototype = Object.create(GameLib.D3.API.Texture.prototype); GameLib.D3.Texture.prototype = Object.create(GameLib.D3.API.Texture.prototype);
GameLib.D3.Texture.prototype.constructor = GameLib.D3.Texture; GameLib.D3.Texture.prototype.constructor = GameLib.D3.Texture;
/**
* Loads a texture from the image factory, it could already have downloaded, and then it updates the instance
* @param force boolean to force a reload
*/
GameLib.D3.Texture.prototype.loadTexture = function(force, onComplete) {
this.imageData = this.imageFactory.loadImage(this.imagePath, force);
this.imageData.then(
function (imageInstance){
this.imageInstance = imageInstance;
this.instance = this.createInstance();
if (onComplete) {
onComplete();
}
// this.parentObjects.map(
// function(parentObject){
// if (parentObject instanceof GameLib.D3.Material && parentObject.updateInstance) {
// parentObject.updateInstance();
// }
// }
// )
}.bind(this),
function onRejected(message) {
console.warn(message);
if (onComplete) {
onComplete(message);
}
}
);
};
/** /**
* Texture Formats * Texture Formats
* @type {number} * @type {number}
@ -196,61 +157,34 @@ GameLib.D3.Texture.TEXTURE_TYPE_SPECULAR = 'specular';
* @returns {*} * @returns {*}
*/ */
GameLib.D3.Texture.prototype.createInstance = function(update) { GameLib.D3.Texture.prototype.createInstance = function(update) {
var instance = null;
if (update) { if (update) {
instance = this.instance; this.instance.name = this.name;
instance.mapping = this.mapping; this.instance.flipY = this.flipY;
instance.wrapS = this.wrapS; this.instance.encoding = this.encoding;
instance.wrapT = this.wrapT; this.instance.offset.x = this.offset.x;
instance.magFilter = this.magFilter; this.instance.offset.y = this.offset.y;
instance.minFilter = this.minFilter; this.instance.repeat.x = this.repeat.x;
instance.anisotropy = this.anisotropy; this.instance.repeat.y = this.repeat.y;
} else { } else {
instance = new THREE.Texture( var instance = new THREE.Texture(
this.imageInstance, this.image.instance
this.mapping,
this.wrapS,
this.wrapT,
this.magFilter,
this.minFilter,
undefined, //format and textureType is different on different archs
undefined,
this.anisotropy
); );
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;
return instance;
} }
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.mipmaps = this.mipmaps;
// instance.unpackAlignment = this.unpackAlignment;
// instance.premultiplyAlpha = this.premultiplyAlpha;
// instance.textureType = this.textureType;
instance.needsUpdate = true;
return instance;
}; };
/** /**
* Updates the instance with the current state * Updates the instance with the current state
*/ */
GameLib.D3.Texture.prototype.updateInstance = function() { GameLib.D3.Texture.prototype.updateInstance = function() {
this.instance = this.createInstance(true); this.createInstance(true);
};
/**
* Clones this texture object
* @returns {*}
*/
GameLib.D3.Texture.prototype.clone = function() {
return _.cloneDeep(this);
}; };
/** /**
@ -259,11 +193,16 @@ GameLib.D3.Texture.prototype.clone = function() {
*/ */
GameLib.D3.Texture.prototype.toApiObject = function() { GameLib.D3.Texture.prototype.toApiObject = function() {
var apiImage = null;
if (this.image) {
apiImage = this.image.toApiObject();
}
return new GameLib.D3.API.Texture( return new GameLib.D3.API.Texture(
this.id, this.id,
this.typeId, this.typeId,
this.name, this.name,
this.imagePath, apiImage,
this.wrapS, this.wrapS,
this.wrapT, this.wrapT,
this.repeat.toApiObject(), this.repeat.toApiObject(),
@ -290,19 +229,16 @@ GameLib.D3.Texture.prototype.toApiObject = function() {
* Converts from an Object texture to a GameLib.D3.Texture * Converts from an Object texture to a GameLib.D3.Texture
* @param graphics GameLib.D3.Graphics * @param graphics GameLib.D3.Graphics
* @param objectTexture Object * @param objectTexture Object
* @param imageFactory GameLib.D3.ImageFactory
* @constructor * @constructor
*/ */
GameLib.D3.Texture.FromObjectTexture = function( GameLib.D3.Texture.FromObjectTexture = function(
graphics, graphics,
objectTexture, objectTexture
imageFactory
) { ) {
var apiTexture = GameLib.D3.API.Texture.FromObjectTexture(objectTexture); var apiTexture = GameLib.D3.API.Texture.FromObjectTexture(objectTexture);
return new GameLib.D3.Texture( return new GameLib.D3.Texture(
graphics, graphics,
apiTexture, apiTexture
imageFactory
); );
}; };

View File

@ -380,12 +380,88 @@ GameLib.EntityManager.prototype.onParentSceneChange = function(data) {
} }
}; };
/**
* Change parent entity
* TODO: also change parent entity of children objects
* @param data
*/
GameLib.EntityManager.prototype.onParentEntityChange = function(data) { GameLib.EntityManager.prototype.onParentEntityChange = function(data) {
data.originalEntity.removeComponent(data.object);
if (data.originalEntity) {
data.originalEntity.removeComponent(data.object);
}
data.newEntity.addComponent(data.object); data.newEntity.addComponent(data.object);
}; };
GameLib.EntityManager.prototype.onTextureLoaded = function(data) {
var allGuis = [];
this.entities.map(
function(entity) {
var materials = entity.getComponents(GameLib.D3.Material);
materials.map(
function(material) {
for (var property in material.linkedObjects) {
if (
material.linkedObjects.hasOwnProperty(property) &&
material.linkedObjects[property] === GameLib.D3.Texture &&
material[property]
) {
var texture = material[property];
var dataImagePath = GameLib.Utils.StripImageExtension(data.imagePath);
var textureImagePath = GameLib.Utils.StripImageExtension(texture.imagePath);
if (dataImagePath === textureImagePath) {
/**
* Update the image path in case the file extension changed
* @type {*}
*/
texture.imagePath = data.imagePath;
texture.imageInstance = data.imageInstance;
if (texture.instance) {
texture.updateInstance();
} else {
texture.instance = texture.createInstance();
}
texture.instance.needsUpdate = true;
material.updateInstance();
material.instance.needsUpdate = true;
}
}
}
}
);
var guis = entity.getComponents(GameLib.GUI);
guis.map(function(gui) {
allGuis.push(gui);
})
}
);
allGuis.map(function(gui){
gui.build(this);
}.bind(this))
};
/** /**
* *
*/ */
@ -401,6 +477,11 @@ GameLib.EntityManager.prototype.registerCallbacks = function() {
this.onParentEntityChange this.onParentEntityChange
); );
this.subscribe(
GameLib.Event.TEXTURE_LOADED,
this.onTextureLoaded
)
}; };
/** /**

View File

@ -678,7 +678,7 @@ GameLib.GUI.prototype.buildControl = function(folder, object, property) {
object[property] = value; object[property] = value;
} }
object.updateInstance(); object.updateInstance();
object.needsUpdate = true; // object.needsUpdate = true;
} }
); );
} }

View File

@ -368,100 +368,52 @@ GameLib.System.prototype.notify = function(property, constructor, action) {
}; };
GameLib.System.prototype.load = function() { GameLib.System.prototype.load = function(onLoaded) {
//
if (this.systemType === GameLib.System.SYSTEM_TYPE_STORAGE) { // if (this.systemType === GameLib.System.SYSTEM_TYPE_STORAGE) {
//
} // }
//
/** // /**
* First check if this is a client or server side request // * First check if this is a client or server side request
*/ // */
if (typeof XMLHttpRequest === 'undefined') { // if (typeof XMLHttpRequest === 'undefined') {
console.warn('implement server side loading from API here'); // console.warn('implement server side loading from API here');
return onLoaded( // return onLoaded(
null, // null,
new Error('not implemented') // new Error('not implemented')
); // );
} // }
//
var xhr = new XMLHttpRequest(); // var xhr = new XMLHttpRequest();
xhr.open( // xhr.open(
'GET', // 'GET',
apiUrl + '/scene/load' + partialSceneObject.path + '/' + partialSceneObject.name // apiUrl + '/scene/load' + partialSceneObject.path + '/' + partialSceneObject.name
); // );
//
xhr.onreadystatechange = function(xhr) { // xhr.onreadystatechange = function(xhr) {
//
return function() { // return function() {
//
if (xhr.readyState === 4) { // if (xhr.readyState === 4) {
//
try { // try {
var response = JSON.parse(xhr.responseText); // var response = JSON.parse(xhr.responseText);
} catch (e) { // } catch (e) {
return onLoaded(null, new Error('Could not load scene : ' + e.message)); // return onLoaded(null, new Error('Could not load scene : ' + e.message));
} // }
//
if (!response.scene || response.scene.length === 0) { // if (!response.scene || response.scene.length === 0) {
return onLoaded(null, new Error('Could not load scene')); // return onLoaded(null, new Error('Could not load scene'));
} // }
//
var objectScene = response.scene[0]; // var objectScene = response.scene[0];
//
GameLib.D3.Scene.LoadScene( // onLoaded(GameLib.D3.Scene.FromObjectScene(graphics, objectScene));
graphics, // }
objectScene, // }
true, // }(xhr);
onLoaded //
); // xhr.send();
}
}
}(xhr);
xhr.send();
};
/**
* Transforms raw scene data into a GameLib.D3.Scene
* @param graphics
* @param objectScene Object (as it comes from the API)
* @param computeNormals
* @param onLoaded
* @param imageFactory GameLib.D3.ImageFactory
* @constructor
*/
GameLib.D3.Scene.LoadScene = function(
graphics,
objectScene,
computeNormals,
onLoaded,
imageFactory
) {
var scene = GameLib.D3.Scene.FromObjectScene(
graphics,
objectScene,
computeNormals,
imageFactory
);
onLoaded(scene);
};
/**
* Loads a scene directly from the API
* @param graphics GameLib.D3.Graphics
* @param partialSceneObject Object {path: '', name: ''}
* @param apiUrl
* @param onLoaded
*/
GameLib.D3.Scene.LoadSceneFromApi = function(
graphics,
partialSceneObject,
apiUrl,
onLoaded
) {
}; };

View File

@ -10,6 +10,15 @@ GameLib.Utils.Extend = function(
} }
}; };
/**
* Strips image extension from given path
* @param imagePath
* @constructor
*/
GameLib.Utils.StripImageExtension = function(imagePath) {
return imagePath.replace(/(\.png$|\.gif$|\.jpeg$|\.jpg$)/,'')
};
/** /**
* Returns id of object with the name if it exists in the array, otherwise null * Returns id of object with the name if it exists in the array, otherwise null
* @param name * @param name