/** * Viewport Runtime * @param graphics GameLib.D3.Graphics * @param apiViewport GameLib.D3.API.Viewport * @param imageFactory GameLib.D3.ImageFactory * @constructor */ GameLib.D3.Viewport = function ( graphics, apiViewport ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); if (GameLib.Utils.UndefinedOrNull(apiViewport)) { apiViewport = {}; } GameLib.D3.API.Viewport.call( this, apiViewport.id, apiViewport.name, apiViewport.width, apiViewport.height, apiViewport.x, apiViewport.y, apiViewport.composer, apiViewport.renderer, apiViewport.scene, apiViewport.camera, apiViewport.parentEntity ); if (this.composer instanceof GameLib.D3.API.Composer) { this.composer = new GameLib.D3.Composer( this.graphics, this.composer ) } if (this.renderer instanceof GameLib.D3.API.Renderer) { this.renderer = new GameLib.D3.Renderer( this.graphics, this.renderer ) } if (this.scene instanceof GameLib.D3.API.Scene) { this.scene = new GameLib.D3.Scene( this.graphics, this.scene ) } if (this.camera instanceof GameLib.D3.API.Camera) { this.camera = new GameLib.D3.Camera( this.graphics, this.camera ) } this.buildIdToObject(); this.instance = this.createInstance(); }; GameLib.D3.Viewport.prototype = Object.create(GameLib.D3.API.Viewport.prototype); GameLib.D3.Viewport.prototype.constructor = GameLib.D3.Viewport; //GameLib.D3.Viewport.VIEWPORT_TYPE_GAME = 0x1; /** * * @param update * @returns {*} */ GameLib.D3.Viewport.prototype.createInstance = function(update) { var instance = null; if (update) { instance = this.instance; } if (this.renderer) { this.renderer.width = this.width - this.x; this.renderer.height = this.height - this.y; this.renderer.updateInstance(); this.renderer.instance.setViewport( this.x, this.y, this.width, this.height ); } else if (this.composer) { this.composer.renderer.width = this.width - this.x; this.composer.renderer.height = this.height - this.y; this.composer.renderer.updateInstance(); this.composer.renderer.instance.setViewport( this.x, this.y, this.width, this.height ); this.composer.passes.map( function(pass) { if (pass.camera instanceof GameLib.D3.Camera) { pass.camera.aspect = (this.width - this.x) / (this.height - this.y); pass.camera.updateInstance(); } }.bind(this) ) } if (this.camera) { this.camera.aspect = (this.width - this.x) / (this.height - this.y); this.camera.updateInstance(); } return instance; }; /** * */ GameLib.D3.Viewport.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; /** * GameLib.D3.Viewport to GameLib.D3.API.Viewport * @returns {GameLib.D3.API.Viewport} */ GameLib.D3.Viewport.prototype.toApiComponent = function() { var apiViewport = new GameLib.D3.API.Viewport( this.id, this.name, this.width, this.height, this.x, this.y, GameLib.Utils.IdOrNull(this.composer), GameLib.Utils.IdOrNull(this.scene), GameLib.Utils.IdOrNull(this.camera), GameLib.Utils.IdOrNull(this.parentEntity) ); return apiViewport; }; /** * GameLib.D3.Viewport from Object Viewport * @param graphics * @param objectComponent * @returns {GameLib.D3.Viewport} * @constructor */ GameLib.D3.Viewport.FromObjectComponent = function(graphics, objectComponent) { var apiViewport = GameLib.D3.API.Viewport.FromObjectComponent(objectComponent); return new GameLib.D3.Viewport( graphics, apiViewport ); }; /** * Component update override */ GameLib.D3.Viewport.prototype.update = function() { if (this.renderer && this.scene && this.camera) { this.renderer.instance.render( this.scene.instance, this.camera.instance ) } else if (this.composer) { this.composer.instance.render(); } };