-
Notifications
You must be signed in to change notification settings - Fork 5
/
inside.html
executable file
·261 lines (201 loc) · 5.92 KB
/
inside.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>In a bunch of cubes, using three.js</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000;
color: #fff;
margin: 0px;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
</body>
<!--
three.js 3d library
-->
<script src="js/lib/three.min.js"></script>
<!--rotating quaternions library-->
<script src="js/lib/gl-matrix.js"></script>
<!--
VRControls.js acquires positional information from connected VR devices and applies the transformations to a three.js camera object.
-->
<script src="js/vr/PhoneVR.js"></script>
<script src="js/vr/VRControls.js"></script>
<!--
VREffect.js handles stereo camera setup and rendering.
-->
<script src="js/vr/VREffect.js"></script>
<script>
/*
Setup three.js WebGL renderer
*/
var renderer = new THREE.WebGLRenderer( { antialias: true } );
/*
Append the canvas element created by the renderer to document body element.
*/
document.body.appendChild( renderer.domElement );
/*
Create a three.js scene
*/
var scene = new THREE.Scene();
/*
Create a three.js camera
*/
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
/*
Apply VR headset positional data to camera.
*/
var controls = new THREE.VRControls( camera );
/*
Apply VR stereo rendering to renderer
*/
var effect = new THREE.VREffect( renderer );
effect.setSize( window.innerWidth, window.innerHeight );
/*
define interaction variables
*/
var mouseY = 1;
var mouseX = 1;
var clicky = 0;
/*
Create 3d objects
*/
var geometrycube = new THREE.BoxGeometry( 100, 100, 100 );
var material1 = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
var material2 = new THREE.MeshBasicMaterial( { color: 0xffff00, wireframe: true } );
var material3 = new THREE.MeshBasicMaterial( { color: 0x0000ff, wireframe: true } );
var material4 = new THREE.MeshBasicMaterial( { color: 0x00ffff, wireframe: true } );
var material5 = new THREE.MeshBasicMaterial( { color: 0x006600, wireframe: true } );
var materialX = material1;
//create spin factor
var spinX = 1;
var spinY = 1;
//create spinnycube
var cube = new THREE.Mesh( geometrycube, materialX );
var cube2 = new THREE.Mesh( geometrycube, materialX );
var cube3 = new THREE.Mesh( geometrycube, materialX );
var cube4 = new THREE.Mesh( geometrycube, materialX );
var cube5 = new THREE.Mesh( geometrycube, materialX );
cube2.position.x = 0;
cube3.position.x = 0;
cube4.position.x = 0;
cube5.position.x = 0;
scene.add( cube2 );
scene.add( cube3 );
scene.add( cube4 );
scene.add( cube5 );
/*
Request animation frame loop function
*/
function animate() {
/*
Apply rotation to cube mesh dependent on mouse position
*/
spinX = ((mouseX-200)/800);
spinY = ((mouseY-200)/400);
//spinnycube:
cube2.rotation.x += 0.005*spinX;
cube2.rotation.z += 0.007*spinY;
cube3.rotation.x += 0.006*spinX;
cube3.rotation.z += 0.008*spinY;
cube4.rotation.x += 0.007*spinX;
cube4.rotation.z += 0.009*spinY;
cube5.rotation.x += 0.008*spinX;
cube5.rotation.z += 0.01*spinY;
//change color on click:
if(clicky==1){
cube2.material = material1;
cube3.material = material1;
cube4.material = material1;
cube5.material = material1;
}else{
cube2.material = material2;
cube3.material = material3;
cube4.material = material4;
cube5.material = material5;
}
/*
Update VR headset position and apply to camera.
*/
controls.update();
/*
Render the scene through the VREffect.
*/
effect.render( scene, camera );
requestAnimationFrame( animate );
}
/*
Kick off animation loop
*/
animate();
//listen for mouse movement to get mouseX and mouseY
document.body.addEventListener( 'mousemove', function (event) {
mouseY = event.clientY;
mouseX = event.clientX;
});
//listen for click
document.body.addEventListener( 'click', function(){
clicky = !clicky;
})
/*
Listen for click event to enter full-screen VR mode
*/
document.body.addEventListener( 'click', function() {
effect.setFullScreen( true );
});
/*
Listen for keyboard event and zero positional sensor on appropriate keypress.
*/
function onkey(event) {
event.preventDefault();
if (event.keyCode == 90) { // z
controls.zeroSensor();
}
};
//linking function
function link(){
window.location="http://vihart.github.io/webVR-playing-with/spindex.html";
}
/*
Listen for keyboard events zero positional sensor on appropriate keypress.
*/
function onkey(event) {
event.preventDefault();
if (event.keyCode == 90) { // z
controls.zeroSensor(); //zero rotation
} else if (event.keyCode == 70 || event.keyCode == 13) { //f or enter
effect.setFullScreen(true) //fullscreen
} else if (event.keyCode == 32) {//space
link()
}
};
window.addEventListener("keydown", onkey, true);
//hold down keys to do rotations and stuff
function key(event, sign) {
var control = controls.manualControls[String.fromCharCode(event.keyCode).toLowerCase()];
if (!control)
return;
if (sign === 1 && control.active || sign === -1 && !control.active)
return;
control.active = (sign === 1);
controls.manualRotateRate[control.index] += sign * control.sign;
}
document.addEventListener('keydown', function(event) { key(event, 1); }, false);
document.addEventListener('keyup', function(event) { key(event, -1); }, false);
/*
Handle window resizes
*/
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
effect.setSize( window.innerWidth, window.innerHeight );
}
window.addEventListener( 'resize', onWindowResize, false );
</script>
</html>