Browse Source

Add setNumViews() to WebGLMultiview

Fernando Serrano 6 years ago
parent
commit
75e66cdfec

+ 13 - 0
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;
+
 	}
 
 } );

+ 2 - 2
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 );
 
 		}
 

+ 47 - 37
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 );
 
 	}
 

+ 4 - 4
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 );
 
 					}