diff --git a/src/game-lib-api-entity-manager.js b/src/game-lib-api-entity-manager.js index d7456a3..d4f6347 100644 --- a/src/game-lib-api-entity-manager.js +++ b/src/game-lib-api-entity-manager.js @@ -11,3 +11,12 @@ GameLib.API.EntityManager = function( } this.entities = entities; }; + +/** + * Creates an API entity manager from an Object entity manager + * @param objectEntityManager + * @constructor + */ +GameLib.API.EntityManager.FromObjectEntityManager = function(objectEntityManager) { + +}; diff --git a/src/game-lib-api-entity.js b/src/game-lib-api-entity.js index ec20fec..d05652f 100644 --- a/src/game-lib-api-entity.js +++ b/src/game-lib-api-entity.js @@ -53,4 +53,17 @@ GameLib.API.Entity.prototype.removeComponent = function(component) { this.components.splice(index, 1); return true; -}; \ No newline at end of file +}; + +/** + * Returns an API entity from an Object entity + * @param objectEntity + * @constructor + */ +GameLib.API.Entity.FromObjectEntity = function(objectEntity) { + return new GameLib.API.Entity( + objectEntity.id, + objectEntity.name, + objectEntity.components + ) +}; diff --git a/src/game-lib-api-matrix4.js b/src/game-lib-api-matrix4.js index cea78f5..45a8c0b 100644 --- a/src/game-lib-api-matrix4.js +++ b/src/game-lib-api-matrix4.js @@ -131,3 +131,36 @@ GameLib.API.Matrix4.prototype.identity = function () { ]; }; +/** + * Returns an API matrix from an Object matrix + * @param objectMatrix + * @constructor + */ +GameLib.API.Matrix4.FromObjectMatrix = function(objectMatrix) { + return new GameLib.API.Matrix4( + new GameLib.API.Vector4( + objectMatrix.rows[0].x, + objectMatrix.rows[0].y, + objectMatrix.rows[0].z, + objectMatrix.rows[0].w + ), + new GameLib.API.Vector4( + objectMatrix.rows[1].x, + objectMatrix.rows[1].y, + objectMatrix.rows[1].z, + objectMatrix.rows[1].w + ), + new GameLib.API.Vector4( + objectMatrix.rows[2].x, + objectMatrix.rows[2].y, + objectMatrix.rows[2].z, + objectMatrix.rows[2].w + ), + new GameLib.API.Vector4( + objectMatrix.rows[3].x, + objectMatrix.rows[3].y, + objectMatrix.rows[3].z, + objectMatrix.rows[3].w + ) + ) +}; diff --git a/src/game-lib-api-quaternion-a.js b/src/game-lib-api-quaternion-a.js index b2b4e83..939742d 100644 --- a/src/game-lib-api-quaternion-a.js +++ b/src/game-lib-api-quaternion-a.js @@ -194,4 +194,24 @@ GameLib.API.Quaternion.prototype.slerp = function (quaternion, t) { this.w = this.instance.w; return this; +}; + +/** + * Returns an API quaternion from an Object quaternion + * @param objectQuaternion + * @constructor + */ +GameLib.API.Quaternion.FromObjectQuaternion = function (objectQuaternion) { + return new GameLib.API.Quaternion( + objectQuaternion.x, + objectQuaternion.y, + objectQuaternion.z, + objectQuaternion.w, + new GameLib.API.Vector3( + objectQuaternion.axis.x, + objectQuaternion.axis.y, + objectQuaternion.axis.z + ), + objectQuaternion.angle + ) }; \ No newline at end of file diff --git a/src/game-lib-api-vector2.js b/src/game-lib-api-vector2.js index 7d6aa6b..a8054f1 100644 --- a/src/game-lib-api-vector2.js +++ b/src/game-lib-api-vector2.js @@ -17,4 +17,16 @@ GameLib.API.Vector2.prototype.copy = function () { this.x, this.y ); -}; \ No newline at end of file +}; + +/** + * Returns an API vector from an Object vector + * @param objectVector + * @constructor + */ +GameLib.API.Vector2.FromObjectVector = function (objectVector) { + return new GameLib.API.Vector2( + objectVector.x, + objectVector.y + ) +}; diff --git a/src/game-lib-api-vector3.js b/src/game-lib-api-vector3.js index ba2c0b1..8472541 100644 --- a/src/game-lib-api-vector3.js +++ b/src/game-lib-api-vector3.js @@ -251,4 +251,17 @@ GameLib.API.Vector3.prototype.reflect = function(normal) { GameLib.API.Vector3.prototype.angleTo = function (v) { var theta = this.dot( v ) / ( Math.sqrt( this.lengthSq() * v.lengthSq() ) ); return Math.acos( exports.Math.clamp( theta, - 1, 1 ) ); -}; \ No newline at end of file +}; + +/** + * Returns an API vector from an Object vector + * @param objectVector + * @constructor + */ +GameLib.API.Vector3.FromObjectVector = function (objectVector) { + return new GameLib.API.Vector3( + objectVector.x, + objectVector.y, + objectVector.z + ) +}; diff --git a/src/game-lib-api-vector4.js b/src/game-lib-api-vector4.js index 4b72d54..2afe8d0 100644 --- a/src/game-lib-api-vector4.js +++ b/src/game-lib-api-vector4.js @@ -19,4 +19,18 @@ GameLib.API.Vector4 = function (x, y, z, w) { w = 1; } this.w = w; -}; \ No newline at end of file +}; + +/** + * Returns an API vector from an Object vector + * @param objectVector + * @constructor + */ +GameLib.API.Vector4.FromObjectVector = function (objectVector) { + return new GameLib.API.Vector2( + objectVector.x, + objectVector.y, + objectVector.z, + objectVector.w + ) +}; diff --git a/src/game-lib-component-a.js b/src/game-lib-component-a.js index a84035f..51dea84 100644 --- a/src/game-lib-component-a.js +++ b/src/game-lib-component-a.js @@ -43,6 +43,7 @@ GameLib.Component.COMPONENT_MESH = 0x8; GameLib.Component.COMPONENT_SPLINE = 0x9; GameLib.Component.COMPONENT_LIGHT = 0xa; GameLib.Component.COMPONENT_INPUT_DRIVE = 0xb; +GameLib.Component.COMPONENT_MATERIAL = 0xc; GameLib.Component.prototype.toApiComponent = function() { return this.id; diff --git a/src/game-lib-d3-api-bone.js b/src/game-lib-d3-api-bone.js index 94945a7..c2257ce 100644 --- a/src/game-lib-d3-api-bone.js +++ b/src/game-lib-d3-api-bone.js @@ -60,4 +60,22 @@ GameLib.D3.API.Bone = function ( up = new GameLib.API.Vector3(0,1,0); } this.up = up; -}; \ No newline at end of file +}; + +/** + * Returns an API bone from an Object bone + * @param objectBone + * @constructor + */ +GameLib.D3.API.Bone.FromObjectBone = function(objectBone) { + return new GameLib.D3.API.Bone( + objectBone.id, + objectBone.name, + objectBone.childBoneIds, + objectBone.parentBoneIds, + GameLib.API.Vector3.FromObjectVector(objectBone.position), + GameLib.API.Quaternion.FromObjectQuaternion(objectBone.quaternion), + GameLib.API.Vector3.FromObjectVector(objectBone.scale), + GameLib.API.Vector3.FromObjectVector(objectBone.up) + ); +}; diff --git a/src/game-lib-d3-api-camera.js b/src/game-lib-d3-api-camera.js index 50c77cc..9f86447 100644 --- a/src/game-lib-d3-api-camera.js +++ b/src/game-lib-d3-api-camera.js @@ -137,3 +137,32 @@ GameLib.D3.API.Camera = function( GameLib.D3.API.Camera.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.API.Camera.prototype.constructor = GameLib.D3.API.Camera; + +/** + * Creates an API camera from an Object camera + * @param objectCamera + * @constructor + */ +GameLib.D3.API.Camera.FromObjectCamera = function(objectCamera) { + + return new GameLib.D3.API.Camera( + objectCamera.id, + objectCamera.cameraType, + objectCamera.name, + objectCamera.fov, + objectCamera.aspect, + objectCamera.near, + objectCamera.far, + GameLib.API.Vector3.FromObjectVector(objectCamera.position), + GameLib.API.Vector3.FromObjectVector(objectCamera.lookAt), + objectCamera.minX, + objectCamera.maxX, + objectCamera.minY, + objectCamera.maxY, + objectCamera.minZ, + objectCamera.maxZ, + GameLib.API.Quaternion.FromObjectQuaternion(objectCamera.quaternion), + objectCamera.parentEntity + ); + +}; diff --git a/src/game-lib-d3-api-color.js b/src/game-lib-d3-api-color.js index 945757f..2fc89a9 100644 --- a/src/game-lib-d3-api-color.js +++ b/src/game-lib-d3-api-color.js @@ -1,4 +1,4 @@ -GameLib.D3.API.Color = function ApiColor(r, g, b, a) { +GameLib.D3.API.Color = function (r, g, b, a) { if (GameLib.Utils.UndefinedOrNull(r)) { r = 1; @@ -20,4 +20,20 @@ GameLib.D3.API.Color = function ApiColor(r, g, b, a) { } this.a = a; -}; \ No newline at end of file +}; + +/** + * Returns an API color from an Object color + * @param objectColor + * @constructor + */ +GameLib.D3.API.Color.FromObjectColor = function(objectColor) { + + return new GameLib.D3.API.Color( + objectColor.r, + objectColor.g, + objectColor.b, + objectColor.a + ); + +}; diff --git a/src/game-lib-d3-api-light.js b/src/game-lib-d3-api-light.js index efce4bb..f9aa9ef 100644 --- a/src/game-lib-d3-api-light.js +++ b/src/game-lib-d3-api-light.js @@ -121,4 +121,30 @@ GameLib.D3.API.Light = function( }; GameLib.D3.API.Light.prototype = Object.create(GameLib.Component.prototype); -GameLib.D3.API.Light.prototype.constructor = GameLib.D3.API.Light; \ No newline at end of file +GameLib.D3.API.Light.prototype.constructor = GameLib.D3.API.Light; + +/** + * Returns an API light from an Object light + * @param objectLight + * @constructor + */ +GameLib.D3.API.Light.FromObjectLight = function(objectLight) { + return new GameLib.D3.API.Light( + objectLight.id, + objectLight.lightType, + objectLight.name, + GameLib.D3.API.Color.FromObjectColor(objectLight.color), + objectLight.intensity, + GameLib.API.Vector3.FromObjectVector(objectLight.position), + GameLib.API.Vector3.FromObjectVector(objectLight.targetPosition), + GameLib.API.Quaternion.FromObjectQuaternion(objectLight.quaternion), + GameLib.API.Vector3.FromObjectVector(objectLight.rotation), + GameLib.API.Vector3.FromObjectVector(objectLight.scale), + objectLight.distance, + objectLight.decay, + objectLight.power, + objectLight.angle, + objectLight.penumbra, + objectLight.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-material.js b/src/game-lib-d3-api-material.js index ae547c8..225dfa6 100644 --- a/src/game-lib-d3-api-material.js +++ b/src/game-lib-d3-api-material.js @@ -6,7 +6,6 @@ * @param opacity * @param side * @param transparent - * @param maps * @param specular * @param lightMapIntensity * @param aoMapIntensity @@ -57,6 +56,19 @@ * @param pointSizeAttenuation * @param spriteRotation * @param envMapIntensity + * @param alphaMap + * @param aoMap + * @param bumpMap + * @param diffuseMap + * @param displacementMap + * @param emissiveMap + * @param environmentMap + * @param lightMap + * @param metalnessMap + * @param normalMap + * @param roughnessMap + * @param specularMap + * @param parentEntity * @constructor */ GameLib.D3.API.Material = function( @@ -66,7 +78,6 @@ GameLib.D3.API.Material = function( opacity, side, transparent, - maps, specular, lightMapIntensity, aoMapIntensity, @@ -116,8 +127,42 @@ GameLib.D3.API.Material = function( pointSize, pointSizeAttenuation, spriteRotation, - envMapIntensity + envMapIntensity, + alphaMap, + aoMap, + bumpMap, + diffuseMap, + displacementMap, + emissiveMap, + environmentMap, + lightMap, + metalnessMap, + normalMap, + roughnessMap, + specularMap, + parentEntity ) { + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_MATERIAL, + { + alphaMap : GameLib.D3.Texture, + aoMap : GameLib.D3.Texture, + bumpMap : GameLib.D3.Texture, + diffuseMap : GameLib.D3.Texture, + displacementMap : GameLib.D3.Texture, + emissiveMap : GameLib.D3.Texture, + environmentMap : GameLib.D3.Texture, + lightMap : GameLib.D3.Texture, + metalnessMap : GameLib.D3.Texture, + normalMap : GameLib.D3.Texture, + roughnessMap : GameLib.D3.Texture, + specularMap : GameLib.D3.Texture + }, + false, + parentEntity + ); + if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -148,11 +193,6 @@ GameLib.D3.API.Material = function( } this.transparent = transparent; - if (GameLib.Utils.UndefinedOrNull(maps)) { - maps = GameLib.D3.API.TextureMapTemplate(); - } - this.maps = maps; - if (GameLib.Utils.UndefinedOrNull(specular)) { specular = new GameLib.D3.API.Color(0.06, 0.06, 0.06, 0.06); } @@ -402,4 +442,202 @@ GameLib.D3.API.Material = function( envMapIntensity = 1.0; } this.envMapIntensity = envMapIntensity; -}; \ No newline at end of file + + if (GameLib.Utils.UndefinedOrNull(alphaMap)) { + alphaMap = null; + } + this.alphaMap = alphaMap; + + if (GameLib.Utils.UndefinedOrNull(aoMap)) { + aoMap = null; + } + this.aoMap = aoMap; + + if (GameLib.Utils.UndefinedOrNull(bumpMap)) { + bumpMap = null; + } + this.bumpMap = bumpMap; + + if (GameLib.Utils.UndefinedOrNull(diffuseMap)) { + diffuseMap = null; + } + this.diffuseMap = diffuseMap; + + if (GameLib.Utils.UndefinedOrNull(displacementMap)) { + displacementMap = null; + } + this.displacementMap = displacementMap; + + if (GameLib.Utils.UndefinedOrNull(emissiveMap)) { + emissiveMap = null; + } + this.emissiveMap = emissiveMap; + + if (GameLib.Utils.UndefinedOrNull(lightMap)) { + lightMap = null; + } + this.lightMap = lightMap; + + if (GameLib.Utils.UndefinedOrNull(metalnessMap)) { + metalnessMap = null; + } + this.metalnessMap = metalnessMap; + + if (GameLib.Utils.UndefinedOrNull(normalMap)) { + normalMap = null; + } + this.normalMap = normalMap; + + if (GameLib.Utils.UndefinedOrNull(roughnessMap)) { + roughnessMap = null; + } + this.roughnessMap = roughnessMap; + + if (GameLib.Utils.UndefinedOrNull(specularMap)) { + specularMap = null; + } + this.specularMap = specularMap; +}; + +GameLib.D3.API.Material.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.Material.prototype.constructor = GameLib.D3.API.Material; + +/** + * Returns an API Material from an Object material + * @param objectMaterial + * @constructor + */ +GameLib.D3.API.Material.FromObjectMaterial = function(objectMaterial) { + + var apiAlphaMap = null; + var apiAoMap = null; + var apiBumpMap = null; + var apiDiffuseMap = null; + var apiDisplacementMap = null; + var apiEmissiveMap = null; + var apiEnvironmentMap = null; + var apiLightMap = null; + var apiMetalnessMap = null; + var apiNormalMap = null; + var apiRoughnessMap = null; + var apiSpecularMap = null; + + if (objectMaterial.alphaMap) { + apiAlphaMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.alphaMap); + } + + if (objectMaterial.aoMap) { + apiAoMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.aoMap); + } + + if (objectMaterial.bumpMap) { + apiBumpMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.bumpMap); + } + + if (objectMaterial.diffuseMap) { + apiDiffuseMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.diffuseMap); + } + + if (objectMaterial.displacementMap) { + apiDisplacementMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.displacementMap); + } + + if (objectMaterial.emissiveMap) { + apiEmissiveMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.emissiveMap); + } + + if (objectMaterial.environmentMap) { + apiEnvironmentMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.environmentMap); + } + + if (objectMaterial.lightMap) { + apiLightMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.lightMap); + } + + if (objectMaterial.metalnessMap) { + apiMetalnessMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.metalnessMap); + } + + if (objectMaterial.normalMap) { + apiNormalMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.normalMap); + } + + if (objectMaterial.roughnessMap) { + apiRoughnessMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.roughnessMap); + } + + if (objectMaterial.specularMap) { + apiSpecularMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.specularMap); + } + + return new GameLib.D3.API.Material( + objectMaterial.id, + objectMaterial.materialType, + objectMaterial.name, + objectMaterial.opacity, + objectMaterial.side, + objectMaterial.transparent, + GameLib.D3.API.Color.FromObjectColor(objectMaterial.specular), + objectMaterial.lightMapIntensity, + objectMaterial.aoMapIntensity, + GameLib.D3.API.Color.FromObjectColor(objectMaterial.color), + GameLib.D3.API.Color.FromObjectColor(objectMaterial.emissive), + objectMaterial.emissiveIntensity, + objectMaterial.combine, + objectMaterial.shininess, + objectMaterial.reflectivity, + objectMaterial.refractionRatio, + objectMaterial.fog, + objectMaterial.wireframe, + objectMaterial.wireframeLineWidth, + objectMaterial.wireframeLineCap, + objectMaterial.wireframeLineJoin, + objectMaterial.vertexColors, + objectMaterial.skinning, + objectMaterial.morphTargets, + objectMaterial.morphNormals, + objectMaterial.lineWidth, + objectMaterial.lineCap, + objectMaterial.lineJoin, + objectMaterial.dashSize, + objectMaterial.gapWidth, + objectMaterial.blending, + objectMaterial.blendSrc, + objectMaterial.blendDst, + objectMaterial.blendEquation, + objectMaterial.depthTest, + objectMaterial.depthFunc, + objectMaterial.depthWrite, + objectMaterial.polygonOffset, + objectMaterial.polygonOffsetFactor, + objectMaterial.polygonOffsetUnits, + objectMaterial.alphaTest, + objectMaterial.clippingPlanes, + objectMaterial.clipShadows, + objectMaterial.visible, + objectMaterial.overdraw, + objectMaterial.shading, + objectMaterial.bumpScale, + objectMaterial.normalScale, + objectMaterial.displacementScale, + objectMaterial.displacementBias, + objectMaterial.roughness, + objectMaterial.metalness, + objectMaterial.pointSize, + objectMaterial.pointSizeAttenuation, + objectMaterial.spriteRotation, + objectMaterial.envMapIntensity, + apiAlphaMap, + apiAoMap, + apiBumpMap, + apiDiffuseMap, + apiDisplacementMap, + apiEmissiveMap, + apiEnvironmentMap, + apiLightMap, + apiMetalnessMap, + apiNormalMap, + apiRoughnessMap, + apiSpecularMap + ) +}; diff --git a/src/game-lib-d3-api-mesh.js b/src/game-lib-d3-api-mesh.js index d2859d3..514750d 100644 --- a/src/game-lib-d3-api-mesh.js +++ b/src/game-lib-d3-api-mesh.js @@ -166,4 +166,51 @@ GameLib.D3.API.Mesh = function( }; GameLib.D3.API.Mesh.prototype = Object.create(GameLib.Component.prototype); -GameLib.D3.API.Mesh.prototype.constructor = GameLib.D3.API.Mesh; \ No newline at end of file +GameLib.D3.API.Mesh.prototype.constructor = GameLib.D3.API.Mesh; + +/** + * Returns an API Mesh from an Object mesh + * @param objectMesh + * @constructor + */ +GameLib.D3.API.Mesh.FromObjectMesh = function (objectMesh){ + + var apiSkeleton = null; + + if (objectMesh.skeleton) { + apiSkeleton = GameLib.D3.API.Skeleton.FromObjectSkeleton(objectMesh.skeleton); + } + + return new GameLib.D3.API.Mesh( + objectMesh.id, + objectMesh.meshType, + objectMesh.name, + objectMesh.vertices.map( + function (objectVertex) { + return GameLib.D3.API.Vertex.FromObjectVertex(objectVertex); + } + ), + objectMesh.faces, + objectMesh.faceVertexUvs, + objectMesh.materials.map( + function (objectMaterial) { + return GameLib.D3.API.Material.FromObjectMaterial(objectMaterial); + } + ), + objectMesh.parentMesh, + objectMesh.parentScene, + apiSkeleton, + objectMesh.skinIndices, + objectMesh.skinWeights, + GameLib.API.Vector3.FromObjectVector(objectMesh.position), + GameLib.API.Quaternion.FromObjectQuaternion(objectMesh.quaternion), + GameLib.API.Vector3.FromObjectVector(objectMesh.scale), + GameLib.API.Vector3.FromObjectVector(objectMesh.localPosition), + GameLib.API.Vector3.FromObjectVector(objectMesh.localRotation), + GameLib.API.Vector3.FromObjectVector(objectMesh.localScale), + GameLib.API.Vector3.FromObjectVector(objectMesh.up), + GameLib.API.Matrix4.FromObjectMatrix(objectMesh.modelMatrix), + objectMesh.parentEntity, + objectMesh.renderOrder + ); +}; diff --git a/src/game-lib-d3-api-scene.js b/src/game-lib-d3-api-scene.js index 55dd042..b1004d6 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -102,3 +102,47 @@ GameLib.D3.API.Scene = function( } this.activeCameraIndex = activeCameraIndex; }; + +/** + * Returns an API scene from an Object scene + * @param objectScene + * @constructor + */ +GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { + + var apiEntityManager = null; + + if (objectScene.entityManager) { + apiEntityManager = GameLib.API.EntityManager.FromObjectEntityManager(objectScene.entityManager); + } + + return new GameLib.D3.API.Scene( + objectScene.id, + objectScene.path, + objectScene.name, + objectScene.meshes.map( + function (objectMesh) { + return GameLib.D3.API.Mesh.FromObjectMesh(objectMesh) + } + ), + GameLib.API.Vector3.FromObjectVector(objectScene.position), + GameLib.API.Quaternion.FromObjectQuaternion(objectScene.quaternion), + GameLib.API.Vector3.FromObjectVector(objectScene.scale), + objectScene.parentSceneId, + objectScene.lights.map( + function (objectLight) { + return GameLib.D3.API.Light.FromObjectLight(objectLight) + } + ), + [], //TODO : implement worlds here + apiEntityManager, + [], //TODO : implement shapes here + objectScene.cameras.map( + function (objectCamera) { + return GameLib.D3.API.Camera.FromObjectCamera(objectCamera); + } + ), + objectScene.activeCameraIndex + ); + +}; diff --git a/src/game-lib-d3-api-skeleton.js b/src/game-lib-d3-api-skeleton.js index 4a09666..7ddcb12 100644 --- a/src/game-lib-d3-api-skeleton.js +++ b/src/game-lib-d3-api-skeleton.js @@ -77,3 +77,33 @@ GameLib.D3.API.Skeleton = function ( this.boneTexture = boneTexture; }; +/** + * Creates an API skeleton from an Object skeleton + * @param objectSkeleton + * @constructor + */ +GameLib.D3.API.Skeleton.FromObjectSkeleton = function(objectSkeleton) { + return new GameLib.D3.API.Skeleton( + objectSkeleton.id, + objectSkeleton.name, + objectSkeleton.bones.map( + function (objectBone) { + return GameLib.D3.API.Bone.FromObjectBone(objectBone); + } + ), + objectSkeleton.boneInverses.map( + function (boneInverse) { + return GameLib.D3.API.Matrix4.FromObjectMatrix(boneInverse); + } + ), + objectSkeleton.useVertexTexture, + objectSkeleton.boneTextureWidth, + objectSkeleton.boneTextureHeight, + objectSkeleton.boneMatrices.map( + function (boneMatrix) { + return GameLib.D3.API.Matrix4.FromObjectMatrix(boneMatrix); + } + ), + objectSkeleton.boneTexture + ); +}; \ No newline at end of file diff --git a/src/game-lib-d3-api-texture-map-template.js b/src/game-lib-d3-api-texture-map-template.js deleted file mode 100644 index 6ce45cb..0000000 --- a/src/game-lib-d3-api-texture-map-template.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Raw API Texture Map Template - should match the contents of the Material Schema (maps property) - * @constructor - */ -GameLib.D3.API.TextureMapTemplate = function() { - return new GameLib.D3.TextureMaps( - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap(), - new GameLib.D3.TextureMap() - ); -}; \ No newline at end of file diff --git a/src/game-lib-d3-api-texture.js b/src/game-lib-d3-api-texture.js index cf312bf..3760478 100644 --- a/src/game-lib-d3-api-texture.js +++ b/src/game-lib-d3-api-texture.js @@ -151,3 +151,34 @@ GameLib.D3.API.Texture = function( } this.encoding = encoding; }; + +/** + * Creates an API texture from Object data + * @param objectTexture + * @constructor + */ +GameLib.D3.API.Texture.FromObjectTexture = function(objectTexture) { + new GameLib.D3.API.Texture( + objectTexture.id, + objectTexture.textureType, + objectTexture.name, + objectTexture.imagePath, + objectTexture.wrapS, + objectTexture.wrapT, + GameLib.API.Vector2.FromObjectVector(objectTexture.repeat), + objectTexture.data, + objectTexture.format, + objectTexture.mapping, + objectTexture.magFilter, + objectTexture.minFilter, + objectTexture.textureType, + objectTexture.anisotropy, + GameLib.API.Vector2.FromObjectVector(objectTexture.offset), + objectTexture.generateMipmaps, + objectTexture.flipY, + objectTexture.mipmaps, + objectTexture.unpackAlignment, + objectTexture.premultiplyAlpha, + objectTexture.encoding + ) +}; diff --git a/src/game-lib-d3-api-vertex.js b/src/game-lib-d3-api-vertex.js index 1d90746..457e5c6 100644 --- a/src/game-lib-d3-api-vertex.js +++ b/src/game-lib-d3-api-vertex.js @@ -19,3 +19,15 @@ GameLib.D3.API.Vertex = function( } this.boneWeights = boneWeights; }; + +/** + * Creates an API vertex from an Object vertex + * @param objectVertex + * @constructor + */ +GameLib.D3.API.Vertex.FromObjectVertex = function(objectVertex) { + return new GameLib.D3.API.Vertex( + GameLib.API.Vector3.FromObjectVector(objectVertex.position), + objectVertex.boneWeights + ); +}; diff --git a/src/game-lib-d3-bone.js b/src/game-lib-d3-bone.js index d049e84..a5c5076 100644 --- a/src/game-lib-d3-bone.js +++ b/src/game-lib-d3-bone.js @@ -4,7 +4,7 @@ * @param graphics GameLib.D3.Graphics * @param apiBone GameLib.D3.API.Bone */ -GameLib.D3.Bone = function RuntimeBone( +GameLib.D3.Bone = function ( graphics, apiBone ) { @@ -121,48 +121,16 @@ GameLib.D3.Bone.prototype.toApiBone = function() { * Returns a GameLib.D3.Bone from a bone Object * @param graphics GameLib.D3.Graphics * @param objectBone Object - * @returns GameLib.D3.Bone + * @returns {GameLib.D3.Bone} * @constructor */ GameLib.D3.Bone.FromObjectBone = function( graphics, objectBone ) { - var apiBone = new GameLib.D3.API.Bone( - objectBone.id, - objectBone.name, - objectBone.childBoneIds, - objectBone.parentBoneIds, - new GameLib.API.Vector3( - objectBone.position.x, - objectBone.position.y, - objectBone.position.z - ), - new GameLib.API.Quaternion( - objectBone.quaternion.x, - objectBone.quaternion.y, - objectBone.quaternion.z, - objectBone.quaternion.w, - new GameLib.API.Vector3( - objectBone.quaternion.axis.x, - objectBone.quaternion.axis.y, - objectBone.quaternion.axis.z - ), - objectBone.quaternion.angle - ), - new GameLib.API.Vector3( - objectBone.scale.x, - objectBone.scale.y, - objectBone.scale.z - ), - new GameLib.API.Vector3( - objectBone.up.x, - objectBone.up.y, - objectBone.up.z - ) - ); + var apiBone = GameLib.D3.API.Bone.FromObjectBone(objectBone); - var bone = new GameLib.D3.Bone( + var bone = GameLib.D3.Bone( graphics, apiBone ); diff --git a/src/game-lib-d3-camera.js b/src/game-lib-d3-camera.js index 8441957..2c22c3a 100644 --- a/src/game-lib-d3-camera.js +++ b/src/game-lib-d3-camera.js @@ -174,44 +174,7 @@ GameLib.D3.Camera.prototype.toApiCamera = function() { */ GameLib.D3.Camera.FromObjectCamera = function(graphics, objectCamera) { - var apiCamera = new GameLib.D3.API.Camera( - objectCamera.id, - objectCamera.cameraType, - objectCamera.name, - objectCamera.fov, - objectCamera.aspect, - objectCamera.near, - objectCamera.far, - new GameLib.API.Vector3( - objectCamera.position.x, - objectCamera.position.y, - objectCamera.position.z - ), - new GameLib.API.Vector3( - objectCamera.lookAt.x, - objectCamera.lookAt.y, - objectCamera.lookAt.z - ), - objectCamera.minX, - objectCamera.maxX, - objectCamera.minY, - objectCamera.maxY, - objectCamera.minZ, - objectCamera.maxZ, - new GameLib.API.Quaternion( - objectCamera.quaternion.x, - objectCamera.quaternion.y, - objectCamera.quaternion.z, - objectCamera.quaternion.w, - new GameLib.API.Vector3( - objectCamera.quaternion.axis.x, - objectCamera.quaternion.axis.y, - objectCamera.quaternion.axis.z - ), - objectCamera.quaternion.angle - ), - objectCamera.parentEntity - ); + var apiCamera = GameLib.D3.API.Camera.FromObjectCamera(objectCamera); return new GameLib.D3.Camera( graphics, diff --git a/src/game-lib-d3-color.js b/src/game-lib-d3-color.js index 4368bd7..c0013b5 100644 --- a/src/game-lib-d3-color.js +++ b/src/game-lib-d3-color.js @@ -6,20 +6,19 @@ * @param grain Number * @constructor */ -GameLib.D3.Color = function RuntimeColor(graphics, parentObject, apiColor, grain) { - - for (var property in apiColor) { - if (apiColor.hasOwnProperty(property)) { - this[property] = apiColor[property]; - } - } - - GameLib.Utils.Extend(GameLib.D3.Color, GameLib.D3.API.Color); +GameLib.D3.Color = function (graphics, parentObject, apiColor, grain) { this.graphics = graphics; - this.graphics.isNotThreeThrow(); + GameLib.D3.API.Color.call( + this, + apiColor.r, + apiColor.g, + apiColor.b, + apiColor.a + ); + this.parentObject = parentObject; if (GameLib.Utils.UndefinedOrNull(grain)) { @@ -30,6 +29,9 @@ GameLib.D3.Color = function RuntimeColor(graphics, parentObject, apiColor, grain this.instance = this.createInstance(); }; +GameLib.D3.Color.prototype = Object.create(GameLib.D3.API.Color.prototype); +GameLib.D3.Color.prototype.constructor = GameLib.D3.Color; + /** * Creates an instance color * @param update @@ -45,7 +47,7 @@ GameLib.D3.Color.prototype.createInstance = function(update) { instance.g = this.g; instance.b = this.b; } else { - instance = new this.graphics.instance.Color(this.r, this.g, this.b); + instance = THREE.Color(this.r, this.g, this.b); } return instance; diff --git a/src/game-lib-d3-image-factory.js b/src/game-lib-d3-image-factory.js new file mode 100644 index 0000000..90bf0cb --- /dev/null +++ b/src/game-lib-d3-image-factory.js @@ -0,0 +1,71 @@ +/** + * 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 + * @param baseUrl + * @returns {Function} + * @constructor + */ +GameLib.D3.ImageFactory = function ( + graphics, + baseUrl +) { + + graphics.isNotThreeThrow(); + + var promiseList = {}; + + return function(imagePath, progressCallback) { + + if (!imagePath) { + console.log('Attempted to download bad URL : ' + imagePath); + throw new Error('Bad URL : ' + imagePath); + } + + if (promiseList[imagePath]) { + return promiseList[imagePath]; + } + + var defer = Q.defer(); + + promiseList[imagePath] = defer.promise; + + GameLib.D3.ImageFactory.LoadImage(graphics, baseUrl + imagePath, defer, progressCallback); + + return promiseList[imagePath]; + } +}; + +/** + * 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 + '?ts=' + Date.now(), + 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); + } + ); +}; diff --git a/src/game-lib-d3-image.js b/src/game-lib-d3-image.js index b54d4d7..a92543f 100644 --- a/src/game-lib-d3-image.js +++ b/src/game-lib-d3-image.js @@ -1,72 +1,10 @@ -/** - * 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, baseUrl) { - - graphics.isNotThreeThrow(); - - var promiseList = {}; - - return function(imagePath, progressCallback) { - - if (!imagePath) { - console.log('Attempted to download bad URL : ' + imagePath); - throw new Error('Bad URL : ' + imagePath); - } - - if (promiseList[imagePath]) { - return promiseList[imagePath]; - } - - var defer = Q.defer(); - - promiseList[imagePath] = defer.promise; - - GameLib.D3.ImageFactory.LoadImage(graphics, baseUrl + imagePath, defer, progressCallback); - - return promiseList[imagePath]; - } -}; - -/** - * 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 + '?ts=' + Date.now(), - 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 filename * @param size * @param contentType + * @param textureLink * @constructor */ GameLib.D3.Image = function( diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js index afe337e..8cb7718 100644 --- a/src/game-lib-d3-light.js +++ b/src/game-lib-d3-light.js @@ -207,56 +207,7 @@ GameLib.D3.Light.FromObjectLight = function(graphics, objectLight) { return new GameLib.D3.Light( graphics, - new GameLib.D3.API.Light( - objectLight.id, - objectLight.lightType, - objectLight.name, - new GameLib.D3.API.Color( - objectLight.color.r, - objectLight.color.g, - objectLight.color.b, - objectLight.color.a - ), - objectLight.intensity, - new GameLib.API.Vector3( - objectLight.position.x, - objectLight.position.y, - objectLight.position.z - ), - new GameLib.API.Vector3( - objectLight.targetPosition.x, - objectLight.targetPosition.y, - objectLight.targetPosition.z - ), - new GameLib.API.Quaternion( - objectLight.quaternion.x, - objectLight.quaternion.y, - objectLight.quaternion.z, - objectLight.quaternion.w, - new GameLib.API.Vector3( - objectLight.quaternion.axis.x, - objectLight.quaternion.axis.y, - objectLight.quaternion.axis.z - ), - objectLight.quaternion.angle - ), - new GameLib.API.Vector3( - objectLight.rotation.x, - objectLight.rotation.y, - objectLight.rotation.z - ), - new GameLib.API.Vector3( - objectLight.scale.x, - objectLight.scale.y, - objectLight.scale.z - ), - objectLight.distance, - objectLight.decay, - objectLight.power, - objectLight.angle, - objectLight.penumbra, - objectLight.parentEntity - ) + GameLib.D3.API.Light.FromObjectLight(objectLight) ); }; \ No newline at end of file diff --git a/src/game-lib-d3-material.js b/src/game-lib-d3-material.js index 6050cbd..ac6cf53 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -3,21 +3,91 @@ * created * @param graphics GameLib.D3.Graphics * @param apiMaterial GameLib.D3.API.Material + * @param imageFactory GameLib.D3.ImageFactory * @constructor * @returns {GameLib.D3.Material | GameLib.D3.API.Material} */ GameLib.D3.Material = function Material( graphics, - apiMaterial + apiMaterial, + imageFactory ) { - for (var property in apiMaterial) { - if (apiMaterial.hasOwnProperty(property)) { - this[property] = apiMaterial[property]; - } - } this.graphics = graphics; this.graphics.isNotThreeThrow(); + + GameLib.D3.API.Material.call( + this, + apiMaterial.id, + apiMaterial.materialType, + apiMaterial.name, + apiMaterial.opacity, + apiMaterial.side, + apiMaterial.transparent, + apiMaterial.specular, + apiMaterial.lightMapIntensity, + apiMaterial.aoMapIntensity, + apiMaterial.color, + apiMaterial.emissive, + 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, + apiMaterial.alphaMap, + apiMaterial.aoMap, + apiMaterial.bumpMap, + apiMaterial.diffuseMap, + apiMaterial.displacementMap, + apiMaterial.emissiveMap, + apiMaterial.environmentMap, + apiMaterial.lightMap, + apiMaterial.metalnessMap, + apiMaterial.normalMap, + apiMaterial.roughnessMap, + apiMaterial.specularMap, + apiMaterial.parentEntity + ); this.specular = new GameLib.D3.Color( graphics, @@ -37,11 +107,109 @@ GameLib.D3.Material = function Material( this.emissive ); + this.alphaMap = new GameLib.D3.Texture( + this.graphics, + this.alphaMap, + this, + 'alphaMap', + imageFactory + ); + + this.aoMap = new GameLib.D3.Texture( + this.graphics, + this.aoMap, + this, + 'aoMap', + imageFactory + ); + + this.bumpMap = new GameLib.D3.Texture( + this.graphics, + this.bumpMap, + this, + 'bumpMap', + imageFactory + ); + + this.diffuseMap = new GameLib.D3.Texture( + this.graphics, + this.diffuseMap, + this, + 'map', + imageFactory + ); + + this.displacementMap = new GameLib.D3.Texture( + this.graphics, + this.displacementMap, + this, + 'displacementMap', + imageFactory + ); + + this.emissiveMap = new GameLib.D3.Texture( + this.graphics, + this.emissiveMap, + this, + 'emissiveMap', + imageFactory + ); + + this.environmentMap = new GameLib.D3.Texture( + this.graphics, + this.environmentMap, + this, + 'envMap', + imageFactory + ); + + this.lightMap = new GameLib.D3.Texture( + this.graphics, + this.lightMap, + this, + 'lightMap', + imageFactory + ); + + this.metalnessMap = new GameLib.D3.Texture( + this.graphics, + this.metalnessMap, + this, + 'metalnessMap', + imageFactory + ); + + this.normalMap = new GameLib.D3.Texture( + this.graphics, + this.normalMap, + this, + 'normalMap', + imageFactory + ); + + this.roughnessMap = new GameLib.D3.Texture( + this.graphics, + this.roughnessMap, + this, + 'roughnessMap', + imageFactory + ); + + this.specularMap = new GameLib.D3.Texture( + this.graphics, + this.specularMap, + this, + 'specularMap', + imageFactory + ); + this.instance = this.createInstance(); - this.needsUpdate = false; }; +GameLib.D3.Material.prototype = Object.create(GameLib.D3.API.Material.prototype); +GameLib.D3.Material.prototype.constructor = GameLib.D3.Material; + /** * Combine Method * @type {number} @@ -154,7 +322,7 @@ GameLib.D3.Material.prototype.createInstance = function(update) { if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { - instance = new this.graphics.instance.MeshStandardMaterial({ + instance = new THREE.MeshStandardMaterial({ name: this.name, opacity: this.opacity, transparent: this.transparent, @@ -199,7 +367,7 @@ GameLib.D3.Material.prototype.createInstance = function(update) { } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_PHONG) { - instance = new this.graphics.instance.MeshPhongMaterial({ + instance = new THREE.MeshPhongMaterial({ name: this.name, opacity: this.opacity, transparent: this.transparent, @@ -302,23 +470,55 @@ GameLib.D3.Material.prototype.createInstance = function(update) { console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up"); } } - + if (update) { for (var property in instance) { - if ( - instance.hasOwnProperty(property) && - this.hasOwnProperty(property) - ) { - if (property == 'size') { - instance[property] = this.pointSize; - } + if (instance.hasOwnProperty(property)) { - if (property == 'sizeAttenuation') { - instance[property] = this.pointSizeAttenuation; + if (property == 'alphaMap') { + instance.alphaMap = this.alphaMap.instance; } - - if (instance[property] instanceof THREE.Color) { + else if (property == 'aoMap') { + instance.aoMap = this.aoMap.instance; + } + else if (property == 'bumpMap') { + instance.bumpMap = this.bumpMap.instance; + } + else if (property == 'map') { + instance.map = this.diffuseMap.instance; + } + else if (property == 'displacementMap') { + instance.displacementMap = this.displacementMap.instance; + } + else if (property == 'emissiveMap') { + instance.emissiveMap = this.emissiveMap.instance; + } + else if (property == 'envMap') { + instance.envMap = this.environmentMap.instance; + } + else if (property == 'lightMap') { + instance.lightMap = this.lightMap.instance; + } + else if (property == 'metalnessMap') { + instance.metalnessMap = this.metalnessMap.instance; + } + else if (property == 'normalMap') { + instance.normalMap = this.normalMap.instance; + } + else if (property == 'roughnessMap') { + instance.roughnessMap = this.roughnessMap.instance; + } + else if (property == 'specularMap') { + instance.specularMap = this.specularMap.instance; + } + else if (property == 'size') { + instance.size = this.pointSize; + } + else if (property == 'sizeAttenuation') { + instance.sizeAttenuation = this.pointSizeAttenuation; + } + else if (instance[property] instanceof THREE.Color) { instance[property].copy(this[property]) } else { instance[property] = this[property]; @@ -356,7 +556,6 @@ GameLib.D3.Material.prototype.toApiMaterial = function() { this.opacity, this.side, this.transparent, - this.maps.toApiTextureMaps(), this.specular.toApiColor(), this.lightMapIntensity, this.aoMapIntensity, @@ -406,7 +605,20 @@ GameLib.D3.Material.prototype.toApiMaterial = function() { this.pointSize, this.pointSizeAttenuation, this.spriteRotation, - this.envMapIntensity + this.envMapIntensity, + this.alphaMap.toApiTexture(), + this.aoMap.toApiTexture(), + this.bumpMap.toApiTexture(), + this.diffuseMap.toApiTexture(), + this.displacementMap.toApiTexture(), + this.emissiveMap.toApiTexture(), + this.environmentMap.toApiTexture(), + this.lightMap.toApiTexture(), + this.metalnessMap.toApiTexture(), + this.normalMap.toApiTexture(), + this.roughnessMap.toApiTexture(), + this.specularMap.toApiTexture(), + GameLib.Utils.IdOrNull(this.parentEntity) ); }; @@ -418,98 +630,12 @@ GameLib.D3.Material.prototype.toApiMaterial = function() { * @constructor */ GameLib.D3.Material.FromObjectMaterial = function(graphics, objectMaterial, imageFactory) { - + var gameLibMaterial = new GameLib.D3.Material( graphics, - new GameLib.D3.API.Material( - objectMaterial.id, - objectMaterial.materialType, - objectMaterial.name, - objectMaterial.opacity, - objectMaterial.side, - objectMaterial.transparent, - GameLib.D3.API.TextureMapTemplate(), - new GameLib.D3.API.Color( - objectMaterial.specular.r, - objectMaterial.specular.g, - objectMaterial.specular.b, - objectMaterial.specular.a - ), - objectMaterial.lightMapIntensity, - objectMaterial.aoMapIntensity, - new GameLib.D3.API.Color( - objectMaterial.color.r, - objectMaterial.color.g, - objectMaterial.color.b, - objectMaterial.color.a - ), - new GameLib.D3.API.Color( - objectMaterial.emissive.r, - objectMaterial.emissive.g, - objectMaterial.emissive.b, - objectMaterial.emissive.a - ), - objectMaterial.emissiveIntensity, - objectMaterial.combine, - objectMaterial.shininess, - objectMaterial.reflectivity, - objectMaterial.refractionRatio, - objectMaterial.fog, - objectMaterial.wireframe, - objectMaterial.wireframeLineWidth, - objectMaterial.wireframeLineCap, - objectMaterial.wireframeLineJoin, - objectMaterial.vertexColors, - objectMaterial.skinning, - objectMaterial.morphTargets, - objectMaterial.morphNormals, - objectMaterial.lineWidth, - objectMaterial.lineCap, - objectMaterial.lineJoin, - objectMaterial.dashSize, - objectMaterial.gapWidth, - objectMaterial.blending, - objectMaterial.blendSrc, - objectMaterial.blendDst, - objectMaterial.blendEquation, - objectMaterial.depthTest, - objectMaterial.depthFunc, - objectMaterial.depthWrite, - objectMaterial.polygonOffset, - objectMaterial.polygonOffsetFactor, - objectMaterial.polygonOffsetUnits, - objectMaterial.alphaTest, - objectMaterial.clippingPlanes, - objectMaterial.clipShadows, - objectMaterial.visible, - objectMaterial.overdraw, - objectMaterial.shading, - objectMaterial.bumpScale, - objectMaterial.normalScale, - objectMaterial.displacementScale, - objectMaterial.displacementBias, - objectMaterial.roughness, - objectMaterial.metalness, - objectMaterial.pointSize, - objectMaterial.pointSizeAttenuation, - objectMaterial.spriteRotation, - objectMaterial.envMapIntensity - ) + GameLib.D3.API.Material.FromObjectMaterial(objectMaterial), + imageFactory ); - var objectMaps = objectMaterial.maps; - - var gameLibTextureMap = new GameLib.D3.TextureMapTemplate(graphics); - - for (var map in gameLibTextureMap) { - - if (gameLibTextureMap.hasOwnProperty(map) && objectMaps[map] && objectMaps[map].texture && objectMaps[map].texture.imagePath) { - - gameLibTextureMap[map].texture = GameLib.D3.Texture.FromObjectTexture(graphics, objectMaps[map].texture, gameLibMaterial, map, gameLibTextureMap, imageFactory); - } - } - - gameLibMaterial.maps = gameLibTextureMap; - return gameLibMaterial; }; diff --git a/src/game-lib-d3-mesh.js b/src/game-lib-d3-mesh.js index e20f014..942ce4f 100644 --- a/src/game-lib-d3-mesh.js +++ b/src/game-lib-d3-mesh.js @@ -3,12 +3,14 @@ * @param graphics GameLib.D3.Graphics * @param computeNormals Boolean * @param apiMesh GameLib.D3.API.Mesh + * @param imageFactory GameLib.D3.ImageFactory * @constructor */ -GameLib.D3.Mesh = function RuntimeMesh( +GameLib.D3.Mesh = function ( graphics, + apiMesh, computeNormals, - apiMesh + imageFactory ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); @@ -39,53 +41,77 @@ GameLib.D3.Mesh = function RuntimeMesh( apiMesh.renderOrder ); + this.materials = this.materials.map( + function (apiMaterial) { + return new GameLib.D3.Material( + this.graphics, + apiMaterial, + imageFactory + ) + }.bind(this) + ); + + this.skeleton = new GameLib.D3.Skeleton( + this.graphics, + this.skeleton + ); + + this.vertices = this.vertices.map( + function (apiVertex) { + return new GameLib.D3.Vertex( + this.graphics, + apiVertex + ); + }.bind(this) + ); + this.position = new GameLib.Vector3( - graphics, + this.graphics, this, this.position, 0.001 ); this.scale = new GameLib.Vector3( - graphics, + this.graphics, this, this.scale, 0.001 ); this.up = new GameLib.Vector3( - graphics, + this.graphics, this, this.up, 0.001 ); this.quaternion = new GameLib.Quaternion( - graphics, + this.graphics, this, this.quaternion ); this.localPosition = new GameLib.Vector3( - graphics, + this.graphics, this, this.localPosition ); this.localRotation = new GameLib.Vector3( - graphics, + this.graphics, this, this.localRotation ); this.localScale = new GameLib.Vector3( - graphics, + this.graphics, this, this.localScale ); this.modelMatrix = new GameLib.Matrix4( - graphics, + this.graphics, this, this.modelMatrix ); @@ -247,14 +273,12 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { this.computeNormals = false; } - var instanceMaterial = this.materials[0].instance; - if (this.meshType == GameLib.D3.Mesh.TYPE_NORMAL) { - instance = new THREE.Mesh(instanceGeometry, instanceMaterial); + instance = new THREE.Mesh(instanceGeometry, this.materials[0].instance); } if (this.meshType == GameLib.D3.Mesh.TYPE_CURVE) { - instance = new THREE.Points(instanceGeometry, instanceMaterial); + instance = new THREE.Points(instanceGeometry, this.materials[0].instance); } if (this.meshType == GameLib.D3.Mesh.TYPE_SKINNED) { @@ -287,7 +311,7 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { ); } - instance = new THREE.SkinnedMesh(instanceGeometry, instanceMaterial); + instance = new THREE.SkinnedMesh(instanceGeometry, this.materials[0].instance); instance.add(this.skeleton.rootBoneInstance); @@ -428,113 +452,13 @@ GameLib.D3.Mesh.prototype.toApiMesh = function() { */ GameLib.D3.Mesh.FromObjectMesh = function(graphics, objectMesh, computeNormals, imageFactory) { - var apiMesh = new GameLib.D3.API.Mesh( - objectMesh.id, - objectMesh.meshType, - objectMesh.name, - objectMesh.vertices.map( - function (objectVertex) { - return GameLib.D3.Vertex.FromObjectVertex( - graphics, - objectVertex - ); - } - ), - objectMesh.faces, - objectMesh.faceVertexUvs, - objectMesh.materials.map( - function (objectMaterial) { - return GameLib.D3.Material.FromObjectMaterial( - graphics, - objectMaterial, - imageFactory - ); - } - ), - objectMesh.parentMesh, - objectMesh.parentScene, - GameLib.D3.Skeleton.FromObjectSkeleton( - graphics, - objectMesh.skeleton - ), - objectMesh.skinIndices, - objectMesh.skinWeights, - new GameLib.API.Vector3( - objectMesh.position.x, - objectMesh.position.y, - objectMesh.position.z - ), - new GameLib.API.Quaternion( - objectMesh.quaternion.x, - objectMesh.quaternion.y, - objectMesh.quaternion.z, - objectMesh.quaternion.w, - new GameLib.API.Vector3( - objectMesh.quaternion.axis.x, - objectMesh.quaternion.axis.y, - objectMesh.quaternion.axis.z - ), - objectMesh.quaternion.angle - ), - new GameLib.API.Vector3( - objectMesh.scale.x, - objectMesh.scale.y, - objectMesh.scale.z - ), - new GameLib.API.Vector3( - objectMesh.localPosition.x, - objectMesh.localPosition.y, - objectMesh.localPosition.z - ), - new GameLib.API.Vector3( - objectMesh.localRotation.x, - objectMesh.localRotation.y, - objectMesh.localRotation.z - ), - new GameLib.API.Vector3( - objectMesh.localScale.x, - objectMesh.localScale.y, - objectMesh.localScale.z - ), - new GameLib.API.Vector3( - objectMesh.up.x, - objectMesh.up.y, - objectMesh.up.z - ), - new GameLib.API.Matrix4( - new GameLib.API.Vector4( - objectMesh.modelMatrix.rows[0].x, - objectMesh.modelMatrix.rows[0].y, - objectMesh.modelMatrix.rows[0].z, - objectMesh.modelMatrix.rows[0].w - ), - new GameLib.API.Vector4( - objectMesh.modelMatrix.rows[1].x, - objectMesh.modelMatrix.rows[1].y, - objectMesh.modelMatrix.rows[1].z, - objectMesh.modelMatrix.rows[1].w - ), - new GameLib.API.Vector4( - objectMesh.modelMatrix.rows[2].x, - objectMesh.modelMatrix.rows[2].y, - objectMesh.modelMatrix.rows[2].z, - objectMesh.modelMatrix.rows[2].w - ), - new GameLib.API.Vector4( - objectMesh.modelMatrix.rows[3].x, - objectMesh.modelMatrix.rows[3].y, - objectMesh.modelMatrix.rows[3].z, - objectMesh.modelMatrix.rows[3].w - ) - ), - objectMesh.parentEntity, - objectMesh.renderOrder - ); + var apiMesh = GameLib.D3.API.Mesh.FromObjectMesh(objectMesh); return new GameLib.D3.Mesh( graphics, + apiMesh, computeNormals, - apiMesh + imageFactory ); }; diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 611565e..da2d137 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -5,13 +5,15 @@ * @param progressCallback * @param apiScene GameLib.D3.API.Scene * @param imageFactory + * @param computeNormals * @constructor */ GameLib.D3.Scene = function Scene( graphics, progressCallback, apiScene, - imageFactory + imageFactory, + computeNormals ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); @@ -34,6 +36,40 @@ GameLib.D3.Scene = function Scene( apiScene.activeCameraIndex ); + this.meshes = this.meshes.map( + function(apiMesh) { + return new GameLib.D3.Mesh( + this.graphics, + apiMesh, + computeNormals, + imageFactory + ) + }.bind(this) + ); + + this.lights = this.lights.map( + function(apiLight) { + return new GameLib.D3.Light( + this.graphics, + apiLight + ) + }.bind(this) + ); + + this.entityManager = new GameLib.EntityManager( + this, + this.entityManager + ); + + this.cameras = this.cameras.map( + function(apiCamera) { + return new GameLib.D3.Camera( + this.graphics, + apiCamera + ) + }.bind(this) + ); + this.position = new GameLib.Vector3( graphics, this, @@ -219,81 +255,7 @@ GameLib.D3.Scene.FromObjectScene = function( imageFactory, progressCallback ) { - var apiScene = new GameLib.D3.API.Scene( - objectScene.id, - objectScene.path, - objectScene.name, - objectScene.meshes.map( - function (objectMesh) { - return GameLib.D3.Mesh.FromObjectMesh( - graphics, - objectMesh, - computeNormals, - imageFactory - ) - } - ), - new GameLib.API.Vector3( - objectScene.position.x, - objectScene.position.y, - objectScene.position.z - ), - new GameLib.API.Quaternion( - objectScene.quaternion.x, - objectScene.quaternion.y, - objectScene.quaternion.z, - objectScene.quaternion.w, - new GameLib.API.Vector3( - objectScene.quaternion.axis.x, - objectScene.quaternion.axis.y, - objectScene.quaternion.axis.z - ), - objectScene.quaternion.angle - ), - new GameLib.API.Vector3( - objectScene.scale.x, - objectScene.scale.y, - objectScene.scale.z - ), - objectScene.parentSceneId, - objectScene.lights.map( - function (objectLight) { - return GameLib.D3.Light.FromObjectLight( - graphics, - objectLight - ) - } - ), - objectScene.worlds.map( - function (objectWorld) { - return GameLib.D3.World.FromObjectWorld( - graphics, - objectWorld - ); - } - ), - GameLib.EntityManager.FromObjectEntityManager( - graphics, - objectScene.entityManager - ), - objectScene.shapes.map( - function (objectShape) { - return GameLib.D3.Shape.FromObjectShape( - graphics, - objectShape - ); - } - ), - objectScene.cameras.map( - function (objectCamera) { - return GameLib.D3.Camera.FromObjectCamera( - graphics, - objectCamera - ); - } - ), - objectScene.activeCameraIndex - ); + var apiScene = GameLib.D3.API.Scene.FromObjectScene(objectScene); return new GameLib.D3.Scene( graphics, diff --git a/src/game-lib-d3-skeleton.js b/src/game-lib-d3-skeleton.js index 878eb88..d6954fc 100644 --- a/src/game-lib-d3-skeleton.js +++ b/src/game-lib-d3-skeleton.js @@ -160,79 +160,7 @@ GameLib.D3.Skeleton.FromObjectSkeleton = function( return null; } - var apiSkeleton = new GameLib.D3.API.Skeleton( - objectSkeleton.id, - objectSkeleton.name, - objectSkeleton.bones.map( - function (objectBone) { - return GameLib.D3.Bone.FromObjectBone(graphics, objectBone); - } - ), - objectSkeleton.boneInverses.map( - function (boneInverse) { - return new GameLib.D3.API.Matrix4( - new GameLib.D3.Vector4( - boneInverse[0], - boneInverse[1], - boneInverse[2], - boneInverse[3] - ), - new GameLib.D3.Vector4( - boneInverse[4], - boneInverse[5], - boneInverse[6], - boneInverse[7] - ), - new GameLib.D3.Vector4( - boneInverse[8], - boneInverse[9], - boneInverse[10], - boneInverse[11] - ), - new GameLib.D3.Vector4( - boneInverse[12], - boneInverse[13], - boneInverse[14], - boneInverse[15] - ) - ); - } - ), - objectSkeleton.useVertexTexture, - objectSkeleton.boneTextureWidth, - objectSkeleton.boneTextureHeight, - objectSkeleton.boneMatrices.map( - function (boneMatrix) { - return new GameLib.D3.API.Matrix4( - new GameLib.D3.Vector4( - boneMatrix[0], - boneMatrix[1], - boneMatrix[2], - boneMatrix[3] - ), - new GameLib.D3.Vector4( - boneMatrix[4], - boneMatrix[5], - boneMatrix[6], - boneMatrix[7] - ), - new GameLib.D3.Vector4( - boneMatrix[8], - boneMatrix[9], - boneMatrix[10], - boneMatrix[11] - ), - new GameLib.D3.Vector4( - boneMatrix[12], - boneMatrix[13], - boneMatrix[14], - boneMatrix[15] - ) - ); - } - ), - objectSkeleton.boneTexture - ); + var apiSkeleton = GameLib.D3.API.Skeleton.FromObjectSkeleton(objectSkeleton); var skeleton = new GameLib.D3.Skeleton( graphics, diff --git a/src/game-lib-d3-texture-map-template.js b/src/game-lib-d3-texture-map-template.js deleted file mode 100644 index 606ad37..0000000 --- a/src/game-lib-d3-texture-map-template.js +++ /dev/null @@ -1,64 +0,0 @@ -/** - * 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 - * @constructor - */ -GameLib.D3.TextureMapTemplate = function TextureMapTemplate( - graphics -) { - - this.graphics = graphics; - this.graphics.isNotThreeThrow(); - - return new GameLib.D3.TextureMaps( - new GameLib.D3.TextureMap( - null, - 'alphaMap' - ), - new GameLib.D3.TextureMap( - null, - 'aoMap' - ), - new GameLib.D3.TextureMap( - null, - 'bumpMap' - ), - new GameLib.D3.TextureMap( - null, - 'map' - ), - new GameLib.D3.TextureMap( - null, - 'displacementMap' - ), - new GameLib.D3.TextureMap( - null, - 'emissiveMap' - ), - new GameLib.D3.TextureMap( - null, - 'envMap' - ), - new GameLib.D3.TextureMap( - null, - 'lightMap' - ), - new GameLib.D3.TextureMap( - null, - 'metalnessMap' - ), - new GameLib.D3.TextureMap( - null, - 'normalMap' - ), - new GameLib.D3.TextureMap( - null, - 'roughnessMap' - ), - new GameLib.D3.TextureMap( - null, - 'specularMap' - ) - ); -}; diff --git a/src/game-lib-d3-texture-map.js b/src/game-lib-d3-texture-map.js deleted file mode 100644 index 19c4cbd..0000000 --- a/src/game-lib-d3-texture-map.js +++ /dev/null @@ -1,33 +0,0 @@ -/** - * We have a mapping between GameLib.D3.Texture and the Instance material map. - * Our GameLib.D3.Material.map.alpha corresponds to THREE.Material.alphaMap - * @constructor - * @param texture GameLib.D3.Texture - * @param instanceMapId string - */ -GameLib.D3.TextureMap = function TextureMap( - texture, - instanceMapId -) { - if (GameLib.Utils.UndefinedOrNull(texture)) { - texture = null; - } - this.texture = texture; - - if (GameLib.Utils.UndefinedOrNull(instanceMapId)) { - instanceMapId = null; - } - this.instanceMapId = instanceMapId; -}; - -GameLib.D3.TextureMap.prototype.toApiTextureMap = function() { - - var apiTexture = null; - - if (this.texture) { - apiTexture = this.texture.toApiTexture(); - } - - return new GameLib.D3.TextureMap(apiTexture, this.instanceMapId); - -}; \ No newline at end of file diff --git a/src/game-lib-d3-texture-maps.js b/src/game-lib-d3-texture-maps.js deleted file mode 100644 index 0051b86..0000000 --- a/src/game-lib-d3-texture-maps.js +++ /dev/null @@ -1,66 +0,0 @@ -/** - * We have a mapping between GameLib.D3.Texture and the Instance material map. - * Our GameLib.D3.Material.map.alpha corresponds to THREE.Material.alphaMap - * @returns {{alpha: {texture: null, instanceMapId: string}, ao: {texture: null, instanceMapId: string}, bump: {texture: null, instanceMapId: string}, diffuse: {texture: null, instanceMapId: string}, displacement: {texture: null, instanceMapId: string}, emissive: {texture: null, instanceMapId: string}, environment: {texture: null, instanceMapId: string}, light: {texture: null, instanceMapId: string}, metalness: {texture: null, instanceMapId: string}, normal: {texture: null, instanceMapId: string}, roughness: {texture: null, instanceMapId: string}, specular: {texture: null, instanceMapId: string}}} - * @constructor - * @param alpha - * @param ao - * @param bump - * @param diffuse - * @param displacement - * @param emissive - * @param environment - * @param light - * @param metalness - * @param normal - * @param roughness - * @param specular - */ -GameLib.D3.TextureMaps = function TextureMaps( - alpha, - ao, - bump, - diffuse, - displacement, - emissive, - environment, - light, - metalness, - normal, - roughness, - specular -) { - this.alpha = alpha; - this.ao = ao; - this.bump = bump; - this.diffuse = diffuse; - this.displacement = displacement; - this.emissive = emissive; - this.environment = environment; - this.light = light; - this.metalness = metalness; - this.normal = normal; - this.roughness = roughness; - this.specular = specular; -}; - -/** - * Returns API textures from the Texture Maps - * @returns {GameLib.D3.TextureMaps} - */ -GameLib.D3.TextureMaps.prototype.toApiTextureMaps = function() { - return new GameLib.D3.TextureMaps( - this.alpha.toApiTextureMap(), - this.ao.toApiTextureMap(), - this.bump.toApiTextureMap(), - this.diffuse.toApiTextureMap(), - this.displacement.toApiTextureMap(), - this.emissive.toApiTextureMap(), - this.environment.toApiTextureMap(), - this.light.toApiTextureMap(), - this.metalness.toApiTextureMap(), - this.normal.toApiTextureMap(), - this.roughness.toApiTextureMap(), - this.specular.toApiTextureMap() - ); -}; \ No newline at end of file diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index 419ce2f..d2705eb 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -9,20 +9,39 @@ * @constructor */ GameLib.D3.Texture = function Texture( - apiTexture, graphics, + apiTexture, parentMaterial, parentMaterialInstanceMapId, imageFactory ) { - for (var property in apiTexture) { - if (apiTexture.hasOwnProperty(property)) { - this[property] = apiTexture[property]; - } - } - this.graphics = graphics; this.graphics.isNotThreeThrow(); + + GameLib.API.Texture.call( + this, + apiTexture.id, + apiTexture.typeId, + apiTexture.name, + apiTexture.imagePath, + apiTexture.wrapS, + apiTexture.wrapT, + apiTexture.repeat, + apiTexture.data, + apiTexture.format, + apiTexture.mapping, + apiTexture.magFilter, + apiTexture.minFilter, + apiTexture.textureType, + apiTexture.anisotropy, + apiTexture.offset, + apiTexture.generateMipmaps, + apiTexture.flipY, + apiTexture.mipmaps, + apiTexture.unpackAlignment, + apiTexture.premultiplyAlpha, + apiTexture.encoding + ); this.offset = new GameLib.Vector2( graphics, @@ -47,6 +66,13 @@ GameLib.D3.Texture = function Texture( this.loadTexture(imageFactory); }; +GameLib.D3.Texture.prototype = Object.create(GameLib.D3.API.Texture.prototype); +GameLib.D3.Texture.prototype.constructor = GameLib.D3.Texture; + +/** + * Loads a texture from the image factory, it could already have downloaded, and then it updates the instance + * @param imageFactory + */ GameLib.D3.Texture.prototype.loadTexture = function(imageFactory) { this.imageData = imageFactory(this.imagePath); @@ -238,14 +264,12 @@ GameLib.D3.Texture.prototype.toApiTexture = function() { }; - /** * Converts from an Object texture to a GameLib.D3.Texture * @param graphics GameLib.D3.Graphics * @param objectTexture Object * @param gameLibMaterial GameLib.D3.Material - * @param map GameLib.D3.TextureMap - * @param gameLibTextureMap GameLib.D3.TextureMapTemplate + * @param instanceMapId String * @param imageFactory GameLib.D3.ImageFactory * @constructor */ @@ -253,43 +277,14 @@ GameLib.D3.Texture.FromObjectTexture = function( graphics, objectTexture, gameLibMaterial, - map, - gameLibTextureMap, + instanceMapId, imageFactory ) { return new GameLib.D3.Texture( - new GameLib.D3.API.Texture( - objectTexture.id, - map, - objectTexture.name, - objectTexture.imagePath, - objectTexture.wrapS, - objectTexture.wrapT, - new GameLib.API.Vector2( - objectTexture.repeat.x, - objectTexture.repeat.y - ), - objectTexture.data, - objectTexture.format, - objectTexture.mapping, - objectTexture.magFilter, - objectTexture.minFilter, - objectTexture.textureType, - objectTexture.anisotropy, - new GameLib.API.Vector2( - objectTexture.offset.x, - objectTexture.offset.y - ), - objectTexture.generateMipmaps, - objectTexture.flipY, - objectTexture.mipmaps, - objectTexture.unpackAlignment, - objectTexture.premultiplyAlpha, - objectTexture.encoding - ), graphics, + GameLib.D3.API.Texture.FromObjectTexture(objectTexture), gameLibMaterial, - gameLibTextureMap[map].instanceMapId, + instanceMapId, imageFactory ); -}; \ No newline at end of file +}; diff --git a/src/game-lib-d3-vertex.js b/src/game-lib-d3-vertex.js index add914a..2fbdb50 100644 --- a/src/game-lib-d3-vertex.js +++ b/src/game-lib-d3-vertex.js @@ -83,14 +83,7 @@ GameLib.D3.Vertex.FromObjectVertex = function( graphics, objectVertex ) { - var apiVertex = new GameLib.D3.API.Vertex( - new GameLib.API.Vector3( - objectVertex.position.x, - objectVertex.position.y, - objectVertex.position.z - ), - objectVertex.boneWeights - ); + var apiVertex = GameLib.D3.API.Vertex.FromObjectVertex(objectVertex); return new GameLib.D3.Vertex( graphics, diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 7e7bf92..9673719 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -19,6 +19,16 @@ GameLib.EntityManager = function( apiEntityManager.entities ); + this.entities = this.entities.map( + function(apiEntity) { + return new GameLib.Entity( + this, + this, + apiEntity + ) + } + ); + this.instance = this.createInstance(); }; @@ -165,45 +175,8 @@ GameLib.EntityManager.prototype.toApiEntityManager = function() { GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntities) { var apiEntities = objectEntities.entities.map( - function (objectEntity) { - - objectEntity.components = objectEntity.components.map( - function (component) { - if (component instanceof Object) { - if (component.componentType === GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) { - return GameLib.D3.PathFollowing.FromObjectComponent(graphics, component); - } else if (component.componentType === GameLib.Component.COMPONENT_RENDERABLE) { - return GameLib.D3.Renderable.FromObjectComponent(graphics, component); - } else if (component.componentType === GameLib.Component.COMPONENT_RENDERER) { - return GameLib.D3.Renderer.FromObjectComponent(graphics, component); - } else if (component.componentType === GameLib.Component.COMPONENT_EDITOR_INPUT) { - return GameLib.D3.Input.Editor.FromObjectComponent(graphics, component); - } else if (component.componentType === GameLib.Component.COMPONENT_LOOK_AT) { - return GameLib.D3.LookAt.FromObjectComponent(graphics, component); - } else if (component.componentType === GameLib.Component.COMPONENT_FOLLOW) { - return GameLib.D3.Follow.FromObjectComponent(graphics, component); - } else if (component.componentType === GameLib.Component.COMPONENT_MESH) { - return GameLib.D3.Mesh.FromObjectComponent(graphics, component); - } else if (component.componentType === GameLib.Component.COMPONENT_SPLINE) { - return GameLib.D3.Spline.FromObjectComponent(graphics, component); - } else if (component.componentType === GameLib.Component.COMPONENT_INPUT_DRIVE) { - return GameLib.D3.Input.Drive.FromObjectComponent(graphics, component); - }else { - console.warn('no component was associated with this object'); - throw new Error('no component was associated with this object'); - } - } else { - return component; - } - } - ); - - return new GameLib.API.Entity( - objectEntity.id, - objectEntity.name, - objectEntity.components - ) + return GameLib.API.Entity.FromObjectEntity(objectEntity); } ); @@ -216,16 +189,6 @@ GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntitie apiEntityManager ); - entityManager.entities = entityManager.entities.map( - function(apiEntity) { - return new GameLib.Entity( - entityManager, - entityManager, - apiEntity - ) - } - ); - return entityManager; }; diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index c4e9ea9..789a2ad 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -2,6 +2,13 @@ * Entity Runtime * @constructor */ +/** + * + * @param entityManager GameLib.D3.EntityManager + * @param parentObject + * @param apiEntity GameLib.D3.API.Entity + * @constructor + */ GameLib.Entity = function RuntimeEntity( entityManager, parentObject, @@ -24,6 +31,40 @@ GameLib.Entity = function RuntimeEntity( apiEntity.components ); + this.components = this.components.map( + function (component) { + if (component instanceof Object) { + + component.constructor.FromObjectComponent(entityManager.graphics, component); + // + // if (component.componentType === GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) { + // return GameLib.D3.PathFollowing.FromObjectComponent(entityManager.graphics, component); + // } else if (component.componentType === GameLib.Component.COMPONENT_RENDERABLE) { + // return GameLib.D3.Renderable.FromObjectComponent(entityManager.graphics, component); + // } else if (component.componentType === GameLib.Component.COMPONENT_RENDERER) { + // return GameLib.D3.Renderer.FromObjectComponent(entityManager.graphics, component); + // } else if (component.componentType === GameLib.Component.COMPONENT_EDITOR_INPUT) { + // return GameLib.D3.Input.Editor.FromObjectComponent(entityManager.graphics, component); + // } else if (component.componentType === GameLib.Component.COMPONENT_LOOK_AT) { + // return GameLib.D3.LookAt.FromObjectComponent(entityManager.graphics, component); + // } else if (component.componentType === GameLib.Component.COMPONENT_FOLLOW) { + // return GameLib.D3.Follow.FromObjectComponent(entityManager.graphics, component); + // } else if (component.componentType === GameLib.Component.COMPONENT_MESH) { + // return GameLib.D3.Mesh.FromObjectComponent(entityManager.graphics, component); + // } else if (component.componentType === GameLib.Component.COMPONENT_SPLINE) { + // return GameLib.D3.Spline.FromObjectComponent(entityManager.graphics, component); + // } else if (component.componentType === GameLib.Component.COMPONENT_INPUT_DRIVE) { + // return GameLib.D3.Input.Drive.FromObjectComponent(entityManager.graphics, component); + // }else { + // console.warn('no component was associated with this object'); + // throw new Error('no component was associated with this object'); + // } + } else { + return component; + } + }.bind(this) + ); + this.instance = this.createInstance(); }; @@ -103,11 +144,7 @@ GameLib.Entity.prototype.toApiEntity = function() { */ GameLib.Entity.FromObjectEntity = function(entityManager, objectEntity) { - var apiEntity = new GameLib.API.Entity( - objectEntity.id, - objectEntity.name, - objectEntity.components - ); + var apiEntity = GameLib.API.Entity.FromObjectEntity(objectEntity); return new GameLib.Entity( entityManager,