Skip to content

Commit e1ae34d

Browse files
Olli Etuahogreggman
authored andcommitted
Add a utility for storing different shader programs for Aquarium
Aquarium has a lot of different rendering settings that use different shader programs. This patch adds a class that makes managing these programs easier instead of requiring a separate line of code for each combination of settings. The programs are now compiled on demand instead of all being compiled when loading.
1 parent ab0c317 commit e1ae34d

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

aquarium/aquarium.js

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,36 @@ Scene.prototype.stop = function() {
528528
this.ignore = false;
529529
};
530530

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+
531561
Scene.prototype.onload_ = function(data, exception) {
532562
if (this.ignore) {
533563
return;
@@ -553,11 +583,6 @@ Scene.prototype.onload_ = function(data, exception) {
553583
field.data,
554584
field.type);
555585
}
556-
// setup program
557-
// There are 3 programs
558-
// DM
559-
// DM+NM
560-
// DM+NM+RM
561586
var type;
562587
var vsId;
563588
var fsId;
@@ -588,58 +613,36 @@ Scene.prototype.onload_ = function(data, exception) {
588613
vsId = 'diffuseVertexShader';
589614
fsId = 'diffuseFragmentShader';
590615
}
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());
602619

603620
tdl.log(this.url, ": ", type);
604621
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;
615623
model.extents = arrays.position.computeExtents();
616624
this.models.push(model);
617625
}
618-
setShaders();
619626
}
620627
};
621628

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+
622637
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();
636639
for (var sceneName in g_scenes) {
637640
var scene = g_scenes[sceneName];
638641
var models = scene.models;
639642
var numModels = models.length;
640643
for (var jj = 0; jj < numModels; ++jj) {
641644
var model = models[jj];
642-
model.setProgram(model.programs[name]);
645+
model.setProgram(model.programSet.getProgram(shadingSettings));
643646
}
644647
}
645648
}
@@ -701,9 +704,6 @@ function initLightRay(info) {
701704
function setupLaser() {
702705
var textures = {
703706
colorMap: tdl.textures.loadTexture(g_aquariumConfig.aquariumRoot + 'static_assets/beam.png')};
704-
var program = createProgramFromTags(
705-
'laserVertexShader',
706-
'laserFragmentShader');
707707
var beam1Arrays = tdl.primitives.createPlane(1, 1, 1, 1);
708708
delete beam1Arrays.normal;
709709
tdl.primitives.reorient(beam1Arrays,
@@ -718,7 +718,11 @@ function setupLaser() {
718718
beam1Arrays,
719719
beam2Arrays,
720720
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;
722726
}
723727

724728
function setupLightRay() {
@@ -731,17 +735,18 @@ function setupLightRay() {
731735

732736
var textures = {
733737
colorMap: tdl.textures.loadTexture(g_aquariumConfig.aquariumRoot + 'assets/LightRay.png') };
734-
var program = createProgramFromTags(
735-
'texVertexShader',
736-
'texFragmentShader');
737738
var arrays = tdl.primitives.createPlane(1, 1, 1, 1);
738739
tdl.primitives.reorient(arrays,
739740
[1, 0, 0, 0,
740741
0, 0, -1, 0,
741742
0, 1, 0, 0,
742743
0, 0.5, 0, 1]);
743744
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;
745750
}
746751

747752
function setupBubbles(particleSystem) {

0 commit comments

Comments
 (0)