@@ -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
@@ -1248,11 +1252,21 @@ class WebGLRenderer {
12481252
12491253 if ( _renderBackground ) background . render ( scene ) ;
12501254
1251- for ( let i = 0 , l = cameras . length ; i < l ; i ++ ) {
1255+ if ( xr . enabled && xr . isMultiview ) {
12521256
1253- const camera2 = cameras [ i ] ;
1257+ textures . setDeferTextureUploads ( true ) ;
12541258
1255- renderScene ( currentRenderList , scene , camera2 , camera2 . viewport ) ;
1259+ renderScene ( currentRenderList , scene , camera , camera . cameras [ 0 ] . viewport ) ;
1260+
1261+ } else {
1262+
1263+ for ( let i = 0 , l = cameras . length ; i < l ; i ++ ) {
1264+
1265+ const camera2 = cameras [ i ] ;
1266+
1267+ renderScene ( currentRenderList , scene , camera2 , camera2 . viewport ) ;
1268+
1269+ }
12561270
12571271 }
12581272
@@ -1778,6 +1792,7 @@ class WebGLRenderer {
17781792 materialProperties . vertexAlphas = parameters . vertexAlphas ;
17791793 materialProperties . vertexTangents = parameters . vertexTangents ;
17801794 materialProperties . toneMapping = parameters . toneMapping ;
1795+ materialProperties . numMultiviewViews = parameters . numMultiviewViews ;
17811796
17821797 }
17831798
@@ -1809,6 +1824,8 @@ class WebGLRenderer {
18091824
18101825 }
18111826
1827+ const numMultiviewViews = _currentRenderTarget && _currentRenderTarget . isWebGLMultiviewRenderTarget ? _currentRenderTarget . numViews : 0 ;
1828+
18121829 const morphAttribute = geometry . morphAttributes . position || geometry . morphAttributes . normal || geometry . morphAttributes . color ;
18131830 const morphTargetsCount = ( morphAttribute !== undefined ) ? morphAttribute . length : 0 ;
18141831
@@ -1936,6 +1953,10 @@ class WebGLRenderer {
19361953
19371954 needsProgramChange = true ;
19381955
1956+ } else if ( materialProperties . numMultiviewViews !== numMultiviewViews ) {
1957+
1958+ needsProgramChange = true ;
1959+
19391960 }
19401961
19411962 } else {
@@ -1982,24 +2003,33 @@ class WebGLRenderer {
19822003
19832004 // common camera uniforms
19842005
1985- const reverseDepthBuffer = state . buffers . depth . getReversed ( ) ;
2006+ if ( program . numMultiviewViews > 0 ) {
19862007
1987- if ( reverseDepthBuffer ) {
2008+ multiview . updateCameraProjectionMatricesUniform ( camera , p_uniforms ) ;
2009+ multiview . updateCameraViewMatricesUniform ( camera , p_uniforms ) ;
19882010
1989- _currentProjectionMatrix . copy ( camera . projectionMatrix ) ;
2011+ } else {
19902012
1991- toNormalizedProjectionMatrix ( _currentProjectionMatrix ) ;
1992- toReversedProjectionMatrix ( _currentProjectionMatrix ) ;
2013+ const reverseDepthBuffer = state . buffers . depth . getReversed ( ) ;
19932014
1994- p_uniforms . setValue ( _gl , 'projectionMatrix' , _currentProjectionMatrix ) ;
2015+ if ( reverseDepthBuffer ) {
19952016
1996- } else {
2017+ _currentProjectionMatrix . copy ( camera . projectionMatrix ) ;
19972018
1998- p_uniforms . setValue ( _gl , 'projectionMatrix' , camera . projectionMatrix ) ;
2019+ toNormalizedProjectionMatrix ( _currentProjectionMatrix ) ;
2020+ toReversedProjectionMatrix ( _currentProjectionMatrix ) ;
19992021
2000- }
2022+ p_uniforms . setValue ( _gl , 'projectionMatrix' , _currentProjectionMatrix ) ;
2023+
2024+ } else {
2025+
2026+ p_uniforms . setValue ( _gl , 'projectionMatrix' , camera . projectionMatrix ) ;
20012027
2002- p_uniforms . setValue ( _gl , 'viewMatrix' , camera . matrixWorldInverse ) ;
2028+ }
2029+
2030+ p_uniforms . setValue ( _gl , 'viewMatrix' , camera . matrixWorldInverse ) ;
2031+
2032+ }
20032033
20042034 const uCamPos = p_uniforms . map . cameraPosition ;
20052035
@@ -2161,8 +2191,17 @@ class WebGLRenderer {
21612191
21622192 // common matrices
21632193
2164- p_uniforms . setValue ( _gl , 'modelViewMatrix' , object . modelViewMatrix ) ;
2165- p_uniforms . setValue ( _gl , 'normalMatrix' , object . normalMatrix ) ;
2194+ if ( program . numMultiviewViews > 0 ) {
2195+
2196+ multiview . updateObjectMatricesUniforms ( object , camera , p_uniforms ) ;
2197+
2198+ } else {
2199+
2200+ p_uniforms . setValue ( _gl , 'modelViewMatrix' , object . modelViewMatrix ) ;
2201+ p_uniforms . setValue ( _gl , 'normalMatrix' , object . normalMatrix ) ;
2202+
2203+ }
2204+
21662205 p_uniforms . setValue ( _gl , 'modelMatrix' , object . matrixWorld ) ;
21672206
21682207 // UBOs
@@ -2266,7 +2305,7 @@ class WebGLRenderer {
22662305
22672306 renderTargetProperties . __autoAllocateDepthBuffer = depthTexture === undefined ;
22682307
2269- if ( ! renderTargetProperties . __autoAllocateDepthBuffer ) {
2308+ if ( ! renderTargetProperties . __autoAllocateDepthBuffer && ! _currentRenderTarget . isWebGLMultiviewRenderTarget ) {
22702309
22712310 // The multisample_render_to_texture extension doesn't work properly if there
22722311 // are midframe flushes and an external depth buffer. Disable use of the extension.
0 commit comments