From b4f673adb8988ad2ab7609491ff3300981437949 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Sun, 7 Jan 2018 12:22:29 +0100 Subject: [PATCH] render stuff legacy --- src/game-lib-canvas.js | 12 ++++- src/game-lib-d3-api-render-target.js | 11 +++-- src/game-lib-d3-renderer.js | 71 ++++++++++++++++++---------- src/game-lib-dom-element.js | 6 ++- src/game-lib-system-gui.js | 2 +- src/game-lib-system-render.js | 52 +++++++++++++++----- 6 files changed, 110 insertions(+), 44 deletions(-) diff --git a/src/game-lib-canvas.js b/src/game-lib-canvas.js index af11fe0..1769522 100644 --- a/src/game-lib-canvas.js +++ b/src/game-lib-canvas.js @@ -38,6 +38,11 @@ GameLib.Canvas.prototype.createInstance = function() { this.instance = document.createElement('canvas'); + this.instance.setAttribute('id', this.id); + + this.width = Math.round(this.width); + this.height = Math.round(this.height); + this.instance.width = this.width; this.instance.height = this.height; @@ -53,14 +58,19 @@ GameLib.Canvas.prototype.updateInstance = function(property) { console.warn('unknown property update for Canvas: ' + property); } + if (property === 'id') { + this.instance.setAttribute('id', this.id); + } + if (property === 'width') { + this.width = Math.round(this.width); this.instance.width = this.width; } if (property === 'height') { + this.height = Math.round(this.height); this.instance.height = this.height; } - }; /** diff --git a/src/game-lib-d3-api-render-target.js b/src/game-lib-d3-api-render-target.js index d5862d9..bf77d54 100644 --- a/src/game-lib-d3-api-render-target.js +++ b/src/game-lib-d3-api-render-target.js @@ -29,12 +29,12 @@ GameLib.D3.API.RenderTarget = function ( this.name = name; if (GameLib.Utils.UndefinedOrNull(width)) { - width = 800; + width = 512; } this.width = width; if (GameLib.Utils.UndefinedOrNull(height)) { - height = 600; + height = 512; } this.height = height; @@ -44,7 +44,12 @@ GameLib.D3.API.RenderTarget = function ( this.stencilBuffer = stencilBuffer; if (GameLib.Utils.UndefinedOrNull(texture)) { - texture = null; + texture = new GameLib.D3.API.Texture( + null, + null, + null, + new GameLib.API.Image() + ); } this.texture = texture; diff --git a/src/game-lib-d3-renderer.js b/src/game-lib-d3-renderer.js index e980f81..ae381f8 100644 --- a/src/game-lib-d3-renderer.js +++ b/src/game-lib-d3-renderer.js @@ -29,7 +29,7 @@ GameLib.D3.Renderer = function ( apiRenderer.width, apiRenderer.height, apiRenderer.preserveDrawingBuffer, - apiRenderer.domElement, + apiRenderer.canvas, apiRenderer.clearColor, apiRenderer.camera, apiRenderer.scenes, @@ -49,12 +49,19 @@ GameLib.D3.Renderer = function ( this ); - if (this.domElement instanceof GameLib.API.DomElement) { - this.domElement = new GameLib.DomElement( - this.domElement +if (this.canvas) { + this.width = this.canvas.width; + this.height = this.canvas.height; + } + + +if (this.canvas instanceof GameLib.API.Canvas) { + this.canvas = new GameLib.Canvas( + this.canvas ); } + if (this.camera instanceof GameLib.D3.API.Camera) { this.camera = new GameLib.D3.Camera( this.graphics, @@ -127,7 +134,7 @@ GameLib.D3.Renderer = function ( GameLib.Component.call( this, { - 'domElement' : GameLib.DomElement, + 'canvas' : GameLib.Canvas, 'camera' : GameLib.D3.Camera, 'scenes' : [GameLib.D3.Scene], 'viewports' : [GameLib.D3.Viewport], @@ -150,17 +157,17 @@ GameLib.D3.Renderer.prototype.constructor = GameLib.D3.Renderer; */ GameLib.D3.Renderer.prototype.createInstance = function() { - if (GameLib.Utils.UndefinedOrNull(this.domElement)) { - throw new Error('no dom element'); + if (GameLib.Utils.UndefinedOrNull(this.canvas)) { + throw new Error('no canvas'); } - if (GameLib.Utils.UndefinedOrNull(this.domElement.instance)) { - throw new Error('no dom element instance'); + if (GameLib.Utils.UndefinedOrNull(this.canvas.instance)) { + throw new Error('no canvas instance'); } this.instance = new THREE.WebGLRenderer( { - canvas : this.domElement.instance + canvas : this.canvas.instance } ); @@ -227,14 +234,17 @@ GameLib.D3.Renderer.prototype.updateInstance = function(property) { this.instance.localClippingEnabled = this.localClipping; } - if (property === 'width' || 'height') { - this.instance.setSize( - this.width, - this.height - ); + if (property === 'canvas') { + this.instance.dispose(); + this.createInstance(); + } - this.instance.domElement.width = this.width; - this.instance.domElement.height = this.height; + if (property === 'width' || 'height') { + + this.width = Math.round(this.width); + this.height = Math.round(this.height); + + this.setSize(this.width, this.height); } @@ -292,7 +302,7 @@ GameLib.D3.Renderer.prototype.toApiObject = function() { this.width, this.height, this.preserveDrawingBuffer, - GameLib.Utils.IdOrNull(this.domElement), + GameLib.Utils.IdOrNull(this.canvas), this.clearColor.toApiObject(), GameLib.Utils.IdOrNull(this.camera), this.scenes.map(function(scene){ @@ -396,15 +406,22 @@ GameLib.D3.Renderer.prototype.render = function(delta, scenes) { return; } - /** - * A scene's renderCamera instance overrides the default renderer camera - */ + if (scene.renderCamera && scene.renderCamera.instance) { - this.instance.render( + + /** + * A scene's renderCamera instance overrides the default renderer camera + */ + this.instance.render( scene.instance, scene.renderCamera.instance ) + } else { + + /** + * Ok just do a normal render + */ this.instance.render( scene.instance, this.camera.instance @@ -419,8 +436,14 @@ GameLib.D3.Renderer.prototype.render = function(delta, scenes) { }; GameLib.D3.Renderer.prototype.setSize = function(width, height) { - this.width = width; - this.height = height; + + this.width = Math.round(width); + this.height = Math.round(height); + + this.canvas.width = this.width; + this.canvas.height = this.height; + + this.canvas.updateInstance('width'); if (this.instance) { this.instance.setSize( diff --git a/src/game-lib-dom-element.js b/src/game-lib-dom-element.js index b77753d..29fb335 100644 --- a/src/game-lib-dom-element.js +++ b/src/game-lib-dom-element.js @@ -41,8 +41,10 @@ GameLib.DomElement.prototype.createInstance = function() { /** * Updates instance domElement */ -GameLib.DomElement.prototype.updateInstance = function() { - this.instance = document.getElementById(this.domElementId); +GameLib.DomElement.prototype.updateInstance = function(property) { + if (property === 'domElementId') { + this.createInstance() + } }; /** diff --git a/src/game-lib-system-gui.js b/src/game-lib-system-gui.js index ec7f705..ce90174 100644 --- a/src/game-lib-system-gui.js +++ b/src/game-lib-system-gui.js @@ -447,7 +447,7 @@ GameLib.System.GUI.prototype.buildVectorControl = function(folder, componentTemp }; /** - * Builds a Paren Selection control + * Builds a Parent Selection control * @param folder * @param componentTemplate * @param property diff --git a/src/game-lib-system-render.js b/src/game-lib-system-render.js index c8bf238..8dcb13a 100644 --- a/src/game-lib-system-render.js +++ b/src/game-lib-system-render.js @@ -101,32 +101,35 @@ GameLib.System.Render.prototype.nativeWindowResize = function() { ); }; +GameLib.System.Render.prototype.getOffset = function (el) { + var rect = el.getBoundingClientRect(), + scrollLeft = window.pageXOffset || document.documentElement.scrollLeft, + scrollTop = window.pageYOffset || document.documentElement.scrollTop; + return { top: rect.top + scrollTop, left: rect.left + scrollLeft } +}; + GameLib.System.Render.prototype.windowResize = function(data) { - var aspect = (data.width / data.height); - - var cameras = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CAMERA); - - cameras.map(function(camera){ - camera.aspect = aspect; - camera.updateInstance('aspect'); - }); - var renderers = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.RENDERER); renderers.map( function(renderer) { + renderer.setSize( - data.width, - data.height + renderer.canvas.width, + renderer.canvas.height ); + + renderer.camera.aspect = renderer.canvas.width / renderer.canvas.height; + + renderer.camera.updateInstance('aspect'); + } ); GameLib.Event.Emit( GameLib.Event.CUSTOM_CODE_WINDOW_RESIZE, { - aspect : aspect, width : data.width, height : data.height } @@ -142,6 +145,13 @@ GameLib.System.Render.prototype.instanceCreated = function(data) { if (data.component instanceof GameLib.D3.Renderer) { this.renderers.push(data.component); + GameLib.Event.Emit( + GameLib.Event.WINDOW_RESIZE, + { + width : window.innerWidth, + height : window.innerHeight + } + ); } if (data.component instanceof GameLib.Stats) { @@ -155,9 +165,11 @@ GameLib.System.Render.prototype.instanceCreated = function(data) { */ GameLib.System.Render.prototype.removeComponent = function(data) { + var index; + if (data.component instanceof GameLib.D3.Renderer) { - var index = this.renderers.indexOf(data.component); + index = this.renderers.indexOf(data.component); if (index !== -1) { console.log('removing renderer from system'); @@ -169,6 +181,20 @@ GameLib.System.Render.prototype.removeComponent = function(data) { } } + if (data.component instanceof GameLib.Stats) { + + index = this.statistics.indexOf(data.component); + + if (index !== -1) { + console.log('removing statistics from system'); + + this.statistics.splice(index, 1); + + } else { + console.log('failed to find the statistics in the system : ' + data.component.name); + } + } + }; /**