-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
191 lines (151 loc) · 5.27 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Escape the VRoom</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="css/styles.css" type="text/css">
</head>
<body>
<div id="container">
<button id='vr-toggle'><img class='icon' src='icons/stereoscope.svg'/></button>
</div>
<script src="js/third-party/threejs/min/three-min.js"></script>
<script src="js/third-party/threejs/StereoEffect.js"></script>
<script src="js/third-party/threejs/DeviceOrientationControls.js"></script>
<script src="js/third-party/threejs/OrbitControls.js"></script>
<script src="js/third-party/svg-injector.min.js"></script>
<script>
var camera, scene, renderer;
var effect, controls;
var element, container;
var clock = new THREE.Clock();
var loader = new THREE.TextureLoader();
var raycaster = new THREE.Raycaster(); // create once
var mouse = new THREE.Vector2(); // create once
init();
animate();
function init() {
renderer = new THREE.WebGLRenderer();
element = renderer.domElement;
container = document.getElementById('container');
container.appendChild(element);
effect = renderer; // default to non-vr mode
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(90, 1, 0.001, 700);
camera.position.set(0, 10, 0);
scene.add(camera);
var yellowCircle = addCircle();
controls = new THREE.OrbitControls(camera, element);
controls.rotateUp(Math.PI / 4);
controls.target.set(
camera.position.x + 0.1,
camera.position.y,
camera.position.z
);
controls.noZoom = true;
controls.noPan = true;
function setOrientationControls(e) {
if (!e.alpha) {
return;
}
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);
var material = new THREE.MeshBasicMaterial({
map: loader.load('textures/photos/basement2.jpg')
});
//var geometry = new THREE.PlaneGeometry(1000, 1000);
var geometry = new THREE.SphereGeometry(100, 32, 32);
var mesh = new THREE.Mesh(geometry, material);
mesh.scale.x = -1;
scene.add(mesh);
window.addEventListener('resize', resize, false);
setTimeout(resize, 1);
// set up vr toggle button
vrToggle = document.getElementById('vr-toggle');
vrToggle.addEventListener('click', toggleVr, false);
// inject svg's into dom
var mySVGsToInject = document.querySelectorAll('img.icon');
SVGInjector(mySVGsToInject);
window.addEventListener('click', function(e) {
console.log('event:',event);
mouse.x = ( event.clientX / renderer.domElement.width ) * 2 - 1;
mouse.y = - ( event.clientY / renderer.domElement.height ) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
var items = [];
for (var i = 0; i < scene.children.length; i++) {
if (scene.children[i].name) {
items.push(scene.children[i]);
}
}
console.log('childrens',scene.children);
console.log("items:",items);
var intersects = raycaster.intersectObjects(items);
console.log('intersects:',intersects);
for (var i = 0; i < intersects.length; i++) {
intersects[i].object.material.color.set(0xff0000);
}
}, false);
}
function resize() {
var width = container.offsetWidth;
var height = container.offsetHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
effect.setSize(width, height);
}
function update(dt) {
resize();
camera.updateProjectionMatrix();
controls.update(dt);
}
function render(dt) {
effect.render(scene, camera);
}
function animate(t) {
requestAnimationFrame(animate);
update(clock.getDelta());
render(clock.getDelta());
}
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 toggleVr(e) {
e.target.classList.toggle('active');
if (e.target.classList.contains('active')) {
effect = new THREE.StereoEffect(renderer);
} else {
effect = renderer;
}
}
function addCircle() {
var radius = 3,
segments = 32,
material = new THREE.LineBasicMaterial( { color: 0xffff00 } ),
geometry = new THREE.CircleGeometry( radius, segments ),
circle = new THREE.Line( geometry, material );
geometry.vertices.shift();
circle.castShadow = true;
circle.position.set(7, 9, -1.8); // back/forward, down/up, left/right
circle.rotation.set(0, 2, 0); // twist
circle.scale.set(.1, .1, .1);
circle.name = 'yellowCircle';
scene.add(circle);
}
</script>
</body>
</html>