-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewer.js
294 lines (235 loc) · 7.19 KB
/
viewer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
(December 2015 - January 2016) - Ajay Ramesh
ajayramesh.com
@carpetfizz
//TODO:
- Done for now
useful links:
- "What is Gimbal Lock and why does it occur?" http://www.anticz.com/eularqua.htm
- "Euler (gimbal lock) explained" https://www.youtube.com/watch?v=zc8b2Jo7mno
- "Rotation of Axes" http://www.stewartcalculus.com/data/CALCULUS%20Early%20Transcendentals/upfiles/RotationofAxes.pdf
- "W3C DeviceOrientation Event Spec" http://w3c.github.io/deviceorientation/spec-source-orientation.html
- "THREE.js Pivots" http://stackoverflow.com/questions/15214582/how-do-i-rotate-some-moons-around-a-planet-with-three-js
- "Detecting if Mobile Device" http://stackoverflow.com/a/24600597/896112
resources:
- Sky Textures from: http://www.sketchuptexture.com/2013/02/panoramic-ski-360.html
- Lightsaber Sounds from: /* http://theforce.net/fanfilms/postproduction/soundfx/saberfx_fergo.asp
*/
var socket = io();
var scene,
width,
height,
camera,
renderer,
stereo,
clock,
textureLoader,
controls,
orbitControls,
container,
domElement,
hand,
enemy,
enemies,
lightsaber,
floor,
corridor,
soundDir,
started;
var Sky = require('../../assets/Sky');
var Floor = require('../../assets/Floor');
var Corridor = require('../../assets/Corridor');
var Hand = require('../../assets/Hand');
var Lightsaber = require('../../assets/Lightsaber');
var Enemy = require('../../assets/Enemy');
var Utils = require('./utils');
function init(){
started = false;
width = window.innerWidth;
height = window.innerHeight;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(90, width / height, 0.001, 20000);
renderer = new THREE.WebGLRenderer();
stereo = new THREE.StereoEffect(renderer);
clock = new THREE.Clock();
textureLoader = new THREE.TextureLoader();
renderer.setSize( window.innerWidth, window.innerHeight);
camera.lookAt(0, 0, 0);
camera.position.set(0, 15, 0);
scene.add(camera);
container = document.getElementById("container");
domElement = renderer.domElement;
orbitControls = new THREE.OrbitControls(camera, domElement);
orbitControls.target.set(
camera.position.x+0.15,
camera.position.y,
camera.position.z
);
orbitControls.noPan = true;
orbitControls.noZoom = true;
if(isMobile){
controls = new DeviceOrientationController(camera, renderer.domElement);
controls.connect();
}
$('.landing').hide();
$('.confirm-button').hide();
container.appendChild(domElement);
domElement.addEventListener('click', fullscreen, false);
setupScene();
}
function setupScene(){
enemies = []; // Keep enemies in here so we can manipulate them in update()
var sky = new Sky(textureLoader);
console.log(sky);
scene.add(sky);
floor = new Floor(textureLoader, renderer);
scene.add(floor);
corridor = new Corridor(textureLoader);
scene.add(corridor);
//Compound object from parent to child: Camera -> Hand -> Lightsaber -> Glow
hand = new Hand(camera);
lightsaber = new Lightsaber();
hand.add(lightsaber);
Utils.collidableMeshList.push(lightsaber);
/* LIGHTING */
lightAngle = new THREE.PointLight(0x999999, 1, 500);
lightAngle.position.set(0,50,0);
scene.add(lightAngle);
// AXIS
var axis = new THREE.AxisHelper(200);
//scene.add(axis);
requestAnimationFrame(animate);
}
function setupGame() {
scene.add(hand);
enemy = new Enemy();
window.addEventListener('deviceorientation', setOrientationControls, true);
// Every 1.5 seconds, spawn a new enemy at random position and set its velocity to -1, to come at the player
window.setInterval(function(){
var newEnemy = enemy.clone();
newEnemy.position.set(200, Utils.getRandomInRange(5, 20), Utils.getRandomInRange(-10, 10));
newEnemy.name = "enemy";
newEnemy.velocity = new THREE.Vector3(-1, 0, 0);
enemies.push(newEnemy);
Utils.collidableMeshList.push(newEnemy);
scene.add(newEnemy);
}, 1500);
}
function fullscreen() {
if (container.requestFullscreen) {
container.requestFullscreen();
} else if (container.msRequestFullscreen) {
container.msRequestFullscreen();
} else if (container.mozRequestFullScreen) {
container.mozRequestFullScreen();
} else if (container.webkitRequestFullscreen) {
container.webkitRequestFullscreen();
}
}
function setOrientationControls(e){
if(!e.alpha){
return;
}
controls = new THREE.DeviceOrientationControls(camera, true);
controls.connect();
controls.update();
window.removeEventListener('deviceorientation', setOrientationControls, true);
}
/* UTILS */
function setObjectQuat(object, data) {
/* Degrees to radians */
var gammaRotation = data.g ? data.g * (Math.PI / 180): 0;
var betaRotation = data.b ? data.b * (Math.PI / 180) : 0;
var alphaRotation = data.a ? data.a * (Math.PI / 180): 0;
var alpha, beta, gamma, betaMax, betaMin;
var euler = new THREE.Euler();
beta = betaRotation;
gamma = gammaRotation;
alpha = alphaRotation;
/*
- [BUG] beta jumps to 180 - theta or theta - 180
x = rcos(theta)
y = rsin(theta)
beta - Math.PI/2 because we are still dealing with device in upright position
Added 10 to both to offset the hand in front of the camera
object.position.z = Math.cos(beta - Math.PI/2) - 10;
object.position.y = 5 * Math.sin(beta - Math.PI/2) + 10;
*/
/* beta - Math.PI/2 because rotations on z-axis are made when device is in upright position
-gamma because of the way the lightsaber is facing the camera
*/
euler.set(0, -gamma, beta - Math.PI/2);
/* Using quaternions to combat gimbal lock */
object.quaternion.setFromEuler(euler);
}
/* RENDER */
function resize() {
var newWidth = window.innerWidth;
var newHeight = window.innerHeight;
camera.aspect = newWidth / newHeight;
camera.updateProjectionMatrix();
renderer.setSize(newWidth, newHeight);
stereo.setSize(newWidth, newHeight);
}
function animate(){
var elapsedSeconds = clock.getElapsedTime();
requestAnimationFrame(animate);
update(clock.getDelta());
if(isMobile){
stereo.render(scene, camera);
}else{
renderer.render(scene, camera);
}
}
function update(dt){
resize();
camera.updateProjectionMatrix();
orbitControls.update(dt);
var cameraDir = Utils.cameraLookDir(camera);
if(Math.abs(1 - cameraDir.x) < 0.01 && !started) {
setupGame();
started = true;
}
// Check collision with lightsaber and enemy at every iteration
Utils.checkCollision(lightsaber.children[0], "enemy", true, function(result){
if(result){
socket.emit('sendhit');
result.velocity = new THREE.Vector3(1, 0, 0);
}
});
// Apply velocity vector to enemy, check if they are out of bounds to remove them
for(var i=0; i<enemies.length; i++){
var e = enemies[i];
e.position.add(e.velocity);
if(e.position.x < -10 || e.position.x > 200){
scene.remove(e);
enemies.splice(i, 1);
}
}
if(isMobile) {
controls.update();
}
}
$(document).ready(function(){
$('.confirm-button').click(function(){
init();
animate();
});
});
/* SOCKET.IO */
socket.emit('viewerjoin', {room: roomId});
socket.on('beginsetup', function(data){
// change display
});
socket.on('setupcomplete', function(data){
$('.confirm-button').show();
socket.emit('viewready');
});
socket.on('updateorientation', function(data){
if(hand){
setObjectQuat(hand, data);
}
});
socket.on('updatemotion', function(data){
});