From 75e66cdfecf52be218a07a055e12be5bcc698618 Mon Sep 17 00:00:00 2001 From: Fernando Serrano Date: Tue, 30 Apr 2019 14:15:17 +0200 Subject: [PATCH] Add setNumViews() to WebGLMultiview --- src/renderers/WebGLMultiviewRenderTarget.js | 13 ++++ src/renderers/WebGLRenderer.js | 4 +- src/renderers/webgl/WebGLMultiview.js | 84 ++++++++++++--------- src/renderers/webgl/WebGLTextures.js | 8 +- 4 files changed, 66 insertions(+), 43 deletions(-) diff --git a/src/renderers/WebGLMultiviewRenderTarget.js b/src/renderers/WebGLMultiviewRenderTarget.js index 185d72b329..fc3e22a576 100644 --- a/src/renderers/WebGLMultiviewRenderTarget.js +++ b/src/renderers/WebGLMultiviewRenderTarget.js @@ -30,6 +30,19 @@ WebGLMultiviewRenderTarget.prototype = Object.assign( Object.create( WebGLRender return this; + }, + + setNumViews: function ( numViews ) { + + if ( this.numViews !== numViews ) { + + this.numViews = numViews; + this.dispose(); + + } + + return this; + } } ); diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index 4d3149521c..5120c5a2e7 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -1242,9 +1242,9 @@ function WebGLRenderer( parameters ) { state.setPolygonOffset( false ); - if ( this.multiview.isEnabled() ) { + if ( multiview.isEnabled() ) { - this.multiview.detachRenderTarget( camera ); + multiview.detachRenderTarget( camera ); } diff --git a/src/renderers/webgl/WebGLMultiview.js b/src/renderers/webgl/WebGLMultiview.js index a751e939c6..0230d8d0b6 100644 --- a/src/renderers/webgl/WebGLMultiview.js +++ b/src/renderers/webgl/WebGLMultiview.js @@ -11,44 +11,42 @@ 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 numViews = 2; var renderTarget, currentRenderTarget; - // Auxiliary matrices to be used when updating arrays of uniforms - var aux = { - mat4: [], - mat3: [] - }; + this.getMaxViews = function () { - for ( var i = 0; i < numViews; i ++ ) { + return capabilities.maxMultiviewViews; - aux.mat4[ i ] = new Matrix4(); - aux.mat3[ i ] = new Matrix3(); + }; - } + this.getNumViews = function () { - // + return renderTarget ? renderTarget.numViews : 1; - this.isAvailable = function () { + }; - return capabilities.multiview; + // Auxiliary matrices to be used when updating arrays of uniforms + var mat4 = []; + var mat3 = []; - }; + for ( var i = 0; i < this.getMaxViews(); i ++ ) { - this.getNumViews = function () { + mat4[ i ] = new Matrix4(); + mat3[ i ] = new Matrix3(); - return numViews; + } - }; + // - this.getMaxViews = function () { + this.isAvailable = function () { - return capabilities.maxMultiviewViews; + return capabilities.multiview; }; @@ -72,13 +70,15 @@ function WebGLMultiview( renderer, requested, options ) { } - this.updateCameraProjectionMatrices = function ( camera, p_uniforms ) { + this.updateCameraProjectionMatrices = function ( camera, uniforms ) { + + var numViews = this.getNumViews(); if ( camera.isArrayCamera ) { for ( var i = 0; i < numViews; i ++ ) { - aux.mat4[ i ].copy( camera.cameras[ i ].projectionMatrix ); + mat4[ i ].copy( camera.cameras[ i ].projectionMatrix ); } @@ -86,23 +86,25 @@ function WebGLMultiview( renderer, requested, options ) { for ( var i = 0; i < numViews; i ++ ) { - aux.mat4[ i ].copy( camera.projectionMatrix ); + mat4[ i ].copy( camera.projectionMatrix ); } } - p_uniforms.setValue( gl, 'projectionMatrices', aux.mat4 ); + uniforms.setValue( gl, 'projectionMatrices', mat4 ); }; - this.updateCameraViewMatrices = function ( camera, p_uniforms ) { + this.updateCameraViewMatrices = function ( camera, uniforms ) { + + var numViews = this.getNumViews(); if ( camera.isArrayCamera ) { for ( var i = 0; i < numViews; i ++ ) { - aux.mat4[ i ].copy( camera.cameras[ i ].matrixWorldInverse ); + mat4[ i ].copy( camera.cameras[ i ].matrixWorldInverse ); } @@ -110,44 +112,46 @@ function WebGLMultiview( renderer, requested, options ) { for ( var i = 0; i < numViews; i ++ ) { - aux.mat4[ i ].copy( camera.matrixWorldInverse ); + mat4[ i ].copy( camera.matrixWorldInverse ); } } - p_uniforms.setValue( gl, 'viewMatrices', aux.mat4 ); + uniforms.setValue( gl, 'viewMatrices', mat4 ); }; - this.updateObjectMatrices = function ( object, camera, p_uniforms ) { + this.updateObjectMatrices = function ( object, camera, uniforms ) { + + var numViews = this.getNumViews(); if ( camera.isArrayCamera ) { for ( var i = 0; i < numViews; i ++ ) { - aux.mat4[ i ].multiplyMatrices( camera.cameras[ i ].matrixWorldInverse, object.matrixWorld ); - aux.mat3[ i ].getNormalMatrix( aux.mat4[ i ] ); + mat4[ i ].multiplyMatrices( camera.cameras[ i ].matrixWorldInverse, object.matrixWorld ); + mat3[ i ].getNormalMatrix( mat4[ i ] ); } } else { // In this case we still need to provide an array of matrices but just the first one will be used - aux.mat4[ 0 ].multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld ); - aux.mat3[ 0 ].getNormalMatrix( aux.mat4[ 0 ] ); + mat4[ 0 ].multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld ); + mat3[ 0 ].getNormalMatrix( mat4[ 0 ] ); for ( var i = 1; i < numViews; i ++ ) { - aux.mat4[ i ].copy( aux.mat4[ 0 ] ); - aux.mat3[ i ].copy( aux.mat3[ 0 ] ); + mat4[ i ].copy( mat4[ 0 ] ); + mat3[ i ].copy( mat3[ 0 ] ); } } - p_uniforms.setValue( gl, 'modelViewMatrices', aux.mat4 ); - p_uniforms.setValue( gl, 'normalMatrices', aux.mat3 ); + uniforms.setValue( gl, 'modelViewMatrices', mat4 ); + uniforms.setValue( gl, 'normalMatrices', mat3 ); }; @@ -167,6 +171,12 @@ function WebGLMultiview( renderer, requested, options ) { width *= bounds.z; height *= bounds.w; + renderTarget.setNumViews( camera.cameras.length ); + + } else { + + renderTarget.setNumViews( DEFAULT_NUMVIEWS ); + } renderTarget.setSize( width, height ); @@ -213,7 +223,7 @@ function WebGLMultiview( renderer, requested, options ) { if ( this.isEnabled() ) { - renderTarget = new WebGLMultiviewRenderTarget( canvas.width, canvas.height, numViews ); + renderTarget = new WebGLMultiviewRenderTarget( canvas.width, canvas.height, this.numViews ); } diff --git a/src/renderers/webgl/WebGLTextures.js b/src/renderers/webgl/WebGLTextures.js index 69cf645edb..296225c0aa 100644 --- a/src/renderers/webgl/WebGLTextures.js +++ b/src/renderers/webgl/WebGLTextures.js @@ -1024,11 +1024,11 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, ext.framebufferTextureMultiviewOVR( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, depthStencilTexture, 0, 0, numViews ); var viewFramebuffers = new Array( numViews ); - for ( var viewIndex = 0; viewIndex < numViews; ++ viewIndex ) { + for ( var i = 0; i < numViews; ++ i ) { - viewFramebuffers[ viewIndex ] = _gl.createFramebuffer(); - _gl.bindFramebuffer( _gl.FRAMEBUFFER, viewFramebuffers[ viewIndex ] ); - _gl.framebufferTextureLayer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, colorTexture, 0, viewIndex ); + viewFramebuffers[ i ] = _gl.createFramebuffer(); + _gl.bindFramebuffer( _gl.FRAMEBUFFER, viewFramebuffers[ i ] ); + _gl.framebufferTextureLayer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, colorTexture, 0, i ); } -- GitLab