forked from macdja38/ryke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
455 lines (402 loc) · 13.4 KB
/
index.html
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<!doctype html>
<html lang=en>
<head>
<title>Ryke Carleton Exploration tool</title>
<meta charset="UTF-8"/>
<meta name=viewport content='user-scalable=no,width=device-width,maximum-scale=1.0,minimum-scale=1.0'/>
<style>
body {
margin: 0;
overflow: hidden;
}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 50%;
top: 50%;
transform: translateY(-50%) translateX(-50%);
width: 95%; /* Full width */
height: 95%; /* Full height */
margin: 0;
padding: 0;
background-color: rgb(0, 0, 0); /* Fallback color */
background-color: rgba(0, 0, 0, 0.4); /* Black w/ opacity */
}
#myBtn {
bottom: 0;
right: 0;
position: fixed;
padding: 30px;
margin: 15px;
background: #e9e9e9 10px 10px no-repeat;
border-radius: 16px;
border: 1px solid #d9d9d9;
height: auto;
width: auto;
text-shadow: 1px 1px #fff;
opacity: 0.7;
font-size: 1em;
}
/* Modal Content/Box */
.modal-content {
top: 50%;
transform: translateY(-50%);
margin: auto; /* 15% from the top and centered */
width: 70%; /* Could be more or less, depending on screen size */
position: relative;
}
/* The Close Button */
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<script src="https://js.leapmotion.com/leap-0.6.4.js"></script>
<script src="https://threejs.org/examples/js/libs/stats.min.js"></script>
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://threejs.org/examples/js/controls/FlyControls.js"></script>
<script src="https://threejs.org/examples/js/loaders/ColladaLoader.js"></script>
<script src="https://threejs.org/examples/js/loaders/OBJLoader.js"></script>
<script src="https://threejs.org/examples/js/loaders/collada/Animation.js"></script>
<script src="https://threejs.org/examples/js/loaders/collada/AnimationHandler.js"></script>
<script src="https://threejs.org/examples/js/loaders/collada/KeyFrameAnimation.js"></script>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script type="text/javascript" src="universalCalculator.js"></script>
<script src="https://threejs.org/examples/js/effects/StereoEffect.js"></script>
<script src="https://threejs.org/examples/js/controls/DeviceOrientationControls.js"></script>
<script src="https://threejs.org/examples/js/controls/FirstPersonControls.js"></script>
<script type="text/javascript" src="universalCalculator.js"></script>
<script type="text/javascript" src="config.js"></script>
<script>
let userLocation = new universalCalculator(-50, 50, 150, 1.5 * Math.PI / 2, 0);
let popupText = "";
let id = Date.now();
let players = [];
let modes = {
LEAP: 0,
PHONE: 1,
};
let mode = modes.LEAP;
let effect, info, stats, socket, ground, renderer, scene, camera, controls, light, gandhi = false, yllwStrc = false, lastSend = Date.now();
let clock = new THREE.Clock();
class Player {
constructor(scene, id) {
this.scene = scene;
this.id = id;
this.airplane = false;
this.draw(id);
}
/**
*
* @param {number} x
* @param {number} y
* @param {number} z
* @param {number} phi
*/
setCords(x, y, z, phi) {
if (this.airplane == false) return;
this.airplane.position.x = x;
this.airplane.position.y = y;
this.airplane.position.z = z;
if (phi) {
this.airplane.rotation.z = phi + Math.PI;
}
}
/**
* removes the player from the scene
*/
destroy() {
scene.remove(this.airplane);
}
/**
* draws a player on the scene
* @param {number} id
*/
draw(id) {
// initialise loaders
var colladaLoader = new THREE.ColladaLoader();
// add university to the model
colladaLoader.load('resources/airplane.dae', function (collada) {
this.scene.add(collada.scene);
this.airplane = collada.scene;
this.airplane.name = `${id}`;
}.bind(this))
}
}
/**
* Initialises the scene
*/
function init() {
renderer = new THREE.WebGLRenderer({antialias: true, alpha: 1, clearColor: 0xffffff});
renderer.shadowMap.enabled = true;
renderer.setSize(window.innerWidth, window.innerHeight);
let element = renderer.domElement;
document.body.appendChild(element);
scene = new THREE.Scene();
effect = new THREE.StereoEffect(renderer);
document.body.style.backgroundColor = "lightBlue";
addUniversity(scene);
light = new THREE.PointLight(0xffffff);
light.position.set(-100, 200, 100);
scene.add(light);
light = new THREE.PointLight(0xffffff);
scene.add(light);
var loader = new THREE.TextureLoader();
// Create a grass field
var material = new THREE.MeshBasicMaterial({
color: "green"
});
var geometry = new THREE.PlaneGeometry(12000, 11000);
var plane = new THREE.Mesh(geometry, material);
plane.rotation.z = Math.PI / 8;
plane.rotation.x = -Math.PI / 2;
plane.position.z = -20;
plane.position.x = 65;
plane.position.y = -1;
scene.add(plane);
// load a resource
loader.load(
// resource URL
'resources/surface4.jpg',
// Function when resource is loaded
function (texture) {
// do something with the texture
var material = new THREE.MeshBasicMaterial({
map: texture
});
var geometry = new THREE.PlaneGeometry(4096, 4096);
ground = new THREE.Mesh(geometry, material);
// ground.rotation.z = Math.PI / 60;
ground.position.z = 100; // prev -77
ground.position.y = 0.75;
ground.position.x = -190; // prev -90
ground.rotation.x = -Math.PI / 2;
ground.scale.x = 0.44;
ground.scale.y = 0.43;
scene.add(ground);
},
// Function called when download progresses
function (xhr) {
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
},
// Function called when download errors
function (xhr) {
console.log('An error happened');
}
);
// var axisHelper = new THREE.AxisHelper(1000);
// scene.add(axisHelper);
// axisHelper.position.y = 14;
camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 5000);
userLocation.updateCameraLocation(updateCameraLocation);
// controls = new THREE.OrbitControls(camera, renderer.domElement);
controls = new THREE.FirstPersonControls(camera);
controls.movementSpeed = 20;
controls.lookSpeed = 0.05;
controls.lookVertical = true;
if (typeof Leap !== "undefined") {
mode = modes.LEAP;
Leap.loop({}, function (frame) {
if (mode !== modes.LEAP) return;
userLocation.processLeapFrame(frame);
});
}
function setOrientationControls(e) {
// If device orientation is not available, return.
if (!e.alpha) {
return;
}
console.log("Got device orentation controls, setting mode to phone");
mode = modes.PHONE;
// Create controls for mobile.
controls = new THREE.DeviceOrientationControls(camera, true);
controls.connect();
controls.update();
element.addEventListener('click', fullscreen, false);
window.removeEventListener('deviceorientation', setOrientationControls, true);
}
window.addEventListener('deviceorientation', setOrientationControls, true);
function render() {
userLocation.updateCameraLocation(updateCameraLocation);
requestAnimationFrame(render);
renderer.render(scene, camera);
if (Date.now() - lastSend > 100) {
lastSend = Date.now();
sendLocation();
checkSpecialLocations(camera)
}
//console.log(userLocation.x);
//console.log(userLocation.z);
}
render();
//controls.update();
if (typeof Leap !== "undefined") {
Leap.loopController.setBackground(true);
}
}
/**
* Update Camera location
* @param {number} x x coordinate
* @param {number} y y coordinate
* @param {number} z z coordinate
* @param {number} phi phi coordinate
* @param {number?} theta theta coordinate
*/
function updateCameraLocation(x, y, z, phi, theta) {
if (mode === modes.LEAP) {
camera.position.x = x;
camera.position.y = y;
camera.position.z = z;
camera.rotation.y = phi;
camera.rotation.x = /*theta ||*/ 0;
camera.rotation.z = 0;
light.position.set(x, y, z);
} else {
camera.updateProjectionMatrix();
controls.update(clock.getDelta());
}
}
/**
* initialise a websocket connection for multiplayer support
*/
function initSocket() {
socket = new io('http://ryke.xyz', {resource: 'socket'});
console.log("Started connection")
}
/**
* Send the current location to the webserver
*/
function sendLocation() {
socket.emit('new location', {
id, x: userLocation, y: userLocation.y, z: userLocation.z, phi: userLocation.phi
});
}
/**
* Checks locations configured in config.js and if within the range of one updates the context text
* @param {Camera} camera
*/
function checkSpecialLocations(camera) {
let newText = calculatePopUpText(camera);
if (newText !== popupText) {
document.getElementById("textSpan").innerHTML = newText;
popupText = newText;
}
}
/**
* Calculates the text at the current location
* @param {Camera} camera
* @returns {string}
*/
function calculatePopUpText(camera) {
let cameraPosition = camera.position;
for (let position of config.specialLocations) {
if (position.xMax > cameraPosition.x && position.xMin < cameraPosition.x
&& position.yMax > cameraPosition.y && position.yMin < cameraPosition.y
&& position.zMax > cameraPosition.z && position.zMin < cameraPosition.z) {
return position.text;
}
}
return "";
}
/**
* Update locations of other players
* @param location
*/
function onNewLocation(location) {
if (id == location.id) return;
let player = players[location.id];
if (!player) {
console.log("Created player");
player = new Player(scene, location.id);
players[location.id] = player;
}
player.setCords(location.x, location.y, location.z, location.phi)
}
/**
* Register listeners to websocket server
*/
function registerSocketListeners() {
socket.on('new location', onNewLocation);
socket.on('user left', (data) => {
console.log("got user left", data);
let player = players[data.id];
if (player) {
console.log("Unloading player ", data.id);
player.destroy();
}
})
}
/**
* Adds the university to the scene
* @param scene
*/
function addUniversity(scene) {
// initialise loaders
let colladaLoader = new THREE.ColladaLoader();
// add university to the model
colladaLoader.load('resources/CarletonUniversity5.dae', function (collada) {
collada.scene.rotation.x = -Math.PI / 2;
scene.add(collada.scene);
});
}
initSocket();
init();
registerSocketListeners();
</script>
<!--<label> sliders to adjust the ground position, can be uncommented if ground needs adjustment.
<input type="range" min="-1000" max="1000" step="1" onchange="ground.position.x = this.value">
<input type="range" min="-1000" max="1000" step="1" onchange="ground.position.y = this.value">
<input type="range" min="0" max="2" step="0.005" onchange="ground.scale.x = this.value">
<input type="range" min="0" max="2" step="0.005" onchange="ground.scale.y = this.value">
</label>-->
<div id="contextTextBox" style="bottom:50%;left:0;position: fixed;background-color: black;opacity:0.7">
<span id="textSpan" style="color:white;opacity:1;"></span>
</div>
<!-- Trigger/Open The Modal -->
<button id="myBtn">?</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<img style="width: 40%" src="resources/help1.png"/>
<img style="width: 40%" src="resources/help2.png"/>
</div>
</div>
<script type="text/javascript">
// Get the modal
let modal = document.getElementById('myModal');
// Get the button that opens the modal
let btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
let span = document.getElementsByClassName("modal")[0];
// When the user clicks on the button, open the modal
btn.onclick = function () {
modal.style.display = "block";
};
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
modal.style.display = "none";
};
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>