diff --git a/src/game-lib-camera.js b/src/game-lib-camera.js index a215802..6e1c893 100644 --- a/src/game-lib-camera.js +++ b/src/game-lib-camera.js @@ -1,20 +1,227 @@ /** * Creates a camera object - * @param graphics + * @param graphics GameLib.D3.Graphics + * @param apiCamera GameLib.D3.Camera.API * @constructor */ -GameLib.D3.Camera = function(graphics) { +GameLib.D3.Camera = function( + graphics, + apiCamera +) { + for (var property in apiCamera) { + if (apiCamera.hasOwnProperty(property)) { + this[property] = apiCamera[property]; + } + } + this.graphics = graphics; this.graphics.isNotThreeThrow(); - + this.instance = this.createInstance(); + + this.needsUpdate = false; } ; +/** + * Raw Camera API object - should always correspond with the Camera Schema + * @param id + * @param name + * @param cameraType GameLib.D3.Camera.CAMERA_TYPE_* + * @param fov + * @param aspect + * @param near + * @param far + * @param position GameLib.D3.Vector3 + * @param lookAt GameLib.D3.Vector3 + * @param minX + * @param maxX + * @param minY + * @param maxY + * @param minZ + * @param maxZ + * @constructor + */ +GameLib.D3.Camera.API = function( + id, + cameraType, + name, + fov, + aspect, + near, + far, + position, + lookAt, + minX, + maxX, + minY, + maxY, + minZ, + maxZ +) { + if (GameLib.D3.Utils.UndefinedOrNull(id)) { + id = GameLib.D3.Tools.RandomId(); + } + this.id = id; + + if (GameLib.D3.Utils.UndefinedOrNull(cameraType)) { + cameraType = GameLib.D3.Material.CAMERA_TYPE_PERSPECTIVE; + } + this.cameraType = cameraType; + + if (GameLib.D3.Utils.UndefinedOrNull(name)) { + name = 'Camera (' + cameraType + ')'; + } + this.name = name; + + if (GameLib.D3.Utils.UndefinedOrNull(fov)) { + fov = 75; + } + this.fov = fov; + + if (GameLib.D3.Utils.UndefinedOrNull(aspect)) { + aspect = window.innerWidth / window.innerHeight; + } + this.aspect = aspect; + + if (GameLib.D3.Utils.UndefinedOrNull(near)) { + near = 0.1; + } + this.near = near; + + if (GameLib.D3.Utils.UndefinedOrNull(far)) { + far = 1000; + } + this.far = far; + + if (GameLib.D3.Utils.UndefinedOrNull(position)) { + position = new GameLib.D3.Vector3( + 15, + 15, + 15 + ); + } + this.position = position; + + if (GameLib.D3.Utils.UndefinedOrNull(lookAt)) { + lookAt = new GameLib.D3.Vector3( + 0, + 0, + 0 + ); + } + this.lookAt = lookAt; + + if (GameLib.D3.Utils.UndefinedOrNull(minX)) { + minX = -100; + } + this.minX = minX; + + if (GameLib.D3.Utils.UndefinedOrNull(maxX)) { + maxX = 100; + } + this.maxX = maxX; + + if (GameLib.D3.Utils.UndefinedOrNull(minY)) { + minY = -100; + } + this.minY = minY; + + if (GameLib.D3.Utils.UndefinedOrNull(maxY)) { + maxY = 100; + } + this.maxY = maxY; + + if (GameLib.D3.Utils.UndefinedOrNull(minZ)) { + minZ = -100; + } + this.minZ = minZ; + + if (GameLib.D3.Utils.UndefinedOrNull(maxZ)) { + maxZ = 100; + } + this.maxZ = maxZ; +}; + +/** + * Camera types + * @type {number} + */ +GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE = 0x1; +GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL = 0x2; +GameLib.D3.Camera.CAMERA_TYPE_STEREO = 0x3; + /** * Creates a camera instance of 'graphics' type (only THREE for now) * @returns {THREE.Camera} */ -GameLib.D3.Camera.prototype.createInstance = function() { - return new this.graphics.instance.Camera(); +GameLib.D3.Camera.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + } + + if (!instance || + (instance && this.cameraType != instance.cameraType) + ) { + if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE) { + instance = new this.graphics.instance.PerspectiveCamera( + this.fov, + this.aspect, + this.near, + this.far + ); + } else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) { + instance = new this.graphics.instance.OrthographicCamera( + this.minX, + this.maxX, + this.minY, + this.maxY, + this.minZ, + this.maxZ + ); + } + } + + if (update && this.cameraType == instance.cameraType) { + instance.minX = this.minX; + instance.maxX = this.maxX; + instance.minY = this.minY; + instance.maxY = this.maxY; + instance.minZ = this.minZ; + instance.maxZ = this.maxZ; + instance.fov = this.fov; + instance.aspect = this.aspect; + instance.near = this.near; + instance.far = this.far; + } + + if (update) { + instance.cameraType = this.cameraType; + } + + instance.position.x = this.position.x; + instance.position.y = this.position.y; + instance.position.z = this.position.z; + + instance.lookAt( + new this.graphics.instance.Vector3( + this.lookAt.x, + this.lookAt.y, + this.lookAt.z + ) + ); + + instance.updateProjectionMatrix(); + + return instance; +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Camera.prototype.updateInstance = function() { + this.instance = this.createInstance(true); }; \ No newline at end of file diff --git a/src/game-lib-color.js b/src/game-lib-color.js index caf8055..76cfaa6 100644 --- a/src/game-lib-color.js +++ b/src/game-lib-color.js @@ -11,4 +11,17 @@ GameLib.D3.Color = function(r, g, b, a) { this.g = g; this.b = b; this.a = a; +}; + +/** + * Converts the current color to HTML hex format (ex. #ffffff) + * @returns {string} + */ +GameLib.D3.Color.prototype.toHex = function() { + + var rf = Math.floor(this.r == 1? 255 : this.r * 256.0).toString(16); + var gf = Math.floor(this.g == 1? 255 : this.g * 256.0).toString(16); + var bf = Math.floor(this.b == 1? 255 : this.b * 256.0).toString(16); + + return '#' + rf + gf + bf; }; \ No newline at end of file diff --git a/src/game-lib-image.js b/src/game-lib-image.js index 4a7beb3..9734a99 100644 --- a/src/game-lib-image.js +++ b/src/game-lib-image.js @@ -21,7 +21,7 @@ GameLib.D3.ImageFactory = function (graphics) { return promiseList[url]; } - var defer = q.defer(); + var defer = Q.defer(); promiseList[url] = defer.promise; diff --git a/src/game-lib-light.js b/src/game-lib-light.js index cc307c2..5e5f929 100644 --- a/src/game-lib-light.js +++ b/src/game-lib-light.js @@ -1,12 +1,10 @@ /** * 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 ) { @@ -73,11 +71,18 @@ GameLib.D3.Light.API = function( } this.name = name; + if (GameLib.D3.Utils.UndefinedOrNull(color)) { + color = new GameLib.D3.Color(1,1,1,1); + } this.color = color; + + if (GameLib.D3.Utils.UndefinedOrNull(intensity)) { + intensity = 1; + } this.intensity = intensity; if (typeof position == 'undefined') { - position = new GameLib.D3.Vector3(0,0,0); + position = new GameLib.D3.Vector3(0,10,0); } this.position = position; @@ -140,34 +145,72 @@ GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4; * Creates a light instance * @returns {*} */ -GameLib.D3.Light.createInstance = function() { +GameLib.D3.Light.prototype.createInstance = function(update) { var instance = null; - if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) { - instance = new this.graphics.instance.AmbientLight(this.color, this.intensity); + if (update) { + instance = this.instance; } - if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) { - instance = new this.graphics.instance.DirectionalLight(this.color, this.intensity); - } + if ((!instance) || + (instance && instance.lightType != this.lightType)) { - 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_AMBIENT) { + instance = new this.graphics.instance.AmbientLight( + new this.graphics.instance.Color( + this.color.r, + this.color.g, + this.color.b + ), + this.intensity + ); + } - 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; + if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) { + instance = new this.graphics.instance.DirectionalLight( + new this.graphics.instance.Color( + this.color.r, + this.color.g, + this.color.b + ), + this.intensity + ); + } + + if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) { + instance = new this.graphics.instance.PointLight( + new this.graphics.instance.Color( + this.color.r, + this.color.g, + this.color.b + ), + 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( + new this.graphics.instance.Color( + this.color.r, + this.color.g, + this.color.b + ), + this.intensity + ); + instance.distance = this.distance; + instance.angle = this.angle; + instance.penumbra = this.penumbra; + instance.decay = this.decay; + } } instance.gameLibObject = this; + instance.name = this.name; + instance.position.x = this.position.x; instance.position.y = this.position.y; instance.position.z = this.position.z; @@ -177,4 +220,11 @@ GameLib.D3.Light.createInstance = function() { instance.rotation.z = this.rotation.z; return instance; -}; \ No newline at end of file +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Light.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; diff --git a/src/game-lib-material.js b/src/game-lib-material.js index 4e1d4f5..dc9badd 100644 --- a/src/game-lib-material.js +++ b/src/game-lib-material.js @@ -20,6 +20,8 @@ GameLib.D3.Material = function( this.graphics.isNotThreeThrow(); this.instance = this.createInstance(); + + this.needsUpdate = false; }; /** @@ -147,7 +149,7 @@ GameLib.D3.Material.API = function( } this.id = id; - if (typeof materialType == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(materialType)) { materialType = GameLib.D3.Material.MATERIAL_TYPE_STANDARD; } this.materialType = materialType; @@ -157,272 +159,272 @@ GameLib.D3.Material.API = function( } this.name = name; - if (typeof opacity == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(opacity)) { opacity = 1.0; } this.opacity = opacity; - if (typeof side == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(side)) { side = GameLib.D3.Material.TYPE_FRONT_SIDE; } this.side = side; - if (typeof transparent == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(transparent)) { transparent = false; } this.transparent = transparent; - if (typeof maps == 'undefined') { - maps = new GameLib.D3.TextureMapTemplate.API(); + if (GameLib.D3.Utils.UndefinedOrNull(maps)) { + maps = GameLib.D3.TextureMapTemplate.API(); } this.maps = maps; - if (typeof specular == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(specular)) { specular = new GameLib.D3.Color(0.06, 0.06, 0.06, 0.06); } this.specular = specular; - if (typeof lightMapIntensity == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(lightMapIntensity)) { lightMapIntensity = 1; } this.lightMapIntensity = lightMapIntensity; - if (typeof aoMapIntensity == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(aoMapIntensity)) { aoMapIntensity = 1; } this.aoMapIntensity = aoMapIntensity; - if (typeof color == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(color)) { color = new GameLib.D3.Color(1, 1, 1, 1) } this.color = color; - if (typeof emissive == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(emissive)) { emissive = new GameLib.D3.Color(0, 0, 0, 0); } this.emissive = emissive; - if (typeof emissiveIntensity == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(emissiveIntensity)) { emissiveIntensity = 1; } this.emissiveIntensity = emissiveIntensity; - if (typeof combine == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(combine)) { combine = GameLib.D3.Material.TYPE_MULTIPLY_OPERATION; } this.combine = combine; - if (typeof shininess == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(shininess)) { shininess = 30; } this.shininess = shininess; - if (typeof reflectivity == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(reflectivity)) { reflectivity = 1; } this.reflectivity = reflectivity; - if (typeof refractionRatio == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(refractionRatio)) { refractionRatio = 0.98; } this.refractionRatio = refractionRatio; - if (typeof fog == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(fog)) { fog = true; } this.fog = fog; - if (typeof wireframe == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(wireframe)) { wireframe = false; } this.wireframe = wireframe; - if (typeof wireframeLineWidth == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(wireframeLineWidth)) { wireframeLineWidth = 1; } this.wireframeLineWidth = wireframeLineWidth; - if (typeof wireframeLineCap == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(wireframeLineCap)) { wireframeLineCap = 'round'; } this.wireframeLineCap = wireframeLineCap; - if (typeof wireframeLineJoin == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(wireframeLineJoin)) { wireframeLineJoin = 'round'; } this.wireframeLineJoin = wireframeLineJoin; - if (typeof vertexColors == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(vertexColors)) { vertexColors = GameLib.D3.Material.TYPE_NO_COLORS; } this.vertexColors = vertexColors; - if (typeof skinning == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(skinning)) { skinning = false; } this.skinning = skinning; - if (typeof morphTargets == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(morphTargets)) { morphTargets = false; } this.morphTargets = morphTargets; - if (typeof morphNormals == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(morphNormals)) { morphNormals = false; } this.morphNormals = morphNormals; - if (typeof overdraw == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(overdraw)) { overdraw = 0; } this.overdraw = overdraw; - if (typeof lineWidth == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(lineWidth)) { lineWidth = 1; } this.lineWidth = lineWidth; - if (typeof lineCap == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(lineCap)) { lineCap = 'round'; } this.lineCap = lineCap; - if (typeof lineJoin == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(lineJoin)) { lineJoin = 'round'; } this.lineJoin = lineJoin; - if (typeof dashSize == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(dashSize)) { dashSize = 3; } this.dashSize = dashSize; - if (typeof gapWidth == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(gapWidth)) { gapWidth = 1; } this.gapWidth = gapWidth; - if (typeof blending == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(blending)) { blending = GameLib.D3.Material.TYPE_NORMAL_BLENDING; } this.blending = blending; - if (typeof blendSrc == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(blendSrc)) { blendSrc = GameLib.D3.Material.TYPE_SRC_ALPHA_FACTOR; } this.blendSrc = blendSrc; - if (typeof blendDst == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(blendDst)) { blendDst = GameLib.D3.Material.TYPE_ONE_MINUS_SRC_ALPHA_FACTOR; } this.blendDst = blendDst; - if (typeof blendEquation == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(blendEquation)) { blendEquation = GameLib.D3.Material.TYPE_ADD_EQUATION; } this.blendEquation = blendEquation; - if (typeof depthTest == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(depthTest)) { depthTest = true; } this.depthTest = depthTest; - if (typeof depthFunc == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(depthFunc)) { depthFunc = GameLib.D3.Material.TYPE_LESS_EQUAL_DEPTH; } this.depthFunc = depthFunc; - if (typeof depthWrite == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(depthWrite)) { depthWrite = true; } this.depthWrite = depthWrite; - if (typeof polygonOffset == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(polygonOffset)) { polygonOffset = false; } this.polygonOffset = polygonOffset; - if (typeof polygonOffsetFactor == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(polygonOffsetFactor)) { polygonOffsetFactor = 1; } this.polygonOffsetFactor = polygonOffsetFactor; - if (typeof polygonOffsetUnits == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(polygonOffsetUnits)) { polygonOffsetUnits = 1; } this.polygonOffsetUnits = polygonOffsetUnits; - if (typeof alphaTest == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(alphaTest)) { alphaTest = 0; } this.alphaTest = alphaTest; - if (typeof clippingPlanes == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(clippingPlanes)) { clippingPlanes = []; } this.clippingPlanes = clippingPlanes; - if (typeof clipShadows == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(clipShadows)) { clipShadows = false; } this.clipShadows = clipShadows; - if (typeof visible == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(visible)) { visible = true; } this.visible = visible; - if (typeof shading == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(shading)) { shading = GameLib.D3.Material.TYPE_FLAT_SHADING; } this.shading = shading; - if (typeof bumpScale == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(bumpScale)) { bumpScale = 1; } this.bumpScale = bumpScale; - if (typeof normalScale == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(normalScale)) { normalScale = 1; } this.normalScale = normalScale; - if (typeof displacementScale == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(displacementScale)) { displacementScale = 1; } this.displacementScale = displacementScale; - if (typeof displacementBias == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(displacementBias)) { displacementBias = 0; } this.displacementBias = displacementBias; - if (typeof roughness == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(roughness)) { roughness = 0.5; } this.roughness = roughness; - if (typeof metalness == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(metalness)) { metalness = 0.5; } this.metalness = metalness; - if (typeof pointSize == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(pointSize)) { pointSize = 1; } this.pointSize = pointSize; - if (typeof pointSizeAttenuation == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(pointSizeAttenuation)) { pointSizeAttenuation = true; } this.pointSizeAttenuation = pointSizeAttenuation; - if (typeof spriteRotation == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(spriteRotation)) { spriteRotation = 0; } this.spriteRotation = spriteRotation; - if (typeof envMapIntensity == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(envMapIntensity)) { envMapIntensity = 1.0; } this.envMapIntensity = envMapIntensity; @@ -528,124 +530,141 @@ GameLib.D3.Material.MATERIAL_TYPE_MULTIPLE = "MultiMaterial"; * Material instance * @returns {*} */ -GameLib.D3.Material.prototype.createInstance = function() { +GameLib.D3.Material.prototype.createInstance = function(update) { var instance = null; - if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { + if (update) { + instance = this.instance; + } - 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: 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: 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 - }); + if ( + (!instance) || + (instance && instance.materialType != this.materialType) + ) { - } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_PHONG) { + if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { - 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 this.graphics.instance.Color( - this.specular.r, - this.specular.g, - this.specular.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: 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 - }); + 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: 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: 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 { - console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up"); + } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_PHONG) { + + 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 this.graphics.instance.Color( + this.specular.r, + this.specular.g, + this.specular.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: 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: " + this.materialType + " - material indexes could be screwed up"); + } } return instance; +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Material.prototype.updateInstance = function() { + this.instance = this.createInstance(true); }; \ No newline at end of file diff --git a/src/game-lib-mesh.js b/src/game-lib-mesh.js index 2dfd6f4..70ba7b1 100644 --- a/src/game-lib-mesh.js +++ b/src/game-lib-mesh.js @@ -1,20 +1,15 @@ /** * 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, - name, graphics, computeNormals, apiMesh ) { - for (var property in apiMesh) { if (apiMesh.hasOwnProperty(property)) { this[property] = apiMesh[property]; @@ -27,7 +22,9 @@ GameLib.D3.Mesh = function( this.computeNormals = computeNormals; - this.instance = this.createInstance(); + this.instance = this.createInstance(false); + + this.needsUpdate = false; }; /** @@ -95,62 +92,62 @@ GameLib.D3.Mesh.API = function( } this.faces = faces; - if (typeof parentMeshId == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(parentMeshId)) { parentMeshId = null; } this.parentMeshId = parentMeshId; - if (typeof parentSceneId == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(parentSceneId)) { parentSceneId = null; } this.parentSceneId = parentSceneId; - if (typeof skeleton == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(skeleton)) { skeleton = null; } this.skeleton = skeleton; - if (typeof faceVertexUvs == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(faceVertexUvs)) { faceVertexUvs = []; } this.faceVertexUvs = faceVertexUvs; - if (typeof skinIndices == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(skinIndices)) { skinIndices = []; } this.skinIndices = skinIndices; - if (typeof skinWeights == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(skinWeights)) { skinWeights = []; } this.skinWeights = skinWeights; - if (typeof materials == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(materials)) { materials = []; } this.materials = materials; - if (typeof position == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(position)) { position = new GameLib.D3.Vector3(0,0,0); } this.position = position; - if (typeof quaternion == 'undefined') { - new GameLib.D3.Vector4(); + if (GameLib.D3.Utils.UndefinedOrNull(quaternion)) { + quaternion = new GameLib.D3.Vector4(); } this.quaternion = quaternion; - if (typeof rotation == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(rotation)) { rotation = new GameLib.D3.Vector3(0,0,0); } this.rotation = rotation; - if (typeof scale == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(scale)) { scale = new GameLib.D3.Vector3(1,1,1); } this.scale = scale; - if (typeof up == 'undefined') { + if (GameLib.D3.Utils.UndefinedOrNull(up)) { up = new GameLib.D3.Vector3(0,1,0); } this.up = up; @@ -164,239 +161,243 @@ GameLib.D3.Mesh.TYPE_NORMAL = 0; GameLib.D3.Mesh.TYPE_SKINNED = 1; /** - * Creates a mesh instance + * Creates a mesh instance or updates it */ -GameLib.D3.Mesh.prototype.createInstance = function() { +GameLib.D3.Mesh.prototype.createInstance = function(update) { var instance = null; - 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 (update) { + instance = this.instance; } - /** - * Setup faces - */ - for (var f = 0; f < this.faces.length; f++) { + if (!update) { + var instanceGeometry = new this.graphics.instance.Geometry(); - var face = new this.graphics.instance.Face3( - this.faces[f].v0, - this.faces[f].v1, - this.faces[f].v2, - new this.graphics.instance.Vector3( + /** + * 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 + ) + ) + } + + /** + * Setup faces + */ + for (var f = 0; f < this.faces.length; f++) { + + 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 + ); + + 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 + ) + ]; + + face.normal = 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 - ); - - 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 - ) - ]; - - face.normal = new this.graphics.instance.Vector3( - this.faces[f].normal.x, - this.faces[f].normal.y, - this.faces[f].normal.z - ); - - 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 - ) ); + + 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); } - } - /** - * 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 this.graphics.instance.Bone(); - - bone.name = bones[bi].name; - - bone.position.x = bones[bi].position.x; - bone.position.y = bones[bi].position.y; - bone.position.z = bones[bi].position.z; - - bone.rotation.x = bones[bi].rotation.x; - bone.rotation.y = bones[bi].rotation.y; - bone.rotation.z = bones[bi].rotation.z; - - bone.quaternion.x = bones[bi].quaternion.x; - bone.quaternion.y = bones[bi].quaternion.y; - bone.quaternion.z = bones[bi].quaternion.z; - bone.quaternion.w = bones[bi].quaternion.w; - - bone.scale.x = bones[bi].scale.x; - bone.scale.y = bones[bi].scale.y; - bone.scale.z = bones[bi].scale.z; - - bone.up.x = bones[bi].up.x; - bone.up.y = bones[bi].up.y; - bone.up.z = bones[bi].up.z; - - instanceBones.push(bone); - } + instanceGeometry.faceVertexUvs = []; /** - * Setup the bone relationships + * Setup face UVs */ - for (var br = 0; br < bones.length; br++) { - for (var cbi = 0; cbi < bones[br].childBoneIds.length; cbi++) { - instanceBones[br].add(instanceBones[bones[br].childBoneIds[cbi]]); + 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 + ) + ); } } /** - * Setup bones (indexes) + * Re-calculate normals (if we have to) + * @type {Array} */ - for (var si = 0; si < this.skinIndices.length; si++) { - instanceGeometry.skinIndices.push( - new this.graphics.instance.Vector4( - this.skinIndices[si].x, - this.skinIndices[si].y, - this.skinIndices[si].z, - this.skinIndices[si].w - ) - ); + if (this.computeNormals) { + instanceGeometry.computeFaceNormals(); + instanceGeometry.computeVertexNormals(); } - /** - * Setup bones (weights) - */ - for (var sw = 0; sw < this.skinWeights.length; sw++) { - instanceGeometry.skinWeights.push( - new this.graphics.instance.Vector4( - this.skinWeights[sw].x, - this.skinWeights[sw].y, - this.skinWeights[sw].z, - this.skinWeights[sw].w - ) - ); + var instanceMaterial = this.materials[0].instance; + + if (this.meshType == GameLib.D3.Mesh.TYPE_NORMAL) { + instance = new this.graphics.instance.Mesh(instanceGeometry, instanceMaterial); } - instance = new this.graphics.instance.SkinnedMesh(instanceGeometry, instanceMaterial); + if (this.meshType == GameLib.D3.Mesh.TYPE_SKINNED) { - var skeleton = new this.graphics.instance.Skeleton(instanceBones); + var bones = this.skeleton.bones; - skeleton.useVertexTexture = this.skeleton.useVertexTexture; + var instanceBones = []; - for (var i = 0; i < bones.length; i++) { - if (bones[i].parentBoneId === null) { - instance.add(instanceBones[i]); - break; + for (var bi = 0; bi < bones.length; bi++) { + + var bone = new this.graphics.instance.Bone(); + + bone.name = bones[bi].name; + + bone.position.x = bones[bi].position.x; + bone.position.y = bones[bi].position.y; + bone.position.z = bones[bi].position.z; + + bone.rotation.x = bones[bi].rotation.x; + bone.rotation.y = bones[bi].rotation.y; + bone.rotation.z = bones[bi].rotation.z; + + bone.quaternion.x = bones[bi].quaternion.x; + bone.quaternion.y = bones[bi].quaternion.y; + bone.quaternion.z = bones[bi].quaternion.z; + bone.quaternion.w = bones[bi].quaternion.w; + + bone.scale.x = bones[bi].scale.x; + bone.scale.y = bones[bi].scale.y; + bone.scale.z = bones[bi].scale.z; + + bone.up.x = bones[bi].up.x; + bone.up.y = bones[bi].up.y; + bone.up.z = bones[bi].up.z; + + instanceBones.push(bone); } + + /** + * Setup the bone relationships + */ + for (var br = 0; br < bones.length; br++) { + for (var cbi = 0; cbi < bones[br].childBoneIds.length; cbi++) { + instanceBones[br].add(instanceBones[bones[br].childBoneIds[cbi]]); + } + } + + /** + * Setup bones (indexes) + */ + for (var si = 0; si < this.skinIndices.length; si++) { + instanceGeometry.skinIndices.push( + new this.graphics.instance.Vector4( + this.skinIndices[si].x, + this.skinIndices[si].y, + this.skinIndices[si].z, + this.skinIndices[si].w + ) + ); + } + + /** + * Setup bones (weights) + */ + for (var sw = 0; sw < this.skinWeights.length; sw++) { + instanceGeometry.skinWeights.push( + new this.graphics.instance.Vector4( + this.skinWeights[sw].x, + this.skinWeights[sw].y, + this.skinWeights[sw].z, + this.skinWeights[sw].w + ) + ); + } + + instance = new this.graphics.instance.SkinnedMesh(instanceGeometry, instanceMaterial); + + var skeleton = new this.graphics.instance.Skeleton(instanceBones); + + skeleton.useVertexTexture = this.skeleton.useVertexTexture; + + for (var i = 0; i < bones.length; i++) { + if (bones[i].parentBoneId === null) { + instance.add(instanceBones[i]); + break; + } + } + + instance.bind(skeleton); + + instance.pose(); + + instance.skeleton.skeletonHelper = new this.graphics.instance.SkeletonHelper(instance); + instance.skeleton.skeletonHelper.material.linewidth = 5; } - instance.bind(skeleton); - - instance.pose(); - - instance.skeleton.skeletonHelper = new this.graphics.instance.SkeletonHelper(instance); - instance.skeleton.skeletonHelper.material.linewidth = 5; + if (instance == null) { + console.log('cannot handle meshes of type ' + this.meshType + ' yet.'); + } } - if (instance == null) { - console.log('cannot handle meshes of type ' + this.meshType + ' yet.'); - } - - this.threeMeshId = instance.id; - instance.name = this.name; instance.gameLibObject = this; @@ -421,6 +422,10 @@ GameLib.D3.Mesh.prototype.createInstance = function() { return instance; }; +GameLib.D3.Mesh.updateInstance = function() { + this.instance = this.createInstance(true); +}; + GameLib.D3.prototype.invertWindingOrder = function(triangles) { for (var i = 0; i < triangles.length; i++) { diff --git a/src/game-lib-scene.js b/src/game-lib-scene.js index 8e69ff3..46d6ee6 100644 --- a/src/game-lib-scene.js +++ b/src/game-lib-scene.js @@ -30,6 +30,8 @@ GameLib.D3.Scene = function( this.progressCallback = progressCallback; this.instance = this.createInstance(); + + this.needsUpdate = false; }; /** @@ -184,6 +186,7 @@ GameLib.D3.Scene.prototype.createInstance = function() { * @param graphics * @param uploadUrl * @param progressCallback + * @param computeNormals * @constructor */ GameLib.D3.Scene.LoadScene = function( @@ -191,7 +194,8 @@ GameLib.D3.Scene.LoadScene = function( onLoaded, graphics, uploadUrl, - progressCallback + progressCallback, + computeNormals ) { var entities = []; @@ -281,7 +285,7 @@ GameLib.D3.Scene.LoadScene = function( apiMaterial.opacity, apiMaterial.side, apiMaterial.transparent, - null, + GameLib.D3.TextureMapTemplate.API(), new GameLib.D3.Color( apiMaterial.specular.r, apiMaterial.specular.g, @@ -402,46 +406,48 @@ GameLib.D3.Scene.LoadScene = function( } 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 + computeNormals, + new GameLib.D3.Mesh.API( + apiMesh.id, + apiMesh.meshType, + apiMesh.name, + apiMesh.vertices, + apiMesh.faces, + apiMesh.faceVertexUvs, + gameLibMaterials, + apiMesh.parentMeshId, + apiMesh.parentSceneId, + apiMesh.skeleton, + apiMesh.skinIndices, + apiMesh.skinWeights, + new GameLib.D3.Vector3( + apiMesh.position.x, + apiMesh.position.y, + apiMesh.position.z + ), + new GameLib.D3.Vector4( + apiMesh.quaternion.x, + apiMesh.quaternion.y, + apiMesh.quaternion.z, + apiMesh.quaternion.w + ), + 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 + ) + ) ); gameLibMeshes.push(gameLibMesh); @@ -454,7 +460,7 @@ GameLib.D3.Scene.LoadScene = function( scene.id, scene.path, scene.name, - scene.meshes, + gameLibMeshes, new GameLib.D3.Vector4( scene.quaternion.x, scene.quaternion.y, @@ -534,7 +540,7 @@ GameLib.D3.Scene.LoadSceneFromApi = function( var scene = response.scene[0]; - GameLib.D3.Scene.LoadScene(scene, onLoaded, graphics, uploadUrl, progressCallback); + GameLib.D3.Scene.LoadScene(scene, onLoaded, graphics, uploadUrl, progressCallback, false); } } }(xhr); diff --git a/src/game-lib-texture.js b/src/game-lib-texture.js index 51e0afd..debbc80 100644 --- a/src/game-lib-texture.js +++ b/src/game-lib-texture.js @@ -38,6 +38,9 @@ GameLib.D3.Texture = function( function (imageInstance){ this.imageInstance = imageInstance; this.instance = this.createInstance(); + + this.needsUpdate = false; + this.parentMaterial[this.parentMaterialInstanceMapId] = this.instance; this.parentMaterial.needsUpdate = true; }.bind(this), @@ -267,19 +270,25 @@ GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256. * Creates an instance of our texture object * @returns {GameLib.D3.Texture|THREE.SoftwareRenderer.Texture|THREE.Texture|*|Texture} */ -GameLib.D3.Texture.prototype.createInstance = function() { +GameLib.D3.Texture.prototype.createInstance = function(update) { - 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 - ); + var instance = null; + + if (update) { + instance = this.instance; + } else { + 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; @@ -294,3 +303,10 @@ GameLib.D3.Texture.prototype.createInstance = function() { return instance; }; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Texture.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; diff --git a/src/game-lib-vector-4.js b/src/game-lib-vector-4.js index b19eaaf..ef91499 100644 --- a/src/game-lib-vector-4.js +++ b/src/game-lib-vector-4.js @@ -3,7 +3,7 @@ GameLib.D3.Vector4 = function(x, y, z, w) { this.x = 0; this.y = 0; this.z = 0; - this.w = 0; + this.w = 1; if (x) { this.x = x;