@@ -38,6 +38,7 @@ import { WebGLGeometries } from './webgl/WebGLGeometries.js';
3838import { WebGLIndexedBufferRenderer } from './webgl/WebGLIndexedBufferRenderer.js' ;
3939import { WebGLInfo } from './webgl/WebGLInfo.js' ;
4040import { WebGLMorphtargets } from './webgl/WebGLMorphtargets.js' ;
41+ import { WebGLMultiview } from './webgl/WebGLMultiview.js' ;
4142import { WebGLObjects } from './webgl/WebGLObjects.js' ;
4243import { WebGLPrograms } from './webgl/WebGLPrograms.js' ;
4344import { WebGLProperties } from './webgl/WebGLProperties.js' ;
@@ -71,6 +72,7 @@ class WebGLRenderer {
7172 powerPreference = 'default' ,
7273 failIfMajorPerformanceCaveat = false ,
7374 reverseDepthBuffer = false ,
75+ multiviewStereo = false ,
7476 } = parameters ;
7577
7678 this . isWebGLRenderer = true ;
@@ -275,6 +277,7 @@ class WebGLRenderer {
275277 let extensions , capabilities , state , info ;
276278 let properties , textures , cubemaps , cubeuvmaps , attributes , geometries , objects ;
277279 let programCache , materials , renderLists , renderStates , clipping , shadowMap ;
280+ let multiview ;
278281
279282 let background , morphtargets , bufferRenderer , indexedBufferRenderer ;
280283
@@ -313,6 +316,7 @@ class WebGLRenderer {
313316 renderLists = new WebGLRenderLists ( ) ;
314317 renderStates = new WebGLRenderStates ( extensions ) ;
315318 background = new WebGLBackground ( _this , cubemaps , cubeuvmaps , state , objects , _alpha , premultipliedAlpha ) ;
319+ multiview = new WebGLMultiview ( _this , extensions , _gl ) ;
316320 shadowMap = new WebGLShadowMap ( _this , objects , capabilities ) ;
317321 uniformsGroups = new WebGLUniformsGroups ( _gl , info , capabilities , state ) ;
318322
@@ -1249,11 +1253,21 @@ class WebGLRenderer {
12491253
12501254 if ( _renderBackground ) background . render ( scene ) ;
12511255
1252- for ( let i = 0 , l = cameras . length ; i < l ; i ++ ) {
1256+ if ( xr . enabled && xr . isMultiview ) {
12531257
1254- const camera2 = cameras [ i ] ;
1258+ textures . setDeferTextureUploads ( true ) ;
12551259
1256- renderScene ( currentRenderList , scene , camera2 , camera2 . viewport ) ;
1260+ renderScene ( currentRenderList , scene , camera , camera . cameras [ 0 ] . viewport ) ;
1261+
1262+ } else {
1263+
1264+ for ( let i = 0 , l = cameras . length ; i < l ; i ++ ) {
1265+
1266+ const camera2 = cameras [ i ] ;
1267+
1268+ renderScene ( currentRenderList , scene , camera2 , camera2 . viewport ) ;
1269+
1270+ }
12571271
12581272 }
12591273
@@ -1779,6 +1793,7 @@ class WebGLRenderer {
17791793 materialProperties . vertexAlphas = parameters . vertexAlphas ;
17801794 materialProperties . vertexTangents = parameters . vertexTangents ;
17811795 materialProperties . toneMapping = parameters . toneMapping ;
1796+ materialProperties . numMultiviewViews = parameters . numMultiviewViews ;
17821797
17831798 }
17841799
@@ -1810,6 +1825,8 @@ class WebGLRenderer {
18101825
18111826 }
18121827
1828+ const numMultiviewViews = _currentRenderTarget && _currentRenderTarget . isWebGLMultiviewRenderTarget ? _currentRenderTarget . numViews : 0 ;
1829+
18131830 const morphAttribute = geometry . morphAttributes . position || geometry . morphAttributes . normal || geometry . morphAttributes . color ;
18141831 const morphTargetsCount = ( morphAttribute !== undefined ) ? morphAttribute . length : 0 ;
18151832
@@ -1937,6 +1954,10 @@ class WebGLRenderer {
19371954
19381955 needsProgramChange = true ;
19391956
1957+ } else if ( materialProperties . numMultiviewViews !== numMultiviewViews ) {
1958+
1959+ needsProgramChange = true ;
1960+
19401961 }
19411962
19421963 } else {
@@ -1983,24 +2004,33 @@ class WebGLRenderer {
19832004
19842005 // common camera uniforms
19852006
1986- const reverseDepthBuffer = state . buffers . depth . getReversed ( ) ;
2007+ if ( program . numMultiviewViews > 0 ) {
19872008
1988- if ( reverseDepthBuffer ) {
2009+ multiview . updateCameraProjectionMatricesUniform ( camera , p_uniforms ) ;
2010+ multiview . updateCameraViewMatricesUniform ( camera , p_uniforms ) ;
19892011
1990- _currentProjectionMatrix . copy ( camera . projectionMatrix ) ;
2012+ } else {
19912013
1992- toNormalizedProjectionMatrix ( _currentProjectionMatrix ) ;
1993- toReversedProjectionMatrix ( _currentProjectionMatrix ) ;
2014+ const reverseDepthBuffer = state . buffers . depth . getReversed ( ) ;
19942015
1995- p_uniforms . setValue ( _gl , 'projectionMatrix' , _currentProjectionMatrix ) ;
2016+ if ( reverseDepthBuffer ) {
19962017
1997- } else {
2018+ _currentProjectionMatrix . copy ( camera . projectionMatrix ) ;
19982019
1999- p_uniforms . setValue ( _gl , 'projectionMatrix' , camera . projectionMatrix ) ;
2020+ toNormalizedProjectionMatrix ( _currentProjectionMatrix ) ;
2021+ toReversedProjectionMatrix ( _currentProjectionMatrix ) ;
20002022
2001- }
2023+ p_uniforms . setValue ( _gl , 'projectionMatrix' , _currentProjectionMatrix ) ;
2024+
2025+ } else {
2026+
2027+ p_uniforms . setValue ( _gl , 'projectionMatrix' , camera . projectionMatrix ) ;
20022028
2003- p_uniforms . setValue ( _gl , 'viewMatrix' , camera . matrixWorldInverse ) ;
2029+ }
2030+
2031+ p_uniforms . setValue ( _gl , 'viewMatrix' , camera . matrixWorldInverse ) ;
2032+
2033+ }
20042034
20052035 const uCamPos = p_uniforms . map . cameraPosition ;
20062036
@@ -2162,8 +2192,17 @@ class WebGLRenderer {
21622192
21632193 // common matrices
21642194
2165- p_uniforms . setValue ( _gl , 'modelViewMatrix' , object . modelViewMatrix ) ;
2166- p_uniforms . setValue ( _gl , 'normalMatrix' , object . normalMatrix ) ;
2195+ if ( program . numMultiviewViews > 0 ) {
2196+
2197+ multiview . updateObjectMatricesUniforms ( object , camera , p_uniforms ) ;
2198+
2199+ } else {
2200+
2201+ p_uniforms . setValue ( _gl , 'modelViewMatrix' , object . modelViewMatrix ) ;
2202+ p_uniforms . setValue ( _gl , 'normalMatrix' , object . normalMatrix ) ;
2203+
2204+ }
2205+
21672206 p_uniforms . setValue ( _gl , 'modelMatrix' , object . matrixWorld ) ;
21682207
21692208 // UBOs
@@ -2267,7 +2306,7 @@ class WebGLRenderer {
22672306
22682307 renderTargetProperties . __autoAllocateDepthBuffer = depthTexture === undefined ;
22692308
2270- if ( ! renderTargetProperties . __autoAllocateDepthBuffer ) {
2309+ if ( ! renderTargetProperties . __autoAllocateDepthBuffer && ! _currentRenderTarget . isWebGLMultiviewRenderTarget ) {
22712310
22722311 // The multisample_render_to_texture extension doesn't work properly if there
22732312 // are midframe flushes and an external depth buffer. Disable use of the extension.
0 commit comments