/** * 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(); if (GameLib.Utils.UndefinedOrNull(apiComposer)) { apiComposer = {}; } if (apiComposer instanceof GameLib.D3.Composer) { return apiComposer; } GameLib.D3.API.Composer.call( this, apiComposer.id, apiComposer.name, apiComposer.renderer, apiComposer.renderTarget, apiComposer.passes, apiComposer.parentEntity ); if (this.renderer instanceof GameLib.D3.API.Renderer) { this.renderer = new GameLib.D3.Renderer( this.graphics, this.renderer ) } if (this.renderTarget instanceof GameLib.D3.API.RenderTarget) { this.renderTarget = new GameLib.D3.RenderTarget( this.graphics, this.renderTarget ) } this.passes = this.passes.map( function (apiPass) { if (apiPass instanceof GameLib.D3.API.Pass) { return GameLib.D3.Pass( this.graphics, apiPass ) } else { console.warn('apiPass not of type API.Pass'); throw new Error('apiPass not of type API.Pass'); } }.bind(this) ); this.buildIdToObject(); 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; } if (this.renderer && this.renderTarget) { 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.toApiObject = 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.FromObject = function(graphics, objectComponent) { var apiComposer = GameLib.D3.API.Composer.FromObject(objectComponent); return new GameLib.D3.Composer( graphics, apiComposer ); };