/** * Renders a scene with a camera * @param graphics GameLib.D3.Graphics * @param apiRenderer GameLib.D3.API.Renderer * @constructor */ GameLib.D3.Renderer = function ( graphics, apiRenderer ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); GameLib.D3.API.Renderer.call( this, apiRenderer.id, apiRenderer.name, apiRenderer.rendererType, apiRenderer.scene, apiRenderer.camera, apiRenderer.autoClear, apiRenderer.localClipping, apiRenderer.width, apiRenderer.height, apiRenderer.parentEntity, apiRenderer.preserveDrawingBuffer ); if (this.camera instanceof Object) { if (this.camera.cameraType == GameLib.Component.COMPONENT_CAMERA) { this.camera = new GameLib.D3.Camera( this.graphics, this.camera ); } else if (this.camera.cameraType == GameLib.Component.COMPONENT_STEREO_CAMERA) { this.camera = new GameLib.D3.StereoCamera( this.graphics, this.camera ); } else { console.warn('Camera type not supported : ' + this.camera.cameraType); throw new Error('Camera type not supported : ' + this.camera.cameraType) } } this.instance = this.createInstance(); }; GameLib.D3.Renderer.prototype = Object.create(GameLib.D3.API.Renderer.prototype); GameLib.D3.Renderer.prototype.constructor = GameLib.D3.Renderer; GameLib.D3.Renderer.prototype.createInstance = function(update) { var instance = null; if (update) { instance = this.instance; } else { instance = new THREE.WebGLRenderer(); } instance.localClippingEnabled = this.localClipping; instance.setSize(this.width, this.height); instance.autoClear = this.autoClear; instance.preserveDrawingBuffer = this.preserveDrawingBuffer; if (this.camera) { this.camera.aspect = this.width / this.height; this.camera.updateInstance(); } return instance; }; GameLib.D3.Renderer.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; GameLib.D3.Renderer.prototype.toApiComponent = function() { var apiRenderer = new GameLib.D3.API.Renderer( this.id, this.name, this.rendererType, GameLib.Utils.IdOrNull(this.scene), GameLib.Utils.IdOrNull(this.camera), this.autoClear, this.localClipping, this.width, this.height, GameLib.Utils.IdOrNull(this.parentEntity), this.preserveDrawingBuffer ); return apiRenderer; }; /** * * @param graphics * @param objectComponent * @returns {GameLib.D3.Renderer} * @constructor */ GameLib.D3.Renderer.FromObjectComponent = function(graphics, objectComponent) { var apiRenderer = GameLib.D3.API.Renderer.FromObjectComponent(objectComponent); return new GameLib.D3.Renderer( graphics, this, apiRenderer ); }; /** * */ GameLib.D3.Renderer.prototype.render = function() { this.instance.render( this.scene.instance, this.camera.instance ); };