diff --git a/src/game-lib-api-component.js b/src/game-lib-api-component.js index a8a7545..88c0134 100644 --- a/src/game-lib-api-component.js +++ b/src/game-lib-api-component.js @@ -47,12 +47,12 @@ GameLib.API.Component.FromObjectComponent = function(objectComponent) { return GameLib.D3.API.Renderer.FromObjectComponent(objectComponent); } - if (objectComponent.componentType === GameLib.Component.COMPONENT_STEREO_RENDERER) { - return GameLib.D3.API.Renderer.FromObjectComponent(objectComponent); + if (objectComponent.componentType === GameLib.Component.COMPONENT_COMPOSER) { + return GameLib.D3.API.Composer.FromObjectComponent(objectComponent); } - if (objectComponent.componentType === GameLib.Component.COMPONENT_STEREO_CAMERA) { - return GameLib.D3.API.Camera.FromObjectComponent(objectComponent); + if (objectComponent.componentType === GameLib.Component.COMPONENT_PASS) { + return GameLib.D3.API.Pass.FromObjectComponent(objectComponent); } if (objectComponent.componentType === GameLib.Component.COMPONENT_LOOK_AT) { @@ -63,8 +63,8 @@ GameLib.API.Component.FromObjectComponent = function(objectComponent) { return GameLib.D3.API.Follow.FromObjectComponent(objectComponent); } - if (objectComponent.componentType === GameLib.Component.COMPONENT_MESH) { - return GameLib.D3.API.Mesh.FromObjectComponent(objectComponent); + if (objectComponent.componentType === GameLib.Component.COMPONENT_RENDER_TARGET) { + return GameLib.D3.API.RenderTarget.FromObjectComponent(objectComponent); } if (objectComponent.componentType === GameLib.Component.COMPONENT_SPLINE) { diff --git a/src/game-lib-component-a.js b/src/game-lib-component-a.js index 2e04e0e..ee70af4 100644 --- a/src/game-lib-component-a.js +++ b/src/game-lib-component-a.js @@ -36,9 +36,13 @@ GameLib.Component.COMPONENT_MESH = 0x8; GameLib.Component.COMPONENT_SPLINE = 0x9; GameLib.Component.COMPONENT_LIGHT = 0xa; GameLib.Component.COMPONENT_INPUT_DRIVE = 0xb; -GameLib.Component.COMPONENT_STEREO_RENDERER = 0xc; -GameLib.Component.COMPONENT_STEREO_CAMERA = 0xd; -GameLib.Component.COMPONENT_ORTHOGONAL_CAMERA = 0xe; +GameLib.Component.COMPONENT_COMPOSER = 0xc; +GameLib.Component.COMPONENT_RENDER_TARGET = 0xd; +GameLib.Component.COMPONENT_PASS = 0xe; +GameLib.Component.COMPONENT_SCENE = 0xf; +GameLib.Component.COMPONENT_GAME = 0x10; +GameLib.Component.COMPONENT_INPUT_EDITOR = 0x11; +GameLib.Component.COMPONENT_EDITOR = 0x12; /** * Components are linked at runtime - for storing, we just store the ID diff --git a/src/game-lib-d3-api-composer.js b/src/game-lib-d3-api-composer.js new file mode 100644 index 0000000..cfd0e80 --- /dev/null +++ b/src/game-lib-d3-api-composer.js @@ -0,0 +1,75 @@ +/** + * This component renders a scene + * @param id String + * @param name String + * @param renderer GameLib.D3.Renderer + * @param renderTarget GameLib.D3.API.RenderTarget + * @param passes GameLib.D3.API.Pass[] + * @param parentEntity + * @constructor + */ +GameLib.D3.API.Composer = function ( + id, + name, + renderer, + renderTarget, + passes, + parentEntity +) { + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_COMPOSER, + { + 'renderer' : GameLib.D3.Renderer, + 'renderTarget' : GameLib.D3.RenderTarget, + 'passes' : [GameLib.D3.Pass] + }, + null, + parentEntity + ); + + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Composer (' + id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(renderer)) { + renderer = null; + } + this.renderer = renderer; + + if (GameLib.Utils.UndefinedOrNull(renderTarget)) { + renderTarget = null; + } + this.renderTarget = renderTarget; + + if (GameLib.Utils.UndefinedOrNull(passes)) { + passes = []; + } + this.passes = passes; +}; + +GameLib.D3.API.Composer.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.Composer.prototype.constructor = GameLib.D3.API.Composer; + +/** + * Object to GameLib.D3.API.Composer + * @param objectComponent + * @constructor + */ +GameLib.D3.API.Composer.FromObjectComponent = function(objectComponent) { + return new GameLib.D3.API.Composer( + objectComponent.id, + objectComponent.name, + objectComponent.renderer, + objectComponent.renderTarget, + objectComponent.passes, + objectComponent.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-editor.js b/src/game-lib-d3-api-editor.js new file mode 100644 index 0000000..d6ba9f6 --- /dev/null +++ b/src/game-lib-d3-api-editor.js @@ -0,0 +1,63 @@ +/** + * Raw Editor API object - should always correspond with the Editor Schema + * @param id + * @param name + * @param games [GameLib.API.D3.Game] + * @param allSelected + * @param parentEntity + * @constructor + */ +GameLib.D3.API.Editor = function( + id, + name, + games, + allSelected, + parentEntity +) { + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_EDITOR, + null, + null, + parentEntity + ); + + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Editor (' + this.id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(games)) { + games = []; + } + this.games = games; + + if (GameLib.Utils.UndefinedOrNull(allSelected)) { + allSelected = false; + } + this.allSelected = allSelected; +}; + +GameLib.D3.API.Editor.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.Editor.prototype.constructor = GameLib.D3.API.Editor; + +/** + * Creates an API Editor from an Object Editor + * @param objectEditor + * @constructor + */ +GameLib.D3.API.Editor.FromObjectEditor = function(objectEditor) { + return new GameLib.D3.API.Editor( + objectEditor.id, + objectEditor.name, + objectEditor.games, + objectEditor.allSelected, + objectEditor.parentEntity + ); +}; + diff --git a/src/game-lib-d3-api-game.js b/src/game-lib-d3-api-game.js new file mode 100644 index 0000000..5c25521 --- /dev/null +++ b/src/game-lib-d3-api-game.js @@ -0,0 +1,223 @@ +/** + * Raw Game API object - should always correspond with the Game Schema + * @param id + * @param name + * @param gameType + * @param width + * @param height + * @param scenes + * @param cameras + * @param renderers + * @param composers + * @param systems + * @param entityManager + * @param mouse + * @param raycaster + * @param activeCameraIndex + * @param activeRendererIndex + * @param parentEntity + * @constructor + */ +GameLib.D3.API.Game = function( + id, + name, + gameType, + width, + height, + scenes, + cameras, + renderers, + composers, + systems, + entityManager, + mouse, + raycaster, + activeCameraIndex, + activeRendererIndex, + parentEntity +) { + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_GAME, + { + 'scenes' : [GameLib.D3.Scene], + 'cameras' : [GameLib.D3.Camera], + 'renderers' : [GameLib.D3.Renderer], + 'composers' : [GameLib.D3.Composer], + 'systems' : [GameLib.D3.System], + 'entityManager' : GameLib.EntityManager, + 'mouse' : GameLib.D3.Mouse, + 'raycaster' : GameLib.D3.Raycaster + }, + null, + parentEntity + ); + + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Game (' + this.id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(gameType)) { + gameType = GameLib.D3.Game.GAME_TYPE_VR_PONG; + } + this.gameType = gameType; + + if (GameLib.Utils.UndefinedOrNull(width)) { + width = 800; + } + this.width = width; + + if (GameLib.Utils.UndefinedOrNull(height)) { + height = 600; + } + this.height = height; + + if (GameLib.Utils.UndefinedOrNull(scenes)) { + scenes = []; + } + this.scenes = scenes; + + if (GameLib.Utils.UndefinedOrNull(cameras)) { + cameras = []; + } + this.cameras = cameras; + + if (GameLib.Utils.UndefinedOrNull(renderers)) { + renderers = []; + } + this.renderers = renderers; + + if (GameLib.Utils.UndefinedOrNull(composers)) { + composers = []; + } + this.composers = composers; + + if (GameLib.Utils.UndefinedOrNull(systems)) { + systems = []; + } + this.systems = systems; + + if (GameLib.Utils.UndefinedOrNull(entityManager)) { + entityManager = new GameLib.API.EntityManager(); + } + this.entityManager = entityManager; + + if (GameLib.Utils.UndefinedOrNull(mouse)) { + mouse = null; + } + this.mouse = mouse; + + if (GameLib.Utils.UndefinedOrNull(raycaster)) { + raycaster = null; + } + this.raycaster = raycaster; + + if (GameLib.Utils.UndefinedOrNull(activeCameraIndex)) { + activeCameraIndex = 0; + } + this.activeCameraIndex = activeCameraIndex; + + if (GameLib.Utils.UndefinedOrNull(activeRendererIndex)) { + activeRendererIndex = 0; + } + this.activeRendererIndex = activeRendererIndex; +}; + +GameLib.D3.API.Game.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.Game.prototype.constructor = GameLib.D3.API.Game; + +/** + * Creates an API camera from an Object camera + * @param objectGame + * @constructor + */ +GameLib.D3.API.Game.FromObjectGame = function(objectGame) { + + var apiScenes = []; + var apiCameras = []; + var apiRenderers = []; + var apiComposers = []; + var apiSystems = []; + + var apiEntityManager = null; + var apiMouse = null; + var apiRaycaster = null; + + if (objectGame.scenes) { + apiScenes = objectGame.scenes.map( + function(objectScene){ + return GameLib.API.Scene.FromObjectScene(objectScene); + } + ); + } + + if (objectGame.cameras) { + apiCameras = objectGame.cameras.map( + function(objectCamera){ + return GameLib.API.Camera.FromObjectCamera(objectCamera); + } + ); + } + + if (objectGame.renderers) { + apiRenderers = objectGame.renderers.map( + function(objectRenderer){ + return GameLib.API.Renderer.FromObjectComponent(objectRenderer); + } + ); + } + + if (objectGame.composers) { + apiComposers = objectGame.composers.map( + function(objectComposer){ + return GameLib.API.Composer.FromObjectComponent(objectComposer); + } + ); + } + + if (objectGame.systems) { + apiSystems = objectGame.systems.map( + function(objectSystem){ + return GameLib.API.System.FromObjectComponent(objectSystem); + } + ); + } + + if (objectGame.entityManager) { + apiEntityManager = GameLib.API.Entity.FromObjectEntity(objectGame.entityManager); + } + + if (objectGame.mouse) { + apiMouse = GameLib.API.Mouse.FromObjectMouse(objectGame.mouse); + } + + if (objectGame.raycaster) { + apiRaycaster = GameLib.API.Raycaster.FromObjectRaycaster(objectGame.raycaster); + } + + return new GameLib.D3.API.Game( + objectGame.id, + objectGame.name, + objectGame.gameType, + objectGame.width, + objectGame.height, + apiScenes, + apiCameras, + apiRenderers, + apiComposers, + apiSystems, + apiEntityManager, + apiMouse, + apiRaycaster, + objectGame.activeCameraIndex, + objectGame.activeRendererIndex, + objectGame.parentEntity + ); + +}; diff --git a/src/game-lib-d3-api-input-editor.js b/src/game-lib-d3-api-input-editor.js new file mode 100644 index 0000000..bcb577b --- /dev/null +++ b/src/game-lib-d3-api-input-editor.js @@ -0,0 +1,65 @@ +/** + * This component makes the parentEntity (ex. car) follow the path provided by the spline + * @param id String + * @param name String + * @param domElementId + * @param camera + * @param parentEntity + * @constructor + */ +GameLib.D3.API.Input.Editor = function ( + id, + name, + domElementId, + camera, + parentEntity +) { + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_INPUT_EDITOR, + { + 'camera' : GameLib.D3.Camera + }, + null, + parentEntity + ); + + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = this.constructor.name; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(domElementId)) { + domElementId = 'divCanvas'; + } + this.domElementId = domElementId; + + if (GameLib.Utils.UndefinedOrNull(camera)) { + camera = null; + } + this.camera = camera; +}; + +GameLib.D3.API.Input.Editor.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.Input.Editor.prototype.constructor = GameLib.D3.API.Input.Editor; + +/** + * Object to GameLib.D3.API.Input.Editor + * @param objectComponent + * @returns {GameLib.D3.API.Input.Editor} + * @constructor + */ +GameLib.D3.API.Input.Editor.FromObjectComponent = function(objectComponent) { + return new GameLib.D3.API.Input.Editor( + objectComponent.id, + objectComponent.name, + objectComponent.domElementId, + objectComponent.camera, + objectComponent.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-mesh.js b/src/game-lib-d3-api-mesh.js index 514750d..aa56496 100644 --- a/src/game-lib-d3-api-mesh.js +++ b/src/game-lib-d3-api-mesh.js @@ -52,8 +52,10 @@ GameLib.D3.API.Mesh = function( this, GameLib.Component.COMPONENT_MESH, { - 'parentMesh' : GameLib.D3.Mesh, - 'parentScene' : GameLib.D3.Scene + 'parentMesh' : GameLib.D3.Mesh, + 'parentScene' : GameLib.D3.Scene, + 'materials' : [GameLib.D3.Material], + 'skeleton' : GameLib.D3.Skeleton }, null, parentEntity @@ -176,40 +178,89 @@ GameLib.D3.API.Mesh.prototype.constructor = GameLib.D3.API.Mesh; GameLib.D3.API.Mesh.FromObjectMesh = function (objectMesh){ var apiSkeleton = null; - if (objectMesh.skeleton) { apiSkeleton = GameLib.D3.API.Skeleton.FromObjectSkeleton(objectMesh.skeleton); } + var apiMaterials = []; + if (objectMesh.materials) { + apiMaterials = objectMesh.materials.map( + function (objectMaterial) { + return GameLib.D3.API.Material.FromObjectMaterial(objectMaterial); + } + ) + } + + var apiVertices = []; + if (objectMesh.vertices) { + apiVertices = objectMesh.vertices.map( + function (objectVertex) { + return GameLib.D3.API.Vertex.FromObjectVertex(objectVertex); + } + ) + } + + var apiPosition = new GameLib.API.Vector3(); + if (objectMesh.position) { + apiPosition = GameLib.API.Vector3.FromObjectVector(objectMesh.position); + } + + var apiQuaternion = new GameLib.API.Quaternion(); + if (objectMesh.quaternion) { + apiQuaternion = GameLib.API.Quaternion.FromObjectQuaternion(objectMesh.quaternion); + } + + var apiScale = new GameLib.API.Vector3(1,1,1); + if (objectMesh.scale) { + apiScale = GameLib.API.Vector3.FromObjectVector(objectMesh.scale); + } + + var apiLocalPosition = new GameLib.API.Vector3(); + if (objectMesh.localPosition) { + apiLocalPosition = GameLib.API.Vector3.FromObjectVector(objectMesh.localPosition); + } + + var apiLocalRotation = new GameLib.API.Vector3(); + if (objectMesh.localRotation) { + apiLocalRotation = GameLib.API.Vector3.FromObjectVector(objectMesh.localRotation); + } + + var apiLocalScale = new GameLib.API.Vector3(1,1,1); + if (objectMesh.localScale) { + apiLocalScale = GameLib.API.Vector3.FromObjectVector(objectMesh.localScale); + } + + var apiUp = new GameLib.API.Vector3(0,1,0); + if (objectMesh.up) { + apiUp = GameLib.API.Vector3.FromObjectVector(objectMesh.up); + } + + var apiModelMatrix = new GameLib.API.Matrix4(); + if (objectMesh.modelMatrix) { + apiModelMatrix = GameLib.API.Matrix4.FromObjectMatrix(objectMesh.modelMatrix); + } + return new GameLib.D3.API.Mesh( objectMesh.id, objectMesh.meshType, objectMesh.name, - objectMesh.vertices.map( - function (objectVertex) { - return GameLib.D3.API.Vertex.FromObjectVertex(objectVertex); - } - ), + apiVertices, objectMesh.faces, objectMesh.faceVertexUvs, - objectMesh.materials.map( - function (objectMaterial) { - return GameLib.D3.API.Material.FromObjectMaterial(objectMaterial); - } - ), + apiMaterials, 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), + apiPosition, + apiQuaternion, + apiScale, + apiLocalPosition, + apiLocalRotation, + apiLocalScale, + apiUp, + apiModelMatrix, objectMesh.parentEntity, objectMesh.renderOrder ); diff --git a/src/game-lib-d3-api-pass.js b/src/game-lib-d3-api-pass.js new file mode 100644 index 0000000..333360c --- /dev/null +++ b/src/game-lib-d3-api-pass.js @@ -0,0 +1,93 @@ +/** + * This component renders a scene + * @param id String + * @param name String + * @param passType + * @param camera + * @param scene + * @param renderToScreen + * @param parentEntity + * @constructor + */ +GameLib.D3.API.Pass = function ( + id, + name, + passType, + camera, + scene, + renderToScreen, + parentEntity +) { + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_PASS, + { + 'camera' : GameLib.D3.Camera, + 'scene' : GameLib.D3.Scene + }, + null, + parentEntity + ); + + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Render Pass (' + id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Render Pass (' + id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(passType)) { + passType = GameLib.D3.Pass.PASS_TYPE_RENDER; + } + this.passType = passType; + + if (GameLib.Utils.UndefinedOrNull(scene)) { + scene = null; + } + this.scene = scene; + + if (GameLib.Utils.UndefinedOrNull(renderToScreen)) { + + if (this.passType == GameLib.D3.Pass.PASS_TYPE_RENDER) { + renderToScreen = false; + } else if (GameLib.D3.Pass.PASS_TYPE_COPY_SHADER) { + renderToScreen = true; + } else { + console.warn('Unsupported Render Pass Type : ' + this.passType); + throw new Error('Unsupported Render Pass Type : ' + this.passType); + } + + } + this.renderToScreen = renderToScreen; + + +}; + +GameLib.D3.API.Pass.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.Pass.prototype.constructor = GameLib.D3.API.Pass; + +/** + * Object to GameLib.D3.API.Pass + * @param objectComponent + * @constructor + */ +GameLib.D3.API.Pass.FromObjectComponent = function(objectComponent) { + return new GameLib.D3.API.Pass( + objectComponent.id, + objectComponent.name, + objectComponent.passType, + objectComponent.camera, + objectComponent.scene, + objectComponent.renderToScreen, + objectComponent.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-render-target.js b/src/game-lib-d3-api-render-target.js new file mode 100644 index 0000000..7664359 --- /dev/null +++ b/src/game-lib-d3-api-render-target.js @@ -0,0 +1,96 @@ +/** + * This component renders a scene + * @param id String + * @param name String + * @param width + * @param height + * @param minFilter + * @param magFilter + * @param format + * @param stencilBuffer + * @param parentEntity + * @constructor + */ +GameLib.D3.API.RenderTarget = function ( + id, + name, + width, + height, + minFilter, + magFilter, + format, + stencilBuffer, + parentEntity +) { + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_RENDER_TARGET, + null, + null, + parentEntity + ); + + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Render Target (' + id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(width)) { + width = 800; + } + this.width = width; + + if (GameLib.Utils.UndefinedOrNull(height)) { + height = 600; + } + this.height = height; + + if (GameLib.Utils.UndefinedOrNull(minFilter)) { + minFilter = GameLib.D3.RenderTarget.LINEAR_FILTER; + } + this.minFilter = minFilter; + + if (GameLib.Utils.UndefinedOrNull(magFilter)) { + magFilter = GameLib.D3.RenderTarget.LINEAR_FILTER; + } + this.magFilter = magFilter; + + if (GameLib.Utils.UndefinedOrNull(format)) { + format = GameLib.D3.RenderTarget.RGB_FORMAT; + } + this.format = format; + + if (GameLib.Utils.UndefinedOrNull(stencilBuffer)) { + stencilBuffer = false; + } + this.stencilBuffer = stencilBuffer; + +}; + +GameLib.D3.API.RenderTarget.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.RenderTarget.prototype.constructor = GameLib.D3.API.RenderTarget; + +/** + * Object to GameLib.D3.API.RenderTarget + * @param objectComponent + * @constructor + */ +GameLib.D3.API.RenderTarget.FromObjectComponent = function(objectComponent) { + return new GameLib.D3.API.RenderTarget( + objectComponent.id, + objectComponent.name, + objectComponent.width, + objectComponent.height, + objectComponent.minFilter, + objectComponent.magFilter, + objectComponent.format, + objectComponent.stencilBuffer, + objectComponent.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-renderer.js b/src/game-lib-d3-api-renderer.js index 509c5aa..cc60ef9 100644 --- a/src/game-lib-d3-api-renderer.js +++ b/src/game-lib-d3-api-renderer.js @@ -3,41 +3,33 @@ * @param id String * @param name String * @param rendererType - * @param scene GameLib.D3.Scene - * @param camera GameLib.D3.Camera * @param autoClear bool * @param localClipping * @param width * @param height * @param parentEntity * @param preserveDrawingBuffer + * @param composer GameLib.D3.API.Composer * @constructor */ GameLib.D3.API.Renderer = function ( id, name, rendererType, - scene, - camera, autoClear, localClipping, width, height, parentEntity, - preserveDrawingBuffer + preserveDrawingBuffer, + composer ) { - if (GameLib.Utils.UndefinedOrNull(rendererType)) { - rendererType = GameLib.Component.COMPONENT_RENDERER; - } - this.rendererType = rendererType; - GameLib.Component.call( this, - rendererType, + GameLib.Component.COMPONENT_RENDERER, { - 'scene' : GameLib.D3.Scene, - 'camera' : GameLib.D3.Camera + 'composer' : GameLib.D3.Composer }, null, parentEntity @@ -53,18 +45,21 @@ GameLib.D3.API.Renderer = function ( } this.name = name; - if (GameLib.Utils.UndefinedOrNull(scene)) { - scene = null; + if (GameLib.Utils.UndefinedOrNull(rendererType)) { + rendererType = GameLib.D3.Renderer.RENDER_TYPE_NORMAL; } - this.scene = scene; - - if (GameLib.Utils.UndefinedOrNull(camera)) { - camera = null; - } - this.camera = camera; + this.rendererType = rendererType; if (GameLib.Utils.UndefinedOrNull(autoClear)) { - autoClear = true; + + if (this.rendererType == GameLib.D3.Renderer.RENDER_TYPE_NORMAL) { + autoClear = true; + } else if (this.rendererType == GameLib.D3.Renderer.RENDER_TYPE_STEREO) { + autoClear = false; + } else { + console.warn('Unhandled render type : ' + this.rendererType); + throw new Error('Unhandled render type : ' + this.rendererType); + } } this.autoClear = autoClear; @@ -84,9 +79,22 @@ GameLib.D3.API.Renderer = function ( this.height = height; if (GameLib.Utils.UndefinedOrNull(preserveDrawingBuffer)) { - preserveDrawingBuffer = false; + + if (this.rendererType == GameLib.D3.Renderer.RENDER_TYPE_NORMAL) { + preserveDrawingBuffer = false; + } else if (this.rendererType == GameLib.D3.Renderer.RENDER_TYPE_STEREO) { + preserveDrawingBuffer = true; + } else { + console.warn('Unhandled render type : ' + this.rendererType); + throw new Error('Unhandled render type : ' + this.rendererType); + } } this.preserveDrawingBuffer = preserveDrawingBuffer; + + if (GameLib.Utils.UndefinedOrNull(composer)) { + composer = null; + } + this.composer = composer; }; GameLib.D3.API.Renderer.prototype = Object.create(GameLib.Component.prototype); @@ -102,13 +110,12 @@ GameLib.D3.API.Renderer.FromObjectComponent = function(objectComponent) { objectComponent.id, objectComponent.name, objectComponent.rendererType, - objectComponent.scene, - objectComponent.camera, objectComponent.autoClear, objectComponent.localClipping, objectComponent.width, objectComponent.height, objectComponent.parentEntity, - objectComponent.preserveDrawingBuffer + objectComponent.preserveDrawingBuffer, + objectComponent.composer ); }; diff --git a/src/game-lib-d3-api-scene.js b/src/game-lib-d3-api-scene.js index e98fc30..c772001 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -1,76 +1,69 @@ /** * Raw Scene API object - should always correspond with the Scene Schema * @param id String - * @param path String * @param name String - * @param width - * @param height - * @param meshes GameLib.D3.API.Mesh [] + * @param baseUrl String + * @param path String + * @param meshes [GameLib.D3.API.Mesh] * @param position GameLib.API.Vector3 * @param quaternion GameLib.API.Quaternion * @param scale GameLib.API.Vector3 - * @param parentSceneId - * @param lights GameLib.D3.API.Light[] - * @param worlds GameLib.D3.API.World[] - * @param entityManager GameLib.EntityManager - * @param shapes GameLib.D3.API.Shape[] - * @param cameras - * @param activeCameraIndex - * @param textures GameLib.D3.Texture[] - additional textures - * @param renderers - * @param activeRendererIndex - * @param raycaster - * @param mouse + * @param parentGameId + * @param lights [GameLib.D3.API.Light] + * @param materials [GameLib.D3.API.Material] + * @param textures [GameLib.D3.API.Texture] + * @param parentEntity * @constructor */ GameLib.D3.API.Scene = function( id, - path, name, - width, - height, + baseUrl, + path, meshes, position, quaternion, scale, - parentSceneId, + parentGameId, lights, - worlds, - entityManager, - shapes, - cameras, - activeCameraIndex, + materials, textures, - renderers, - activeRendererIndex, - raycaster, - mouse + parentEntity ) { + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_SCENE, + { + 'meshes' : [GameLib.D3.Mesh], + 'lights' : [GameLib.D3.Light], + 'textures' : [GameLib.D3.Texture], + 'materials' : [GameLib.D3.Material] + }, + false, + parentEntity + ); + if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } this.id = id; + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Scene (' + this.id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(baseUrl)) { + baseUrl = ''; + console.warn('The base URL required for downloading images is not set - textured meshes will not render properly'); + } + this.baseUrl = baseUrl; + if (GameLib.Utils.UndefinedOrNull(path)) { path = null; } this.path = path; - if (GameLib.Utils.UndefinedOrNull(name)) { - name = 'unnamed'; - } - this.name = name; - - if (GameLib.Utils.UndefinedOrNull(width)) { - width = 800; - } - this.width = width; - - if (GameLib.Utils.UndefinedOrNull(height)) { - height = 600; - } - this.height = height; - if (GameLib.Utils.UndefinedOrNull(meshes)) { meshes = []; } @@ -91,87 +84,30 @@ GameLib.D3.API.Scene = function( } this.scale = scale; - if (GameLib.Utils.UndefinedOrNull(parentSceneId)) { - parentSceneId = null; + if (GameLib.Utils.UndefinedOrNull(parentGameId)) { + parentGameId = null; } - this.parentSceneId = parentSceneId; + this.parentGameId = parentGameId; if (GameLib.Utils.UndefinedOrNull(lights)) { lights = []; } this.lights = lights; - if (GameLib.Utils.UndefinedOrNull(worlds)) { - worlds = []; + if (GameLib.Utils.UndefinedOrNull(materials)) { + materials = []; } - this.worlds = worlds; - - if (GameLib.Utils.UndefinedOrNull(entityManager)) { - entityManager = new GameLib.API.EntityManager(); - } - this.entityManager = entityManager; - - if (GameLib.Utils.UndefinedOrNull(shapes)) { - shapes = []; - } - this.shapes = shapes; - - if (GameLib.Utils.UndefinedOrNull(cameras)) { - cameras = [ - new GameLib.D3.API.Camera( - null, - GameLib.Component.COMPONENT_CAMERA, - null, - null, - this.width / this.height - ) - ]; - } - this.cameras = cameras; - - if (GameLib.Utils.UndefinedOrNull(activeCameraIndex)) { - activeCameraIndex = 0; - } - this.activeCameraIndex = activeCameraIndex; + this.materials = materials; if (GameLib.Utils.UndefinedOrNull(textures)) { textures = []; } this.textures = textures; - - if (GameLib.Utils.UndefinedOrNull(renderers)) { - renderers = [ - new GameLib.D3.API.Renderer( - null, - null, - GameLib.Component.COMPONENT_RENDERER, - this, - this.cameras[this.activeCameraIndex], - true, - false, - this.width, - this.height - ) - ]; - } - this.renderers = renderers; - - if (GameLib.Utils.UndefinedOrNull(activeRendererIndex)) { - activeRendererIndex = 0; - } - this.activeRendererIndex = activeRendererIndex; - - if (GameLib.Utils.UndefinedOrNull(raycaster)) { - raycaster = new GameLib.D3.API.Raycaster(); - } - this.raycaster = raycaster; - - if (GameLib.Utils.UndefinedOrNull(mouse)) { - mouse = new GameLib.API.Mouse(); - } - this.mouse = mouse; }; +GameLib.D3.API.Scene.prototype = Object.create(GameLib.Scene.prototype); +GameLib.D3.API.Scene.prototype.constructor = GameLib.D3.API.Scene; + /** * Returns an API scene from an Object scene * @param objectScene @@ -179,75 +115,73 @@ GameLib.D3.API.Scene = function( */ GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { - var apiEntityManager = null; + var apiMeshes = []; + var apiLights = []; + var apiMaterials = []; + var apiTextures = []; - var apiTextures = null; - var apiRenderers = null; - var apiRaycaster = null; - var apiMouse = null; + var apiPosition = new GameLib.API.Vector3(); + var apiQuaternion = new GameLib.API.Quaternion(); + var apiScale = new GameLib.API.Vector3(1,1,1); - if (objectScene.entityManager) { - apiEntityManager = GameLib.API.EntityManager.FromObjectEntityManager(objectScene.entityManager); + if (objectScene.meshes) { + apiMeshes = objectScene.meshes.map( + function(objectMesh) { + return GameLib.D3.API.Mesh.FromObjectMesh(objectMesh); + } + ) + } + + if (objectScene.lights) { + apiLights = objectScene.lights.map( + function(objectLight) { + return GameLib.D3.API.Light.FromObjectLight(objectLight); + } + ) } if (objectScene.textures) { apiTextures = objectScene.textures.map( function(objectTexture) { - return GameLib.D3.API.Texture.FromObjectTexture(objectTexture) + return GameLib.D3.API.Texture.FromObjectTexture(objectTexture); } ) } - if (objectScene.renderers) { - apiRenderers = objectScene.renderers.map( - function(objectRenderer) { - return GameLib.D3.API.Renderer.FromObjectComponent(objectRenderer) + if (objectScene.materials) { + apiMaterials = objectScene.materials.map( + function(objectMaterial) { + return GameLib.D3.API.Material.FromObjectMaterial(objectMaterial); } ) } - if (objectScene.raycaster) { - apiRaycaster = GameLib.D3.API.Raycaster.FromObjectRaycaster(objectScene.raycaster); + if (objectScene.position) { + apiPosition = GameLib.API.Vector3.FromObjectVector(objectScene.position); } - if (objectScene.mouse) { - apiMouse = GameLib.API.Mouse.FromObjectMouse(objectScene.mouse); + if (objectScene.quaternion) { + apiQuaternion = GameLib.API.Quaternion.FromObjectQuaternion(objectScene.quaternion); + } + + if (objectScene.scale) { + apiScale = GameLib.API.Vector3.FromObjectVector(objectScene.scale); } return new GameLib.D3.API.Scene( objectScene.id, - objectScene.path, objectScene.name, - objectScene.width, - objectScene.height, - 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, + objectScene.baseUrl, + objectScene.path, + apiMeshes, + apiPosition, + apiQuaternion, + apiScale, + objectScene.parentGameId, + apiLights, + apiMaterials, apiTextures, - apiRenderers, - objectScene.activeRendererIndex, - apiRaycaster, - apiMouse + objectScene.parentEntity ); }; diff --git a/src/game-lib-d3-camera.js b/src/game-lib-d3-camera.js index 9114938..e1f650e 100644 --- a/src/game-lib-d3-camera.js +++ b/src/game-lib-d3-camera.js @@ -61,6 +61,10 @@ GameLib.D3.Camera = function( GameLib.D3.Camera.prototype = Object.create(GameLib.D3.API.Camera.prototype); GameLib.D3.Camera.prototype.constructor = GameLib.D3.Camera; +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} @@ -74,14 +78,14 @@ GameLib.D3.Camera.prototype.createInstance = function(update) { } if (!instance) { - if (this.cameraType == GameLib.Component.COMPONENT_CAMERA) { + if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ) { instance = new THREE.PerspectiveCamera( this.fov, this.aspect, this.near, this.far ); - } else if (this.cameraType == GameLib.Component.COMPONENT_ORTHOGONAL_CAMERA) { + } else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) { instance = new THREE.OrthographicCamera( this.minX, this.maxX, @@ -90,20 +94,37 @@ GameLib.D3.Camera.prototype.createInstance = function(update) { this.minZ, this.maxZ ); + } else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) { + if (!instance) { + instance = new THREE.StereoCamera(); + } } } if (update) { - 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 (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) { + instance.minX = this.minX; + instance.maxX = this.maxX; + instance.minY = this.minY; + instance.maxY = this.maxY; + instance.minZ = this.minZ; + instance.maxZ = this.maxZ; + } + + if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE) { + instance.fov = this.fov; + instance.aspect = this.aspect; + instance.near = this.near; + instance.far = this.far; + } + + if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) { + instance.eyeSeparation = this.eyeSeparation; + instance.focalLength = this.focalLength; + + instance.update(this.camera.instance); + } } instance.position.x = this.position.x; diff --git a/src/game-lib-d3-composer.js b/src/game-lib-d3-composer.js new file mode 100644 index 0000000..9cffe20 --- /dev/null +++ b/src/game-lib-d3-composer.js @@ -0,0 +1,108 @@ +/** + * Renders a scene with a camera + * @param graphics GameLib.D3.Graphics + * @param apiComposer GameLib.D3.API.Composer + * @constructor + */ +GameLib.D3.Composer = function ( + graphics, + apiComposer +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.API.Composer.call( + this, + apiComposer.id, + apiComposer.name, + apiComposer.renderer, + apiComposer.renderTarget, + apiComposer.passes, + apiComposer.parentEntity + ); + + this.instance = this.createInstance(); +}; + +GameLib.D3.Composer.prototype = Object.create(GameLib.D3.API.Composer.prototype); +GameLib.D3.Composer.prototype.constructor = GameLib.D3.Composer; + +/** + * Creates a Composer instance + * @param update + * @returns {*} + */ +GameLib.D3.Composer.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + } else { + + if (!THREE.EffectComposer) { + console.warn('No THREE.EffectComposer'); + throw new Error('No THREE.EffectComposer'); + } + + instance = new THREE.EffectComposer( + this.renderer.instance, + this.renderTarget.instance + ); + + this.passes.map( + function(pass) { + this.instance.addPass(pass.instance); + }.bind(this) + ); + } + + return instance; +}; + +/** + * Updates Composer instance + */ +GameLib.D3.Composer.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + +/** + * GameLib.D3.Composer to GameLib.D3.API.Composer + * @returns {GameLib.D3.API.Composer} + */ +GameLib.D3.Composer.prototype.toApiComponent = function() { + + var apiComposer = new GameLib.D3.API.Composer( + this.id, + this.name, + GameLib.Utils.IdOrNull(this.renderer), + GameLib.Utils.IdOrNull(this.renderTarget), + this.passes.map( + function(pass) { + return GameLib.Utils.IdOrNull(pass); + } + ), + GameLib.Utils.IdOrNull(this.parentEntity) + ); + + return apiComposer; +}; + +/** + * + * @param graphics + * @param objectComponent + * @returns {GameLib.D3.Composer} + * @constructor + */ +GameLib.D3.Composer.FromObjectComponent = function(graphics, objectComponent) { + + var apiComposer = GameLib.D3.API.Composer.FromObjectComponent(objectComponent); + + return new GameLib.D3.Composer( + graphics, + apiComposer + ); +}; diff --git a/src/game-lib-d3-editor.js b/src/game-lib-d3-editor.js new file mode 100644 index 0000000..a8b880b --- /dev/null +++ b/src/game-lib-d3-editor.js @@ -0,0 +1,106 @@ +/** + * Creates a Editor object + * @param graphics GameLib.D3.Graphics + * @param apiEditor GameLib.D3.API.Editor + * @param document + * @param window + * @constructor + */ +GameLib.D3.Editor = function( + graphics, + apiEditor, + document, + window +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.API.Editor.call( + this, + apiEditor.id, + apiEditor.name, + apiEditor.games, + apiEditor.allSelected, + apiEditor.parentEntity + ); + + if (GameLib.Utils.UndefinedOrNull(document)) { + console.warn('Cannot create an Editor Object without reference to the DOM document'); + throw new Error('Cannot create an Editor Object without reference to the DOM document'); + } + this.document = document; + + if (GameLib.Utils.UndefinedOrNull(window)) { + console.warn('Cannot create an Editor Object without reference to the DOM window'); + throw new Error('Cannot create an Editor Object without reference to the DOM window'); + } + this.window = window; + + this.instance = this.createInstance(); +}; + +GameLib.D3.Editor.prototype = Object.create(GameLib.D3.API.Editor.prototype); +GameLib.D3.Editor.prototype.constructor = GameLib.D3.Editor; + +/** + * Creates a camera instance of 'graphics' type (only THREE for now) + * @returns {THREE.Editor} + */ +GameLib.D3.Editor.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + } + + return instance; +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Editor.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + +/** + * Converts a GameLib.D3.Editor to a new GameLib.D3.API.Editor + * @returns {GameLib.D3.API.Editor} + */ +GameLib.D3.Editor.prototype.toApiEditor = function() { + + var apiGames = this.games.map( + function(game) { + return game.toApiComponent(); + } + ); + + return new GameLib.D3.API.Editor( + this.id, + this.name, + apiGames, + this.allSelected, + GameLib.Utils.IdOrNull(this.parentEntity) + ); + +}; + +/** + * Converts from an Object Editor to a GameLib.D3.Editor + * @param graphics GameLib.D3.Graphics + * @param objectEditor Object + * @returns {GameLib.D3.Editor} + * @constructor + */ +GameLib.D3.Editor.FromObjectEditor = function(graphics, objectEditor) { + + var apiEditor = GameLib.D3.API.Editor.FromObjectEditor(objectEditor); + + return new GameLib.D3.Editor( + graphics, + apiEditor + ); + +}; diff --git a/src/game-lib-d3-game.js b/src/game-lib-d3-game.js new file mode 100644 index 0000000..e34c142 --- /dev/null +++ b/src/game-lib-d3-game.js @@ -0,0 +1,111 @@ +/** + * Game Runtime + * @param graphics GameLib.D3.Graphics + * @param apiGame GameLib.D3.API.Game + * @constructor + */ +GameLib.D3.Game = function ( + graphics, + apiGame +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.API.Game.call( + this, + apiGame.id, + apiGame.name, + apiGame.gameType, + apiGame.width, + apiGame.height, + apiGame.scenes, + apiGame.cameras, + apiGame.renderers, + apiGame.composers, + apiGame.systems, + apiGame.entityManager, + apiGame.mouse, + apiGame.raycaster, + apiGame.activeCameraIndex, + apiGame.activeRendererIndex, + apiGame.parentEntity + ); + + this.scenes = this.scenes.map( + function(apiScene) { + return GameLib.D3.Scene( + this.graphics, + apiScene + ) + }.bind(this) + ); + + this.entityManager.entities.map( + function (entity) { + this.idToObject[entity.id] = entity; + entity.components.map( + function(component) { + if (component instanceof GameLib.Component) { + this.idToObject[component.id] = component; + } + }.bind(this) + ) + }.bind(this) + ); + + this.entityManager.linkObjects(this.idToObject); + + this.scenes = {}; +}; + +GameLib.D3.Game.GAME_TYPE_VR_PONG = 0x1; +GameLib.D3.Game.GAME_TYPE_VR_RACER = 0x2; + +GameLib.D3.Game.prototype.addScene = function( + scene, + identifer +) { + this.scenes[identifer] = scene; +}; + +GameLib.D3.Game.prototype.processPhysics = function ( + dt +) { + for(var s in this.scenes) { + var scene = this.scenes[s]; + + for(var w in scene.worlds) { + var world = scene.worlds[w]; + world.step(dt); + } + } +}; + +GameLib.D3.Game.prototype.render = function( + dt, + renderer +) { + for(var s in this.scenes) { + var scene = this.scenes[s]; + scene.render(dt, renderer); + } +}; + +GameLib.D3.Game.prototype.update = function( + dt, + fixedDt +) { + for(var s in this.scenes) { + var scene = this.scenes[s]; + + for(var w in scene.worlds) { + var world = scene.worlds[w]; + // NOTE: We are calling the step function with a variable timestep! + world.step(fixedDt, dt); + } + + scene.update(dt); + scene.lateUpdate(dt); + } +}; \ No newline at end of file diff --git a/src/game-lib-d3-input-drive.js b/src/game-lib-d3-input-drive.js index b9492ef..619acab 100644 --- a/src/game-lib-d3-input-drive.js +++ b/src/game-lib-d3-input-drive.js @@ -1,7 +1,6 @@ /** * Input parent class * @param graphics GameLib.D3.Graphics - * @param parentObject * @param apiInputDrive GameLib.D3.API.Input.Drive * @constructor */ diff --git a/src/game-lib-d3-input-editor.js b/src/game-lib-d3-input-editor.js new file mode 100644 index 0000000..3bfd484 --- /dev/null +++ b/src/game-lib-d3-input-editor.js @@ -0,0 +1,132 @@ +/** + * Input parent class + * @param graphics GameLib.D3.Graphics + * @param apiInputEditor GameLib.D3.API.Input.Editor + * @constructor + */ +GameLib.D3.Input.Editor = function ( + graphics, + apiInputEditor +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.API.Input.Editor.call( + this, + apiInputEditor.id, + apiInputEditor.name, + apiInputEditor.camera, + apiInputEditor.parentEntity + ); + + this.instance = this.createInstance(); +}; + +GameLib.D3.Input.Editor.prototype = Object.create(GameLib.D3.API.Input.Editor.prototype); +GameLib.D3.Input.Editor.prototype.constructor = GameLib.D3.Input.Editor; + +GameLib.D3.Input.Editor.prototype.createInstance = function(update) { + + if (update) { + return this.instance; + } + + var instance = document.getElementById(this.domElementId); + + this.resize = this.onWindowResize.bind(this); + + window.addEventListener('resize', this.resize, false ); + + // this.clearScene(); + this.boundMouseMove = this.mouseMove.bind(this); + + instance.addEventListener('contextmenu', function(event){ + + if (event.stopPropagation) { + event.stopPropagation(); + } + + if (event.preventDefault) { + event.preventDefault(); + } + + event.cancelBubble = true; + return false; + }, false); + + instance.addEventListener('mousedown', this.mouseDown.bind(this), false); + instance.addEventListener('keydown', this.keypress.bind(this), false); + instance.addEventListener('mousemove', this.boundMouseMove, false); + + return instance; +}; + +GameLib.D3.Input.Editor.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + +/** + * GameLib.D3.Input.Editor to GameLib.D3.API.Input.Editor + * @returns {GameLib.D3.API.Input.Editor} + */ +GameLib.D3.Input.Editor.prototype.toApiComponent = function() { + + var apiInputEditor = new GameLib.D3.API.Input.Editor( + this.id, + this.name, + this.domElementId, + GameLib.Utils.IdOrNull(this.pathFollowingComponent), + GameLib.Utils.IdOrNull(this.parentEntity), + GameLib.Utils.IdOrNull(this.wheelFL), + GameLib.Utils.IdOrNull(this.wheelFR), + GameLib.Utils.IdOrNull(this.wheelRL), + GameLib.Utils.IdOrNull(this.wheelRR), + this.heightOffset, + this.distance, + this.distanceGrain, + this.rotationFactor + ); + + return apiInputEditor; +}; + +GameLib.D3.Input.Editor.FromObjectComponent = function(graphics, objectComponent) { + + var apiInputEditor = GameLib.D3.API.Input.Editor.FromObjectComponent(objectComponent); + + return new GameLib.D3.Input.Editor( + graphics, + apiInputEditor + ); +}; + +GameLib.D3.Input.Editor.prototype.update = function(deltaTime) { + if (this.pathFollowingComponent) { + + this.pathFollowingComponent.mesh.localPosition.x = (this.heightOffset * this.pathFollowingComponent.rotationMatrix.up.x); + this.pathFollowingComponent.mesh.localPosition.y = (this.heightOffset * this.pathFollowingComponent.rotationMatrix.up.y); + this.pathFollowingComponent.mesh.localPosition.z = (this.heightOffset * this.pathFollowingComponent.rotationMatrix.up.z); + + if (this.keyLeft) { + this.distance -= this.distanceGrain; + } + + if (this.keyRight) { + this.distance += this.distanceGrain; + } + + this.pathFollowingComponent.mesh.localPosition.x += (this.distance * this.pathFollowingComponent.rotationMatrix.left.x); + this.pathFollowingComponent.mesh.localPosition.y += (this.distance * this.pathFollowingComponent.rotationMatrix.left.y); + this.pathFollowingComponent.mesh.localPosition.z += (this.distance * this.pathFollowingComponent.rotationMatrix.left.z); + + this.wheelFL.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + this.wheelFR.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + + this.wheelFL.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + this.wheelFR.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + + this.wheelRL.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + this.wheelRR.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + } +}; \ No newline at end of file diff --git a/src/game-lib-d3-pass.js b/src/game-lib-d3-pass.js new file mode 100644 index 0000000..b77a999 --- /dev/null +++ b/src/game-lib-d3-pass.js @@ -0,0 +1,118 @@ +/** + * Renders a scene with a camera + * @param graphics GameLib.D3.Graphics + * @param apiPass GameLib.D3.API.Pass + * @constructor + */ +GameLib.D3.Pass = function ( + graphics, + apiPass +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.API.Pass.call( + this, + apiPass.id, + apiPass.name, + apiPass.passType, + apiPass.camera, + apiPass.scene, + apiPass.renderToScreen, + apiPass.parentEntity + ); + + this.instance = this.createInstance(); +}; + +GameLib.D3.Pass.prototype = Object.create(GameLib.D3.API.Pass.prototype); +GameLib.D3.Pass.prototype.constructor = GameLib.D3.Pass; + +GameLib.D3.Pass.PASS_TYPE_RENDER = 0x1; +GameLib.D3.Pass.PASS_TYPE_COPY_SHADER = 0x2; + +/** + * Create Pass instance + * @param update + * @returns {*} + */ +GameLib.D3.Pass.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + } else { + + if (this.passType == GameLib.D3.Pass.PASS_TYPE_RENDER) { + + if (!THREE.RenderPass) { + console.warn('No THREE.RenderPass'); + throw new Error('No THREE.RenderPass'); + } + + instance = new THREE.RenderPass( + this.scene.instance, + this.camera.instance + ); + } else if (this.passType == GameLib.D3.Pass.PASS_TYPE_COPY_SHADER) { + if (!THREE.CopyShader) { + console.warn('No THREE.CopyShader'); + throw new Error('No THREE.CopyShader'); + } + + instance = THREE.CopyShader; + } else { + console.warn('Render pass not supported yet: ' + this.passType); + throw new Error('Render pass not supported yet: ' + this.passType); + } + } + + instance.renderToScreen = this.renderToScreen; + + return instance; +}; + +/** + * Update Pass instance + */ +GameLib.D3.Pass.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + +/** + * GameLib.D3.Pass to GameLib.D3.API.Pass + * @returns {GameLib.D3.API.Pass} + */ +GameLib.D3.Pass.prototype.toApiComponent = function() { + + var apiPass = new GameLib.D3.API.Pass( + this.id, + this.name, + this.passType, + GameLib.Utils.IdOrNull(this.camera), + GameLib.Utils.IdOrNull(this.scene), + this.renderToScreen, + GameLib.Utils.IdOrNull(this.parentEntity) + ); + + return apiPass; +}; + +/** + * GameLib.D3.Pass from Object + * @param graphics + * @param objectComponent + * @returns {GameLib.D3.Pass} + * @constructor + */ +GameLib.D3.Pass.FromObjectComponent = function(graphics, objectComponent) { + + var apiPass = GameLib.D3.API.Pass.FromObjectComponent(objectComponent); + + return new GameLib.D3.Pass( + graphics, + apiPass + ); +}; diff --git a/src/game-lib-d3-render-target.js b/src/game-lib-d3-render-target.js new file mode 100644 index 0000000..b708f3c --- /dev/null +++ b/src/game-lib-d3-render-target.js @@ -0,0 +1,117 @@ +/** + * Renders a scene with a camera + * @param graphics GameLib.D3.Graphics + * @param apiRenderTarget GameLib.D3.API.RenderTarget + * @constructor + */ +GameLib.D3.RenderTarget = function ( + graphics, + apiRenderTarget +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.API.RenderTarget.call( + this, + apiRenderTarget.id, + apiRenderTarget.name, + apiRenderTarget.width, + apiRenderTarget.height, + apiRenderTarget.minFilter, + apiRenderTarget.magFilter, + apiRenderTarget.format, + apiRenderTarget.stencilBuffer + ); + + this.instance = this.createInstance(); +}; + +GameLib.D3.RenderTarget.prototype = Object.create(GameLib.D3.API.RenderTarget.prototype); +GameLib.D3.RenderTarget.prototype.constructor = GameLib.D3.RenderTarget; + +/** + * Some constants (based on THREE.js constants - update if needed) + * @type {number} + */ +GameLib.D3.RenderTarget.LINEAR_FILTER = 1006; +GameLib.D3.RenderTarget.RGB_FORMAT = 1022; +GameLib.D3.RenderTarget.RGBA_FORMAT = 1023; + +/** + * Creates a Render Target instance + * @param update + * @returns {*} + */ +GameLib.D3.RenderTarget.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + instance.width = this.width; + instance.height = this.height; + instance.minFilter = this.minFilter; + instance.magFilter = this.magFilter; + instance.format = this.format; + instance.stencilBuffer = this.stencilBuffer; + } else { + instance = new THREE.WebGLRenderTarget( + this.width, + this.height, + { + minFilter : this.minFilter, + magFilter : this.magFilter, + format : this.format, + stencilBuffer : this.stencilBuffer + } + ); + } + + return instance; +}; + +/** + * updates instance + */ +GameLib.D3.RenderTarget.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + +/** + * Render Target to API Render Target + * @returns {GameLib.D3.API.RenderTarget} + */ +GameLib.D3.RenderTarget.prototype.toApiComponent = function() { + + var apiRenderTarget = new GameLib.D3.API.RenderTarget( + this.id, + this.name, + this.width, + this.height, + this.minFilter, + this.magFilter, + this.format, + this.stencilBuffer, + GameLib.Utils.IdOrNull(this.parentEntity) + ); + + return apiRenderTarget; +}; + +/** + * + * @param graphics + * @param objectComponent + * @returns {GameLib.D3.RenderTarget} + * @constructor + */ +GameLib.D3.RenderTarget.FromObjectComponent = function(graphics, objectComponent) { + + var apiRenderTarget = GameLib.D3.API.RenderTarget.FromObjectComponent(objectComponent); + + return new GameLib.D3.RenderTarget( + graphics, + apiRenderTarget + ); +}; diff --git a/src/game-lib-d3-renderer.js b/src/game-lib-d3-renderer.js index e252b23..6a61ffc 100644 --- a/src/game-lib-d3-renderer.js +++ b/src/game-lib-d3-renderer.js @@ -17,39 +17,29 @@ GameLib.D3.Renderer = function ( apiRenderer.id, apiRenderer.name, apiRenderer.rendererType, - apiRenderer.scene, - apiRenderer.camera, apiRenderer.autoClear, apiRenderer.localClipping, apiRenderer.width, apiRenderer.height, apiRenderer.parentEntity, - apiRenderer.preserveDrawingBuffer + apiRenderer.preserveDrawingBuffer, + apiRenderer.composer ); - if (this.camera instanceof Object) { - if (this.camera.cameraType == GameLib.Component.COMPONENT_CAMERA) { - this.camera = new GameLib.D3.Camera( - this.graphics, - this.camera - ); - } else if (this.camera.cameraType == GameLib.Component.COMPONENT_STEREO_CAMERA) { - this.camera = new GameLib.D3.StereoCamera( - this.graphics, - this.camera - ); - } else { - console.warn('Camera type not supported : ' + this.camera.cameraType); - throw new Error('Camera type not supported : ' + this.camera.cameraType) - } - } - this.instance = this.createInstance(); }; GameLib.D3.Renderer.prototype = Object.create(GameLib.D3.API.Renderer.prototype); GameLib.D3.Renderer.prototype.constructor = GameLib.D3.Renderer; +GameLib.D3.Renderer.RENDER_TYPE_NORMAL = 0x1; +GameLib.D3.Renderer.RENDER_TYPE_STEREO = 0x2; + +/** + * Create Renderer Instance + * @param update + * @returns {*} + */ GameLib.D3.Renderer.prototype.createInstance = function(update) { var instance = null; @@ -65,11 +55,6 @@ GameLib.D3.Renderer.prototype.createInstance = function(update) { instance.autoClear = this.autoClear; instance.preserveDrawingBuffer = this.preserveDrawingBuffer; - if (this.camera) { - this.camera.aspect = this.width / this.height; - this.camera.updateInstance(); - } - return instance; }; @@ -83,14 +68,13 @@ GameLib.D3.Renderer.prototype.toApiComponent = function() { this.id, this.name, this.rendererType, - GameLib.Utils.IdOrNull(this.scene), - GameLib.Utils.IdOrNull(this.camera), this.autoClear, this.localClipping, this.width, this.height, GameLib.Utils.IdOrNull(this.parentEntity), - this.preserveDrawingBuffer + preserveDrawingBuffer, + GameLib.Utils.IdOrNull(this.composer) ); return apiRenderer; @@ -109,17 +93,6 @@ GameLib.D3.Renderer.FromObjectComponent = function(graphics, objectComponent) { return new GameLib.D3.Renderer( graphics, - this, apiRenderer ); }; - -/** - * - */ -GameLib.D3.Renderer.prototype.render = function() { - this.instance.render( - this.scene.instance, - this.camera.instance - ); -}; diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 41a14c4..b77f11a 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -2,7 +2,6 @@ * Scene Superset - The apiScene properties get moved into the Scene object itself, and then the instance is * created * @param graphics - * @param progressCallback * @param apiScene GameLib.D3.API.Scene * @param imageFactory * @param computeNormals @@ -10,7 +9,6 @@ */ GameLib.D3.Scene = function Scene( graphics, - progressCallback, apiScene, imageFactory, computeNormals @@ -18,130 +16,61 @@ GameLib.D3.Scene = function Scene( this.graphics = graphics; this.graphics.isNotThreeThrow(); - this.imageFactory = imageFactory; - GameLib.D3.API.Scene.call( this, apiScene.id, - apiScene.path, apiScene.name, - apiScene.width, - apiScene.height, - apiScene.meshes, - apiScene.position, - apiScene.quaternion, - apiScene.scale, - apiScene.parentSceneId, - apiScene.lights, - apiScene.worlds, - apiScene.entityManager, - apiScene.shapes, - apiScene.cameras, - apiScene.activeCameraIndex, - apiScene.textures, - apiScene.renderers, - apiScene.activeRendererIndex, - apiScene.raycaster, - apiScene.mouse + apiScene.baseUrl, + apiScene.path, + apiScene.meshes, + apiScene.position, + apiScene.quaternion, + apiScene.scale, + apiScene.parentGameId, + apiScene.lights, + apiScene.materials, + apiScene.textures, + apiScene.parentEntity ); + if (GameLib.Utils.UndefinedOrNull(imageFactory)) { + imageFactory = GameLib.D3.ImageFactory( + this.graphics, + this.baseUrl + ); + } + this.imageFactory = imageFactory; + + this.idToObject = {}; + this.meshes = this.meshes.map( function(apiMesh) { - return new GameLib.D3.Mesh( + + var mesh = 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.idToObject[mesh.id] = mesh; - if (this.entityManager) { - this.entityManager = new GameLib.EntityManager( - this.graphics, - this.entityManager - ); - } + if (mesh.skeleton) { - this.cameras = this.cameras.map( - function(apiCamera) { - if (apiCamera.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) { - return new GameLib.D3.StereoCamera( - this.graphics, - apiCamera - ) - } else { - return new GameLib.D3.Camera( - this.graphics, - apiCamera + this.idToObject[mesh.skeleton.id] = mesh.skeleton; + + mesh.skeleton.bones.map( + function (bone) { + this.idToObject[bone.id] = bone; + }.bind(this) ) } + + return mesh; + }.bind(this) ); - this.textures = this.textures.map( - function(apiTexture) { - return new GameLib.D3.Texture( - this.graphics, - apiTexture, - null, - null, - this.imageFactory - ) - }.bind(this) - ); - - this.renderers = this.renderers.map( - function(apiRenderer) { - var renderer = null; - - if (apiRenderer.rendererType == GameLib.Component.COMPONENT_RENDERER) { - - renderer = new GameLib.D3.Renderer( - this.graphics, - apiRenderer - ); - - } else if (apiRenderer.rendererType == GameLib.Component.COMPONENT_STEREO_RENDERER) { - renderer = new GameLib.D3.StereoRenderer( - this.graphics, - apiRenderer - ); - } - - if (renderer.scene.id == this.id) { - renderer.scene = this; - } - - if (!renderer) { - console.log("Renderer type not supported : " + apiRenderer.rendererType); - throw new Error("Renderer type not supported : " + apiRenderer.rendererType); - } - - return renderer; - }.bind(this) - ); - - this.raycaster = new GameLib.D3.Raycaster( - this.graphics, - this.raycaster - ); - - this.mouse = new GameLib.Mouse( - this.graphics, - this, - this.mouse - ); - this.position = new GameLib.Vector3( graphics, this, @@ -160,83 +89,51 @@ GameLib.D3.Scene = function Scene( this.scale ); - this.progressCallback = progressCallback; - - this.instance = this.createInstance(); - - this.interestingProperties = [ - "cameras", - "meshes", - "lights", - "textures", - "renderers", - "raycaster", - "mouse" - ]; - - this.idToObject = {}; - - this.idToObject[this.id] = this; - - var material = null; - - var object = null; - - for (var p = 0; p < this.interestingProperties.length; p++) { - property = this.interestingProperties[p]; - if (this.hasOwnProperty(property)) { - - if (_.isArray(this[property])) { - for (var i = 0; i < this[property].length; i++) { - object = this[property][i]; - this.idToObject[object.id] = object; - } - } else { - object = this[property]; - this.idToObject[object.id] = object; - } - } - } - - for (var m = 0; m < this.meshes.length; m++) { - if (this.meshes[m].skeleton) { - this.meshes[m].skeleton.bones.map( - function (bone) { - this.idToObject[bone.id] = bone; - }.bind(this) + this.lights = this.lights.map( + function(apiLight) { + var light = new GameLib.D3.Light( + this.graphics, + apiLight ); - this.idToObject[this.meshes[m].skeleton.id] = this.meshes[m].skeleton; - } + this.idToObject[light.id] = light; - if (this.meshes[m].materials[0]) { - - material = this.meshes[m].materials[0]; - - for (var property in material) { - if (material.hasOwnProperty(property) && material[property] instanceof GameLib.D3.Texture) { - this.idToObject[material[property].id] = material[property]; - } - } - - this.idToObject[material.id] = material; - } - } - - this.entityManager.entities.map( - function (entity) { - this.idToObject[entity.id] = entity; - entity.components.map( - function(component) { - if (component instanceof GameLib.Component) { - this.idToObject[component.id] = component; - } - }.bind(this) - ) + return light; }.bind(this) ); - this.entityManager.linkObjects(this.idToObject); + this.materials = this.materials.map( + function(apiMaterial) { + var material = new GameLib.D3.Material( + this.graphics, + apiMaterial, + this.imageFactory + ); + + this.idToObject[material.id] = material; + + return material; + }.bind(this) + ); + + + this.textures = this.textures.map( + function(apiTexture) { + var texture = new GameLib.D3.Texture( + this.graphics, + apiTexture, + this.imageFactory + ); + + this.idToObject[texture.id] = texture; + + return texture; + }.bind(this) + ); + + this.idToObject[this.id] = this; + + this.instance = this.createInstance(); }; GameLib.D3.Scene.prototype = Object.create(GameLib.D3.API.Scene.prototype); @@ -244,7 +141,7 @@ GameLib.D3.Scene.prototype.constructor = GameLib.D3.Scene; /** * Creates an instance scene - * @returns {GameLib.D3.Scene|THREE.Scene|ApiLib.Scene|*|Scene} + * @returns {THREE.Scene} */ GameLib.D3.Scene.prototype.createInstance = function() { @@ -266,8 +163,6 @@ GameLib.D3.Scene.prototype.createInstance = function() { instance.add(this.lights[l].instance); } - instance.render = true; - return instance; }; @@ -289,26 +184,9 @@ GameLib.D3.Scene.prototype.toApiScene = function() { } ); - var apiEntityManager = null; - if (this.entityManager) { - apiEntityManager = this.entityManager.toApiEntityManager(); - } - - var apiCameras = this.cameras.map( - function(camera) { - return camera.toApiCamera(); - } - ); - - var apiWorlds = this.worlds.map( - function(world) { - return world.toApiWorld(); - } - ); - - var apiShapes = this.shapes.map( - function(shape) { - return shape.toApiShape(); + var apiMaterials = this.materials.map( + function(material) { + return material.toApiShape(); } ); @@ -318,34 +196,20 @@ GameLib.D3.Scene.prototype.toApiScene = function() { } ); - var apiRenderers = this.renderers.map( - function(renderer) { - return renderer.toApiComponent(); - } - ); - return new GameLib.D3.API.Scene( this.id, - this.path, this.name, - this.width, - this.height, + this.baseUrl, + this.path, apiMeshes, this.position.toApiVector(), this.quaternion.toApiQuaternion(), this.scale.toApiVector(), - this.parentSceneId, + this.parentGameId, apiLights, - apiWorlds, - apiEntityManager, - apiShapes, - apiCameras, - this.activeCameraIndex, + apiMaterials, apiTextures, - apiRenderers, - this.activeRendererIndex, - this.raycaster.toApiRaycaster(), - this.mouse.toApiMouse() + GameLib.Utils.IdOrNull(this.parentEntity) ); }; @@ -355,7 +219,6 @@ GameLib.D3.Scene.prototype.toApiScene = function() { * @param objectScene Object * @param computeNormals boolean to indicate whether or not to recalculate normals * @param imageFactory GameLib.D3.ImageFactory - * @param progressCallback callback * @returns {GameLib.D3.Scene} * @constructor */ @@ -363,14 +226,12 @@ GameLib.D3.Scene.FromObjectScene = function( graphics, objectScene, computeNormals, - imageFactory, - progressCallback + imageFactory ) { var apiScene = GameLib.D3.API.Scene.FromObjectScene(objectScene); return new GameLib.D3.Scene( graphics, - progressCallback, apiScene, imageFactory, computeNormals @@ -380,52 +241,39 @@ GameLib.D3.Scene.FromObjectScene = function( /** * Transforms raw scene data into a GameLib.D3.Scene - * @param objectScene Object (as it comes from the API) - * @param onLoaded * @param graphics - * @param uploadUrl - * @param progressCallback + * @param objectScene Object (as it comes from the API) * @param computeNormals + * @param onLoaded * @constructor */ GameLib.D3.Scene.LoadScene = function( - objectScene, - onLoaded, graphics, - uploadUrl, - progressCallback, - computeNormals + objectScene, + computeNormals, + onLoaded ) { - onLoaded( - GameLib.D3.Scene.FromObjectScene( - graphics, - objectScene, - computeNormals, - GameLib.D3.ImageFactory( - graphics, - uploadUrl - ), - progressCallback - ) + var scene = GameLib.D3.Scene.FromObjectScene( + graphics, + objectScene, + computeNormals ); + + onLoaded(scene); }; /** * Loads a scene directly from the API - * @param partialSceneObject Object {path: '', name: ''} - * @param onLoaded callback * @param graphics GameLib.D3.Graphics - * @param uploadUrl String - * @param progressCallback callback + * @param partialSceneObject Object {path: '', name: ''} * @param apiUrl + * @param onLoaded */ GameLib.D3.Scene.LoadSceneFromApi = function( - partialSceneObject, - onLoaded, graphics, - uploadUrl, - progressCallback, - apiUrl + partialSceneObject, + apiUrl, + onLoaded ) { /** @@ -448,18 +296,27 @@ GameLib.D3.Scene.LoadSceneFromApi = function( if (xhr.readyState == 4) { - var response = JSON.parse(xhr.responseText); - - if (!response.scene || response.scene.length == 0) { - return onLoaded(null, null, new Error('Could not load scene')); + try { + var response = JSON.parse(xhr.responseText); + } catch (e) { + return onLoaded(null, new Error('Could not load scene : ' + e.message)); } - var scene = response.scene[0]; + if (!response.scene || response.scene.length == 0) { + return onLoaded(null, new Error('Could not load scene')); + } - GameLib.D3.Scene.LoadScene(scene, onLoaded, graphics, uploadUrl, progressCallback, true); + var objectScene = response.scene[0]; + + GameLib.D3.Scene.LoadScene( + graphics, + objectScene, + true, + onLoaded + ); } } }(xhr); xhr.send(); -}; +}; \ No newline at end of file diff --git a/src/game-lib-d3-stereo-camera.js b/src/game-lib-d3-stereo-camera.js deleted file mode 100644 index c0cd4d4..0000000 --- a/src/game-lib-d3-stereo-camera.js +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Creates a camera object - * @param graphics GameLib.D3.Graphics - * @param apiCamera GameLib.D3.API.Camera - * @constructor - */ -GameLib.D3.StereoCamera = function ( - graphics, - apiCamera -) { - - this.graphics = graphics; - this.graphics.isNotThreeThrow(); - - GameLib.D3.Camera.call( - this, - this.graphics, - apiCamera - ); - - this.instance = this.createInstance(); - - this.needsUpdate = false; -} ; - -GameLib.D3.StereoCamera.prototype = Object.create(GameLib.D3.Camera.prototype); -GameLib.D3.StereoCamera.prototype.constructor = GameLib.D3.StereoCamera; - -/** - * Creates a camera instance of 'graphics' type (only THREE for now) - * @returns {THREE.Camera} - */ -GameLib.D3.StereoCamera.prototype.createInstance = function(update) { - - var instance = null; - - if (update) { - instance = this.instance; - } - - if (!instance) { - instance = new THREE.StereoCamera(); - } - - if (update) { - // 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; - } - - // instance.position.x = this.position.x; - // instance.position.y = this.position.y; - // instance.position.z = this.position.z; - // - // instance.quaternion.x = this.quaternion.x; - // instance.quaternion.y = this.quaternion.y; - // instance.quaternion.z = this.quaternion.z; - // instance.quaternion.w = this.quaternion.w; - - // instance.lookAt(this.lookAt.instance); - // - // instance.updateProjectionMatrix(); - - return instance; -}; \ No newline at end of file diff --git a/src/game-lib-d3-stereo-renderer.js b/src/game-lib-d3-stereo-renderer.js deleted file mode 100644 index 50df4b9..0000000 --- a/src/game-lib-d3-stereo-renderer.js +++ /dev/null @@ -1,106 +0,0 @@ -/** - * Renders a scene with a camera - * @param graphics GameLib.D3.Graphics - * @param apiRenderer GameLib.D3.API.Renderer - * @constructor - */ -GameLib.D3.StereoRenderer = function ( - graphics, - apiRenderer -) { - - this.graphics = graphics; - this.graphics.isNotThreeThrow(); - - GameLib.D3.Renderer.call( - this, - this.graphics, - apiRenderer - ); - - this.autoClear = false; - - this.composer = null; - - this.renderPass = null; - - this.instance = this.createInstance(); -}; - -GameLib.D3.StereoRenderer.prototype = Object.create(GameLib.D3.Renderer.prototype); -GameLib.D3.StereoRenderer.prototype.constructor = GameLib.D3.StereoRenderer; - -GameLib.D3.StereoRenderer.prototype.createInstance = function(update) { - - var instance = null; - - if (update) { - instance = this.instance; - } else { - instance = new THREE.WebGLRenderer(); - } - - instance.localClippingEnabled = this.localClipping; - instance.setSize(this.width, this.height); - instance.autoClear = false; - - var renderTargetParameters = { - minFilter: THREE.LinearFilter, - magFilter: THREE.LinearFilter, - format: THREE.RGBFormat, - stencilBuffer: false - }; - - var renderTarget = new THREE.WebGLRenderTarget( - this.width, - this.height, - renderTargetParameters - ); - - if (this.camera instanceof GameLib.D3.Camera) { - this.camera.aspect = this.width / this.height; - this.camera.updateInstance(); - } - - if (this.camera instanceof GameLib.D3.Camera && this.scene instanceof GameLib.D3.Scene) { - - if (!THREE.EffectComposer) { - throw "No EffectComposer"; - } - - this.composer = new THREE.EffectComposer( - instance, - renderTarget - ); - - this.renderPass = new THREE.RenderPass( - this.scene.instance, - this.camera.instance - ); - - this.composer.addPass(this.renderPass); - - if (!THREE.CopyShader) { - throw "No CopyShader found"; - } - - var copyShader = THREE.CopyShader; - var copyPass = new THREE.ShaderPass(copyShader); - copyPass.renderToScreen = true; - - this.composer.addPass(copyPass); - } - - return instance; -}; - -GameLib.D3.StereoRenderer.prototype.render = function() { - - this.instance.setViewport(0, 0, this.width / 2, this.height); - this.renderPass.camera = this.camera.instance.left; - this.composer.render(); - - this.instance.setViewport(this.width / 2, 0, this.width / 2, this.height); - this.renderPass.camera = this.camera.instance.right; - this.composer.render(); -}; \ No newline at end of file diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index 6b5ed20..05ba6f2 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -3,16 +3,14 @@ * created * @param apiTexture * @param graphics GameLib.D3.Graphics - * @param parentMaterial GameLib.D3.Material - * @param parentMaterialInstanceMapId String * @param imageFactory GameLib.D3.ImageFactory result * @constructor */ GameLib.D3.Texture = function Texture( graphics, apiTexture, - parentMaterial, - parentMaterialInstanceMapId, + // parentMaterial, + // parentMaterialInstanceMapId, imageFactory ) { this.graphics = graphics; @@ -55,9 +53,9 @@ GameLib.D3.Texture = function Texture( this.repeat ); - this.parentMaterial = parentMaterial; - - this.parentMaterialInstanceMapId = parentMaterialInstanceMapId; + // this.parentMaterial = parentMaterial; + // + // this.parentMaterialInstanceMapId = parentMaterialInstanceMapId; this.imageInstance = null; @@ -81,8 +79,8 @@ GameLib.D3.Texture.prototype.loadTexture = function(imageFactory) { function (imageInstance){ this.imageInstance = imageInstance; this.instance = this.createInstance(); - this.parentMaterial.instance[this.parentMaterialInstanceMapId] = this.instance; - this.parentMaterial.instance.needsUpdate = true; + // this.parentMaterial.instance[this.parentMaterialInstanceMapId] = this.instance; + // this.parentMaterial.instance.needsUpdate = true; }.bind(this), function onRejected() { } @@ -177,31 +175,29 @@ GameLib.D3.Texture.TEXTURE_TYPE_SPECULAR = 'specular'; */ GameLib.D3.Texture.prototype.createInstance = function(update) { - // var instance = null; - // - // if (update) { - // instance = this.instance; - // instance.mapping = this.mapping; - // instance.wrapS = this.wrapS; - // instance.wrapT = this.wrapT; - // instance.magFilter = this.magFilter; - // instance.minFilter = this.minFilter; - // instance.anisotropy = this.anisotropy; - // } else { - // - // } + var instance = null; - var instance = new THREE.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 - ); + if (update) { + instance = this.instance; + instance.mapping = this.mapping; + instance.wrapS = this.wrapS; + instance.wrapT = this.wrapT; + instance.magFilter = this.magFilter; + instance.minFilter = this.minFilter; + instance.anisotropy = this.anisotropy; + } else { + instance = new THREE.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; @@ -215,11 +211,11 @@ GameLib.D3.Texture.prototype.createInstance = function(update) { instance.premultiplyAlpha = this.premultiplyAlpha; instance.textureType = this.textureType; - if (this.parentMaterial && - this.parentMaterial.instance && - this.parentMaterialInstanceMapId) { - this.parentMaterial.instance[this.parentMaterialInstanceMapId] = instance; - } + // if (this.parentMaterial && + // this.parentMaterial.instance && + // this.parentMaterialInstanceMapId) { + // this.parentMaterial.instance[this.parentMaterialInstanceMapId] = instance; + // } instance.needsUpdate = true; @@ -277,23 +273,21 @@ 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 instanceMapId String * @param imageFactory GameLib.D3.ImageFactory * @constructor */ GameLib.D3.Texture.FromObjectTexture = function( graphics, objectTexture, - gameLibMaterial, - instanceMapId, + // gameLibMaterial, + // instanceMapId, imageFactory ) { + var apiTexture = GameLib.D3.API.Texture.FromObjectTexture(objectTexture); + return new GameLib.D3.Texture( graphics, - GameLib.D3.API.Texture.FromObjectTexture(objectTexture), - gameLibMaterial, - instanceMapId, + apiTexture, imageFactory ); }; diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index 66cf474..92799f5 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -28,19 +28,23 @@ GameLib.Entity = function ( } if (apiComponent instanceof GameLib.D3.API.Renderer) { - if (apiComponent.componentType == GameLib.Component.COMPONENT_RENDERER) { - return new GameLib.D3.Renderer(this.graphics, apiComponent); - } else if (apiComponent.componentType == GameLib.Component.COMPONENT_STEREO_RENDERER) { - return new GameLib.D3.StereoRenderer(this.graphics, apiComponent); - } + return new GameLib.D3.Renderer(this.graphics, apiComponent); + } + + if (apiComponent instanceof GameLib.D3.API.RenderTarget) { + return new GameLib.D3.RenderTarget(this.graphics, apiComponent); + } + + if (apiComponent instanceof GameLib.D3.API.Pass) { + return new GameLib.D3.Pass(this.graphics, apiComponent); + } + + if (apiComponent instanceof GameLib.D3.API.Composer) { + return new GameLib.D3.Composer(this.graphics, apiComponent); } if (apiComponent instanceof GameLib.D3.API.Camera) { - if (apiComponent.componentType == GameLib.Component.COMPONENT_CAMERA) { - return new GameLib.D3.Camera(this.graphics, apiComponent); - } else if (apiComponent.componentType == GameLib.Component.COMPONENT_STEREO_CAMERA) { - return new GameLib.D3.StereoCamera(this.graphics, apiComponent); - } + return new GameLib.D3.Camera(this.graphics, apiComponent); } if (apiComponent instanceof GameLib.D3.API.LookAt) { diff --git a/src/game-lib-game.js b/src/game-lib-game.js deleted file mode 100644 index 004fb3d..0000000 --- a/src/game-lib-game.js +++ /dev/null @@ -1,53 +0,0 @@ -GameLib.D3.Game = function ( - -) { - this.scenes = {}; -}; - -GameLib.D3.Game.prototype.addScene = function( - scene, - identifer -) { - this.scenes[identifer] = scene; -}; - -GameLib.D3.Game.prototype.processPhysics = function ( - dt -) { - for(var s in this.scenes) { - var scene = this.scenes[s]; - - for(var w in scene.worlds) { - var world = scene.worlds[w]; - world.step(dt); - } - } -}; - -GameLib.D3.Game.prototype.render = function( - dt, - renderer -) { - for(var s in this.scenes) { - var scene = this.scenes[s]; - scene.render(dt, renderer); - } -}; - -GameLib.D3.Game.prototype.update = function( - dt, - fixedDt -) { - for(var s in this.scenes) { - var scene = this.scenes[s]; - - for(var w in scene.worlds) { - var world = scene.worlds[w]; - // NOTE: We are calling the step function with a variable timestep! - world.step(fixedDt, dt); - } - - scene.update(dt); - scene.lateUpdate(dt); - } -}; \ No newline at end of file diff --git a/src/game-lib-system-render.js b/src/game-lib-system-render.js index 496eac6..f2063a7 100644 --- a/src/game-lib-system-render.js +++ b/src/game-lib-system-render.js @@ -66,23 +66,31 @@ GameLib.System.Render.prototype.update = function(deltaTime) { } ); - // renderObjects.forEach(function(object) { - // - // //TODO camera component stuff - // object.quaternion.x = this.parentEntity.quaternion.x; - // object.quaternion.y = this.parentEntity.quaternion.y; - // object.quaternion.z = this.parentEntity.quaternion.z; - // object.quaternion.w = this.parentEntity.quaternion.w; - // - // object.position.x = this.parentEntity.position.x; - // object.position.y = this.parentEntity.position.y; - // object.position.z = this.parentEntity.position.z; - // - // object.updateInstance(); - // - // //TODO scene component stuff - // renderer.render(this.instance, this.cameras[this.activeCameraIndex].instance); - // }); + var renderer = this.renderers[this.activeRendererIndex]; + + var camera = this.cameras[this.activeCameraIndex]; + + if (renderer.rendererType == GameLib.D3.Renderer.RENDER_TYPE_NORMAL) { + renderer.instance.render( + this.instance, + camera.instance + ); + } + + if (renderer.rendererType = GameLib.D3.Renderer.RENDER_TYPE_STEREO) { + + renderer.composer.passes[0].instance.scene = this; + + renderer.instance.setViewport(0, 0, this.width / 2, this.height); + renderer.composer.passes[0].instance.camera = camera.instance.left; + renderer.composer.instance.render(); + + renderer.instance.setViewport(this.width / 2, 0, this.width / 2, this.height); + renderer.composer.passes[0].instance.camera = camera.instance.right; + renderer.composer.instance.render(); + } + + };