Skip to content

Commit 4984387

Browse files
committed
add numFish query parameter
1 parent 4a0eefc commit 4984387

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

aquarium/README.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ <h2>Controls</h2>
2929
<li>Press SPACE to change the view.</li>
3030
<li>Press L for sharks with frikken lasers. Looks best from an outside view.</li>
3131
</ul>
32+
<h2>Options</h2>
33+
<ul>
34+
<li>
35+
<h3>numFish</h3>
36+
<p>set the number of fish</p>
37+
<pre>
38+
<a href="aquarium.html?numFish=1234">https://webglsamples.github.io/aquarium/aquarium.html?numFish=1234</a>
39+
</pre>
40+
</li>
41+
</ul>
3242
<h2>Running across multiple machines</h2>
3343
<p>The Aquarium demo can by run synchronized across multiple machines.</p>
3444

aquarium/aquarium.js

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use strict";
2+
13
tdl.require('tdl.buffers');
24
tdl.require('tdl.clock');
35
tdl.require('tdl.fast');
@@ -14,6 +16,7 @@ tdl.require('tdl.textures');
1416
tdl.require('tdl.webgl');
1517

1618
// globals
19+
const g_query = parseQueryString(window.location.search);
1720
var gl; // the gl context.
1821
var canvas; // the canvas
1922
var math; // the math lib.
@@ -30,11 +33,11 @@ var g_scenes = {}; // each of the models
3033
var g_sceneGroups = {}; // the placement of the models
3134
var g_fog = true;
3235
var g_requestId;
36+
var g_numFish = [1, 100, 500, 1000, 5000, 10000, 15000, 20000, 25000, 30000];
3337

3438
//g_debug = true;
3539
//g_drawOnce = true;
3640

37-
var g_numSharks = 0;
3841
var g_tailOffsetMult = 1;
3942
var g_endOfDome = Math.PI / 8;
4043
var g_tankRadius = 74;
@@ -347,6 +350,15 @@ var g_skyBoxUrls = [
347350
// 'static_assets/skybox/InteriorCubeEnv_EM.png'
348351
]
349352

353+
function parseQueryString(s) {
354+
const q = {};
355+
(s.startsWith('?') ? s.substring(1) : s).split('&').forEach(pair => {
356+
const parts = pair.split('=').map(decodeURIComponent);
357+
q[parts[0]] = parts[1];
358+
});
359+
return q;
360+
}
361+
350362
function ValidateNoneOfTheArgsAreUndefined(functionName, args) {
351363
for (var ii = 0; ii < args.length; ++ii) {
352364
if (args[ii] === undefined) {
@@ -829,7 +841,10 @@ function handleContextRestored() {
829841
}
830842

831843
function initialize() {
832-
var maxViewportDims = gl.getParameter(gl.MAX_VIEWPORT_DIMS);
844+
const maxViewportDims = gl.getParameter(gl.MAX_VIEWPORT_DIMS);
845+
if (g_query.numFish) {
846+
g_numFish[0] = parseInt(g_query.numFish);
847+
}
833848

834849
gl.enable(gl.DEPTH_TEST);
835850
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
@@ -841,49 +856,47 @@ function initialize() {
841856
Log("--Setup Laser----------------------------------------");
842857
var laser = setupLaser();
843858

844-
var num = [1, 100, 500, 1000, 5000, 10000, 15000, 20000, 25000, 30000];
845859
var changeViewElem = document.getElementById("setSettingChangeView");
846860
var parentElem = changeViewElem.parentNode;
847-
for (var i = 0; i < num.length; ++i) {
861+
g_numFish.forEach((numFish, ndx) => {
848862
var div = document.createElement("div");
849863
div.className = "clickable";
850-
div.id = "setSetting" + i;
851-
div.innerHTML = num[i];
864+
div.id = "setSetting" + ndx;
865+
div.innerHTML = numFish;
852866
parentElem.insertBefore(div, changeViewElem);
853-
}
867+
});
854868

855-
for (var ff = 0; ff < g_fishTable.length; ++ff) {
856-
g_fishTable[ff].fishData = [];
857-
g_fishTable[ff].num = [];
858-
}
869+
g_fishTable.forEach(info => {
870+
info.fishData = [];
871+
info.num = [];
872+
});
859873

860-
var type = ["Big", "Medium", "Small"];
861-
for (var i = 0; i < num.length; ++i) {
862-
var numLeft = num[i];
863-
for (var j = 0; j < type.length; ++j) {
864-
for (var ff = 0; ff < g_fishTable.length; ++ff) {
865-
var fishInfo = g_fishTable[ff];
874+
var types = ["Big", "Medium", "Small"];
875+
g_numFish.forEach((totalFish) => {
876+
var numLeft = totalFish;
877+
types.forEach((type) => {
878+
g_fishTable.forEach((fishInfo) => {
866879
var fishName = fishInfo.name;
867-
if (!fishName.startsWith(type[j])) {
868-
continue;
880+
if (!fishName.startsWith(type)) {
881+
return;
869882
}
870883
var numType = numLeft;
871-
if (type[j] == "Big") {
872-
numType = Math.min(numLeft, num[i] < 100 ? 1 : 2);
873-
} else if (type[j] == "Medium") {
874-
if (num[i] < 1000) {
875-
numType = Math.min(numLeft, num[i] / 10 | 0);
876-
} else if (num[i] < 10000) {
884+
if (type == "Big") {
885+
numType = Math.min(numLeft, totalFish < 100 ? 1 : 2);
886+
} else if (type == "Medium") {
887+
if (totalFish < 1000) {
888+
numType = Math.min(numLeft, totalFish / 10 | 0);
889+
} else if (totalFish < 10000) {
877890
numType = Math.min(numLeft, 80);
878891
} else {
879892
numType = Math.min(numLeft, 160);
880893
}
881894
}
882895
numLeft = numLeft - numType;
883896
fishInfo.num.push(numType);
884-
}
885-
}
886-
}
897+
});
898+
})
899+
});
887900

888901
var particleSystem = new tdl.particles.ParticleSystem(
889902
gl, null, math.pseudoRandom);
@@ -1638,7 +1651,9 @@ function setupCountButtons() {
16381651
}}(elem, ii);
16391652
}
16401653

1641-
if (g.net.sync) {
1654+
if (g_query.numFish) {
1655+
setSetting(document.getElementById("setSetting0"), 0);
1656+
} else if (g.net.sync) {
16421657
setSetting(document.getElementById("setSetting4"), 4);
16431658
} else {
16441659
setSetting(document.getElementById("setSetting2"), 2);

0 commit comments

Comments
 (0)