/** * 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(); if (GameLib.Utils.UndefinedOrNull(apiRenderTarget)) { apiRenderTarget = {}; } if (apiRenderTarget instanceof GameLib.D3.RenderTarget) { return apiRenderTarget; } GameLib.D3.API.RenderTarget.call( this, apiRenderTarget.id, apiRenderTarget.name, apiRenderTarget.width, apiRenderTarget.height, apiRenderTarget.stencilBuffer, apiRenderTarget.texture ); GameLib.Component.call( this, GameLib.Component.COMPONENT_RENDER_TARGET, { 'texture' : GameLib.D3.Texture } ); }; GameLib.D3.RenderTarget.prototype = Object.create(GameLib.D3.API.RenderTarget.prototype); GameLib.D3.RenderTarget.prototype.constructor = GameLib.D3.RenderTarget; /** * Creates a Render Target instance * @returns {*} */ GameLib.D3.RenderTarget.prototype.createInstance = function() { if (GameLib.Utils.UndefinedOrNull(this.texture)) { throw new Error('no texture'); } if (GameLib.Utils.UndefinedOrNull(this.texture.instance)) { throw new Error('no texture instance'); } this.instance = new THREE.WebGLRenderTarget( this.width, this.height, { stencilBuffer : this.stencilBuffer } ); this.instance.texture = this.texture.instance; GameLib.Component.prototype.createInstance.call(this); }; /** * updates instance */ GameLib.D3.RenderTarget.prototype.updateInstance = function() { if (this.instance) { this.instance.setSize(this.width, this.height); this.instance.stencilBuffer = this.stencilBuffer; if (this.texture && this.texture.loaded) { this.instance.texture = this.texture.instance; this.instance.texture.needsUpdate = true; } else { this.instance.texture = null; } } else { try { this.createInstance(); this.loaded = true; } catch (error) { console.error(error); } } }; /** * Render Target to API Render Target * @returns {GameLib.D3.API.RenderTarget} */ GameLib.D3.RenderTarget.prototype.toApiObject = function() { var apiRenderTarget = new GameLib.D3.API.RenderTarget( this.id, this.name, this.width, this.height, this.stencilBuffer, GameLib.Utils.IdOrNull(this.texture), GameLib.Utils.IdOrNull(this.parentEntity) ); return apiRenderTarget; }; /** * * @param graphics * @param objectComponent * @returns {GameLib.D3.RenderTarget} * @constructor */ GameLib.D3.RenderTarget.FromObject = function(graphics, objectComponent) { var apiRenderTarget = GameLib.D3.API.RenderTarget.FromObject(objectComponent); return new GameLib.D3.RenderTarget( graphics, apiRenderTarget ); };