@@ -528,6 +528,36 @@ Scene.prototype.stop = function() {
528
528
this . ignore = false ;
529
529
} ;
530
530
531
+ var ProgramSet = function ( vsId , fsId , fogMask ) {
532
+ this . vsId = vsId ;
533
+ this . fsId = fsId ;
534
+ this . fogMask = fogMask ; // If fogMask is false, then programs from this program set never have fog turned on.
535
+ this . cache = { } ;
536
+ } ;
537
+
538
+ ProgramSet . prototype . getKey = function ( shadingSettings ) {
539
+ var key = 'p' ;
540
+ if ( this . fogMask && shadingSettings . fog ) {
541
+ key += 'Fog' ;
542
+ }
543
+ if ( shadingSettings . reflection ) {
544
+ key += 'Reflect' ;
545
+ }
546
+ if ( shadingSettings . normalMap ) {
547
+ key += 'Normalmap' ;
548
+ }
549
+ return key ;
550
+ } ;
551
+
552
+ ProgramSet . prototype . getProgram = function ( shadingSettings ) {
553
+ var key = this . getKey ( shadingSettings ) ;
554
+ if ( ! this . cache . hasOwnProperty ( key ) ) {
555
+ var fog = this . fogMask && shadingSettings . fog ;
556
+ this . cache [ key ] = createProgramFromTags ( this . vsId , this . fsId , fog , shadingSettings . reflection , shadingSettings . normalMap ) ;
557
+ }
558
+ return this . cache [ key ] ;
559
+ } ;
560
+
531
561
Scene . prototype . onload_ = function ( data , exception ) {
532
562
if ( this . ignore ) {
533
563
return ;
@@ -553,11 +583,6 @@ Scene.prototype.onload_ = function(data, exception) {
553
583
field . data ,
554
584
field . type ) ;
555
585
}
556
- // setup program
557
- // There are 3 programs
558
- // DM
559
- // DM+NM
560
- // DM+NM+RM
561
586
var type ;
562
587
var vsId ;
563
588
var fsId ;
@@ -588,58 +613,36 @@ Scene.prototype.onload_ = function(data, exception) {
588
613
vsId = 'diffuseVertexShader' ;
589
614
fsId = 'diffuseFragmentShader' ;
590
615
}
591
- var program = createProgramFromTags ( vsId , fsId , this . fog ) ;
592
- var noFog = createProgramFromTags ( vsId , fsId , false ) ;
593
- var noReflection = createProgramFromTags ( vsId , fsId , this . fog , false ) ;
594
- var noFognoReflection = createProgramFromTags ( vsId , fsId , false , false ) ;
595
- var noNormalMaps = createProgramFromTags ( vsId , fsId , this . fog , false ) ;
596
- var noFognoNormalMaps =
597
- createProgramFromTags ( vsId , fsId , false , false ) ;
598
- var noReflectionnoNormalMaps =
599
- createProgramFromTags ( vsId , fsId , this . fog , false , false ) ;
600
- var noFognoReflectionnoNormalMaps =
601
- createProgramFromTags ( vsId , fsId , false , false , false ) ;
616
+
617
+ var programSet = new ProgramSet ( vsId , fsId , this . fog ) ;
618
+ var program = programSet . getProgram ( getShadingSettings ( ) ) ;
602
619
603
620
tdl . log ( this . url , ": " , type ) ;
604
621
var model = new tdl . models . Model ( program , arrays , textures ) ;
605
- model . programs = {
606
- base : program ,
607
- noFog : noFog ,
608
- noReflection : noReflection ,
609
- noFognoReflection : noFognoReflection ,
610
- noNormalMaps : noNormalMaps ,
611
- noFognoNormalMaps : noFognoNormalMaps ,
612
- noReflectionnoNormalMaps : noReflectionnoNormalMaps ,
613
- noFognoReflectionnoNormalMaps : noFognoReflectionnoNormalMaps
614
- } ;
622
+ model . programSet = programSet ;
615
623
model . extents = arrays . position . computeExtents ( ) ;
616
624
this . models . push ( model ) ;
617
625
}
618
- setShaders ( ) ;
619
626
}
620
627
} ;
621
628
629
+ function getShadingSettings ( ) {
630
+ return {
631
+ fog : g . options . fog . enabled ,
632
+ reflection : g . options . reflection . enabled ,
633
+ normalMap : g . options . normalMaps . enabled
634
+ } ;
635
+ }
636
+
622
637
function setShaders ( ) {
623
- var name = '' ;
624
- if ( ! g . options . fog . enabled ) {
625
- name += 'noFog' ;
626
- }
627
- if ( ! g . options . reflection . enabled ) {
628
- name += 'noReflection' ;
629
- }
630
- if ( ! g . options . normalMaps . enabled ) {
631
- name += 'noNormalMaps' ;
632
- }
633
- if ( name == '' ) {
634
- name = 'base' ;
635
- }
638
+ var shadingSettings = getShadingSettings ( ) ;
636
639
for ( var sceneName in g_scenes ) {
637
640
var scene = g_scenes [ sceneName ] ;
638
641
var models = scene . models ;
639
642
var numModels = models . length ;
640
643
for ( var jj = 0 ; jj < numModels ; ++ jj ) {
641
644
var model = models [ jj ] ;
642
- model . setProgram ( model . programs [ name ] ) ;
645
+ model . setProgram ( model . programSet . getProgram ( shadingSettings ) ) ;
643
646
}
644
647
}
645
648
}
@@ -701,9 +704,6 @@ function initLightRay(info) {
701
704
function setupLaser ( ) {
702
705
var textures = {
703
706
colorMap : tdl . textures . loadTexture ( g_aquariumConfig . aquariumRoot + 'static_assets/beam.png' ) } ;
704
- var program = createProgramFromTags (
705
- 'laserVertexShader' ,
706
- 'laserFragmentShader' ) ;
707
707
var beam1Arrays = tdl . primitives . createPlane ( 1 , 1 , 1 , 1 ) ;
708
708
delete beam1Arrays . normal ;
709
709
tdl . primitives . reorient ( beam1Arrays ,
@@ -718,7 +718,11 @@ function setupLaser() {
718
718
beam1Arrays ,
719
719
beam2Arrays ,
720
720
beam3Arrays ] ) ;
721
- return new tdl . models . Model ( program , arrays , textures ) ;
721
+ var programSet = new ProgramSet ( 'laserVertexShader' , 'laserFragmentShader' , false ) ;
722
+ var program = programSet . getProgram ( getShadingSettings ( ) ) ;
723
+ var model = new tdl . models . Model ( program , arrays , textures ) ;
724
+ model . programSet = programSet ;
725
+ return model ;
722
726
}
723
727
724
728
function setupLightRay ( ) {
@@ -731,17 +735,18 @@ function setupLightRay() {
731
735
732
736
var textures = {
733
737
colorMap : tdl . textures . loadTexture ( g_aquariumConfig . aquariumRoot + 'assets/LightRay.png' ) } ;
734
- var program = createProgramFromTags (
735
- 'texVertexShader' ,
736
- 'texFragmentShader' ) ;
737
738
var arrays = tdl . primitives . createPlane ( 1 , 1 , 1 , 1 ) ;
738
739
tdl . primitives . reorient ( arrays ,
739
740
[ 1 , 0 , 0 , 0 ,
740
741
0 , 0 , - 1 , 0 ,
741
742
0 , 1 , 0 , 0 ,
742
743
0 , 0.5 , 0 , 1 ] ) ;
743
744
delete arrays . normal ;
744
- return new tdl . models . Model ( program , arrays , textures ) ;
745
+ var programSet = new ProgramSet ( 'texVertexShader' , 'texFragmentShader' , false ) ;
746
+ var program = programSet . getProgram ( getShadingSettings ( ) ) ;
747
+ var model = new tdl . models . Model ( program , arrays , textures ) ;
748
+ model . programSet = programSet ;
749
+ return model ;
745
750
}
746
751
747
752
function setupBubbles ( particleSystem ) {
0 commit comments