From 481cd05374097d285bc77ddc0db98feeca00176d Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Mon, 21 Nov 2016 16:08:39 +0100 Subject: [PATCH] starting to recurse through properties --- src/game-lib-bone-weight.js | 2 +- src/game-lib-bone.js | 2 +- src/game-lib-broadphase.js | 2 +- src/game-lib-camera.js | 2 +- src/game-lib-color.js | 2 +- src/game-lib-engine.js | 2 +- src/game-lib-graphics.js | 2 +- src/game-lib-heightmap.js | 2 +- src/game-lib-image.js | 2 +- src/game-lib-light.js | 11 +- src/game-lib-material.js | 6 +- src/game-lib-mesh.js | 8 +- src/game-lib-scene.js | 140 +++++++++++++++--------- src/game-lib-skeleton.js | 2 +- src/game-lib-texture-map-template.js | 156 +++++++++++---------------- src/game-lib-texture-map.js | 21 ++++ src/game-lib-texture-maps.js | 45 ++++++++ src/game-lib-texture.js | 28 +++-- src/game-lib-triangle-face.js | 2 +- src/game-lib-vector-2.js | 2 +- src/game-lib-vector-3.js | 2 +- src/game-lib-vector-4.js | 2 +- src/game-lib-vertex.js | 2 +- 23 files changed, 272 insertions(+), 173 deletions(-) create mode 100644 src/game-lib-texture-map.js create mode 100644 src/game-lib-texture-maps.js diff --git a/src/game-lib-bone-weight.js b/src/game-lib-bone-weight.js index 954b81f..5249d59 100644 --- a/src/game-lib-bone-weight.js +++ b/src/game-lib-bone-weight.js @@ -4,7 +4,7 @@ * @param weight float * @constructor */ -GameLib.D3.BoneWeight = function( +GameLib.D3.BoneWeight = function BoneWeight( boneIndex, weight ) { diff --git a/src/game-lib-bone.js b/src/game-lib-bone.js index 0e8585b..7eeade5 100644 --- a/src/game-lib-bone.js +++ b/src/game-lib-bone.js @@ -12,7 +12,7 @@ * @param up * @constructor */ -GameLib.D3.Bone = function( +GameLib.D3.Bone = function Bone( id, boneId, name, diff --git a/src/game-lib-broadphase.js b/src/game-lib-broadphase.js index f646a9c..8053e30 100644 --- a/src/game-lib-broadphase.js +++ b/src/game-lib-broadphase.js @@ -6,7 +6,7 @@ * @param engine GameLib.D3.Engine * @constructor */ -GameLib.D3.Broadphase = function( +GameLib.D3.Broadphase = function Broadphase( id, name, broadphaseType, diff --git a/src/game-lib-camera.js b/src/game-lib-camera.js index 6e1c893..1421950 100644 --- a/src/game-lib-camera.js +++ b/src/game-lib-camera.js @@ -4,7 +4,7 @@ * @param apiCamera GameLib.D3.Camera.API * @constructor */ -GameLib.D3.Camera = function( +GameLib.D3.Camera = function Camera( graphics, apiCamera ) { diff --git a/src/game-lib-color.js b/src/game-lib-color.js index 76cfaa6..62b06e1 100644 --- a/src/game-lib-color.js +++ b/src/game-lib-color.js @@ -6,7 +6,7 @@ * @param a * @constructor */ -GameLib.D3.Color = function(r, g, b, a) { +GameLib.D3.Color = function Color(r, g, b, a) { this.r = r; this.g = g; this.b = b; diff --git a/src/game-lib-engine.js b/src/game-lib-engine.js index e019c55..f0d4479 100644 --- a/src/game-lib-engine.js +++ b/src/game-lib-engine.js @@ -4,7 +4,7 @@ * @param instance {CANNON | Ammo | Goblin} * @constructor */ -GameLib.D3.Engine = function( +GameLib.D3.Engine = function Engine( engineType, instance ) { diff --git a/src/game-lib-graphics.js b/src/game-lib-graphics.js index 7ffb678..578a410 100644 --- a/src/game-lib-graphics.js +++ b/src/game-lib-graphics.js @@ -4,7 +4,7 @@ * @param instance {THREE} * @constructor */ -GameLib.D3.Graphics = function( +GameLib.D3.Graphics = function Graphics( graphicsType, instance ) { diff --git a/src/game-lib-heightmap.js b/src/game-lib-heightmap.js index ac63b51..28c6b4a 100644 --- a/src/game-lib-heightmap.js +++ b/src/game-lib-heightmap.js @@ -6,7 +6,7 @@ * @param heightScale Number * @constructor */ -GameLib.D3.Heightmap = function( +GameLib.D3.Heightmap = function Heightmap( sizeX, sizeY, matrix, diff --git a/src/game-lib-image.js b/src/game-lib-image.js index 9734a99..bb1b5a4 100644 --- a/src/game-lib-image.js +++ b/src/game-lib-image.js @@ -46,7 +46,7 @@ GameLib.D3.ImageFactory.LoadImage = function (graphics, url, defer, progressCall loader.crossOrigin = ''; loader.load( - url, + url + '?ts=' + Date.now(), function (image) { defer.resolve(image); }, diff --git a/src/game-lib-light.js b/src/game-lib-light.js index 5e5f929..72e0ae0 100644 --- a/src/game-lib-light.js +++ b/src/game-lib-light.js @@ -4,7 +4,7 @@ * @param apiLight GameLib.D3.Light.API * @constructor */ -GameLib.D3.Light = function( +GameLib.D3.Light = function Light( graphics, apiLight ) { @@ -207,6 +207,11 @@ GameLib.D3.Light.prototype.createInstance = function(update) { } } + if (!instance) { + console.warn('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?'); + throw new Error('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?'); + } + instance.gameLibObject = this; instance.name = this.name; @@ -228,3 +233,7 @@ GameLib.D3.Light.prototype.createInstance = function(update) { GameLib.D3.Light.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; + +GameLib.D3.Light.prototype.clone = function() { + return _.cloneDeep(this); +}; diff --git a/src/game-lib-material.js b/src/game-lib-material.js index dc9badd..c906077 100644 --- a/src/game-lib-material.js +++ b/src/game-lib-material.js @@ -6,7 +6,7 @@ * @constructor * @returns {GameLib.D3.Material | GameLib.D3.Material.API} */ -GameLib.D3.Material = function( +GameLib.D3.Material = function Material( graphics, apiMaterial ) { @@ -667,4 +667,8 @@ GameLib.D3.Material.prototype.createInstance = function(update) { */ GameLib.D3.Material.prototype.updateInstance = function() { this.instance = this.createInstance(true); +}; + +GameLib.D3.Material.prototype.clone = function() { + return _.cloneDeep(this); }; \ No newline at end of file diff --git a/src/game-lib-mesh.js b/src/game-lib-mesh.js index 70ba7b1..89c9b7e 100644 --- a/src/game-lib-mesh.js +++ b/src/game-lib-mesh.js @@ -5,7 +5,7 @@ * @param apiMesh GameLib.D3.Mesh.API * @constructor */ -GameLib.D3.Mesh = function( +GameLib.D3.Mesh = function Mesh( graphics, computeNormals, apiMesh @@ -422,7 +422,7 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { return instance; }; -GameLib.D3.Mesh.updateInstance = function() { +GameLib.D3.Mesh.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; @@ -721,4 +721,8 @@ GameLib.D3.fixPolyZPlane = function(verticesFlat, grain) { } return vertices; +}; + +GameLib.D3.Mesh.prototype.clone = function() { + return _.cloneDeep(this); }; \ No newline at end of file diff --git a/src/game-lib-scene.js b/src/game-lib-scene.js index 46d6ee6..bc112f6 100644 --- a/src/game-lib-scene.js +++ b/src/game-lib-scene.js @@ -6,10 +6,11 @@ * @param apiScene GameLib.D3.Scene.API * @constructor */ -GameLib.D3.Scene = function( +GameLib.D3.Scene = function Scene( graphics, progressCallback, - apiScene + apiScene, + imageFactory ) { for (var property in apiScene) { @@ -31,7 +32,8 @@ GameLib.D3.Scene = function( this.instance = this.createInstance(); - this.needsUpdate = false; + this.imageFactory = imageFactory; + }; /** @@ -205,62 +207,67 @@ GameLib.D3.Scene.LoadScene = function( var shapes = []; + var imageFactory = GameLib.D3.ImageFactory(graphics); + if (scene.worlds && scene.worlds.length > 0) { console.warn('Implement physics worlds code here'); } - var lights3d = []; + var lights = []; if (scene.lights && scene.lights.length > 0) { for (var l = 0; l < scene.lights.length; l++) { - var light = scene.lights[l]; + var apiLight = scene.lights[l]; - var light3d = new GameLib.D3.Light( - light.id, - light.lightType, - light.name, - new GameLib.D3.Color( - light.color.r, - light.color.g, - light.color.b, - light.color.a - ), - light.intensity, - new GameLib.D3.Vector3( - light.position.x, - light.position.y, - light.position.z - ), - new GameLib.D3.Vector3( - light.targetPosition.x, - light.targetPosition.y, - light.targetPosition.z - ), - new GameLib.D3.Vector4( - light.quaternion.x, - light.quaternion.y, - light.quaternion.z, - light.quaternion.w - ), - new GameLib.D3.Vector3( - light.rotation.x, - light.rotation.y, - light.rotation.z - ), - new GameLib.D3.Vector3( - light.scale.x, - light.scale.y, - light.scale.z - ), - light.distance, - light.decay, - light.power, - light.angle, - light.penumbra + var light = new GameLib.D3.Light( + graphics, + new GameLib.D3.Light.API( + apiLight.id, + apiLight.lightType, + apiLight.name, + new GameLib.D3.Color( + apiLight.color.r, + apiLight.color.g, + apiLight.color.b, + apiLight.color.a + ), + apiLight.intensity, + new GameLib.D3.Vector3( + apiLight.position.x, + apiLight.position.y, + apiLight.position.z + ), + new GameLib.D3.Vector3( + apiLight.targetPosition.x, + apiLight.targetPosition.y, + apiLight.targetPosition.z + ), + new GameLib.D3.Vector4( + apiLight.quaternion.x, + apiLight.quaternion.y, + apiLight.quaternion.z, + apiLight.quaternion.w + ), + new GameLib.D3.Vector3( + apiLight.rotation.x, + apiLight.rotation.y, + apiLight.rotation.z + ), + new GameLib.D3.Vector3( + apiLight.scale.x, + apiLight.scale.y, + apiLight.scale.z + ), + apiLight.distance, + apiLight.decay, + apiLight.power, + apiLight.angle, + apiLight.penumbra + ) ); - lights3d.push(light3d); + lights.push(light); } } @@ -395,7 +402,8 @@ GameLib.D3.Scene.LoadScene = function( ), graphics, gameLibMaterial, - gameLibTextureMap[map].instanceMapId + gameLibTextureMap[map].instanceMapId, + imageFactory ); } } @@ -405,6 +413,33 @@ GameLib.D3.Scene.LoadScene = function( gameLibMaterials.push(gameLibMaterial); } + var gameLibVertices = []; + + for (var v = 0; v < apiMesh.vertices.length; v++) { + + var boneWeights = []; + + for (var bw = 0; bw < apiMesh.vertices[v].boneWeights.length; bw++) { + boneWeights.push( + new GameLib.D3.BoneWeight( + apiMesh.vertices[v].boneWeights[bw].boneIndex, + apiMesh.vertices[v].boneWeights[bw].weight + ) + ) + } + + gameLibVertices.push( + new GameLib.D3.Vertex( + new GameLib.D3.Vector3( + apiMesh.vertices[v].position.x, + apiMesh.vertices[v].position.y, + apiMesh.vertices[v].position.z + ), + boneWeights + ) + ) + } + var gameLibMesh = new GameLib.D3.Mesh( graphics, computeNormals, @@ -412,7 +447,7 @@ GameLib.D3.Scene.LoadScene = function( apiMesh.id, apiMesh.meshType, apiMesh.name, - apiMesh.vertices, + gameLibVertices, apiMesh.faces, apiMesh.faceVertexUvs, gameLibMaterials, @@ -483,12 +518,13 @@ GameLib.D3.Scene.LoadScene = function( scene.scale.z ), scene.parentSceneId, - lights3d, + lights, worlds, entities, components, shapes - ) + ), + imageFactory ); onLoaded(scene3d); diff --git a/src/game-lib-skeleton.js b/src/game-lib-skeleton.js index d843a5f..d0f0714 100644 --- a/src/game-lib-skeleton.js +++ b/src/game-lib-skeleton.js @@ -10,7 +10,7 @@ * @param boneTexture * @constructor */ -GameLib.D3.Skeleton = function( +GameLib.D3.Skeleton = function Skeleton( id, bones, boneInverses, diff --git a/src/game-lib-texture-map-template.js b/src/game-lib-texture-map-template.js index 54f7947..a0be510 100644 --- a/src/game-lib-texture-map-template.js +++ b/src/game-lib-texture-map-template.js @@ -2,110 +2,84 @@ * We have a mapping between GameLib.D3.Texture and the Instance material map. * Our GameLib.D3.Material.map.alpha corresponds to THREE.Material.alphaMap * @param graphics - * @returns {{alpha: {texture: null, instanceMapId: string}, ao: {texture: null, instanceMapId: string}, bump: {texture: null, instanceMapId: string}, diffuse: {texture: null, instanceMapId: string}, displacement: {texture: null, instanceMapId: string}, emissive: {texture: null, instanceMapId: string}, environment: {texture: null, instanceMapId: string}, light: {texture: null, instanceMapId: string}, metalness: {texture: null, instanceMapId: string}, normal: {texture: null, instanceMapId: string}, roughness: {texture: null, instanceMapId: string}, specular: {texture: null, instanceMapId: string}}} * @constructor */ -GameLib.D3.TextureMapTemplate = function( +GameLib.D3.TextureMapTemplate = function TextureMapTemplate( graphics ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); - return { - alpha: { - texture : null, - instanceMapId: 'alhpaMap' - }, - ao: { - texture : null, - instanceMapId: 'aoMap' - }, - bump: { - texture : null, - instanceMapId: 'bumpMap' - }, - diffuse: { - texture : null, - instanceMapId: 'map' - }, - displacement: { - texture : null, - instanceMapId: 'displacementMap' - }, - emissive: { - texture : null, - instanceMapId: 'emissiveMap' - }, - environment: { - texture : null, - instanceMapId: 'envMap' - }, - light: { - texture : null, - instanceMapId: 'lightMap' - }, - metalness: { - texture : null, - instanceMapId: 'metalnessMap' - }, - normal: { - texture : null, - instanceMapId: 'normalMap' - }, - roughness: { - texture : null, - instanceMapId: 'roughnessMap' - }, - specular: { - texture : null, - instanceMapId: 'specularMap' - } - }; + return new GameLib.D3.TextureMaps( + new GameLib.D3.TextureMap( + null, + 'alphaMap' + ), + new GameLib.D3.TextureMap( + null, + 'aoMap' + ), + new GameLib.D3.TextureMap( + null, + 'bumpMap' + ), + new GameLib.D3.TextureMap( + null, + 'map' + ), + new GameLib.D3.TextureMap( + null, + 'displacementMap' + ), + new GameLib.D3.TextureMap( + null, + 'emissiveMap' + ), + new GameLib.D3.TextureMap( + null, + 'envMap' + ), + new GameLib.D3.TextureMap( + null, + 'lightMap' + ), + new GameLib.D3.TextureMap( + null, + 'metalnessMap' + ), + new GameLib.D3.TextureMap( + null, + 'normalMap' + ), + new GameLib.D3.TextureMap( + null, + 'roughnessMap' + ), + new GameLib.D3.TextureMap( + null, + 'specularMap' + ) + ); }; /** * Raw API Texture Map Template - should match the contents of the Material Schema (maps property) - * @returns {{alpha: {texture: null}, ao: {texture: null}, bump: {texture: null}, diffuse: {texture: null}, displacement: {texture: null}, emissive: {texture: null}, environment: {texture: null}, light: {texture: null}, metalness: {texture: null}, normal: {texture: null}, roughness: {texture: null}, specular: {texture: null}}} * @constructor */ GameLib.D3.TextureMapTemplate.API = function() { - return { - alpha: { - texture : null - }, - ao: { - texture : null - }, - bump: { - texture : null - }, - diffuse: { - texture : null - }, - displacement: { - texture : null - }, - emissive: { - texture : null - }, - environment: { - texture : null - }, - light: { - texture : null - }, - metalness: { - texture : null - }, - normal: { - texture : null - }, - roughness: { - texture : null - }, - specular: { - texture : null - } - }; + return new GameLib.D3.TextureMaps( + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap(), + new GameLib.D3.TextureMap() + ); }; diff --git a/src/game-lib-texture-map.js b/src/game-lib-texture-map.js new file mode 100644 index 0000000..4c09bfc --- /dev/null +++ b/src/game-lib-texture-map.js @@ -0,0 +1,21 @@ +/** + * We have a mapping between GameLib.D3.Texture and the Instance material map. + * Our GameLib.D3.Material.map.alpha corresponds to THREE.Material.alphaMap + * @constructor + * @param texture GameLib.D3.Texture + * @param instanceMapId string + */ +GameLib.D3.TextureMap = function TextureMap( + texture, + instanceMapId +) { + if (GameLib.D3.Utils.UndefinedOrNull(texture)) { + texture = null; + } + this.texture = texture; + + if (GameLib.D3.Utils.UndefinedOrNull(instanceMapId)) { + instanceMapId = null; + } + this.instanceMapId = instanceMapId; +}; \ No newline at end of file diff --git a/src/game-lib-texture-maps.js b/src/game-lib-texture-maps.js new file mode 100644 index 0000000..95d42b3 --- /dev/null +++ b/src/game-lib-texture-maps.js @@ -0,0 +1,45 @@ +/** + * We have a mapping between GameLib.D3.Texture and the Instance material map. + * Our GameLib.D3.Material.map.alpha corresponds to THREE.Material.alphaMap + * @returns {{alpha: {texture: null, instanceMapId: string}, ao: {texture: null, instanceMapId: string}, bump: {texture: null, instanceMapId: string}, diffuse: {texture: null, instanceMapId: string}, displacement: {texture: null, instanceMapId: string}, emissive: {texture: null, instanceMapId: string}, environment: {texture: null, instanceMapId: string}, light: {texture: null, instanceMapId: string}, metalness: {texture: null, instanceMapId: string}, normal: {texture: null, instanceMapId: string}, roughness: {texture: null, instanceMapId: string}, specular: {texture: null, instanceMapId: string}}} + * @constructor + * @param alpha + * @param ao + * @param bump + * @param diffuse + * @param displacement + * @param emissive + * @param environment + * @param light + * @param metalness + * @param normal + * @param roughness + * @param specular + */ +GameLib.D3.TextureMaps = function TextureMaps( + alpha, + ao, + bump, + diffuse, + displacement, + emissive, + environment, + light, + metalness, + normal, + roughness, + specular +) { + this.alpha = alpha; + this.ao = ao; + this.bump = bump; + this.diffuse = diffuse; + this.displacement = displacement; + this.emissive = emissive; + this.environment = environment; + this.light = light; + this.metalness = metalness; + this.normal = normal; + this.roughness = roughness; + this.specular = specular; +}; \ No newline at end of file diff --git a/src/game-lib-texture.js b/src/game-lib-texture.js index debbc80..d578728 100644 --- a/src/game-lib-texture.js +++ b/src/game-lib-texture.js @@ -5,13 +5,15 @@ * @param graphics GameLib.D3.Graphics * @param parentMaterial GameLib.D3.Material * @param parentMaterialInstanceMapId String + * @param imageFactory GameLib.D3.ImageFactory result * @constructor */ -GameLib.D3.Texture = function( +GameLib.D3.Texture = function Texture( apiTexture, graphics, parentMaterial, - parentMaterialInstanceMapId + parentMaterialInstanceMapId, + imageFactory ) { for (var property in apiTexture) { if (apiTexture.hasOwnProperty(property)) { @@ -26,23 +28,23 @@ GameLib.D3.Texture = function( this.parentMaterialInstanceMapId = parentMaterialInstanceMapId; - this.imageFactory = GameLib.D3.ImageFactory(graphics); - - this.imageData = this.imageFactory(apiTexture.imagePath); - this.imageInstance = null; this.instance = null; + this.loadTexture(imageFactory); +}; + +GameLib.D3.Texture.prototype.loadTexture = function(imageFactory) { + + this.imageData = imageFactory(this.imagePath); + this.imageData.then( function (imageInstance){ this.imageInstance = imageInstance; this.instance = this.createInstance(); - - this.needsUpdate = false; - - this.parentMaterial[this.parentMaterialInstanceMapId] = this.instance; - this.parentMaterial.needsUpdate = true; + this.parentMaterial.instance[this.parentMaterialInstanceMapId] = this.instance; + this.parentMaterial.instance.needsUpdate = true; }.bind(this), function onRejected() { } @@ -310,3 +312,7 @@ GameLib.D3.Texture.prototype.createInstance = function(update) { GameLib.D3.Texture.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; + +GameLib.D3.Texture.prototype.clone = function() { + return _.cloneDeep(this); +}; \ No newline at end of file diff --git a/src/game-lib-triangle-face.js b/src/game-lib-triangle-face.js index aba5f52..5c5f391 100644 --- a/src/game-lib-triangle-face.js +++ b/src/game-lib-triangle-face.js @@ -13,7 +13,7 @@ * @param normal * @constructor */ -GameLib.D3.TriangleFace = function( +GameLib.D3.TriangleFace = function TriangleFace( v0, v1, v2, diff --git a/src/game-lib-vector-2.js b/src/game-lib-vector-2.js index 460e5a9..82b2873 100644 --- a/src/game-lib-vector-2.js +++ b/src/game-lib-vector-2.js @@ -1,4 +1,4 @@ -GameLib.D3.Vector2 = function(x, y) { +GameLib.D3.Vector2 = function Vector2(x, y) { this.x = 0; this.y = 0; diff --git a/src/game-lib-vector-3.js b/src/game-lib-vector-3.js index 8ca7cfe..9598772 100644 --- a/src/game-lib-vector-3.js +++ b/src/game-lib-vector-3.js @@ -1,4 +1,4 @@ -GameLib.D3.Vector3 = function(x, y, z) { +GameLib.D3.Vector3 = function Vector3(x, y, z) { this.x = 0; this.y = 0; diff --git a/src/game-lib-vector-4.js b/src/game-lib-vector-4.js index ef91499..994a999 100644 --- a/src/game-lib-vector-4.js +++ b/src/game-lib-vector-4.js @@ -1,4 +1,4 @@ -GameLib.D3.Vector4 = function(x, y, z, w) { +GameLib.D3.Vector4 = function Vector4(x, y, z, w) { this.x = 0; this.y = 0; diff --git a/src/game-lib-vertex.js b/src/game-lib-vertex.js index 2e09dae..e8cc431 100644 --- a/src/game-lib-vertex.js +++ b/src/game-lib-vertex.js @@ -4,7 +4,7 @@ * @param boneWeights GameLib.D3.BoneWeight[] * @constructor */ -GameLib.D3.Vertex = function( +GameLib.D3.Vertex = function Vertex( position, boneWeights ) {