123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /**
- * @author fernandojsg / http://fernandojsg.com
- * @author Takahiro https://github.com/takahirox
- */
- import { WebGLMultiviewRenderTarget } from '../WebGLMultiviewRenderTarget.js';
- import { Matrix3 } from '../../math/Matrix3.js';
- import { Matrix4 } from '../../math/Matrix4.js';
- function WebGLMultiview( renderer, requested, options ) {
- options = Object.assign( {}, { debug: false }, options );
- var DEFAULT_NUMVIEWS = 2;
- var gl = renderer.context;
- var canvas = renderer.domElement;
- var capabilities = renderer.capabilities;
- var properties = renderer.properties;
- var renderTarget, currentRenderTarget;
- var mat3, mat4, cameraArray;
- this.getMaxViews = function () {
- return capabilities.maxMultiviewViews;
- };
- this.getNumViews = function () {
- if ( renderTarget && renderer.getRenderTarget() === renderTarget ) {
- return renderTarget.numViews;
- }
- return 0;
- };
- function getCameraArray( camera ) {
- if ( camera.isArrayCamera ) return camera.cameras;
- cameraArray[ 0 ] = camera;
- return cameraArray;
- }
- //
- this.isAvailable = function () {
- return capabilities.multiview;
- };
- this.isEnabled = function () {
- return requested && this.isAvailable();
- };
- if ( options.debug ) {
- if ( requested && ! this.isAvailable() ) {
- console.warn( 'WebGLRenderer: Multiview requested but not supported by the browser' );
- } else if ( requested !== false && this.isAvailable() ) {
- console.info( 'WebGLRenderer: Multiview enabled' );
- }
- }
- this.updateCameraProjectionMatrices = function ( camera, uniforms ) {
- var cameras = getCameraArray( camera );
- for ( var i = 0; i < cameras.length; i ++ ) {
- mat4[ i ].copy( cameras[ i ].projectionMatrix );
- }
- uniforms.setValue( gl, 'projectionMatrices', mat4 );
- };
- this.updateCameraViewMatrices = function ( camera, uniforms ) {
- var cameras = getCameraArray( camera );
- for ( var i = 0; i < cameras.length; i ++ ) {
- mat4[ i ].copy( cameras[ i ].matrixWorldInverse );
- }
- uniforms.setValue( gl, 'viewMatrices', mat4 );
- };
- this.updateObjectMatrices = function ( object, camera, uniforms ) {
- var cameras = getCameraArray( camera );
- for ( var i = 0; i < cameras.length; i ++ ) {
- mat4[ i ].multiplyMatrices( cameras[ i ].matrixWorldInverse, object.matrixWorld );
- mat3[ i ].getNormalMatrix( mat4[ i ] );
- }
- uniforms.setValue( gl, 'modelViewMatrices', mat4 );
- uniforms.setValue( gl, 'normalMatrices', mat3 );
- };
- this.attachRenderTarget = function ( camera ) {
- currentRenderTarget = renderer.getRenderTarget();
- // Resize if needed
- var width = canvas.width;
- var height = canvas.height;
- if ( camera.isArrayCamera ) {
- // Every camera must have the same size, so we just get the size from the first one
- var bounds = camera.cameras[ 0 ].bounds;
- width *= bounds.z;
- height *= bounds.w;
- renderTarget.setNumViews( camera.cameras.length );
- } else {
- renderTarget.setNumViews( DEFAULT_NUMVIEWS );
- }
- renderTarget.setSize( width, height );
- renderer.setRenderTarget( renderTarget );
- };
- this.detachRenderTarget = function ( camera ) {
- var viewFramebuffers = properties.get( renderTarget ).__webglViewFramebuffers;
- // @todo Use actual framebuffer
- gl.bindFramebuffer( gl.FRAMEBUFFER, null );
- if ( camera.isArrayCamera ) {
- for ( var i = 0; i < camera.cameras.length; i ++ ) {
- var bounds = camera.cameras[ i ].bounds;
- var x = bounds.x * canvas.width;
- var y = bounds.y * canvas.height;
- var width = bounds.z * canvas.width;
- var height = bounds.w * canvas.height;
- gl.bindFramebuffer( gl.READ_FRAMEBUFFER, viewFramebuffers[ i ] );
- gl.blitFramebuffer( 0, 0, width, height, x, y, x + width, y + height, gl.COLOR_BUFFER_BIT, gl.NEAREST );
- }
- } else {
- // If no array camera, blit just one view
- gl.bindFramebuffer( gl.READ_FRAMEBUFFER, viewFramebuffers[ 0 ] );
- gl.blitFramebuffer( 0, 0, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height, gl.COLOR_BUFFER_BIT, gl.NEAREST );
- }
- renderer.setRenderTarget( currentRenderTarget );
- };
- if ( this.isEnabled() ) {
- renderTarget = new WebGLMultiviewRenderTarget( canvas.width, canvas.height, this.numViews );
- // Auxiliary matrices to be used when updating arrays of uniforms
- mat4 = [];
- mat3 = [];
- cameraArray = [];
- for ( var i = 0; i < this.getMaxViews(); i ++ ) {
- mat4[ i ] = new Matrix4();
- mat3[ i ] = new Matrix3();
- }
- }
- }
- export { WebGLMultiview };
|