Skip to content

Commit b11a491

Browse files
Olli Etuahophemavax
authored andcommitted
Update timers only once per frame in Aquarium VR
This prevents everything from moving twice as fast in VR mode as well as making sure that left and right eyes are in sync with each other.
1 parent 572740a commit b11a491

File tree

1 file changed

+41
-49
lines changed

1 file changed

+41
-49
lines changed

aquarium-vr/aquarium-vr.js

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,23 +1148,7 @@ function initialize() {
11481148
viewMatrix[15] = 1;
11491149
}
11501150

1151-
function render(elapsedTime, projectionMatrix, pose) {
1152-
/*
1153-
var now = theClock.getTime();
1154-
var elapsedTime;
1155-
if(then == 0.0) {
1156-
elapsedTime = 0.0;
1157-
} else {
1158-
elapsedTime = now - then;
1159-
}
1160-
then = now;
1161-
1162-
frameCount++;
1163-
1164-
g_fpsTimer.update(elapsedTime);
1165-
fpsElem.innerHTML = g_fpsTimer.averageFPS;
1166-
*/
1167-
1151+
function render(projectionMatrix, pose) {
11681152
// If we are running > 40hz then turn on a few more options.
11691153
if (setPretty && g_fpsTimer.averageFPS > 40) {
11701154
setPretty = false;
@@ -1176,15 +1160,6 @@ function initialize() {
11761160
setCanvasSize(canvas, canvas.clientWidth, canvas.clientHeight);
11771161
}
11781162

1179-
if (g.net.sync) {
1180-
clock = now * g.globals.speed;
1181-
eyeClock = now * g.globals.eyeSpeed;
1182-
} else {
1183-
// we have our own clock.
1184-
clock += elapsedTime * g.globals.speed;
1185-
eyeClock += elapsedTime * g.globals.eyeSpeed;
1186-
}
1187-
11881163
ambient[0] = g.globals.ambientRed;
11891164
ambient[1] = g.globals.ambientGreen;
11901165
ambient[2] = g.globals.ambientBlue;
@@ -1515,20 +1490,6 @@ function initialize() {
15151490
}
15161491
}
15171492

1518-
bubbleTimer -= elapsedTime * g.globals.speed;
1519-
if (bubbleTimer < 0) {
1520-
bubbleTimer = 2 + Math.random() * 8;
1521-
var radius = Math.random() * 50;
1522-
var angle = Math.random() * Math.PI * 2;
1523-
fast.matrix4.translation(
1524-
world,
1525-
[Math.sin(angle) * radius,
1526-
0,
1527-
Math.cos(angle) * radius]);
1528-
g_bubbleSets[bubbleIndex].trigger(world);
1529-
++bubbleIndex;
1530-
bubbleIndex = bubbleIndex % g_numBubbleSets;
1531-
}
15321493
fast.matrix4.translation(world, [0, 0, 0]);
15331494
if (g.options.bubbles.enabled) {
15341495
particleSystem.draw(viewProjection, world, viewInverse);
@@ -1544,10 +1505,6 @@ function initialize() {
15441505
var info = g_lightRayInfo[ii];
15451506
var lerp = info.timer / info.duration;
15461507
var y = Math.max(70, Math.min(120, g_lightRayY + g.globals.eyeHeight));
1547-
info.timer -= elapsedTime * g.globals.speed;
1548-
if (info.timer < 0) {
1549-
initLightRay(info);
1550-
}
15511508
fast.matrix4.mul(
15521509
m4t1,
15531510
fast.matrix4.rotationZ(m4t0, info.rot + lerp * g_lightRayRotLerp),
@@ -1662,6 +1619,42 @@ function initialize() {
16621619
}
16631620
then = now;
16641621

1622+
if (g.net.sync) {
1623+
clock = now * g.globals.speed;
1624+
eyeClock = now * g.globals.eyeSpeed;
1625+
} else {
1626+
// we have our own clock.
1627+
clock += elapsedTime * g.globals.speed;
1628+
eyeClock += elapsedTime * g.globals.eyeSpeed;
1629+
}
1630+
1631+
if (g.options.lightRays.enabled) {
1632+
for (var ii = 0; ii < g_lightRayInfo.length; ++ii) {
1633+
var info = g_lightRayInfo[ii];
1634+
info.timer -= elapsedTime * g.globals.speed;
1635+
if (info.timer < 0) {
1636+
initLightRay(info);
1637+
}
1638+
}
1639+
}
1640+
1641+
if (g.options.bubbles.enabled) {
1642+
bubbleTimer -= elapsedTime * g.globals.speed;
1643+
if (bubbleTimer < 0) {
1644+
bubbleTimer = 2 + Math.random() * 8;
1645+
var radius = Math.random() * 50;
1646+
var angle = Math.random() * Math.PI * 2;
1647+
fast.matrix4.translation(
1648+
world,
1649+
[Math.sin(angle) * radius,
1650+
0,
1651+
Math.cos(angle) * radius]);
1652+
g_bubbleSets[bubbleIndex].trigger(world);
1653+
++bubbleIndex;
1654+
bubbleIndex = bubbleIndex % g_numBubbleSets;
1655+
}
1656+
}
1657+
16651658
frameCount++;
16661659

16671660
g_fpsTimer.update(elapsedTime);
@@ -1715,22 +1708,22 @@ function initialize() {
17151708
}
17161709

17171710
gl.viewport(0, 0, canvas.width * 0.5, canvas.height);
1718-
render(elapsedTime, g_frameData.leftProjectionMatrix, g_frameData.pose);
1711+
render(g_frameData.leftProjectionMatrix, g_frameData.pose);
17191712

17201713
gl.viewport(canvas.width * 0.5, 0, canvas.width * 0.5, canvas.height);
1721-
render(elapsedTime, g_frameData.rightProjectionMatrix, g_frameData.pose);
1714+
render(g_frameData.rightProjectionMatrix, g_frameData.pose);
17221715

17231716
g_vrDisplay.submitFrame();
17241717
} else {
17251718
gl.viewport(0, 0, canvas.width, canvas.height);
1726-
render(elapsedTime);
1719+
render();
17271720
}
17281721
} else {
17291722
if (!g_drawOnce) {
17301723
g_requestId = tdl.webgl.requestAnimationFrame(onAnimationFrame, canvas);
17311724
}
17321725
gl.viewport(0, 0, canvas.width, canvas.height);
1733-
render(elapsedTime);
1726+
render();
17341727
}
17351728

17361729
// Set the alpha to 255.
@@ -1739,7 +1732,6 @@ function initialize() {
17391732
gl.clear(gl.COLOR_BUFFER_BIT);
17401733
}
17411734

1742-
//render();
17431735
onAnimationFrame();
17441736
return true;
17451737
}

0 commit comments

Comments
 (0)