Browse Source

Remove matrices from camera and object and move them to aux variables to reuse them

Fernando Serrano 6 years ago
parent
commit
4645cdfbe7
2 changed files with 63 additions and 53 deletions
  1. 5 15
      src/renderers/WebGLRenderer.js
  2. 58 38
      src/renderers/webgl/WebGLMultiview.js

+ 5 - 15
src/renderers/WebGLRenderer.js

@@ -1466,16 +1466,8 @@ function WebGLRenderer( parameters ) {
 		object.onBeforeRender( _this, scene, camera, geometry, material, group );
 		object.onBeforeRender( _this, scene, camera, geometry, material, group );
 		currentRenderState = renderStates.get( scene, _currentArrayCamera || camera );
 		currentRenderState = renderStates.get( scene, _currentArrayCamera || camera );
 
 
-		if ( multiview.isEnabled() ) {
-
-			multiview.computeObjectMatrices( object, camera );
-
-		} else {
-
-			object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
-			object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
-
-		}
+		object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
+		object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
 
 
 		if ( object.isImmediateRenderObject ) {
 		if ( object.isImmediateRenderObject ) {
 
 
@@ -1748,8 +1740,7 @@ function WebGLRenderer( parameters ) {
 
 
 			if ( material.supportsMultiview && multiview.isEnabled() ) {
 			if ( material.supportsMultiview && multiview.isEnabled() ) {
 
 
-				multiview.computeCameraMatrices( camera );
-				p_uniforms.setValue( _gl, 'projectionMatrices', camera.projectionMatrices );
+				multiview.updateCameraProjectionMatrices( camera, p_uniforms );
 
 
 			} else {
 			} else {
 
 
@@ -1805,7 +1796,7 @@ function WebGLRenderer( parameters ) {
 
 
 				if ( material.supportsMultiview && multiview.isEnabled() ) {
 				if ( material.supportsMultiview && multiview.isEnabled() ) {
 
 
-					p_uniforms.setValue( _gl, 'viewMatrices', camera.viewMatrices );
+					multiview.updateCameraViewMatrices( camera, p_uniforms );
 
 
 				} else {
 				} else {
 
 
@@ -2011,8 +2002,7 @@ function WebGLRenderer( parameters ) {
 
 
 		if ( material.supportsMultiview && multiview.isEnabled() ) {
 		if ( material.supportsMultiview && multiview.isEnabled() ) {
 
 
-			p_uniforms.setValue( _gl, 'modelViewMatrices', object.modelViewMatrices );
-			p_uniforms.setValue( _gl, 'normalMatrices', object.normalMatrices );
+			multiview.updateObjectMatrices( object, camera, p_uniforms );
 
 
 		} else {
 		} else {
 
 

+ 58 - 38
src/renderers/webgl/WebGLMultiview.js

@@ -7,13 +7,33 @@ import { WebGLMultiviewRenderTarget } from '../WebGLMultiviewRenderTarget.js';
 import { Matrix3 } from '../../math/Matrix3.js';
 import { Matrix3 } from '../../math/Matrix3.js';
 import { Matrix4 } from '../../math/Matrix4.js';
 import { Matrix4 } from '../../math/Matrix4.js';
 
 
-function WebGLMultiview( renderer, requested ) {
+function WebGLMultiview( renderer, requested, options ) {
+
+	options = Object.assign( {}, { debug: false }, options );
 
 
 	var gl = renderer.context;
 	var gl = renderer.context;
 	var canvas = renderer.domElement;
 	var canvas = renderer.domElement;
 	var capabilities = renderer.capabilities;
 	var capabilities = renderer.capabilities;
 	var properties = renderer.properties;
 	var properties = renderer.properties;
 
 
+	var numViews = 2;
+	var renderTarget, currentRenderTarget;
+
+	// Auxiliary matrices to be used when updating arrays of uniforms
+	var aux = {
+		mat4: [],
+		mat3: []
+	};
+
+	for ( var i = 0; i < numViews; i ++ ) {
+
+		aux.mat4[ i ] = new Matrix4();
+		aux.mat3[ i ] = new Matrix3();
+
+	}
+
+	//
+
 	this.isAvailable = function () {
 	this.isAvailable = function () {
 
 
 		return capabilities.multiview;
 		return capabilities.multiview;
@@ -38,41 +58,27 @@ function WebGLMultiview( renderer, requested ) {
 
 
 	};
 	};
 
 
-	if ( requested && ! this.isAvailable() ) {
-
-		console.warn( 'WebGLRenderer: Multiview requested but not supported by the browser' );
+	if ( options.debug ) {
 
 
-	} else if ( requested !== false && this.isAvailable() ) {
+		if ( requested && ! this.isAvailable() ) {
 
 
-		console.info( 'WebGLRenderer: Multiview enabled' );
+			console.warn( 'WebGLRenderer: Multiview requested but not supported by the browser' );
 
 
-	}
+		} else if ( requested !== false && this.isAvailable() ) {
 
 
-	var numViews = 2;
-	var renderTarget, currentRenderTarget;
+			console.info( 'WebGLRenderer: Multiview enabled' );
 
 
-	this.computeCameraMatrices = function ( camera ) {
-
-		if ( ! camera.projectionMatrices ) {
-
-			camera.projectionMatrices = new Array( numViews );
-			camera.viewMatrices = new Array( numViews );
-
-			for ( var i = 0; i < numViews; i ++ ) {
-
-				camera.projectionMatrices[ i ] = new Matrix4();
-				camera.viewMatrices[ i ] = new Matrix4();
+		}
 
 
-			}
+	}
 
 
-		}
+	this.updateCameraProjectionMatrices = function ( camera, p_uniforms ) {
 
 
 		if ( camera.isArrayCamera ) {
 		if ( camera.isArrayCamera ) {
 
 
 			for ( var i = 0; i < numViews; i ++ ) {
 			for ( var i = 0; i < numViews; i ++ ) {
 
 
-				camera.projectionMatrices[ i ].copy( camera.cameras[ i ].projectionMatrix );
-				camera.viewMatrices[ i ].copy( camera.cameras[ i ].matrixWorldInverse );
+				aux.mat4[ i ].copy( camera.cameras[ i ].projectionMatrix );
 
 
 			}
 			}
 
 
@@ -80,55 +86,69 @@ function WebGLMultiview( renderer, requested ) {
 
 
 			for ( var i = 0; i < numViews; i ++ ) {
 			for ( var i = 0; i < numViews; i ++ ) {
 
 
-				camera.projectionMatrices[ i ].copy( camera.projectionMatrix );
-				camera.viewMatrices[ i ].copy( camera.matrixWorldInverse );
+				aux.mat4[ i ].copy( camera.projectionMatrix );
 
 
 			}
 			}
 
 
 		}
 		}
 
 
+		p_uniforms.setValue( gl, 'projectionMatrices', aux.mat4 );
+
 	};
 	};
 
 
-	this.computeObjectMatrices = function ( object, camera ) {
+	this.updateCameraViewMatrices = function ( camera, p_uniforms ) {
+
+		if ( camera.isArrayCamera ) {
+
+			for ( var i = 0; i < numViews; i ++ ) {
 
 
-		if ( ! object.modelViewMatrices ) {
+				aux.mat4[ i ].copy( camera.cameras[ i ].matrixWorldInverse );
 
 
-			object.modelViewMatrices = new Array( numViews );
-			object.normalMatrices = new Array( numViews );
+			}
+
+		} else {
 
 
 			for ( var i = 0; i < numViews; i ++ ) {
 			for ( var i = 0; i < numViews; i ++ ) {
 
 
-				object.modelViewMatrices[ i ] = new Matrix4();
-				object.normalMatrices[ i ] = new Matrix3();
+				aux.mat4[ i ].copy( camera.matrixWorldInverse );
 
 
 			}
 			}
 
 
 		}
 		}
 
 
+		p_uniforms.setValue( gl, 'viewMatrices', aux.mat4 );
+
+	};
+
+	this.updateObjectMatrices = function ( object, camera, p_uniforms ) {
+
 		if ( camera.isArrayCamera ) {
 		if ( camera.isArrayCamera ) {
 
 
 			for ( var i = 0; i < numViews; i ++ ) {
 			for ( var i = 0; i < numViews; i ++ ) {
 
 
-				object.modelViewMatrices[ i ].multiplyMatrices( camera.cameras[ i ].matrixWorldInverse, object.matrixWorld );
-				object.normalMatrices[ i ].getNormalMatrix( object.modelViewMatrices[ i ] );
+				aux.mat4[ i ].multiplyMatrices( camera.cameras[ i ].matrixWorldInverse, object.matrixWorld );
+				aux.mat3[ i ].getNormalMatrix( aux.mat4[ i ] );
 
 
 			}
 			}
 
 
 		} else {
 		} else {
 
 
 			// In this case we still need to provide an array of matrices but just the first one will be used
 			// In this case we still need to provide an array of matrices but just the first one will be used
-			object.modelViewMatrices[ 0 ].multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
-			object.normalMatrices[ 0 ].getNormalMatrix( object.modelViewMatrices[ 0 ] );
+			aux.mat4[ 0 ].multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
+			aux.mat3[ 0 ].getNormalMatrix( aux.mat4[ 0 ] );
 
 
 			for ( var i = 1; i < numViews; i ++ ) {
 			for ( var i = 1; i < numViews; i ++ ) {
 
 
-				object.modelViewMatrices[ i ].copy( object.modelViewMatrices[ 0 ] );
-				object.normalMatrices[ i ].copy( object.normalMatrices[ 0 ] );
+				aux.mat4[ i ].copy( aux.mat4[ 0 ] );
+				aux.mat3[ i ].copy( aux.mat3[ 0 ] );
 
 
 			}
 			}
 
 
 		}
 		}
 
 
+		p_uniforms.setValue( gl, 'modelViewMatrices', aux.mat4 );
+		p_uniforms.setValue( gl, 'normalMatrices', aux.mat3 );
+
 	};
 	};
 
 
 	this.attachRenderTarget = function ( camera ) {
 	this.attachRenderTarget = function ( camera ) {