diff --git a/package.json b/package.json index 7bca358..ffb8541 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "gulp-concat": "^2.6.0", "gulp-minify": "0.0.14", "gulp-sort": "^2.0.0", - "lodash": "^4.17.1", + "lodash": "^4.17.2", "q": "^1.4.1", "three": "^0.81.2" }, diff --git a/src/game-lib-a.js b/src/game-lib-a.js index cacc090..7f7486e 100644 --- a/src/game-lib-a.js +++ b/src/game-lib-a.js @@ -7,6 +7,7 @@ if (typeof GameLib.D3 == 'undefined') { } if (typeof Q == 'undefined') { + if (typeof require == 'undefined') { console.warn('You need the Q promise library for the GameLib.D3'); throw new Error('You need the Q promise library for the GameLib.D3'); @@ -16,10 +17,11 @@ if (typeof Q == 'undefined') { } if (typeof _ == 'undefined') { + if (typeof require == 'undefined') { console.warn('You need the lowdash library for the GameLib.D3'); throw new Error('You need the lowdash library for the GameLib.D3'); } - var _ = require('_'); -} \ No newline at end of file + var _ = require('lodash'); +} diff --git a/src/game-lib-image.js b/src/game-lib-image.js index e9e82d9..4a7beb3 100644 --- a/src/game-lib-image.js +++ b/src/game-lib-image.js @@ -1,7 +1,69 @@ +/** + * The image factory takes care that we only make requests for Image URLs which we have not already started downloading + * @param graphics GameLib.D3.Graphics + * @returns {Function} + * @constructor + */ +GameLib.D3.ImageFactory = function (graphics) { + + graphics.isNotThreeThrow(); + + var promiseList = {}; + + return function(url, progressCallback) { + + if (!url) { + console.log('Attempted to download bad URL : ' + url); + throw new Error('Bad URL : ' + url); + } + + if (promiseList[url]) { + return promiseList[url]; + } + + var defer = q.defer(); + + promiseList[url] = defer.promise; + + GameLib.D3.ImageFactory.LoadImage(graphics, url, defer, progressCallback); + + return promiseList[url]; + } +}; + +/** + * Loads an image and resolves the defered promise once it succeeded (or failed) + * @param graphics + * @param url + * @param defer + * @param progressCallback + * @constructor + */ +GameLib.D3.ImageFactory.LoadImage = function (graphics, url, defer, progressCallback) { + + var loader = new graphics.instance.ImageLoader(); + + loader.crossOrigin = ''; + + loader.load( + url, + function (image) { + defer.resolve(image); + }, + function onProgress(xhr) { + if (progressCallback) { + progressCallback((xhr.loaded / xhr.total * 100)); + } + }, + function onError() { + defer.reject('Failed to download image : ' + url); + } + ); +}; + /** * Image * @param id - * @param textureLink * @param filename * @param size * @param contentType @@ -9,17 +71,17 @@ */ GameLib.D3.Image = function( id, - textureLink, filename, size, contentType ) { + if (GameLib.D3.Utils.UndefinedOrNull(id)) { + id = GameLib.D3.Tools.RandomId(); + } this.id = id; this.filename = filename; - this.textureLink = textureLink; - if (typeof size == 'undefined') { size = 0; } @@ -42,4 +104,4 @@ GameLib.D3.Image = function( } } this.contentType = contentType; -}; \ No newline at end of file +}; diff --git a/src/game-lib-light.js b/src/game-lib-light.js index f8fea00..cc307c2 100644 --- a/src/game-lib-light.js +++ b/src/game-lib-light.js @@ -1,5 +1,29 @@ /** - * Light Superset + * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created + * @param id + * @param graphics GameLib.D3.Graphics + * @param apiLight GameLib.D3.Light.API + * @constructor + */ +GameLib.D3.Light = function( + id, + graphics, + apiLight +) { + for (var property in apiLight) { + if (apiLight.hasOwnProperty(property)) { + this[property] = apiLight[property]; + } + } + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + this.instance = this.createInstance(); +}; + +/** + * Raw Light API object - should always correspond with the Light Schema * @param id * @param lightType * @param name @@ -17,7 +41,7 @@ * @param penumbra * @constructor */ -GameLib.D3.Light = function( +GameLib.D3.Light.API = function( id, lightType, name, @@ -34,9 +58,21 @@ GameLib.D3.Light = function( angle, penumbra ) { - this.id = GameLib.D3.Tools.RandomId(); + if (GameLib.D3.Utils.UndefinedOrNull(id)) { + id = GameLib.D3.Tools.RandomId(); + } + this.id = id; + + if (GameLib.D3.Utils.UndefinedOrNull(lightType)) { + lightType = GameLib.D3.Light.LIGHT_TYPE_AMBIENT; + } this.lightType = lightType; + + if (GameLib.D3.Utils.UndefinedOrNull(name)) { + name = 'Light (' + lightType + ')'; + } this.name = name; + this.color = color; this.intensity = intensity; @@ -89,4 +125,56 @@ GameLib.D3.Light = function( penumbra = 0; } this.penumbra = penumbra; +}; + +/** + * Light Types + * @type {number} + */ +GameLib.D3.Light.LIGHT_TYPE_AMBIENT = 0x1; +GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL = 0x2; +GameLib.D3.Light.LIGHT_TYPE_POINT = 0x3; +GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.Light.createInstance = function() { + + var instance = null; + + if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) { + instance = new this.graphics.instance.AmbientLight(this.color, this.intensity); + } + + if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) { + instance = new this.graphics.instance.DirectionalLight(this.color, this.intensity); + } + + if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) { + instance = new this.graphics.instance.PointLight(this.color, this.intensity); + instance.distance = this.distance; + instance.decay = this.decay; + } + + if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT ) { + instance = new this.graphics.instance.SpotLight(this.color, this.intensity); + instance.distance = this.distance; + instance.angle = this.angle; + instance.penumbra = this.penumbra; + instance.decay = this.decay; + } + + instance.gameLibObject = this; + + instance.position.x = this.position.x; + instance.position.y = this.position.y; + instance.position.z = this.position.z; + + instance.rotation.x = this.rotation.x; + instance.rotation.y = this.rotation.y; + instance.rotation.z = this.rotation.z; + + return instance; }; \ No newline at end of file diff --git a/src/game-lib-material.js b/src/game-lib-material.js index c31d8cf..4e1d4f5 100644 --- a/src/game-lib-material.js +++ b/src/game-lib-material.js @@ -1,8 +1,32 @@ /** - * Material Superset + * Material Superset - The apiMaterial properties get moved into the Material object itself, and then the instance is + * created + * @param graphics GameLib.D3.Graphics + * @param apiMaterial GameLib.D3.Material.API + * @constructor + * @returns {GameLib.D3.Material | GameLib.D3.Material.API} + */ +GameLib.D3.Material = function( + graphics, + apiMaterial +) { + for (var property in apiMaterial) { + if (apiMaterial.hasOwnProperty(property)) { + this[property] = apiMaterial[property]; + } + } + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + this.instance = this.createInstance(); +}; + +/** + * Raw material API object - should always correspond with the Material Schema * @param id - * @param name * @param materialType + * @param name * @param opacity * @param side * @param transparent @@ -59,10 +83,10 @@ * @param envMapIntensity * @constructor */ -GameLib.D3.Material = function( +GameLib.D3.Material.API = function( id, - name, materialType, + name, opacity, side, transparent, @@ -118,13 +142,21 @@ GameLib.D3.Material = function( spriteRotation, envMapIntensity ) { + if (GameLib.D3.Utils.UndefinedOrNull(id)) { + id = GameLib.D3.Tools.RandomId(); + } this.id = id; - this.name = name; + if (typeof materialType == 'undefined') { - materialType = GameLib.D3.Material.TYPE_MESH_STANDARD; + materialType = GameLib.D3.Material.MATERIAL_TYPE_STANDARD; } this.materialType = materialType; + if (GameLib.D3.Utils.UndefinedOrNull(name)) { + name = 'Material (' + materialType + ')'; + } + this.name = name; + if (typeof opacity == 'undefined') { opacity = 1.0; } @@ -141,20 +173,7 @@ GameLib.D3.Material = function( this.transparent = transparent; if (typeof maps == 'undefined') { - maps = { - alpha: null, - ao: null, - bump: null, - diffuse: null, - displacement: null, - emissive: null, - environment: null, - light: null, - metalness: null, - normal: null, - roughness: null, - specular: null - }; + maps = new GameLib.D3.TextureMapTemplate.API(); } this.maps = maps; @@ -409,22 +428,6 @@ GameLib.D3.Material = function( this.envMapIntensity = envMapIntensity; }; -/** - * Assigns a copy of the loaded texture map to the instance material - * @param instanceMaterial - * @param instanceMapId - * @returns {Function} - * @constructor - */ -GameLib.D3.Material.ImageLoadedCallback = function( - instanceMaterial, - instanceMapId -) { - return function(texture) { - instanceMaterial[instanceMapId] = texture.clone(); - }; -}; - /** * Combine Method * @type {number} @@ -509,337 +512,140 @@ GameLib.D3.Material.TYPE_SMOOTH_SHADING = 2; * Material Type * @type {string} */ -GameLib.D3.Material.TYPE_LINE_BASIC = "LineBasicMaterial"; -GameLib.D3.Material.TYPE_LINE_DASHED = "LineDashedMaterial"; -GameLib.D3.Material.TYPE_MESH_BASIC = "MeshBasicMaterial"; -GameLib.D3.Material.TYPE_MESH_DEPTH = "MeshDepthMaterial"; -GameLib.D3.Material.TYPE_MESH_LAMBERT = "MeshLambertMaterial"; -GameLib.D3.Material.TYPE_MESH_NORMAL = "MeshNormalMaterial"; -GameLib.D3.Material.TYPE_MESH_PHONG = "MeshPhongMaterial"; -GameLib.D3.Material.TYPE_MESH_STANDARD = "MeshStandardMaterial"; -GameLib.D3.Material.TYPE_POINTS = "PointsMaterial"; -GameLib.D3.Material.TYPE_SPRITE = "SpriteMaterial"; -GameLib.D3.Material.TYPE_MULTI_MATERIAL= "MultiMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_LINE_BASIC = "LineBasicMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_LINE_DASHED = "LineDashedMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_BASIC = "MeshBasicMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_DEPTH = "MeshDepthMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_LAMBERT = "MeshLambertMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_NORMAL = "MeshNormalMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_PHONG = "MeshPhongMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_STANDARD = "MeshStandardMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_POINTS = "PointsMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_SPRITE = "SpriteMaterial"; +GameLib.D3.Material.MATERIAL_TYPE_MULTIPLE = "MultiMaterial"; /** - * Creates an instance Material from a GameLib.D3.Material - * @param apiMaterial GameLib.D3.Material - * @param graphics GameLib.D3.Graphics - * @param gameLibScene GameLib.D3.Scene + * Material instance + * @returns {*} */ -GameLib.D3.Material.CreateInstanceMaterial = function( - apiMaterial, - graphics, - gameLibScene -) { - var maps = apiMaterial.maps; +GameLib.D3.Material.prototype.createInstance = function() { - var gameLibTextures = []; - - for (var map in maps) { - if (maps.hasOwnProperty(map)) { - if (maps[map]) { - var instanceMapId = null; + var instance = null; - if (map == 'alpha') { - instanceMapId = 'alhpaMap'; - } + if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { - if (map == 'ao') { - instanceMapId = 'aoMap'; - } - - if (map == 'bump') { - instanceMapId = 'bumpMap'; - } - - if (map == 'displacement') { - instanceMapId = 'displacementMap'; - } - - if (map == 'emissive') { - instanceMapId = 'emissiveMap'; - } - - if (map == 'environment') { - instanceMapId = 'envMap'; - } - - if (map == 'light') { - instanceMapId = 'lightMap'; - } - - if (map == 'specular') { - instanceMapId = 'specularMap'; - } - - if (map == 'diffuse') { - instanceMapId = 'map'; - } - - if (map == 'roughness') { - instanceMapId = 'roughnessMap'; - } - - if (map == 'metalness') { - instanceMapId = 'metalnessMap'; - } - - if (instanceMapId == null) { - console.warn("unsupported map type : " + map); - } - - var apiTexture = maps[map]; - - var apiImage = apiTexture.image; - - var gameLibTexture = new GameLib.D3.Texture( - apiTexture.id, - apiTexture.name, - new GameLib.D3.Image( - apiImage.id, - apiImage.textureLink, - apiImage.filename, - apiImage.size, - apiImage.contentType - ), - apiTexture.wrapS, - apiTexture.wrapT, - new GameLib.D3.Vector2( - apiTexture.repeat.x, - apiTexture.repeat.y - ), - apiTexture.data, - apiTexture.format, - apiTexture.mapping, - apiTexture.magFilter, - apiTexture.minFilter, - apiTexture.textureType, - apiTexture.anisotropy, - new GameLib.D3.Vector2( - apiTexture.offset.x, - apiTexture.offset.y - ), - apiTexture.generateMipmaps, - apiTexture.flipY, - apiTexture.mipmaps, - apiTexture.unpackAlignment, - apiTexture.premultiplyAlpha, - apiTexture.encoding, - instanceMapId - ); - - gameLibTextures.push(gameLibTexture); - } - } - } - - var gameLibMaterial = new GameLib.D3.Material( - apiMaterial.id, - apiMaterial.name, - apiMaterial.materialType, - apiMaterial.opacity, - apiMaterial.side, - apiMaterial.transparent, - gameLibTextures, - new GameLib.D3.Color( - apiMaterial.specular.r, - apiMaterial.specular.g, - apiMaterial.specular.b, - apiMaterial.specular.a - ), - apiMaterial.lightMapIntensity, - apiMaterial.aoMapIntensity, - new GameLib.D3.Color( - apiMaterial.color.r, - apiMaterial.color.g, - apiMaterial.color.b, - apiMaterial.color.a - ), - new GameLib.D3.Color( - apiMaterial.emissive.r, - apiMaterial.emissive.g, - apiMaterial.emissive.b, - apiMaterial.emissive.a - ), - apiMaterial.emissiveIntensity, - apiMaterial.combine, - apiMaterial.shininess, - apiMaterial.reflectivity, - apiMaterial.refractionRatio, - apiMaterial.fog, - apiMaterial.wireframe, - apiMaterial.wireframeLineWidth, - apiMaterial.wireframeLineCap, - apiMaterial.wireframeLineJoin, - apiMaterial.vertexColors, - apiMaterial.skinning, - apiMaterial.morphTargets, - apiMaterial.morphNormals, - apiMaterial.lineWidth, - apiMaterial.lineCap, - apiMaterial.lineJoin, - apiMaterial.dashSize, - apiMaterial.gapWidth, - apiMaterial.blending, - apiMaterial.blendSrc, - apiMaterial.blendDst, - apiMaterial.blendEquation, - apiMaterial.depthTest, - apiMaterial.depthFunc, - apiMaterial.depthWrite, - apiMaterial.polygonOffset, - apiMaterial.polygonOffsetFactor, - apiMaterial.polygonOffsetUnits, - apiMaterial.alphaTest, - apiMaterial.clippingPlanes, - apiMaterial.clipShadows, - apiMaterial.visible, - apiMaterial.overdraw, - apiMaterial.shading, - apiMaterial.bumpScale, - apiMaterial.normalScale, - apiMaterial.displacementScale, - apiMaterial.displacementBias, - apiMaterial.roughness, - apiMaterial.metalness, - apiMaterial.pointSize, - apiMaterial.pointSizeAttenuation, - apiMaterial.spriteRotation, - apiMaterial.envMapIntensity - ); - - var instanceMaterial = null; - - if (gameLibMaterial.materialType == GameLib.D3.Material.TYPE_MESH_STANDARD) { - - instanceMaterial = new graphics.instance.MeshStandardMaterial({ - name: gameLibMaterial.name, - opacity: gameLibMaterial.opacity, - transparent: gameLibMaterial.transparent, - blending: gameLibMaterial.blending, - blendSrc: gameLibMaterial.blendSrc, - blendDst: gameLibMaterial.blendDst, - blendEquation: gameLibMaterial.blendEquation, - depthTest: gameLibMaterial.depthTest, - depthFunc: gameLibMaterial.depthFunc, - depthWrite: gameLibMaterial.depthWrite, - polygonOffset: gameLibMaterial.polygonOffset, - polygonOffsetFactor: gameLibMaterial.polygonOffsetFactor, - polygonOffsetUnits: gameLibMaterial.polygonOffsetUnits, - alphaTest: gameLibMaterial.alphaTest, - clippingPlanes: gameLibMaterial.clippingPlanes, - clipShadows: gameLibMaterial.clipShadows, - overdraw: gameLibMaterial.overdraw, - visible: gameLibMaterial.visible, - side: gameLibMaterial.side, - color: new graphics.instance.Color( - gameLibMaterial.color.r, - gameLibMaterial.color.g, - gameLibMaterial.color.b + instance = new this.graphics.instance.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: new this.graphics.instance.Color( + this.color.r, + this.color.g, + this.color.b ), - roughness: gameLibMaterial.roughness, - metalness: gameLibMaterial.metalness, - lightMapIntensity: gameLibMaterial.lightMapIntensity, - aoMapIntensity: gameLibMaterial.aoMapIntensity, - emissive: new graphics.instance.Color( - gameLibMaterial.emissive.r, - gameLibMaterial.emissive.g, - gameLibMaterial.emissive.b + roughness: this.roughness, + metalness: this.metalness, + lightMapIntensity: this.lightMapIntensity, + aoMapIntensity: this.aoMapIntensity, + emissive: new this.graphics.instance.Color( + this.emissive.r, + this.emissive.g, + this.emissive.b ), - emissiveIntensity: gameLibMaterial.emissiveIntensity, - bumpScale: gameLibMaterial.bumpScale, - normalScale: gameLibMaterial.normalScale, - displacementScale: gameLibMaterial.displacementScale, - refractionRatio: gameLibMaterial.refractionRatio, - fog: gameLibMaterial.fog, - shading: gameLibMaterial.shading, - wireframe: gameLibMaterial.wireframe, - wireframeLinewidth: gameLibMaterial.wireframeLineWidth, - wireframeLinecap: gameLibMaterial.wireframeLineCap, - wireframeLinejoin: gameLibMaterial.wireframeLineJoin, - vertexColors: gameLibMaterial.vertexColors, - skinning: gameLibMaterial.skinning, - morphTargets: gameLibMaterial.morphTargets, - morphNormals: gameLibMaterial.morphNormals + 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 (gameLibMaterial.materialType == GameLib.D3.Material.TYPE_MESH_PHONG) { + } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_PHONG) { - instanceMaterial = new graphics.instance.MeshPhongMaterial({ - name: gameLibMaterial.name, - opacity: gameLibMaterial.opacity, - transparent: gameLibMaterial.transparent, - blending: gameLibMaterial.blending, - blendSrc: gameLibMaterial.blendSrc, - blendDst: gameLibMaterial.blendDst, - blendEquation: gameLibMaterial.blendEquation, - depthTest: gameLibMaterial.depthTest, - depthFunc: gameLibMaterial.depthFunc, - depthWrite: gameLibMaterial.depthWrite, - polygonOffset: gameLibMaterial.polygonOffset, - polygonOffsetFactor: gameLibMaterial.polygonOffsetFactor, - polygonOffsetUnits: gameLibMaterial.polygonOffsetUnits, - alphaTest: gameLibMaterial.alphaTest, - clippingPlanes: gameLibMaterial.clippingPlanes, - clipShadows: gameLibMaterial.clipShadows, - overdraw: gameLibMaterial.overdraw, - visible: gameLibMaterial.visible, - side: gameLibMaterial.side, - color: new graphics.instance.Color( - gameLibMaterial.color.r, - gameLibMaterial.color.g, - gameLibMaterial.color.b + instance = new this.graphics.instance.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: new this.graphics.instance.Color( + this.color.r, + this.color.g, + this.color.b ), - specular: new graphics.instance.Color( - gameLibMaterial.specular.r, - gameLibMaterial.specular.g, - gameLibMaterial.specular.b + specular: new this.graphics.instance.Color( + this.specular.r, + this.specular.g, + this.specular.b ), - shininess: gameLibMaterial.shininess, - lightMapIntensity: gameLibMaterial.lightMapIntensity, - aoMapIntensity: gameLibMaterial.aoMapIntensity, - emissive: new graphics.instance.Color( - gameLibMaterial.emissive.r, - gameLibMaterial.emissive.g, - gameLibMaterial.emissive.b + shininess: this.shininess, + lightMapIntensity: this.lightMapIntensity, + aoMapIntensity: this.aoMapIntensity, + emissive: new this.graphics.instance.Color( + this.emissive.r, + this.emissive.g, + this.emissive.b ), - emissiveIntensity: gameLibMaterial.emissiveIntensity, - bumpScale: gameLibMaterial.bumpScale, - normalScale: gameLibMaterial.normalScale, - displacementScale: gameLibMaterial.displacementScale, - combine: gameLibMaterial.combine, - refractionRatio: gameLibMaterial.refractionRatio, - fog: gameLibMaterial.fog, - shading: gameLibMaterial.shading, - wireframe: gameLibMaterial.wireframe, - wireframeLinewidth: gameLibMaterial.wireframeLineWidth, - wireframeLinecap: gameLibMaterial.wireframeLineCap, - wireframeLinejoin: gameLibMaterial.wireframeLineJoin, - vertexColors: gameLibMaterial.vertexColors, - skinning: gameLibMaterial.skinning, - morphTargets: gameLibMaterial.morphTargets, - morphNormals: gameLibMaterial.morphNormals + 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 { - console.log("material type is not implemented yet: " + apiMaterial.materialType + " - material indexes could be screwed up"); + console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up"); } - /** - * Here we need to notify the scene of all the textures which need to be created once image loading for the - * textures are complete. (this download process will be started at a later stage) - * The Notification objects take care of applying the loaded instance texture(s) to the instance material - * once it is loaded - */ - for (var t = 0; t < gameLibTextures.length; t++) { - gameLibScene.notify( - new GameLib.D3.Texture.Notification( - gameLibTextures[t], - instanceMaterial - ) - ) - } - - return instanceMaterial; + return instance; }; \ No newline at end of file diff --git a/src/game-lib-mesh.js b/src/game-lib-mesh.js index d4a99bf..2dfd6f4 100644 --- a/src/game-lib-mesh.js +++ b/src/game-lib-mesh.js @@ -1,54 +1,110 @@ /** - * Mesh Superset - * @param id - * @param path - * @param name - * @param meshType - * @param vertices - * @param faces - * @param skeleton - * @param faceVertexUvs - * @param skinIndices - * @param skinWeights - * @param materials - * @param position - * @param quaternion - * @param rotation - * @param scale - * @param up - * @param physics - * @param parentMeshId - * @param parentSceneId + * Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created + * @param id String + * @param name String + * @param graphics GameLib.D3.Graphics + * @param computeNormals Boolean + * @param apiMesh GameLib.D3.Mesh.API * @constructor */ GameLib.D3.Mesh = function( id, - path, name, + graphics, + computeNormals, + apiMesh +) { + + for (var property in apiMesh) { + if (apiMesh.hasOwnProperty(property)) { + this[property] = apiMesh[property]; + } + } + + this.graphics = graphics; + + this.graphics.isNotThreeThrow(); + + this.computeNormals = computeNormals; + + this.instance = this.createInstance(); +}; + +/** + * Raw Mesh API object - should always correspond with the Mesh Schema + * @param id + * @param meshType + * @param name + * @param vertices GameLib.D3.Vertex[] + * @param faces GameLib.D3.TriangleFace[] + * @param faceVertexUvs + * @param materials GameLib.D3.Material.API[] + * @param parentMeshId + * @param parentSceneId + * @param skeleton + * @param skinIndices + * @param skinWeights + * @param position GameLib.D3.Vector3 + * @param quaternion GameLib.D3.Vector4 + * @param rotation GameLib.D3.Vector3 + * @param scale GameLib.D3.Vector3 + * @param up + * @constructor + */ +GameLib.D3.Mesh.API = function( + id, meshType, + name, vertices, faces, - skeleton, faceVertexUvs, + materials, + parentMeshId, + parentSceneId, + skeleton, skinIndices, skinWeights, - materials, position, quaternion, rotation, scale, - up, - physics, - parentMeshId, - parentSceneId + up ) { - this.id = GameLib.D3.Tools.RandomId(); - this.path = path; - this.name = name; + if (GameLib.D3.Utils.UndefinedOrNull(id)) { + id = GameLib.D3.Tools.RandomId(); + } + this.id = id; + + if (GameLib.D3.Utils.UndefinedOrNull(meshType)) { + meshType = GameLib.D3.Mesh.TYPE_NORMAL; + } this.meshType = meshType; + + if (GameLib.D3.Utils.UndefinedOrNull(name)) { + name = 'Mesh (' + meshType + ')'; + } + this.name = name; + + if (GameLib.D3.Utils.UndefinedOrNull(vertices)) { + throw new Error('Cannot create a mesh with no vertices'); + } this.vertices = vertices; + + if (GameLib.D3.Utils.UndefinedOrNull(faces)) { + throw new Error('Cannot create a mesh with no faces'); + } this.faces = faces; + if (typeof parentMeshId == 'undefined') { + parentMeshId = null; + } + this.parentMeshId = parentMeshId; + + if (typeof parentSceneId == 'undefined') { + parentSceneId = null; + } + this.parentSceneId = parentSceneId; + if (typeof skeleton == 'undefined') { skeleton = null; } @@ -98,15 +154,8 @@ GameLib.D3.Mesh = function( up = new GameLib.D3.Vector3(0,1,0); } this.up = up; - - this.physics = physics; - - this.parentMeshId = parentMeshId; - - this.parentSceneId = parentSceneId; }; - /** * Mesh Type * @type {number} @@ -114,36 +163,149 @@ GameLib.D3.Mesh = function( GameLib.D3.Mesh.TYPE_NORMAL = 0; GameLib.D3.Mesh.TYPE_SKINNED = 1; - /** - * Creates a THREE Mesh from GameLib.D3.Mesh - * @param gameLibMesh GameLib.D3.Mesh - * @param instanceGeometry - * @param instanceMaterial - * @param graphics - * @returns {*} + * Creates a mesh instance */ -GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, instanceMaterial, graphics) { +GameLib.D3.Mesh.prototype.createInstance = function() { - var threeMesh = null; + var instance = null; - if (gameLibMesh.meshType == GameLib.D3.Mesh.TYPE_NORMAL) { - threeMesh = new graphics.instance.Mesh(instanceGeometry, instanceMaterial); + var instanceGeometry = new this.graphics.instance.Geometry(); + + /** + * Setup vertices + */ + for (var v = 0; v < this.vertices.length; v++) { + instanceGeometry.vertices.push( + new this.graphics.instance.Vector3( + this.vertices[v].position.x, + this.vertices[v].position.y, + this.vertices[v].position.z + ) + ) } - if (gameLibMesh.meshType == GameLib.D3.Mesh.TYPE_SKINNED) { + /** + * Setup faces + */ + for (var f = 0; f < this.faces.length; f++) { - var bones = gameLibMesh.skeleton.bones; + var face = new this.graphics.instance.Face3( + this.faces[f].v0, + this.faces[f].v1, + this.faces[f].v2, + new this.graphics.instance.Vector3( + this.faces[f].normal.x, + this.faces[f].normal.y, + this.faces[f].normal.z + ), + new this.graphics.instance.Color( + this.faces[f].color.r, + this.faces[f].color.g, + this.faces[f].color.b + ), + this.faces[f].materialIndex + ); - var skinIndices = gameLibMesh.skinIndices; + face.vertexColors = [ + new this.graphics.instance.Color( + this.faces[f].vertexColors[0].r, + this.faces[f].vertexColors[0].g, + this.faces[f].vertexColors[0].b + ), + new this.graphics.instance.Color( + this.faces[f].vertexColors[1].r, + this.faces[f].vertexColors[1].g, + this.faces[f].vertexColors[1].b + ), + new this.graphics.instance.Color( + this.faces[f].vertexColors[2].r, + this.faces[f].vertexColors[2].g, + this.faces[f].vertexColors[2].b + ) + ]; - var skinWeights = gameLibMesh.skinWeights; + face.normal = new this.graphics.instance.Vector3( + this.faces[f].normal.x, + this.faces[f].normal.y, + this.faces[f].normal.z + ); - var threeBones = []; + face.vertexNormals = [ + new this.graphics.instance.Vector3( + this.faces[f].vertexNormals[0].x, + this.faces[f].vertexNormals[0].y, + this.faces[f].vertexNormals[0].z + ), + new this.graphics.instance.Vector3( + this.faces[f].vertexNormals[1].x, + this.faces[f].vertexNormals[1].y, + this.faces[f].vertexNormals[1].z + ), + new this.graphics.instance.Vector3( + this.faces[f].vertexNormals[2].x, + this.faces[f].vertexNormals[2].y, + this.faces[f].vertexNormals[2].z + ) + ]; + + instanceGeometry.faces.push(face); + } + + instanceGeometry.faceVertexUvs = []; + + /** + * Setup face UVs + */ + for (var fm = 0; fm < this.faceVertexUvs.length; fm++) { + + var faceMaterialVertexUvs = this.faceVertexUvs[fm]; + + instanceGeometry.faceVertexUvs[fm] = []; + + for (var fuv = 0; fuv < faceMaterialVertexUvs.length; fuv++) { + instanceGeometry.faceVertexUvs[fm][fuv] = []; + instanceGeometry.faceVertexUvs[fm][fuv].push( + new this.graphics.instance.Vector2( + faceMaterialVertexUvs[fuv][0].x, + faceMaterialVertexUvs[fuv][0].y + ), + new this.graphics.instance.Vector2( + faceMaterialVertexUvs[fuv][1].x, + faceMaterialVertexUvs[fuv][1].y + ), + new this.graphics.instance.Vector2( + faceMaterialVertexUvs[fuv][2].x, + faceMaterialVertexUvs[fuv][2].y + ) + ); + } + } + + /** + * Re-calculate normals (if we have to) + * @type {Array} + */ + if (this.computeNormals) { + instanceGeometry.computeFaceNormals(); + instanceGeometry.computeVertexNormals(); + } + + var instanceMaterial = this.materials[0].instance; + + if (this.meshType == GameLib.D3.Mesh.TYPE_NORMAL) { + instance = new this.graphics.instance.Mesh(instanceGeometry, instanceMaterial); + } + + if (this.meshType == GameLib.D3.Mesh.TYPE_SKINNED) { + + var bones = this.skeleton.bones; + + var instanceBones = []; for (var bi = 0; bi < bones.length; bi++) { - var bone = new graphics.instance.Bone(); + var bone = new this.graphics.instance.Bone(); bone.name = bones[bi].name; @@ -168,7 +330,7 @@ GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, ins bone.up.y = bones[bi].up.y; bone.up.z = bones[bi].up.z; - threeBones.push(bone); + instanceBones.push(bone); } /** @@ -176,20 +338,20 @@ GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, ins */ for (var br = 0; br < bones.length; br++) { for (var cbi = 0; cbi < bones[br].childBoneIds.length; cbi++) { - threeBones[br].add(threeBones[bones[br].childBoneIds[cbi]]); + instanceBones[br].add(instanceBones[bones[br].childBoneIds[cbi]]); } } /** * Setup bones (indexes) */ - for (var si = 0; si < skinIndices.length; si++) { + for (var si = 0; si < this.skinIndices.length; si++) { instanceGeometry.skinIndices.push( - new graphics.instance.Vector4( - skinIndices[si].x, - skinIndices[si].y, - skinIndices[si].z, - skinIndices[si].w + new this.graphics.instance.Vector4( + this.skinIndices[si].x, + this.skinIndices[si].y, + this.skinIndices[si].z, + this.skinIndices[si].w ) ); } @@ -197,45 +359,66 @@ GameLib.D3.Mesh.CreateInstanceMesh = function(gameLibMesh, instanceGeometry, ins /** * Setup bones (weights) */ - for (var sw = 0; sw < skinWeights.length; sw++) { + for (var sw = 0; sw < this.skinWeights.length; sw++) { instanceGeometry.skinWeights.push( - new graphics.instance.Vector4( - skinWeights[sw].x, - skinWeights[sw].y, - skinWeights[sw].z, - skinWeights[sw].w + new this.graphics.instance.Vector4( + this.skinWeights[sw].x, + this.skinWeights[sw].y, + this.skinWeights[sw].z, + this.skinWeights[sw].w ) ); } - threeMesh = new graphics.instance.SkinnedMesh(instanceGeometry, instanceMaterial); + instance = new this.graphics.instance.SkinnedMesh(instanceGeometry, instanceMaterial); - var skeleton = new graphics.instance.Skeleton(threeBones); + var skeleton = new this.graphics.instance.Skeleton(instanceBones); - skeleton.useVertexTexture = gameLibMesh.skeleton.useVertexTexture; + skeleton.useVertexTexture = this.skeleton.useVertexTexture; for (var i = 0; i < bones.length; i++) { if (bones[i].parentBoneId === null) { - threeMesh.add(threeBones[i]); + instance.add(instanceBones[i]); break; } } - threeMesh.bind(skeleton); + instance.bind(skeleton); - threeMesh.pose(); + instance.pose(); - threeMesh.skeleton.skeletonHelper = new graphics.instance.SkeletonHelper(threeMesh); - threeMesh.skeleton.skeletonHelper.material.linewidth = 5; + instance.skeleton.skeletonHelper = new this.graphics.instance.SkeletonHelper(instance); + instance.skeleton.skeletonHelper.material.linewidth = 5; } - if (threeMesh == null) { - console.log('cannot handle meshes of type ' + gameLibMesh.meshType + ' yet.'); + if (instance == null) { + console.log('cannot handle meshes of type ' + this.meshType + ' yet.'); } - gameLibMesh.threeMeshId = threeMesh.id; + this.threeMeshId = instance.id; - return threeMesh; + instance.name = this.name; + + instance.gameLibObject = this; + + instance.position.x = this.position.x; + instance.position.y = this.position.y; + instance.position.z = this.position.z; + + instance.rotation.x = this.rotation.x; + instance.rotation.y = this.rotation.y; + instance.rotation.z = this.rotation.z; + + instance.scale.x = this.scale.x; + instance.scale.y = this.scale.y; + instance.scale.z = this.scale.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; + + return instance; }; GameLib.D3.prototype.invertWindingOrder = function(triangles) { diff --git a/src/game-lib-scene.js b/src/game-lib-scene.js index c769388..8e69ff3 100644 --- a/src/game-lib-scene.js +++ b/src/game-lib-scene.js @@ -1,28 +1,59 @@ /** - * Scenes are objects putting meshes into 'world space' - * @param id Mongo.ObjectId + * Scene Superset - The apiScene properties get moved into the Scene object itself, and then the instance is + * created + * @param graphics + * @param progressCallback + * @param apiScene GameLib.D3.Scene.API + * @constructor + */ +GameLib.D3.Scene = function( + graphics, + progressCallback, + apiScene +) { + + for (var property in apiScene) { + if (apiScene.hasOwnProperty(property)) { + this[property] = apiScene[property]; + } + } + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + // assoc array + this.meshIdToMesh = {}; + + // assoc array + this.idToComponent = {}; + + this.progressCallback = progressCallback; + + this.instance = this.createInstance(); +}; + +/** + * Raw Scene API object - should always correspond with the Scene Schema + * @param id String * @param path String * @param name String - * @param graphics GameLib.D3.Graphics - * @param meshes GameLib.D3.Mesh[] + * @param meshes GameLib.D3.Mesh.API [] * @param quaternion GameLib.D3.Vector4 * @param position GameLib.D3.Vector3 * @param rotation GameLib.D3.Vector3 * @param scale GameLib.D3.Vector3 - * @param parentSceneId String - * @param lights GameLib.D3.Light[] - * @param worlds GameLib.D3.World[] - * @param entities GameLib.D3.Entity[] - * @param progressCallback - * @param uploadUrl String - * @param components + * @param parentSceneId + * @param lights GameLib.D3.Light.API[] + * @param worlds GameLib.D3.World.API[] + * @param entities GameLib.D3.Entity.API[] + * @param components GameLib.D3.Component.API[] + * @param shapes GameLib.D3.Shape.API[] * @constructor */ -GameLib.D3.Scene = function( +GameLib.D3.Scene.API = function( id, path, name, - graphics, meshes, quaternion, position, @@ -32,28 +63,24 @@ GameLib.D3.Scene = function( lights, worlds, entities, - progressCallback, - uploadUrl, components, shapes ) { + if (GameLib.D3.Utils.UndefinedOrNull(id)) { + id = GameLib.D3.Tools.RandomId(); + } this.id = id; - this.sceneId = GameLib.D3.Tools.RandomId(); + + if (GameLib.D3.Utils.UndefinedOrNull(path)) { + path = null; + } this.path = path; + + if (GameLib.D3.Utils.UndefinedOrNull(name)) { + name = 'unnamed'; + } this.name = name; - - this.graphics = graphics; - - if (graphics) { - this.graphics.isNotThreeThrow(); - this.textureLoader = new this.graphics.instance.TextureLoader(); - this.textureLoader.crossOrigin = ''; - } - - if (this.name.trim() == "") { - this.name = 'unnamed'; - } - + if (typeof meshes == 'undefined') { meshes = []; } @@ -99,8 +126,6 @@ GameLib.D3.Scene = function( } this.entities = entities; - this.instance = this.createInstance(); - if (typeof components == 'undefined') { components = []; } @@ -109,166 +134,363 @@ GameLib.D3.Scene = function( if (typeof shapes == 'undefined') { shapes = []; } - this.components = shapes; - - // assoc array - this.meshIdToMesh = {}; - - // assoc array - this.idToComponent = {}; - - this.uploadUrl = uploadUrl; - - this.progressCallback = progressCallback; - - this.urls = []; - this.notifications = []; + this.shapes = shapes; }; +/** + * Creates an instance scene + * @returns {GameLib.D3.Scene|THREE.Scene|ApiLib.Scene|*|Scene} + */ GameLib.D3.Scene.prototype.createInstance = function() { - this.graphics.isNotThreeThrow(); - var instance = new this.graphics.instance.Scene(); + instance.name = this.name; + + instance.position.x = this.position.x; + instance.position.y = this.position.y; + instance.position.z = this.position.z; + + instance.rotation.x = this.rotation.x; + instance.rotation.y = this.rotation.y; + instance.rotation.z = this.rotation.z; + + instance.scale.x = this.scale.x; + instance.scale.y = this.scale.y; + instance.scale.z = this.scale.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; + + for (var im = 0; im < this.meshes.length; im++) { + instance.add(this.meshes[im].instance); + } + + for (var l = 0; l < this.lights.length; l++) { + instance.add(this.lights[l].instance); + } + instance.render = true; return instance; }; /** - * Notification objects manage the links between image filenames, instance texture - * map IDs, and instance materials. - * @param notification GameLib.D3.Texture.Notificatio + * Transforms raw scene data into a GameLib.D3.Scene + * @param scene Object (as it comes from the API) + * @param onLoaded + * @param graphics + * @param uploadUrl + * @param progressCallback * @constructor */ -GameLib.D3.Scene.prototype.notify = function( - notification +GameLib.D3.Scene.LoadScene = function( + scene, + onLoaded, + graphics, + uploadUrl, + progressCallback ) { - var url = this.uploadUrl + '/' + notification.gameLibTexture.image.filename; + var entities = []; - if (this.urls.indexOf(url) == -1) { - this.urls.push(url); + var worlds = []; + + var components = []; + + var shapes = []; + + if (scene.worlds && scene.worlds.length > 0) { + console.warn('Implement physics worlds code here'); } - this.notifications.push(notification); -}; + var lights3d = []; -/** - * Promises to loads a texture - * @param url - * @returns {promise.promise|jQuery.promise|*|promise|Promise} - */ -GameLib.D3.Scene.prototype.loadTexture = function(url) { + if (scene.lights && scene.lights.length > 0) { + for (var l = 0; l < scene.lights.length; l++) { - var defer = Q.defer(); + var light = scene.lights[l]; - this.textureLoader.load( - url, - function onLoad(textureInstance){ - textureInstance.myUrl = url; - defer.resolve(textureInstance); - }, - function onProgress(xhr){ - if (this.progressCallback) { - this.progressCallback(Math.round(xhr.loaded / xhr.total * 100)) - } - }, - function onError() { - defer.resolve(null); + 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 + ); + + lights3d.push(light3d); } - ); - - return defer.promise; -}; - -/** - * Starts a Image Loading Process - when all are done - executes all calbacks registered - * on materials listening for an image load to complete - * @param urls - * @param onLoadedCallback - * @returns {jQuery.promise|promise.promise|*|Promise|promise} - */ -GameLib.D3.Scene.prototype.loadTextures = function(urls, onLoadedCallback) { - - var texturesToLoad = []; - - for (var u = 0; u < urls.length; u++) { - texturesToLoad.push(this.loadTexture(urls[u])); } - Q.all(texturesToLoad).then( + var gameLibMeshes = []; - function onFulfilled(textureInstances) { + for (var m = 0; m < scene.meshes.length; m++) { - var loadedTexturesInstances = []; + var apiMesh = scene.meshes[m]; - for (var t = 0; t < textureInstances.length; t++) { + var gameLibMaterials = []; - var instanceTexture = textureInstances[t]; + for (var mat = 0; mat < apiMesh.materials.length; mat++) { - if (!instanceTexture) { - continue; - } + var apiMaterial = apiMesh.materials[mat]; - for (var n = 0; n < this.notifications.length; n++) { + var gameLibMaterial = new GameLib.D3.Material( + graphics, + new GameLib.D3.Material.API( + apiMaterial.id, + apiMaterial.materialType, + apiMaterial.name, + apiMaterial.opacity, + apiMaterial.side, + apiMaterial.transparent, + null, + new GameLib.D3.Color( + apiMaterial.specular.r, + apiMaterial.specular.g, + apiMaterial.specular.b, + apiMaterial.specular.a + ), + apiMaterial.lightMapIntensity, + apiMaterial.aoMapIntensity, + new GameLib.D3.Color( + apiMaterial.color.r, + apiMaterial.color.g, + apiMaterial.color.b, + apiMaterial.color.a + ), + new GameLib.D3.Color( + apiMaterial.emissive.r, + apiMaterial.emissive.g, + apiMaterial.emissive.b, + apiMaterial.emissive.a + ), + apiMaterial.emissiveIntensity, + apiMaterial.combine, + apiMaterial.shininess, + apiMaterial.reflectivity, + apiMaterial.refractionRatio, + apiMaterial.fog, + apiMaterial.wireframe, + apiMaterial.wireframeLineWidth, + apiMaterial.wireframeLineCap, + apiMaterial.wireframeLineJoin, + apiMaterial.vertexColors, + apiMaterial.skinning, + apiMaterial.morphTargets, + apiMaterial.morphNormals, + apiMaterial.lineWidth, + apiMaterial.lineCap, + apiMaterial.lineJoin, + apiMaterial.dashSize, + apiMaterial.gapWidth, + apiMaterial.blending, + apiMaterial.blendSrc, + apiMaterial.blendDst, + apiMaterial.blendEquation, + apiMaterial.depthTest, + apiMaterial.depthFunc, + apiMaterial.depthWrite, + apiMaterial.polygonOffset, + apiMaterial.polygonOffsetFactor, + apiMaterial.polygonOffsetUnits, + apiMaterial.alphaTest, + apiMaterial.clippingPlanes, + apiMaterial.clipShadows, + apiMaterial.visible, + apiMaterial.overdraw, + apiMaterial.shading, + apiMaterial.bumpScale, + apiMaterial.normalScale, + apiMaterial.displacementScale, + apiMaterial.displacementBias, + apiMaterial.roughness, + apiMaterial.metalness, + apiMaterial.pointSize, + apiMaterial.pointSizeAttenuation, + apiMaterial.spriteRotation, + apiMaterial.envMapIntensity + ) + ); - var notification = this.notifications[n]; + var apiMaps = apiMaterial.maps; - var gameLibTexture = notification.gameLibTexture; + var gameLibTextureMap = new GameLib.D3.TextureMapTemplate(graphics); - var instanceMaterial = notification.instanceMaterial; + for (var map in gameLibTextureMap) { - var instanceMapId = gameLibTexture.instanceMapId; + if (gameLibTextureMap.hasOwnProperty(map) && apiMaps[map] && apiMaps[map].texture) { - var filename = gameLibTexture.image.filename; + var apiTexture = apiMaps[map].texture; - if (instanceTexture.myUrl.match(new RegExp(filename + '$'))) { - instanceMaterial[instanceMapId] = new this.graphics.instance.Texture( - instanceTexture.image, - gameLibTexture.mapping, - gameLibTexture.wrapS, - gameLibTexture.wrapT, - gameLibTexture.magFilter, - gameLibTexture.minFilter - //gameLibTexture.format, - //gameLibTexture.textureType, - //gameLibTexture.anisotropy - ); - - instanceMaterial[instanceMapId].name = gameLibTexture.name; - instanceMaterial[instanceMapId].flipY = gameLibTexture.flipY; - instanceMaterial[instanceMapId].encoding = gameLibTexture.encoding; - instanceMaterial[instanceMapId].flipY = gameLibTexture.flipY; - instanceMaterial[instanceMapId].offset.x = gameLibTexture.offset.x; - instanceMaterial[instanceMapId].offset.y = gameLibTexture.offset.y; - instanceMaterial[instanceMapId].mipmaps = gameLibTexture.mipmaps; - instanceMaterial[instanceMapId].unpackAlignment = gameLibTexture.unpackAlignment; - instanceMaterial[instanceMapId].premultiplyAlpha = gameLibTexture.premultiplyAlpha; - instanceMaterial[instanceMapId].needsUpdate = true; - instanceMaterial.needsUpdate = true; - - loadedTexturesInstances.push(instanceMaterial[instanceMapId]); - } + gameLibTextureMap[map].texture = new GameLib.D3.Texture( + new GameLib.D3.Texture.API( + apiTexture.id, + apiTexture.name, + uploadUrl + '/' + apiTexture.imagePath, + apiTexture.wrapS, + apiTexture.wrapT, + new GameLib.D3.Vector2( + apiTexture.repeat.x, + apiTexture.repeat.y + ), + apiTexture.data, + apiTexture.format, + apiTexture.mapping, + apiTexture.magFilter, + apiTexture.minFilter, + apiTexture.textureType, + apiTexture.anisotropy, + new GameLib.D3.Vector2( + apiTexture.offset.x, + apiTexture.offset.y + ), + apiTexture.generateMipmaps, + apiTexture.flipY, + apiTexture.mipmaps, + apiTexture.unpackAlignment, + apiTexture.premultiplyAlpha, + apiTexture.encoding + ), + graphics, + gameLibMaterial, + gameLibTextureMap[map].instanceMapId + ); } } - onLoadedCallback(loadedTexturesInstances); + gameLibMaterial.maps = gameLibTextureMap; - }.bind(this), - function onRejected(message){ - console.log('Promise failed : ' + message); - }, - function onProgress(){ - console.log('progress'); + gameLibMaterials.push(gameLibMaterial); } + + var gameLibMesh = new GameLib.D3.Mesh( + apiMesh.id, + apiMesh.name, + graphics, + false, + apiMesh.meshType, + apiMesh.vertices, + apiMesh.faces, + apiMesh.skeleton, + apiMesh.faceVertexUvs, + apiMesh.skinIndices, + apiMesh.skinWeights, + gameLibMaterials, + new GameLib.D3.Vector3( + apiMesh.position.x, + apiMesh.position.y, + apiMesh.position.z + ), + new GameLib.D3.Vector3( + apiMesh.quaternion.w, + apiMesh.quaternion.x, + apiMesh.quaternion.y, + apiMesh.quaternion.z + ), + new GameLib.D3.Vector3( + apiMesh.rotation.x, + apiMesh.rotation.y, + apiMesh.rotation.z + ), + new GameLib.D3.Vector3( + apiMesh.scale.x, + apiMesh.scale.y, + apiMesh.scale.z + ), + new GameLib.D3.Vector3( + apiMesh.up.x, + apiMesh.up.y, + apiMesh.up.z + ), + apiMesh.parentMeshId, + apiMesh.parentSceneId + ); + + gameLibMeshes.push(gameLibMesh); + } + + var scene3d = new GameLib.D3.Scene( + graphics, + progressCallback, + new GameLib.D3.Scene.API( + scene.id, + scene.path, + scene.name, + scene.meshes, + new GameLib.D3.Vector4( + scene.quaternion.x, + scene.quaternion.y, + scene.quaternion.z, + scene.quaternion.w + ), + new GameLib.D3.Vector3( + scene.position.x, + scene.position.y, + scene.position.z + ), + new GameLib.D3.Vector3( + scene.rotation.x, + scene.rotation.y, + scene.rotation.z + ), + new GameLib.D3.Vector3( + scene.scale.x, + scene.scale.y, + scene.scale.z + ), + scene.parentSceneId, + lights3d, + worlds, + entities, + components, + shapes + ) ); + onLoaded(scene3d); }; /** * Loads a scene directly from the API - * @param gameLibScene GameLib.D3.Scene + * @param partialSceneObject Object {path: '', name: ''} * @param onLoaded callback * @param graphics GameLib.D3.Graphics * @param uploadUrl String @@ -276,7 +498,7 @@ GameLib.D3.Scene.prototype.loadTextures = function(urls, onLoadedCallback) { * @param apiUrl */ GameLib.D3.Scene.LoadSceneFromApi = function( - gameLibScene, + partialSceneObject, onLoaded, graphics, uploadUrl, @@ -295,11 +517,13 @@ GameLib.D3.Scene.LoadSceneFromApi = function( var xhr = new XMLHttpRequest(); xhr.open( 'GET', - apiUrl + '/scene/load' + gameLibScene.path + '/' + gameLibScene.name + apiUrl + '/scene/load' + partialSceneObject.path + '/' + partialSceneObject.name ); xhr.onreadystatechange = function(xhr) { + return function() { + if (xhr.readyState == 4) { var response = JSON.parse(xhr.responseText); @@ -310,114 +534,7 @@ GameLib.D3.Scene.LoadSceneFromApi = function( var scene = response.scene[0]; - var entities = []; - - var worlds = []; - - var components = []; - - if (scene.worlds && scene.worlds.length > 0) { - console.warn('Implement physics worlds code here'); - //TODO: physics world code here - } - - var lights3d = []; - - if (scene.lights && scene.lights.length > 0) { - for (var l = 0; l < scene.lights.length; l++) { - - var light = 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 - ); - - lights3d.push(light3d); - } - } - - var scene3d = new GameLib.D3.Scene( - scene._id || scene.id, - scene.path, - scene.name, - graphics, - scene.meshes, - new GameLib.D3.Vector4( - scene.quaternion.x, - scene.quaternion.y, - scene.quaternion.z, - scene.quaternion.w - ), - new GameLib.D3.Vector3( - scene.position.x, - scene.position.y, - scene.position.z - ), - new GameLib.D3.Vector3( - scene.rotation.x, - scene.rotation.y, - scene.rotation.z - ), - new GameLib.D3.Vector3( - scene.scale.x, - scene.scale.y, - scene.scale.z - ), - scene.parentSceneId, - lights3d, - worlds, - entities, - progressCallback, - uploadUrl, - components - ); - - GameLib.D3.Scene.LoadScene( - scene3d, - onLoaded, - false, - graphics - ); + GameLib.D3.Scene.LoadScene(scene, onLoaded, graphics, uploadUrl, progressCallback); } } }(xhr); @@ -425,301 +542,6 @@ GameLib.D3.Scene.LoadSceneFromApi = function( xhr.send(); }; -/** - * Loads a GameLib.D3.Scene object into a Graphics Instance Scene object - * @param gameLibScene GameLib.D3.Scene - * @param onLoaded callback when all meshes have loaded - * @param computeNormals set to true to compute new face and vertex normals during load - * @param graphics GameLib.D3.Graphics - */ -GameLib.D3.Scene.LoadScene = function( - gameLibScene, - onLoaded, - computeNormals, - graphics -) { - - console.log("loading scene " + gameLibScene.name); - - if (typeof onLoaded == 'undefined') { - console.warn('No on loaded callback has been specified'); - throw new Error('No on loaded callback has been specified'); - } - - graphics.isNotThreeThrow(); - - var instanceMeshes = []; - - for (var m = 0; m < gameLibScene.meshes.length; m++) { - - var mesh = gameLibScene.meshes[m]; - - console.log("loading mesh " + mesh.name); - - var geometry = new graphics.instance.Geometry(); - - var vertices = mesh.vertices; - - var faces = mesh.faces; - - var faceVertexUvs = mesh.faceVertexUvs; - - var materials = mesh.materials; - - /** - * Setup vertices - */ - for (var v = 0; v < vertices.length; v++) { - geometry.vertices.push( - new graphics.instance.Vector3( - vertices[v].position.x, - vertices[v].position.y, - vertices[v].position.z - ) - ) - } - - /** - * Setup faces - */ - for (var f = 0; f < faces.length; f++) { - - var face = new graphics.instance.Face3( - faces[f].v0, - faces[f].v1, - faces[f].v2, - new graphics.instance.Vector3( - faces[f].normal.x, - faces[f].normal.y, - faces[f].normal.z - ), - new graphics.instance.Color( - faces[f].color.r, - faces[f].color.g, - faces[f].color.b - ), - faces[f].materialIndex - ); - - face.vertexColors = [ - new graphics.instance.Color( - faces[f].vertexColors[0].r, - faces[f].vertexColors[0].g, - faces[f].vertexColors[0].b - ), - new graphics.instance.Color( - faces[f].vertexColors[1].r, - faces[f].vertexColors[1].g, - faces[f].vertexColors[1].b - ), - new graphics.instance.Color( - faces[f].vertexColors[2].r, - faces[f].vertexColors[2].g, - faces[f].vertexColors[2].b - ) - ]; - - face.normal = new graphics.instance.Vector3( - faces[f].normal.x, - faces[f].normal.y, - faces[f].normal.z - ); - - face.vertexNormals = [ - new graphics.instance.Vector3( - faces[f].vertexNormals[0].x, - faces[f].vertexNormals[0].y, - faces[f].vertexNormals[0].z - ), - new graphics.instance.Vector3( - faces[f].vertexNormals[1].x, - faces[f].vertexNormals[1].y, - faces[f].vertexNormals[1].z - ), - new graphics.instance.Vector3( - faces[f].vertexNormals[2].x, - faces[f].vertexNormals[2].y, - faces[f].vertexNormals[2].z - ) - ]; - - geometry.faces.push(face); - } - - geometry.faceVertexUvs = []; - - /** - * Setup face UVs - */ - for (var fm = 0; fm < faceVertexUvs.length; fm++) { - - var faceMaterialVertexUvs = faceVertexUvs[fm]; - - geometry.faceVertexUvs[fm] = []; - - for (var fuv = 0; fuv < faceMaterialVertexUvs.length; fuv++) { - geometry.faceVertexUvs[fm][fuv] = []; - geometry.faceVertexUvs[fm][fuv].push( - new graphics.instance.Vector2( - faceMaterialVertexUvs[fuv][0].x, - faceMaterialVertexUvs[fuv][0].y - ), - new graphics.instance.Vector2( - faceMaterialVertexUvs[fuv][1].x, - faceMaterialVertexUvs[fuv][1].y - ), - new graphics.instance.Vector2( - faceMaterialVertexUvs[fuv][2].x, - faceMaterialVertexUvs[fuv][2].y - ) - ); - } - } - - /** - * Re-calculate normals (if we have to) - * @type {Array} - */ - if (computeNormals) { - geometry.computeFaceNormals(); - geometry.computeVertexNormals(); - } - - var instanceMaterial = GameLib.D3.Material.CreateInstanceMaterial( - materials[0], - graphics, - gameLibScene - ); - - var instanceMesh = GameLib.D3.Mesh.CreateInstanceMesh( - mesh, - geometry, - instanceMaterial, - graphics - ); - - instanceMesh.name = mesh.name; - - instanceMesh.gameLibObject = mesh; - - instanceMesh.position.x = mesh.position.x; - instanceMesh.position.y = mesh.position.y; - instanceMesh.position.z = mesh.position.z; - - instanceMesh.rotation.x = mesh.rotation.x; - instanceMesh.rotation.y = mesh.rotation.y; - instanceMesh.rotation.z = mesh.rotation.z; - - instanceMesh.scale.x = mesh.scale.x; - instanceMesh.scale.y = mesh.scale.y; - instanceMesh.scale.z = mesh.scale.z; - - instanceMesh.quaternion.x = mesh.quaternion.x; - instanceMesh.quaternion.y = mesh.quaternion.y; - instanceMesh.quaternion.z = mesh.quaternion.z; - instanceMesh.quaternion.w = mesh.quaternion.w; - - instanceMeshes.push(instanceMesh); - } - - console.log("created all meshes"); - - var instanceLights = []; - - if (gameLibScene.lights && gameLibScene.lights.length > 0) { - - for (var glsl = 0; glsl < gameLibScene.lights.length; glsl++) { - - var gameLibLight = gameLibScene.lights[glsl]; - - var light = null; - - if (gameLibLight.lightType == 'AmbientLight') { - light = new graphics.instance.AmbientLight(gameLibLight.color, gameLibLight.intensity); - } - - if (gameLibLight.lightType == 'DirectionalLight') { - light = new graphics.instance.DirectionalLight(gameLibLight.color, gameLibLight.intensity); - } - - if (gameLibLight.lightType == 'PointLight') { - light = new graphics.instance.PointLight(gameLibLight.color, gameLibLight.intensity); - light.distance = gameLibLight.distance; - light.decay = gameLibLight.decay; - } - - if (gameLibLight.lightType == 'SpotLight') { - light = new graphics.instance.SpotLight(gameLibLight.color, gameLibLight.intensity); - light.distance = gameLibLight.distance; - light.angle = gameLibLight.angle; - light.penumbra = gameLibLight.penumbra; - light.decay = gameLibLight.decay; - } - - light.gameLibObject = gameLibLight; - - light.position.x = gameLibLight.position.x; - light.position.y = gameLibLight.position.y; - light.position.z = gameLibLight.position.z; - - light.rotation.x = gameLibLight.rotation.x; - light.rotation.y = gameLibLight.rotation.y; - light.rotation.z = gameLibLight.rotation.z; - - if (light == null) { - console.warn('Does not support lights of type :' + gameLibLight.lightType + ', not imported'); - } else { - light.name = gameLibLight.name; - instanceLights.push(light); - } - } - } - - var instanceScene = new graphics.instance.Scene(); - - instanceScene.name = gameLibScene.name; - - instanceScene.position.x = gameLibScene.position.x; - instanceScene.position.y = gameLibScene.position.y; - instanceScene.position.z = gameLibScene.position.z; - - instanceScene.rotation.x = gameLibScene.rotation.x; - instanceScene.rotation.y = gameLibScene.rotation.y; - instanceScene.rotation.z = gameLibScene.rotation.z; - - instanceScene.scale.x = gameLibScene.scale.x; - instanceScene.scale.y = gameLibScene.scale.y; - instanceScene.scale.z = gameLibScene.scale.z; - - instanceScene.quaternion.x = gameLibScene.quaternion.x; - instanceScene.quaternion.y = gameLibScene.quaternion.y; - instanceScene.quaternion.z = gameLibScene.quaternion.z; - instanceScene.quaternion.w = gameLibScene.quaternion.w; - - for (var im = 0; im < instanceMeshes.length; im++) { - instanceScene.add(instanceMeshes[im]); - } - - for (var l = 0; l < instanceLights.length; l++) { - instanceScene.add(instanceLights[l]); - } - - gameLibScene.loadTextures(gameLibScene.urls, function(loadedTextures) { - - console.log('Found image data and applied it to ' + loadedTextures.length + ' textures'); - - onLoaded( - gameLibScene, - { - scene: instanceScene, - lights: instanceLights, - meshes: instanceMeshes - } - ); - - }); - -}; GameLib.D3.Scene.prototype.intitializeComponents = function() { for (var c = 0; c < this.components.length; c++) { @@ -734,38 +556,6 @@ GameLib.D3.Scene.prototype.intitializeComponents = function() { } }; -GameLib.D3.Scene.FromAPIScene = function( - apiScene, - graphics, - progressCallback, - uploadUrl -) { - return new GameLib.D3.Scene( - apiScene.id, - apiScene.path, - apiScene.name, - graphics, - apiScene.meshes, - apiScene.quaternion, - apiScene.position, - apiScene.rotation, - apiScene.scale, - apiScene.parentSceneId, - apiScene.lights, - apiScene.worlds, - apiScene.entities, - progressCallback, - uploadUrl - ) -}; - -/** - * Set on runtime. - */ -GameLib.D3.Scene.prototype.setCamera = function(camera) { - this.sceneCamera = camera; -}; - /** * Updates the scene * @param deltaTime @@ -774,7 +564,9 @@ GameLib.D3.Scene.prototype.update = function( deltaTime ) { for(var e in this.entities) { - this.entities[e].update(deltaTime); + if (this.entities.hasOwnProperty(e)) { + this.entities[e].update(deltaTime); + } } }; @@ -786,7 +578,9 @@ GameLib.D3.Scene.prototype.lateUpdate = function( deltaTime ) { for(var e in this.entities) { - this.entities[e].lateUpdate(deltaTime); + if (this.entities.hasOwnProperty(e)) { + this.entities[e].lateUpdate(deltaTime); + } } }; @@ -794,7 +588,6 @@ GameLib.D3.Scene.prototype.lateUpdate = function( * renders the scene * @param deltaTime * @param renderer - * @param camera */ GameLib.D3.Scene.prototype.render = function( deltaTime, @@ -818,11 +611,12 @@ GameLib.D3.Scene.prototype.registerEntity = function( this.entities.push(entity); entity.register(this); - // link components to entity - for(var c in entity.ids) { + for(var c = 0; c < entity.ids.length; c++) { + var id = entity.ids[c]; var component = this.idToComponent[id]; + if(component) { component.setParentEntity(this, entity); } @@ -841,14 +635,4 @@ GameLib.D3.Scene.prototype.registerComponent = function( if(component.onRegistered && typeof component.onRegistered == 'function') { component.onRegistered(this); } -}; - -/** - * Registers a light to the scene - * @param light THREE.Light - */ -GameLib.D3.Scene.prototype.registerLight = function( - light -) { - this.instance.add(light); }; \ No newline at end of file diff --git a/src/game-lib-texture-map-template.js b/src/game-lib-texture-map-template.js new file mode 100644 index 0000000..54f7947 --- /dev/null +++ b/src/game-lib-texture-map-template.js @@ -0,0 +1,111 @@ +/** + * 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( + 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' + } + }; +}; + +/** + * 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 + } + }; +}; diff --git a/src/game-lib-texture.js b/src/game-lib-texture.js index ce116c3..51e0afd 100644 --- a/src/game-lib-texture.js +++ b/src/game-lib-texture.js @@ -1,8 +1,56 @@ /** - * Texture Superset + * Texture Superset - The apiTexture properties get moved into the Texture object itself, and then the instance is + * created + * @param apiTexture + * @param graphics GameLib.D3.Graphics + * @param parentMaterial GameLib.D3.Material + * @param parentMaterialInstanceMapId String + * @constructor + */ +GameLib.D3.Texture = function( + apiTexture, + graphics, + parentMaterial, + parentMaterialInstanceMapId +) { + for (var property in apiTexture) { + if (apiTexture.hasOwnProperty(property)) { + this[property] = apiTexture[property]; + } + } + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + this.parentMaterial = parentMaterial; + + this.parentMaterialInstanceMapId = parentMaterialInstanceMapId; + + this.imageFactory = GameLib.D3.ImageFactory(graphics); + + this.imageData = this.imageFactory(apiTexture.imagePath); + + this.imageInstance = null; + + this.instance = null; + + this.imageData.then( + function (imageInstance){ + this.imageInstance = imageInstance; + this.instance = this.createInstance(); + this.parentMaterial[this.parentMaterialInstanceMapId] = this.instance; + this.parentMaterial.needsUpdate = true; + }.bind(this), + function onRejected() { + } + ); +}; + +/** + * Raw Texture API object - should always correspond with the Texture Schema * @param id * @param name - * @param image + * @param imagePath * @param wrapS * @param wrapT * @param repeat @@ -20,13 +68,12 @@ * @param unpackAlignment * @param premultiplyAlpha * @param encoding - * @param instanceMapId * @constructor */ -GameLib.D3.Texture = function( +GameLib.D3.Texture.API = function( id, name, - image, + imagePath, wrapS, wrapT, repeat, @@ -43,12 +90,22 @@ GameLib.D3.Texture = function( mipmaps, unpackAlignment, premultiplyAlpha, - encoding, - instanceMapId + encoding ) { + if (GameLib.D3.Utils.UndefinedOrNull(id)) { + id = GameLib.D3.Tools.RandomId(); + } this.id = id; + + if (GameLib.D3.Utils.UndefinedOrNull(name)) { + name = 'Texture (' + image.name + ')'; + } this.name = name; - this.image = image; + + if (GameLib.D3.Utils.UndefinedOrNull(imagePath)) { + throw new Error('Cannot create textures with no image path set'); + } + this.imagePath = imagePath; if (typeof wrapS == 'undefined') { wrapS = GameLib.D3.Texture.TYPE_REPEAT_WRAPPING; @@ -134,10 +191,9 @@ GameLib.D3.Texture = function( encoding = GameLib.D3.Texture.TYPE_LINEAR_ENCODING; } this.encoding = encoding; - - this.instanceMapId = instanceMapId; }; + /** * Texture Formats * @type {number} @@ -208,18 +264,33 @@ GameLib.D3.Texture.TYPE_RGBM16_ENCODING = 3005; GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256. /** - * A Notification object stores an association between a gameLibTexture and a instance material map. - * @param instanceMaterial - * @param gameLibTexture - * @constructor + * Creates an instance of our texture object + * @returns {GameLib.D3.Texture|THREE.SoftwareRenderer.Texture|THREE.Texture|*|Texture} */ -GameLib.D3.Texture.Notification = function( - gameLibTexture, - instanceMaterial -) { - this.gameLibTexture = gameLibTexture; - this.instanceMaterial = instanceMaterial; +GameLib.D3.Texture.prototype.createInstance = function() { + + var instance = new this.graphics.instance.Texture( + this.imageInstance, + 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.flipY = this.flipY; + instance.offset.x = this.offset.x; + instance.offset.y = this.offset.y; + instance.mipmaps = this.mipmaps; + instance.unpackAlignment = this.unpackAlignment; + instance.premultiplyAlpha = this.premultiplyAlpha; + instance.needsUpdate = true; + + return instance; }; - - -