@@ -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 ;
@@ -278,6 +280,7 @@ class WebGLRenderer {
278280 let extensions , capabilities , state , info ;
279281 let properties , textures , cubemaps , cubeuvmaps , attributes , geometries , objects ;
280282 let programCache , materials , renderLists , renderStates , clipping , shadowMap ;
283+ let multiview ;
281284
282285 let background , morphtargets , bufferRenderer , indexedBufferRenderer ;
283286
@@ -316,6 +319,7 @@ class WebGLRenderer {
316319 renderLists = new WebGLRenderLists ( ) ;
317320 renderStates = new WebGLRenderStates ( extensions ) ;
318321 background = new WebGLBackground ( _this , cubemaps , cubeuvmaps , state , objects , _alpha , premultipliedAlpha ) ;
322+ multiview = new WebGLMultiview ( _this , extensions , _gl ) ;
319323 shadowMap = new WebGLShadowMap ( _this , objects , capabilities ) ;
320324 uniformsGroups = new WebGLUniformsGroups ( _gl , info , capabilities , state ) ;
321325
@@ -1252,11 +1256,21 @@ class WebGLRenderer {
12521256
12531257 if ( _renderBackground ) background . render ( scene ) ;
12541258
1255- for ( let i = 0 , l = cameras . length ; i < l ; i ++ ) {
1259+ if ( xr . enabled && xr . isMultiview ) {
12561260
1257- const camera2 = cameras [ i ] ;
1261+ textures . setDeferTextureUploads ( true ) ;
12581262
1259- renderScene ( currentRenderList , scene , camera2 , camera2 . viewport ) ;
1263+ renderScene ( currentRenderList , scene , camera , camera . cameras [ 0 ] . viewport ) ;
1264+
1265+ } else {
1266+
1267+ for ( let i = 0 , l = cameras . length ; i < l ; i ++ ) {
1268+
1269+ const camera2 = cameras [ i ] ;
1270+
1271+ renderScene ( currentRenderList , scene , camera2 , camera2 . viewport ) ;
1272+
1273+ }
12601274
12611275 }
12621276
@@ -1782,6 +1796,7 @@ class WebGLRenderer {
17821796 materialProperties . vertexAlphas = parameters . vertexAlphas ;
17831797 materialProperties . vertexTangents = parameters . vertexTangents ;
17841798 materialProperties . toneMapping = parameters . toneMapping ;
1799+ materialProperties . numMultiviewViews = parameters . numMultiviewViews ;
17851800
17861801 }
17871802
@@ -1813,6 +1828,8 @@ class WebGLRenderer {
18131828
18141829 }
18151830
1831+ const numMultiviewViews = _currentRenderTarget && _currentRenderTarget . isWebGLMultiviewRenderTarget ? _currentRenderTarget . numViews : 0 ;
1832+
18161833 const morphAttribute = geometry . morphAttributes . position || geometry . morphAttributes . normal || geometry . morphAttributes . color ;
18171834 const morphTargetsCount = ( morphAttribute !== undefined ) ? morphAttribute . length : 0 ;
18181835
@@ -1940,6 +1957,10 @@ class WebGLRenderer {
19401957
19411958 needsProgramChange = true ;
19421959
1960+ } else if ( materialProperties . numMultiviewViews !== numMultiviewViews ) {
1961+
1962+ needsProgramChange = true ;
1963+
19431964 }
19441965
19451966 } else {
@@ -1986,24 +2007,33 @@ class WebGLRenderer {
19862007
19872008 // common camera uniforms
19882009
1989- const reverseDepthBuffer = state . buffers . depth . getReversed ( ) ;
2010+ if ( program . numMultiviewViews > 0 ) {
19902011
1991- if ( reverseDepthBuffer ) {
2012+ multiview . updateCameraProjectionMatricesUniform ( camera , p_uniforms ) ;
2013+ multiview . updateCameraViewMatricesUniform ( camera , p_uniforms ) ;
19922014
1993- _currentProjectionMatrix . copy ( camera . projectionMatrix ) ;
2015+ } else {
19942016
1995- toNormalizedProjectionMatrix ( _currentProjectionMatrix ) ;
1996- toReversedProjectionMatrix ( _currentProjectionMatrix ) ;
2017+ const reverseDepthBuffer = state . buffers . depth . getReversed ( ) ;
19972018
1998- p_uniforms . setValue ( _gl , 'projectionMatrix' , _currentProjectionMatrix ) ;
2019+ if ( reverseDepthBuffer ) {
19992020
2000- } else {
2021+ _currentProjectionMatrix . copy ( camera . projectionMatrix ) ;
20012022
2002- p_uniforms . setValue ( _gl , 'projectionMatrix' , camera . projectionMatrix ) ;
2023+ toNormalizedProjectionMatrix ( _currentProjectionMatrix ) ;
2024+ toReversedProjectionMatrix ( _currentProjectionMatrix ) ;
20032025
2004- }
2026+ p_uniforms . setValue ( _gl , 'projectionMatrix' , _currentProjectionMatrix ) ;
2027+
2028+ } else {
2029+
2030+ p_uniforms . setValue ( _gl , 'projectionMatrix' , camera . projectionMatrix ) ;
20052031
2006- p_uniforms . setValue ( _gl , 'viewMatrix' , camera . matrixWorldInverse ) ;
2032+ }
2033+
2034+ p_uniforms . setValue ( _gl , 'viewMatrix' , camera . matrixWorldInverse ) ;
2035+
2036+ }
20072037
20082038 const uCamPos = p_uniforms . map . cameraPosition ;
20092039
@@ -2165,8 +2195,17 @@ class WebGLRenderer {
21652195
21662196 // common matrices
21672197
2168- p_uniforms . setValue ( _gl , 'modelViewMatrix' , object . modelViewMatrix ) ;
2169- p_uniforms . setValue ( _gl , 'normalMatrix' , object . normalMatrix ) ;
2198+ if ( program . numMultiviewViews > 0 ) {
2199+
2200+ multiview . updateObjectMatricesUniforms ( object , camera , p_uniforms ) ;
2201+
2202+ } else {
2203+
2204+ p_uniforms . setValue ( _gl , 'modelViewMatrix' , object . modelViewMatrix ) ;
2205+ p_uniforms . setValue ( _gl , 'normalMatrix' , object . normalMatrix ) ;
2206+
2207+ }
2208+
21702209 p_uniforms . setValue ( _gl , 'modelMatrix' , object . matrixWorld ) ;
21712210
21722211 // UBOs
@@ -2270,7 +2309,7 @@ class WebGLRenderer {
22702309
22712310 renderTargetProperties . __autoAllocateDepthBuffer = depthTexture === undefined ;
22722311
2273- if ( ! renderTargetProperties . __autoAllocateDepthBuffer ) {
2312+ if ( ! renderTargetProperties . __autoAllocateDepthBuffer && ( ! _currentRenderTarget || ! _currentRenderTarget . isWebGLMultiviewRenderTarget ) ) {
22742313
22752314 // The multisample_render_to_texture extension doesn't work properly if there
22762315 // are midframe flushes and an external depth buffer. Disable use of the extension.
0 commit comments