@@ -26,6 +26,10 @@ if (isMultiviewSupportEnabled()) {
26
26
g . options . useMultiview = { enabled : true , text : 'Use multiview' } ;
27
27
}
28
28
29
+ function useMultiviewForStereo ( ) {
30
+ return multiview && g . options . useMultiview . enabled ;
31
+ }
32
+
29
33
// globals
30
34
var gl ; // the gl context.
31
35
var multiview ; // multiview extension.
@@ -43,6 +47,7 @@ var g_fog = true;
43
47
var g_numFish = [ 1 , 100 , 500 , 1000 , 5000 , 10000 , 15000 , 20000 , 25000 , 30000 ] ;
44
48
45
49
var g_stereoDemoActive = false ;
50
+ var g_shadersNeedUpdate = false ; // Set to true whenever the state has changed so that shaders may need to be changed.
46
51
47
52
var g_requestId ;
48
53
var g_syncManager ;
@@ -1246,13 +1251,6 @@ function initialize() {
1246
1251
var lightRayConstInUse = useMultiview ? lightRayConstMultiview : lightRayConst ;
1247
1252
var fishConstInUse = useMultiview ? fishConstMultiview : fishConst ;
1248
1253
1249
- // If we are running > 40hz then turn on a few more options.
1250
- if ( setPretty && g_fpsTimer . averageFPS > 40 ) {
1251
- setPretty = false ;
1252
- if ( ! g . options . normalMaps . enabled ) { g . options . normalMaps . toggle ( ) ; }
1253
- if ( ! g . options . reflection . enabled ) { g . options . reflection . toggle ( ) ; }
1254
- }
1255
-
1256
1254
ambient [ 0 ] = g . globals . ambientRed ;
1257
1255
ambient [ 1 ] = g . globals . ambientGreen ;
1258
1256
ambient [ 2 ] = g . globals . ambientBlue ;
@@ -1635,14 +1633,12 @@ function initialize() {
1635
1633
}
1636
1634
1637
1635
function renderStereo ( leftProjectionMatrix , rightProjectionMatrix , viewInverseMatrix , pose ) {
1638
- var useMultiview = multiview && g . options . useMultiview . enabled ;
1639
- if ( useMultiview ) {
1636
+ if ( useMultiviewForStereo ( ) ) {
1640
1637
setupMultiviewFbIfNeeded ( ) ;
1641
1638
var halfWidth = Math . floor ( canvas . width * 0.5 ) ;
1642
1639
gl . bindFramebuffer ( gl . FRAMEBUFFER , g_multiviewFb ) ;
1643
1640
gl . viewport ( 0 , 0 , halfWidth , canvas . height ) ;
1644
1641
gl . disable ( gl . SCISSOR_TEST ) ;
1645
- setShaders ( true ) ;
1646
1642
render ( [ leftProjectionMatrix , rightProjectionMatrix ] , viewInverseMatrix , true , pose ) ;
1647
1643
1648
1644
gl . bindFramebuffer ( gl . DRAW_FRAMEBUFFER , null ) ;
@@ -1653,7 +1649,6 @@ function initialize() {
1653
1649
} else { // not multiview
1654
1650
gl . viewport ( 0 , 0 , canvas . width * 0.5 , canvas . height ) ;
1655
1651
gl . enable ( gl . SCISSOR_TEST ) ;
1656
- setShaders ( false ) ;
1657
1652
gl . scissor ( 0 , 0 , canvas . width * 0.5 , canvas . height ) ;
1658
1653
render ( leftProjectionMatrix , viewInverseMatrix , false , pose ) ;
1659
1654
@@ -1703,7 +1698,6 @@ function initialize() {
1703
1698
1704
1699
gl . disable ( gl . SCISSOR_TEST ) ;
1705
1700
gl . viewport ( 0 , 0 , gl . drawingBufferWidth , gl . drawingBufferHeight ) ;
1706
- setShaders ( false ) ;
1707
1701
render ( monoProjection , viewInverseTemp ) ;
1708
1702
}
1709
1703
@@ -1758,13 +1752,26 @@ function initialize() {
1758
1752
g_fpsTimer . update ( elapsedTime ) ;
1759
1753
fpsElem . innerHTML = g_fpsTimer . averageFPS ;
1760
1754
1755
+ // If we are running > 40hz then turn on a few more options.
1756
+ if ( setPretty && g_fpsTimer . averageFPS > 40 ) {
1757
+ setPretty = false ;
1758
+ if ( ! g . options . normalMaps . enabled ) { g . options . normalMaps . toggle ( ) ; }
1759
+ if ( ! g . options . reflection . enabled ) { g . options . reflection . toggle ( ) ; }
1760
+ }
1761
+
1761
1762
if ( g_vrDisplay ) {
1762
1763
g_requestId = g_vrDisplay . requestAnimationFrame ( onAnimationFrame ) ;
1763
1764
g_vrDisplay . getFrameData ( g_frameData ) ;
1764
1765
} else {
1765
1766
g_requestId = tdl . webgl . requestAnimationFrame ( onAnimationFrame , canvas ) ;
1766
1767
}
1767
1768
1769
+ if ( g_shadersNeedUpdate ) {
1770
+ var isInStereoMode = ( g_vrDisplay && g_vrDisplay . isPresenting ) || g_stereoDemoActive ;
1771
+ setShaders ( isInStereoMode && useMultiviewForStereo ( ) ) ;
1772
+ g_shadersNeedUpdate = false ;
1773
+ }
1774
+
1768
1775
if ( g_vrDisplay && g_vrDisplay . isPresenting ) {
1769
1776
/* VR UI is enabled in VR Mode. VR UI has two mode, menu mode is the mirror of control panel of
1770
1777
* aquarium and non-menu mode may presents fps(could be turn off) in front of user. These two
@@ -1778,10 +1785,8 @@ function initialize() {
1778
1785
// Query gamepad button clicked event.
1779
1786
g_vrUi . queryGamepadStatus ( ) ;
1780
1787
1781
- var useMultiview = multiview && g . options . useMultiview . enabled ;
1782
-
1783
1788
// TODO: Support VRUI when doing multiview rendering.
1784
- if ( ! useMultiview && g_vrUi . isMenuMode ) {
1789
+ if ( ! useMultiviewForStereo ( ) && g_vrUi . isMenuMode ) {
1785
1790
1786
1791
// When VR UI in menu mode, UI need a cursor to help user do select operation. Currently, cursor uses
1787
1792
// head-neck model which means a point in front of user and user could move the point by rotating their head(with HMD).
@@ -1947,6 +1952,7 @@ function initUIStuff() {
1947
1952
options [ name ] = { enabled :! option . enabled } ;
1948
1953
setSettings ( { options :options } ) ;
1949
1954
elem . style . color = option . enabled ? "red" : "gray" ;
1955
+ g_shadersNeedUpdate = true ;
1950
1956
}
1951
1957
1952
1958
var optionsContainer = document . getElementById ( "optionsContainer" ) ;
@@ -2135,6 +2141,8 @@ $(function(){
2135
2141
// to the recommended dimensions for the display.
2136
2142
resize ( ) ;
2137
2143
2144
+ g_shadersNeedUpdate = true ;
2145
+
2138
2146
if ( g_vrDisplay . isPresenting ) {
2139
2147
if ( g_vrDisplay . capabilities . hasExternalDisplay ) {
2140
2148
removeButton ( vrButton ) ;
@@ -2165,6 +2173,7 @@ $(function(){
2165
2173
2166
2174
function toggleStereoDemo ( ) {
2167
2175
g_stereoDemoActive = ! g_stereoDemoActive ;
2176
+ g_shadersNeedUpdate = true ;
2168
2177
}
2169
2178
2170
2179
function resize ( ) {
@@ -2224,7 +2233,7 @@ $(function(){
2224
2233
}
2225
2234
}
2226
2235
// Regardless of if we have WebVR support, we can demonstrate stereo rendering inside the window.
2227
- stereoDemoButton = addButton ( "Toggle Stereo Demo" , "E " , getCurrentUrl ( ) + "/vr_assets/button.png" , toggleStereoDemo ) ;
2236
+ stereoDemoButton = addButton ( "Toggle Stereo Demo" , "" , getCurrentUrl ( ) + "/vr_assets/button.png" , toggleStereoDemo ) ;
2228
2237
}
2229
2238
window . addEventListener ( 'resize' , function ( ) { onResize ( ) ; } , false ) ;
2230
2239
onResize ( ) ;
0 commit comments