Skip to content

Commit 7e2b32e

Browse files
authored
Merge pull request #11 from shaoboyan/master
Enable "change view" feature for auqarium vr mode.
2 parents d267da2 + c4f405f commit 7e2b32e

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

aquarium-vr/aquarium-vr.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@
128128
<div class="clickable" id="setSetting6">1000</div>
129129
<div class="clickable" id="setSetting7">2000</div>
130130
<div class="clickable" id="setSetting8">4000</div>
131-
<!--
132131
<div class="clickable" id="setSetting9">Change View</div>
133-
-->
134132
<div class="clickable" id="setSetting10">Advanced</div>
135133
<div class="clickable" id="options">Options...
136134
<div id="optionsContainer">

aquarium-vr/aquarium-vr.js

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,35 @@ function initialize() {
11031103
setCanvasSize(canvas, g.globals.width, g.globals.height);
11041104
}
11051105

1106-
function render(elapsedTime, projectionMatrix, viewMatrix) {
1106+
function calculateViewMatrix(viewMatrix, q, v) {
1107+
// According to webvr 1.1 spec, orientation is a quaternion.
1108+
// 1. normalize orientation quaternion.
1109+
var normFactor = Math.sqrt(Math.pow(q[0], 2) + Math.pow(q[1], 2) + Math.pow(q[2], 2) + Math.pow(q[3], 2));
1110+
var b = q[0] / normFactor;
1111+
var c = q[1] / normFactor;
1112+
var d = q[2] / normFactor;
1113+
var a = q[3] / normFactor;
1114+
1115+
//2. calculate rotation matrix and combine transform vector to generate view matrix.
1116+
viewMatrix[0] = Math.pow(a, 2) + Math.pow(b, 2) - Math.pow(c, 2) - Math.pow(d, 2);
1117+
viewMatrix[1] = 2 * b * c + 2 * a * d;
1118+
viewMatrix[2] = 2 * b * d - 2 * a * c;
1119+
viewMatrix[3] = 0;
1120+
viewMatrix[4] = 2 * b * c - 2 * a * d;
1121+
viewMatrix[5] = Math.pow(a, 2) - Math.pow(b, 2) + Math.pow(c, 2) - Math.pow(d, 2);
1122+
viewMatrix[6] = 2 * c * d + 2 * a * b;
1123+
viewMatrix[7] = 0;
1124+
viewMatrix[8] = 2 * b * d + 2 * a * c;
1125+
viewMatrix[9] = 2 * c * d - 2 * a * b;
1126+
viewMatrix[10] = Math.pow(a, 2) - Math.pow(b, 2) - Math.pow(c, 2) + Math.pow(d, 2);
1127+
viewMatrix[11] = 0;
1128+
viewMatrix[12] = v[0];
1129+
viewMatrix[13] = v[1];
1130+
viewMatrix[14] = v[2];
1131+
viewMatrix[15] = 1;
1132+
}
1133+
1134+
function render(elapsedTime, projectionMatrix, pose) {
11071135
/*
11081136
var now = theClock.getTime();
11091137
var elapsedTime;
@@ -1151,12 +1179,6 @@ function initialize() {
11511179
clock += elapsedTime * g.globals.speed;
11521180
eyeClock += elapsedTime * g.globals.eyeSpeed;
11531181
}
1154-
eyePosition[0] = Math.sin(eyeClock) * g.globals.eyeRadius;
1155-
eyePosition[1] = g.globals.eyeHeight;
1156-
eyePosition[2] = Math.cos(eyeClock) * g.globals.eyeRadius;
1157-
target[0] = Math.sin(eyeClock + Math.PI) * g.globals.targetRadius;
1158-
target[1] = g.globals.targetHeight;
1159-
target[2] = Math.cos(eyeClock + Math.PI) * g.globals.targetRadius;
11601182

11611183
ambient[0] = g.globals.ambientRed;
11621184
ambient[1] = g.globals.ambientGreen;
@@ -1179,9 +1201,15 @@ function initialize() {
11791201
var height = Math.abs(top - bottom);
11801202
var xOff = width * g.net.offset[0] * g.net.offsetMult;
11811203
var yOff = height * g.net.offset[1] * g.net.offsetMult;
1182-
if (g_vrDisplay && g_vrDisplay.isPresenting) {
1204+
if (g_vrDisplay && g_vrDisplay.isPresenting && pose.position) {
1205+
// Using head-neck model in VR mode due to unclear distance measurement(vr return position using meters),
1206+
// user could see around but couldn't move around.
1207+
eyePosition[0] = g.globals.eyeRadius;
1208+
eyePosition[1] = g.globals.eyeHeight;
1209+
eyePosition[2] = g.globals.eyeRadius;
1210+
11831211
fast.matrix4.copy(projection, projectionMatrix);
1184-
fast.matrix4.inverse(viewInverse, viewMatrix);
1212+
calculateViewMatrix(viewInverse, pose.orientation, eyePosition);
11851213
} else {
11861214
fast.matrix4.frustum(
11871215
projection,
@@ -1192,6 +1220,13 @@ function initialize() {
11921220
near,
11931221
far);
11941222

1223+
eyePosition[0] = Math.sin(eyeClock) * g.globals.eyeRadius;
1224+
eyePosition[1] = g.globals.eyeHeight;
1225+
eyePosition[2] = Math.cos(eyeClock) * g.globals.eyeRadius;
1226+
target[0] = Math.sin(eyeClock + Math.PI) * g.globals.targetRadius;
1227+
target[1] = g.globals.targetHeight;
1228+
target[2] = Math.cos(eyeClock + Math.PI) * g.globals.targetRadius;
1229+
11951230
fast.matrix4.cameraLookAt(
11961231
viewInverse,
11971232
eyePosition,
@@ -1634,10 +1669,10 @@ function initialize() {
16341669
g_vrDisplay.getFrameData(g_frameData);
16351670
if (g_vrDisplay.isPresenting) {
16361671
gl.viewport(0, 0, canvas.width * 0.5, canvas.height);
1637-
render(elapsedTime, g_frameData.leftProjectionMatrix, g_frameData.leftViewMatrix);
1672+
render(elapsedTime, g_frameData.leftProjectionMatrix, g_frameData.pose);
16381673

16391674
gl.viewport(canvas.width * 0.5, 0, canvas.width * 0.5, canvas.height);
1640-
render(elapsedTime, g_frameData.rightProjectionMatrix, g_frameData.rightViewMatrix);
1675+
render(elapsedTime, g_frameData.rightProjectionMatrix, g_frameData.pose);
16411676

16421677
g_vrDisplay.submitFrame();
16431678
} else {

0 commit comments

Comments
 (0)