From c3475ccef73f2c1570cc3501d1a79954ec010f0c Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Sun, 8 Oct 2017 03:17:36 +0200 Subject: [PATCH] fonts, text meshes, lines, input system refactorings --- src/game-lib-a-1-event.js | 8 + src/game-lib-a-component-a.js | 41 ++- src/game-lib-d3-api-font.js | 51 +++ src/game-lib-d3-api-light.js | 2 +- src/game-lib-d3-api-scene.js | 62 ---- src/game-lib-d3-controls-0.js | 4 +- src/game-lib-d3-controls-editor.js | 47 +-- src/game-lib-d3-font.js | 93 +++++ src/game-lib-d3-light.js | 2 + src/game-lib-d3-material.js | 124 ++++++- src/game-lib-d3-mesh-0.js | 34 +- src/game-lib-d3-mesh-text.js | 219 ++++++++++++ src/game-lib-d3-renderer.js | 4 - src/game-lib-d3-scene.js | 138 +++++--- src/game-lib-d3-shape-convex-hull-cylinder.js | 6 +- src/game-lib-system-gui.js | 77 +++-- src/game-lib-system-input.js | 321 ++++++++---------- src/game-lib-system-linking.js | 37 +- src/game-lib-system-storage.js | 53 +++ 19 files changed, 949 insertions(+), 374 deletions(-) create mode 100644 src/game-lib-d3-api-font.js create mode 100644 src/game-lib-d3-font.js create mode 100644 src/game-lib-d3-mesh-text.js diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index baace5f..db1db9c 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -99,6 +99,10 @@ GameLib.Event.ANIMATION_MESH_ADDED = 0x51; GameLib.Event.ANIMATION_MESH_REMOVED = 0x52; GameLib.Event.GET_SCENE = 0x53; GameLib.Event.CUSTOM_CODE_WINDOW_RESIZE = 0x54; +GameLib.Event.LOAD_FONT = 0x55; +GameLib.Event.FONT_NOT_FOUND = 0x56; +GameLib.Event.FONT_INSTANCE_CREATED = 0x57; +GameLib.Event.REGISTER_DEPENDENCIES = 0x58; /** * Returns string name of event ID @@ -193,6 +197,10 @@ GameLib.Event.GetEventName = function(number) { case 0x52 : return 'animation_mesh_removed'; case 0x53 : return 'get_scene'; case 0x54 : return 'custom_code_window_resize'; + case 0x55 : return 'load_font'; + case 0x56 : return 'font_not_found'; + case 0x57 : return 'font_instance_created'; + case 0x58 : return 'register_dependencies'; break; } diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index 850b22f..42e9aac 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -37,6 +37,13 @@ GameLib.Component = function( } ); + // GameLib.Event.Emit( + // GameLib.Event.COMPONENT_CREATED, + // { + // component : this + // } + // ); + if (this.dependencies.length === 0) { delete this.dependencies; @@ -48,6 +55,13 @@ GameLib.Component = function( this.buildIdToObject(); GameLib.Event.EmitInstanceEvents(this); } + } else { + GameLib.Event.Emit( + GameLib.Event.REGISTER_DEPENDENCIES, + { + component : this + } + ); } @@ -76,16 +90,33 @@ GameLib.Component.prototype.getDependencies = function() { this.hasOwnProperty(property) ){ if (typeof this[property] === 'string') { - dependencies.push(this[property]); + GameLib.Utils.PushUnique(dependencies, this[property]); } if (this[property] instanceof Array) { this[property].map(function(arrayProperty){ - if (typeof arrayProperty === 'string') { - dependencies.push(arrayProperty); + + if (typeof arrayProperty === 'string') { + GameLib.Utils.PushUnique(dependencies, arrayProperty); } + + if (arrayProperty && + typeof arrayProperty === 'object' && + arrayProperty.hasOwnProperty('loaded') && + arrayProperty.loaded === false + ) { + GameLib.Utils.PushUnique(dependencies, arrayProperty.id); + } }); } + + if (this[property] && + typeof this[property] === 'object' && + this[property].hasOwnProperty('loaded') && + this[property].loaded === false + ) { + GameLib.Utils.PushUnique(dependencies, this[property].id); + } } } @@ -154,6 +185,8 @@ GameLib.Component.COMPONENT_CLOCK = 0x37; GameLib.Component.COMPONENT_ANIMATION = 0x38; GameLib.Component.COMPONENT_CONTROLS_KEYBOARD = 0x39; GameLib.Component.COMPONENT_CONTROLS_MOUSE = 0x3a; +GameLib.Component.COMPONENT_MESH_TEXT = 0x3b; +GameLib.Component.COMPONENT_FONT = 0x3c; /** * Returns string name for component number @@ -221,6 +254,8 @@ GameLib.Component.GetComponentName = function(number) { case 0x38 : return 'GameLib.D3.Animation'; case 0x39 : return 'GameLib.D3.Controls.Keyboard'; case 0x3a : return 'GameLib.D3.Controls.Mouse'; + case 0x3b : return 'GameLib.D3.Mesh.Text'; + case 0x3c : return 'GameLib.D3.Font'; break; } diff --git a/src/game-lib-d3-api-font.js b/src/game-lib-d3-api-font.js new file mode 100644 index 0000000..e4409b5 --- /dev/null +++ b/src/game-lib-d3-api-font.js @@ -0,0 +1,51 @@ +/** + * Raw Font API object - should always correspond with the Font Schema + * @param id + * @param name + * @param url + * @param parentEntity + * @constructor + */ +GameLib.D3.API.Font = function( + id, + name, + url, + parentEntity +) { + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Font (' + id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(url)) { + url = '/apiRelative/path/to/font'; + } + this.url = url; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)){ + parentEntity = null; + } + this.parentEntity = parentEntity; +}; + +GameLib.D3.API.Font.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.Font.prototype.constructor = GameLib.D3.API.Font; + +/** + * Returns an API light from an Object light + * @param objectFont + * @constructor + */ +GameLib.D3.API.Font.FromObject = function(objectFont) { + return new GameLib.D3.API.Font( + objectFont.id, + objectFont.name, + objectFont.url, + objectFont.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-light.js b/src/game-lib-d3-api-light.js index ffa48c1..2858ca1 100644 --- a/src/game-lib-d3-api-light.js +++ b/src/game-lib-d3-api-light.js @@ -81,7 +81,7 @@ GameLib.D3.API.Light = function( this.intensity = intensity; if (GameLib.Utils.UndefinedOrNull(position)) { - position = new GameLib.API.Vector3(0,10,0); + position = new GameLib.API.Vector3(10,10,10); } this.position = position; diff --git a/src/game-lib-d3-api-scene.js b/src/game-lib-d3-api-scene.js index d55ee64..75a4ac6 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -3,15 +3,10 @@ * @param id String * @param name String * @param meshes [GameLib.D3.API.Mesh] - * @param position GameLib.API.Vector3 - * @param quaternion GameLib.API.Quaternion - * @param scale GameLib.API.Vector3 - * @param parentGameId * @param lights [GameLib.D3.API.Light] * @param textures [GameLib.D3.API.Texture] * @param materials [GameLib.D3.API.Material] * @param images - * @param activeCamera [GameLib.D3.Camera] * @param parentEntity * @constructor */ @@ -19,15 +14,10 @@ GameLib.D3.API.Scene = function( id, name, meshes, - position, - quaternion, - scale, - parentGameId, lights, textures, materials, images, - activeCamera, parentEntity ) { @@ -46,26 +36,6 @@ GameLib.D3.API.Scene = function( } this.meshes = meshes; - if (GameLib.Utils.UndefinedOrNull(position)) { - position = new GameLib.API.Vector3(); - } - this.position = position; - - if (GameLib.Utils.UndefinedOrNull(quaternion)) { - quaternion = new GameLib.API.Quaternion(); - } - this.quaternion = quaternion; - - if (GameLib.Utils.UndefinedOrNull(scale)) { - scale = new GameLib.API.Vector3(1,1,1); - } - this.scale = scale; - - if (GameLib.Utils.UndefinedOrNull(parentGameId)) { - parentGameId = null; - } - this.parentGameId = parentGameId; - if (GameLib.Utils.UndefinedOrNull(lights)) { lights = []; } @@ -86,11 +56,6 @@ GameLib.D3.API.Scene = function( } this.images = images; - if (GameLib.Utils.UndefinedOrNull(activeCamera)) { - activeCamera = null; - } - this.activeCamera = activeCamera; - if (GameLib.Utils.UndefinedOrNull(parentEntity)) { parentEntity = null; } @@ -114,12 +79,6 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) { var apiMaterials = []; var apiImages = []; - var apiPosition = new GameLib.API.Vector3(); - var apiQuaternion = new GameLib.API.Quaternion(); - var apiScale = new GameLib.API.Vector3(1,1,1); - - var apiActiveCamera = null; - if (objectScene.meshes) { apiMeshes = objectScene.meshes.map( function(objectMesh) { @@ -180,35 +139,14 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) { ) } - if (objectScene.position) { - apiPosition = GameLib.API.Vector3.FromObject(objectScene.position); - } - - if (objectScene.quaternion) { - apiQuaternion = GameLib.API.Quaternion.FromObject(objectScene.quaternion); - } - - if (objectScene.scale) { - apiScale = GameLib.API.Vector3.FromObject(objectScene.scale); - } - - if (objectScene.activeCamera) { - apiActiveCamera = objectScene.activeCamera; - } - return new GameLib.D3.API.Scene( objectScene.id, objectScene.name, apiMeshes, - apiPosition, - apiQuaternion, - apiScale, - objectScene.parentGameId, apiLights, apiTextures, apiMaterials, apiImages, - apiActiveCamera, objectScene.parentEntity ); diff --git a/src/game-lib-d3-controls-0.js b/src/game-lib-d3-controls-0.js index 452e824..c314f90 100644 --- a/src/game-lib-d3-controls-0.js +++ b/src/game-lib-d3-controls-0.js @@ -38,8 +38,8 @@ GameLib.D3.Controls = function ( componentType = GameLib.Component.COMPONENT_CONTROLS_EDITOR; - linkedObjects.raycaster = GameLib.D3.Raycaster; - linkedObjects.renderer = GameLib.D3.Renderer; + linkedObjects.raycaster = GameLib.D3.Raycaster; + linkedObjects.camera = GameLib.D3.Camera; } if (this.controlsType === GameLib.D3.Controls.CONTROLS_TYPE_TOUCH) { diff --git a/src/game-lib-d3-controls-editor.js b/src/game-lib-d3-controls-editor.js index 894c57b..726f6fd 100644 --- a/src/game-lib-d3-controls-editor.js +++ b/src/game-lib-d3-controls-editor.js @@ -3,14 +3,14 @@ * @param graphics GameLib.D3.Graphics * @param apiControls GameLib.D3.API.Controls * @param raycaster - * @param renderer + * @param camera * @constructor */ GameLib.D3.Controls.Editor = function ( graphics, apiControls, raycaster, - renderer + camera ) { this.graphics = graphics; @@ -21,10 +21,10 @@ GameLib.D3.Controls.Editor = function ( } this.raycaster = raycaster; - if (GameLib.Utils.UndefinedOrNull(renderer)) { - renderer = null; + if (GameLib.Utils.UndefinedOrNull(camera)) { + camera = null; } - this.renderer = renderer; + this.camera = camera; if (this.raycaster instanceof GameLib.D3.API.Raycaster) { this.raycaster = new GameLib.D3.Raycaster( @@ -33,10 +33,10 @@ GameLib.D3.Controls.Editor = function ( ); } - if (this.renderer instanceof GameLib.D3.API.Renderer) { - this.renderer = new GameLib.D3.Renderer( + if (this.camera instanceof GameLib.D3.API.Camera) { + this.camera = new GameLib.D3.Camera( this.graphics, - this.renderer + this.camera ) } @@ -66,29 +66,17 @@ GameLib.D3.Controls.Editor.prototype.delayedInstance = function() { console.log('GameLib.D3.Controls.Editor.delayedInstance() called'); - if (!this.renderer) { - throw new Error('No renderer at time of creating instance'); - } - - if (!this.renderer.camera) { + if (!this.camera || !this.camera.instance) { throw new Error('No camera at time of instance'); } - if (!this.renderer.domElement) { + if (!this.domElement || !this.domElement.instance) { throw new Error('No dom element at time of instance'); } - if (!this.renderer.camera.instance) { - throw new Error('No camera instance at time of instance'); - } - - if (!this.renderer.domElement.instance) { - throw new Error('No dom element instance at time of instance'); - } - var instance = new THREE.EditorControls( - this.renderer.camera.instance, - this.renderer.domElement.instance + this.camera.instance, + this.domElement.instance ); return instance; @@ -119,8 +107,8 @@ GameLib.D3.Controls.Editor.prototype.toApiObject = function() { var apiControls = GameLib.D3.Controls.prototype.toApiObject.call(this); - apiControls.raycaster = GameLib.Utils.IdOrNull(this.raycaster); - apiControls.renderer = GameLib.Utils.IdOrNull(this.renderer); + apiControls.raycaster = GameLib.Utils.IdOrNull(this.raycaster); + apiControls.camera = GameLib.Utils.IdOrNull(this.camera); return apiControls; }; @@ -136,12 +124,11 @@ GameLib.D3.Controls.Editor.FromObject = function(graphics, objectControls) { var apiControls = GameLib.D3.API.Controls.FromObject(objectControls); - apiControls.renderer = objectControls.renderer; - apiControls.raycaster = objectControls.raycaster; - return new GameLib.D3.Controls.Editor( graphics, - apiControls + apiControls, + apiControls.raycaster, + apiControls.camera ); }; \ No newline at end of file diff --git a/src/game-lib-d3-font.js b/src/game-lib-d3-font.js new file mode 100644 index 0000000..849164a --- /dev/null +++ b/src/game-lib-d3-font.js @@ -0,0 +1,93 @@ +/** + * Font Superset - The apiFont properties get moved into the Font object itself, and then the instance is created + * @param graphics GameLib.D3.Graphics + * @param apiFont GameLib.D3.API.Font + * @constructor + */ +GameLib.D3.Font = function( + graphics, + apiFont +) { + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiFont)) { + apiFont = {}; + } + + if (apiFont instanceof GameLib.D3.Font) { + return apiFont; + } + + GameLib.D3.API.Font.call( + this, + apiFont.id, + apiFont.name, + apiFont.url, + apiFont.parentEntity + ); + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_FONT + ); +}; + +GameLib.D3.Font.prototype = Object.create(GameLib.D3.API.Font.prototype); +GameLib.D3.Font.prototype.constructor = GameLib.D3.Font; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.Font.prototype.createInstance = function() { + + GameLib.Event.Emit( + GameLib.Event.LOAD_FONT, + { + font : this + } + ); + + return null; +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Font.prototype.updateInstance = function() { + GameLib.Event.Emit( + GameLib.Event.LOAD_FONT, + { + font : this + } + ); +}; + +/** + * Converts a GameLib.D3.Font to a GameLib.D3.API.Font + * @returns {GameLib.D3.API.Font} + */ +GameLib.D3.Font.prototype.toApiObject = function() { + return new GameLib.D3.API.Font( + this.id, + this.name, + this.url, + GameLib.Utils.IdOrNull(this.parentEntity) + ); +}; + +/** + * Returns a new GameLib.D3.Font from a GameLib.D3.API.Font + * @param graphics GameLib.D3.Graphics + * @param objectFont GameLib.D3.API.Font + * @returns {GameLib.D3.Font} + */ +GameLib.D3.Font.FromObject = function(graphics, objectFont) { + + return new GameLib.D3.Font( + graphics, + GameLib.D3.API.Font.FromObject(objectFont) + ); + +}; \ No newline at end of file diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js index 30044b4..f43bd59 100644 --- a/src/game-lib-d3-light.js +++ b/src/game-lib-d3-light.js @@ -36,6 +36,7 @@ GameLib.D3.Light = function( apiLight.power, apiLight.angle, apiLight.penumbra, + apiLight.parentScene, apiLight.parentEntity ); @@ -295,6 +296,7 @@ GameLib.D3.Light.prototype.toApiObject = function() { this.power, this.angle, this.penumbra, + GameLib.Utils.IdOrNull(this.parentScene), GameLib.Utils.IdOrNull(this.parentEntity) ); }; diff --git a/src/game-lib-d3-material.js b/src/game-lib-d3-material.js index 392e213..a5db792 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -339,6 +339,14 @@ GameLib.D3.Material.MATERIAL_TYPE_STANDARD = 0x8; GameLib.D3.Material.MATERIAL_TYPE_POINTS = 0x9; GameLib.D3.Material.MATERIAL_TYPE_SPRITE = 0xa; +GameLib.D3.Material.LINE_CAP_BUTT = 0x1;//'butt'; +GameLib.D3.Material.LINE_CAP_ROUND = 0x2;//'round'; +GameLib.D3.Material.LINE_CAP_SQUARE = 0x3;//'square'; + +GameLib.D3.Material.LINE_JOIN_ROUND = 0x1;//'round'; +GameLib.D3.Material.LINE_JOIN_BEVEL = 0x2;//'bevel'; +GameLib.D3.Material.LINE_JOIN_MITER = 0x3;//'miter'; + GameLib.D3.Material.prototype.createStandardMaterialInstance = function() { return new THREE.MeshStandardMaterial({ name: this.name, @@ -413,6 +421,57 @@ GameLib.D3.Material.prototype.createPointsMaterialInstance = function() { }); }; +GameLib.D3.Material.prototype.createLineBasicMaterialInstance = function() { + + var linecap = 'round'; + + if (this.lineCap === GameLib.D3.Material.LINE_CAP_BUTT) { + linecap = 'butt'; + } + + if (this.lineCap === GameLib.D3.Material.LINE_CAP_SQUARE) { + linecap = 'square'; + } + + var linejoin = 'round'; + + if (this.lineJoin === GameLib.D3.Material.LINE_JOIN_BEVEL) { + linejoin = 'bevel'; + } + + if (this.lineJoin === GameLib.D3.Material.LINE_JOIN_MITER) { + linejoin = 'miter'; + } + + return new THREE.LineBasicMaterial({ + name: this.name, + opacity: this.opacity, + transparent: this.transparent, + // blending: this.blending, + // blendSrc: this.blendSrc, + // blendDst: this.blendDst, + // blendEquation: this.blendEquation, + depthTest: this.depthTest, + depthFunc: this.depthFunc, + depthWrite: this.depthWrite, + // polygonOffset: this.polygonOffset, + // polygonOffsetFactor: this.polygonOffsetFactor, + // polygonOffsetUnits: this.polygonOffsetUnits, + // alphaTest: this.alphaTest, + // clippingPlanes: this.clippingPlanes, + // clipShadows: this.clipShadows, + // overdraw: this.overdraw, + visible: this.visible, + side: this.side, + color: this.color.instance, + linewidth: this.lineWidth, + linecap: linecap, + linejoin: linejoin + // vertexColors: GameLib.D3.Material.TYPE_VERTEX_COLORS, + // fog: this.fog + }); +}; + GameLib.D3.Material.prototype.createPhongMaterialInstance = function() { return new THREE.MeshPhongMaterial({ name: this.name, @@ -644,6 +703,58 @@ GameLib.D3.Material.prototype.updatePointsMaterialInstance = function() { //this.instance.fog = this.fog; }; +GameLib.D3.Material.prototype.updateLineBasicMaterialInstance = function() { + + var linecap = 'round'; + + if (this.lineCap === GameLib.D3.Material.LINE_CAP_BUTT) { + linecap = 'butt'; + } + + if (this.lineCap === GameLib.D3.Material.LINE_CAP_SQUARE) { + linecap = 'square'; + } + + var linejoin = 'round'; + + if (this.lineJoin === GameLib.D3.Material.LINE_JOIN_BEVEL) { + linejoin = 'bevel'; + } + + if (this.lineJoin === GameLib.D3.Material.LINE_JOIN_MITER) { + linejoin = 'miter'; + } + + this.instance.name = this.name; + this.instance.opacity = this.opacity; + this.instance.transparent = this.transparent; + // this.instance.blending = this.blending; + // this.instance.blendSrc = this.blendSrc; + // this.instance.blendDst = this.blendDst; + // this.instance.blendEquation = this.blendEquation; + // this.instance.depthTest = this.depthTest; + this.instance.depthFunc = this.depthFunc; + this.instance.depthWrite = this.depthWrite; + // this.instance.polygonOffset = this.polygonOffset; + // this.instance.polygonOffsetFactor = this.polygonOffsetFactor; + // this.instance.polygonOffsetUnits = this.polygonOffsetUnits; + // this.instance.alphaTest = this.alphaTest; + // this.instance.clippingPlanes = this.clippingPlanes; + // this.instance.clipShadows = this.clipShadows; + // this.instance.overdraw = this.overdraw; + this.instance.visible = this.visible; + this.instance.side = this.side; + this.instance.color = this.color.instance; + + this.instance.linewidth = this.lineWidth; + this.instance.linecap = linecap; + this.instance.linejoin = linejoin; + + //this.instance.vertexColors = GameLib.D3.Material.TYPE_VERTEX_COLORS; + //this.instance.fog = this.fog; +}; + + GameLib.D3.Material.prototype.updatePhongMaterialInstance = function() { this.instance.name = this.name; this.instance.opacity = this.opacity; @@ -740,6 +851,10 @@ GameLib.D3.Material.prototype.createInstance = function() { instance = this.createMeshBasicMaterialInstance(); + } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_LINE_BASIC) { + + instance = this.createLineBasicMaterialInstance(); + } else { console.warn("material type is not implemented yet: " + this.materialType); } @@ -761,7 +876,7 @@ GameLib.D3.Material.prototype.updateInstance = function() { // console.log('material update instance'); if (!this.instance) { - console.warn('Attempt to update a non-existent instance'); + //console.warn('Attempt to update a non-existent instance'); return; } @@ -795,6 +910,13 @@ GameLib.D3.Material.prototype.updateInstance = function() { } else { this.updateMeshBasicMaterialInstance(); } + } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_LINE_BASIC) { + if (!(this.instance instanceof THREE.LineBasicMaterial)) { + this.instance = this.createLineBasicMaterialInstance(); + typeChange = true; + } else { + this.updateLineBasicMaterialInstance(); + } } else { console.warn('not yet implemented (material type = ' + this.materialType + ')'); } diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index 0adf967..06c77f1 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -122,12 +122,19 @@ GameLib.D3.Mesh = function ( this ); - this.size = new GameLib.Vector3( + this.dimensions = new GameLib.Vector3( this.graphics, new GameLib.API.Vector3(), this ); + var linkedObjects = { + 'parentMesh' : GameLib.D3.Mesh, + 'parentScene' : GameLib.D3.Scene, + 'materials' : [GameLib.D3.Material], + 'skeleton' : GameLib.D3.Skeleton + }; + var componentType = GameLib.Component.COMPONENT_MESH; if (this.meshType === GameLib.D3.Mesh.MESH_TYPE_PLANE) { @@ -138,6 +145,11 @@ GameLib.D3.Mesh = function ( componentType = GameLib.Component.COMPONENT_MESH_SPHERE } + if (this.meshType === GameLib.D3.Mesh.MESH_TYPE_TEXT) { + componentType = GameLib.Component.COMPONENT_MESH_TEXT; + linkedObjects.font = GameLib.D3.Font; + } + /** * Runtime meshes have helpers too * @type {null} @@ -149,12 +161,7 @@ GameLib.D3.Mesh = function ( GameLib.Component.call( this, componentType, - { - 'parentMesh' : GameLib.D3.Mesh, - 'parentScene' : GameLib.D3.Scene, - 'materials' : [GameLib.D3.Material], - 'skeleton' : GameLib.D3.Skeleton - } + linkedObjects ); }; @@ -172,6 +179,7 @@ GameLib.D3.Mesh.MESH_TYPE_SPHERE = 0x3; GameLib.D3.Mesh.MESH_TYPE_PLANE = 0x4; GameLib.D3.Mesh.MESH_TYPE_BOX = 0x5; GameLib.D3.Mesh.MESH_TYPE_CYLINDER = 0x6; +GameLib.D3.Mesh.MESH_TYPE_TEXT = 0x7; GameLib.D3.Mesh.prototype.createInstanceGeometry = function(instanceGeometry) { @@ -1353,7 +1361,11 @@ GameLib.D3.Mesh.prototype.addMaterial = function(material) { if (this.materials.length === 1) { this.instance.material = material.instance; } else { - this.instance.material.push(material.instance); + this.instance.material = this.materials.map( + function(material) { + return material.instance; + } + ); } }; @@ -1397,7 +1409,7 @@ GameLib.D3.Mesh.prototype.computeBoundingBox = function(geometry) { } geometry.computeBoundingBox(); - this.size.x = geometry.boundingBox.getSize().x; - this.size.y = geometry.boundingBox.getSize().y; - this.size.z = geometry.boundingBox.getSize().z; + this.dimensions.x = geometry.boundingBox.getSize().x; + this.dimensions.y = geometry.boundingBox.getSize().y; + this.dimensions.z = geometry.boundingBox.getSize().z; }; diff --git a/src/game-lib-d3-mesh-text.js b/src/game-lib-d3-mesh-text.js new file mode 100644 index 0000000..781ee97 --- /dev/null +++ b/src/game-lib-d3-mesh-text.js @@ -0,0 +1,219 @@ +/** + * Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created + * @param graphics GameLib.D3.Graphics + * @param apiMesh GameLib.D3.API.Mesh + * @param font + * @param size + * @param height + * @param curveSegments + * @param bevelEnabled + * @param bevelThickness + * @param bevelSize + * @param bevelSegments + * @param text + * @constructor + */ +GameLib.D3.Mesh.Text = function ( + graphics, + apiMesh, + text, + font, + size, + height, + curveSegments, + bevelEnabled, + bevelThickness, + bevelSize, + bevelSegments +) { + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(text)) { + text = '-= 0) { - - this.renderers = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Renderer); - - this.renderers.map( - - function(renderer) { - - renderer.controls = this.editorControls.reduce( - function(result, editorControl) { - if (editorControl.renderer === renderer) { - result = editorControl; - } - return result; - }, - null - ); - - renderer.mouseDown = this.onMouseDown(renderer, renderer.controls).bind(this); - renderer.domElement.instance.addEventListener( + this.editorControls.map( + function(editorControl) { + editorControl.domElement.instance.addEventListener( 'mousedown', - renderer.mouseDown, + this.mouseDown, false ); - renderer.mouseMove = this.onMouseMove.bind(this); - renderer.domElement.instance.addEventListener( + editorControl.domElement.instance.addEventListener( 'mousemove', - renderer.mouseMove, + this.mouseMove, false ); - renderer.keyDown = this.onKeyDown.bind(this); - renderer.domElement.instance.addEventListener( + editorControl.domElement.instance.addEventListener( 'keydown', - renderer.keyDown, + this.keyDown, false ); - renderer.keyUp = this.onKeyUp.bind(this); - renderer.domElement.instance.addEventListener( + editorControl.domElement.instance.addEventListener( 'keyup', - renderer.keyUp, + this.keyUp, false ); - if (renderer.controls) { - /** - * Create the delayed instance here - it affects the order of event listeners attached to DOM - */ - renderer.controls.instance = renderer.controls.delayedInstance(); - } else { - console.warn('no third party controls for renderer : ' + renderer.name); - } + editorControl.instance = editorControl.delayedInstance(); - renderer.mouseWheel = this.onMouseWheel(renderer.camera).bind(this); - renderer.domElement.instance.addEventListener( + editorControl.domElement.instance.addEventListener( 'mousewheel', - renderer.mouseWheel, + this.mouseWheel, false ); - renderer.mouseUp = this.onMouseUp(renderer.camera, renderer.controls).bind(this); - renderer.domElement.instance.addEventListener( + editorControl.domElement.instance.addEventListener( 'mouseup', - renderer.mouseUp, + this.mouseUp, false ); }.bind(this) - ); + ) } }; @@ -293,7 +275,7 @@ GameLib.System.Input.prototype.onTouchMove = function (event) { this.touches[id].pageX = event.changedTouches[t].pageX; this.touches[id].pageY = event.changedTouches[t].pageY; - console.log(this.touches[id]); + //console.log(this.touches[id]); } this.touches.event = event; @@ -424,89 +406,92 @@ GameLib.System.Input.prototype.onKeyUp = function(event) { } }; -GameLib.System.Input.prototype.onMouseDown = function(renderer, controls) { +GameLib.System.Input.prototype.onMouseDown = function(event) { - return function(event) { + if (event.button === 2) { - if (event.button === 2) { + this.editorControls.map( - if (this.controlLeft) { - return; - } + function(editorControl) { - renderer.mouse.x = (event.offsetX / renderer.instance.domElement.width) * 2 - 1; - renderer.mouse.y = -(event.offsetY / renderer.instance.domElement.height) * 2 + 1; + if (this.controlLeft) { + return; + } - var scenes = renderer.scenes; + this.mouse.x = (event.offsetX / event.target.width ) * 2 - 1; + this.mouse.y = -(event.offsetY / event.target.height) * 2 + 1; - var intersects = scenes.reduce( + var scenes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Scene); - function(result, scene) { + var intersects = scenes.reduce( - controls.raycaster.instance.setFromCamera( - renderer.mouse, - renderer.camera.instance - ); + function (result, scene) { - intersects = controls.raycaster.getIntersectedObjects(scene.meshes); + editorControl.raycaster.instance.setFromCamera( + this.mouse, + editorControl.camera.instance + ); - intersects.map(function(intersect){ - result.push(intersect); - }); + intersects = editorControl.raycaster.getIntersectedObjects(scene.meshes); - return result; - }.bind(this), - [] - ); + intersects.map(function (intersect) { + result.push(intersect); + }); - intersects.sort( - function(a, b) { - if (a.distance < b.distance) { - return -1; - } + return result; + }.bind(this), + [] + ); - if (a.distance > b.distance) { - return 1; - } + intersects.sort( + function (a, b) { + if (a.distance < b.distance) { + return -1; + } - return 0; - } - ); + if (a.distance > b.distance) { + return 1; + } - var meshes = intersects.map(function(intersect){ - return intersect.mesh; - }); + return 0; + } + ); - var mesh = meshes[0]; + var meshes = intersects.map(function (intersect) { + return intersect.mesh; + }); - if (mesh) { + var mesh = meshes[0]; - /** - * Prevent default action (like context menu or whatever) - */ - event.preventDefault(); + if (mesh) { - /** - * Prevent other event listeners for 'mousedown' from executing their actions - */ - event.stopImmediatePropagation(); + /** + * Prevent default action (like context menu or whatever) + */ + event.preventDefault(); - if (mesh.selected) { - this.deSelectMesh(mesh); - } else { - this.selectMesh(mesh); - } + /** + * Prevent other event listeners for 'mousedown' from executing their actions + */ + event.stopImmediatePropagation(); - /** - * Notify our GUI system to build a GUI - */ - GameLib.Event.Emit( - GameLib.Event.BUILD_GUI, - null - ) - } - } - }.bind(this); + if (mesh.selected) { + this.deSelectMesh(mesh); + } else { + this.selectMesh(mesh); + } + + /** + * Notify our GUI system to build a GUI + */ + GameLib.Event.Emit( + GameLib.Event.BUILD_GUI, + null + ) + } + }.bind(this) + ); + } }; /** @@ -519,41 +504,42 @@ GameLib.System.Input.prototype.onMouseMove = function(event) { /** * Update the camera position etc. after mouse up - * @param __camera - * @param __controls * @returns {Function} + * @param event */ -GameLib.System.Input.prototype.onMouseUp = function(__camera, __controls) { - return function(event) { +GameLib.System.Input.prototype.onMouseUp = function(event) { + this.editorControls.map( + function(editorControl) { + editorControl.camera.position.x = editorControl.camera.instance.position.x; + editorControl.camera.position.y = editorControl.camera.instance.position.y; + editorControl.camera.position.z = editorControl.camera.instance.position.z; - __camera.position.x = __camera.instance.position.x; - __camera.position.y = __camera.instance.position.y; - __camera.position.z = __camera.instance.position.z; + editorControl.camera.quaternion.x = editorControl.camera.instance.quaternion.x; + editorControl.camera.quaternion.y = editorControl.camera.instance.quaternion.y; + editorControl.camera.quaternion.z = editorControl.camera.instance.quaternion.z; + editorControl.camera.quaternion.w = editorControl.camera.instance.quaternion.w; - __camera.quaternion.x = __camera.instance.quaternion.x; - __camera.quaternion.y = __camera.instance.quaternion.y; - __camera.quaternion.z = __camera.instance.quaternion.z; - __camera.quaternion.w = __camera.instance.quaternion.w; - - __camera.lookAt.x = __controls.instance.center.x; - __camera.lookAt.y = __controls.instance.center.y; - __camera.lookAt.z = __controls.instance.center.z; - - __camera.lookAt.instance.copy(__controls.instance.center); - }; + editorControl.camera.lookAt.x = editorControl.instance.center.x; + editorControl.camera.lookAt.y = editorControl.instance.center.y; + editorControl.camera.lookAt.z = editorControl.instance.center.z; + editorControl.camera.lookAt.instance.copy(editorControl.instance.center); + } + ); }; /** * Update our camera position after moving the mouse wheel - * @param __camera * @returns {Function} + * @param event */ -GameLib.System.Input.prototype.onMouseWheel = function(__camera) { - return function(event) { - __camera.position.x = __camera.instance.position.x; - __camera.position.y = __camera.instance.position.y; - __camera.position.z = __camera.instance.position.z; - } +GameLib.System.Input.prototype.onMouseWheel = function(event) { + this.editorControls.map( + function(editorControl) { + editorControl.camera.position.x = editorControl.camera.instance.position.x; + editorControl.camera.position.y = editorControl.camera.instance.position.y; + editorControl.camera.position.z = editorControl.camera.instance.position.z; + } + ); }; GameLib.System.Input.prototype.selectMesh = function(mesh) { @@ -606,56 +592,49 @@ GameLib.System.Input.prototype.stop = function() { /** * Now remove all input capabilities */ - this.renderers.map( - - function(renderer) { - - renderer.domElement.instance.removeEventListener( + this.editorControls.map( + function(editorControl) { + editorControl.domElement.instance.removeEventListener( 'mousedown', - renderer.mouseDown, + this.mouseDown, false ); - renderer.domElement.instance.removeEventListener( + editorControl.domElement.instance.removeEventListener( 'mousemove', - renderer.mouseMove, + this.mouseMove, false ); - renderer.domElement.instance.removeEventListener( + editorControl.domElement.instance.removeEventListener( 'keydown', - renderer.keyDown, + this.keyDown, false ); - renderer.domElement.instance.removeEventListener( + editorControl.domElement.instance.removeEventListener( 'keyup', - renderer.keyUp, + this.keyUp, false ); - if (renderer.controls && renderer.controls.instance) { - renderer.controls.instance.dispose(); - } else { - console.warn('no third party controls to stop for renderer : ' + renderer.name); - } + editorControl.instance.dispose(); - renderer.domElement.instance.removeEventListener( + editorControl.domElement.instance.removeEventListener( 'mousewheel', - renderer.mouseWheel, + this.mouseWheel, false ); - renderer.domElement.instance.removeEventListener( + editorControl.domElement.instance.removeEventListener( 'mouseup', - renderer.mouseUp, + this.mouseUp, false ); }.bind(this) - ); + ) } - if (this.touchControls.length > 0) { this.touchControls.map( diff --git a/src/game-lib-system-linking.js b/src/game-lib-system-linking.js index 153dbe0..805e06d 100644 --- a/src/game-lib-system-linking.js +++ b/src/game-lib-system-linking.js @@ -33,6 +33,7 @@ GameLib.System.Linking = function( this.lightInstanceCreatedSubscription = null; this.sceneInstanceCreatedSubscription = null; this.imageInstanceCreatedSubscription = null; + this.fontInstanceCreatedSubscription = null; this.textureInstanceCreatedSubscription = null; this.materialInstanceCreatedSubscription = null; this.meshDeletedSubscription = null; @@ -42,6 +43,7 @@ GameLib.System.Linking = function( this.instanceCreatedSubscription = null; this.shapeInstanceCreatedSubscription = null; this.solverInstanceCreatedSubscription = null; + this.registerDependenciesSubscription = null; }; @@ -92,6 +94,11 @@ GameLib.System.Linking.prototype.start = function() { this.imageInstanceCreated ); + this.fontInstanceCreatedSubscription = this.subscribe( + GameLib.Event.FONT_INSTANCE_CREATED, + this.fontInstanceCreated + ); + this.textureInstanceCreatedSubscription = this.subscribe( GameLib.Event.TEXTURE_INSTANCE_CREATED, this.textureInstanceCreated @@ -152,6 +159,15 @@ GameLib.System.Linking.prototype.start = function() { this.instanceCreated ); + this.registerDependenciesSubscription = this.subscribe( + GameLib.Event.REGISTER_DEPENDENCIES, + this.registerDependenciesDirect + ) + +}; + +GameLib.System.Linking.prototype.registerDependenciesDirect = function(data) { + this.registerDependencies(data.component); }; GameLib.System.Linking.prototype.link = function(component, data) { @@ -560,8 +576,8 @@ GameLib.System.Linking.prototype.meshInstanceCreated = function(data) { var scenes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Scene); scenes.map(function(scene){ - if (data.mesh.parentScene === scene) { - scene.addObject(data.mesh); + if (data.mesh.parentScene === scene.id) { + data.mesh.parentScene = scene; } }); @@ -627,6 +643,8 @@ GameLib.System.Linking.prototype.lightInstanceCreated = function(data) { scenes.map(function(scene){ if (data.light.parentScene === scene) { scene.addObject(data.light); + + //scene.updateInstance(); } }); @@ -642,12 +660,21 @@ GameLib.System.Linking.prototype.sceneInstanceCreated = function(data) { var objects = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Mesh,GameLib.D3.Light]); objects.map(function(object){ if ( - object.parentScene === data.scene + object.parentScene === data.scene.id ) { - data.scene.addObject(object); + object.parentScene = data.scene; } }); + //data.scene.updateInstance(); + +}; + +GameLib.System.Linking.prototype.fontInstanceCreated = function(data) { + /** + * We resolve the dependencies + */ + this.resolveDependencies(data.font); }; GameLib.System.Linking.prototype.imageInstanceCreated = function(data) { @@ -1017,6 +1044,7 @@ GameLib.System.Linking.prototype.stop = function() { this.lightInstanceCreatedSubscription.remove(); this.sceneInstanceCreatedSubscription.remove(); this.imageInstanceCreatedSubscription.remove(); + this.fontInstanceCreatedSubscription.remove(); this.textureInstanceCreatedSubscription.remove(); this.materialInstanceCreatedSubscription.remove(); this.meshDeletedSubscription.remove(); @@ -1027,5 +1055,6 @@ GameLib.System.Linking.prototype.stop = function() { this.physicsWorldInstanceCreatedSubscription.remove(); this.shapeInstanceCreatedSubscription.remove(); this.solverInstanceCreatedSubscription.remove(); + this.registerDependenciesSubscription.remove(); }; diff --git a/src/game-lib-system-storage.js b/src/game-lib-system-storage.js index 7e20964..906256d 100644 --- a/src/game-lib-system-storage.js +++ b/src/game-lib-system-storage.js @@ -128,6 +128,11 @@ GameLib.System.Storage.prototype.start = function() { this.loadImage ); + this.loadFontSubscription = this.subscribe( + GameLib.Event.LOAD_FONT, + this.loadFont + ); + this.blenderDataSubscription = this.subscribe( GameLib.Event.BLENDER_DATA_RECEIVED, this.processBlenderData @@ -855,6 +860,53 @@ GameLib.System.Storage.prototype.processBlenderData = function(data) { */ }; +GameLib.System.Storage.prototype.loadFont = function(data) { + + console.log('loading font : ' + data.font.name); + + this.publish( + GameLib.Event.GET_API_URL, + null, + function(urlData) { + + var url = urlData.apiUrl + '/fonts/' + data.font.url + '?ts=' + Date.now(); + + var loader = new THREE.FontLoader(); + + loader.load( + url, + function ( font ) { + + if (GameLib.Utils.IsEmpty(font.data)) { + GameLib.Event.Emit( + GameLib.Event.FONT_NOT_FOUND, + { + font: data.font + } + ); + } else { + data.font.instance = font; + data.font.loaded = true; + GameLib.Event.Emit( + GameLib.Event.FONT_INSTANCE_CREATED, + { + font: data.font + } + ); + } + } + ); + + }.bind(this), + function(error) { + console.error(error.message); + throw new Error(error.message); + } + ); + + +}; + GameLib.System.Storage.prototype.loadImage = function(data) { console.log('loading image : ' + data.image.name); @@ -1006,6 +1058,7 @@ GameLib.System.Storage.prototype.stop = function() { this.loadSubscription.remove(); this.saveSubscription.remove(); this.loadImageSubscription.remove(); + this.loadFontSubscription.remove(); this.blenderDataSubscription.remove(); this.imageUploadCompleteSubscription.remove(); this.deleteSubscription.remove();