forked from jcarpenter/vr-experiments-july2014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
8-boilerplate.html
408 lines (313 loc) · 11.6 KB
/
8-boilerplate.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>VR Boilerplate</title>
<link href="css/styles2.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="title">
<h1 id="projectName">ARE WE VR YET?</h1>
<p><b>Loading Sequence Test</b></p>
<p><a href="https://twitter.com/joshcarpenter" target="_blank">Josh Carpenter</a></p>
<p>July 11, 2014</p>
<!-- <div id="fullscreen"><div>VR MODE</div></div> -->
<!--
<div id="blue">LOADING</div>
<div id="red">LOADING</div>
-->
</div>
<a id="logo" href="http://www.mozilla.org/en-US/"></a>
<div id="fullscreen" class="hidden">VR MODE</div>
<div id="container"></div>
<!--
<div id="about">
<h1>Disintegration Test</h1>
<h2><a href="https://twitter.com/joshcarpenter" target="_blank">Josh Carpenter</a>, July 10 2014</h2>
<p>Animation is created in <a href="http://www.maxon.net/products/cinema-4d-studio/who-should-use-it.html" target="_blank">Cinema 4D</a> using Dynamics, animations are baked with <a href="http://nitro4d.com/blog/donationware/nitrobake/" target="_blank">NitroBake</a>, the scene is exported to Collada, which is then loaded into a VR-enabled <a href="http://threejs.org/" target="_blank">Three.js</a> scene. <a href="https://twitter.com/dmarcos" target="_blank">Diego Marcos's</a> nice Three.js <a href="https://github.com/mrdoob/three.js/blob/dev/examples/webgl_effects_vr.html" target="_blank">VR Renderer Plugin</a> (available in the <a href="https://github.com/mrdoob/three.js/tree/dev" target="_blank">dev branch</a>) enables devs to implement VR support with just a few lines of JavaScript. The scene works for both VR and non-VR users. By viewing the page with <a href="http://blog.bitops.com/blog/2014/06/26/first-steps-for-vr-on-the-web/" target="_blank">Firefox Desktop Experimental VR Build 1</a> you can enter VR mode, which automatically applies stereoscopy and matrix distortion to the scene and presents in fullscreen.</p>
<div id="fullscreen">VR MODE</div>
</div>
-->
<!-- TODO
* Add Chrome support
* Add browser version detect. If VR capable, show button. If not, tell users to get browser version X.
* Add VR device detect and mode switch. If VR detected, show button. If not, tell users to
* Desktop mode is 3D also?
* VR mode is 3D & immersive
* Add desktop-mobile responsiveness
-->
<script src="js/threejs-dev/three.min.js"></script>
<script src="js/threejs-dev/effects/VREffect.js"></script>
<script src="js/threejs-dev/controls/VRControls.js"></script>
<script src="js/threejs-dev/loaders/ColladaLoader.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<!--<script src="js/tween.min.js"></script>-->
<script type="text/javascript">
//dom elements
var title = document.getElementById("title");
var projectName = document.getElementById("projectName");
var logo = document.getElementById("logo");
var fullscreen = document.getElementById("fullscreen");
//scene variables
var container, scene, renderer;
var camera, cameraLeft, cameraRight
var user, hud;
var geometry, material, mesh, objects, sphere;
//vr and resize variables
var vrEffect;
var vrControls;
var radius = 100, theta = 0;
//collada variables
var model;
var modelURL = "models/_new/ffos-1.dae"
var animations;
var kfAnimations = [ ];
var kfAnimationsLength = 0;
var loader = new THREE.ColladaLoader();
var lastTimestamp;
var progress = 0;
//in this scene, loadModel kicks things off.
//Rest of scene setup does not start until DAE has loaded.
function loadModel() {
loader.load( modelURL, function( collada ) {
model = collada.scene;
animations = collada.animations;
kfAnimationsLength = animations.length;
model.scale.x = model.scale.y = model.scale.z = 1.0;
model.position.z = 0;
//applyDoubleSide(model);
/*
setMaterial(model, new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: false,
opacity: 1,
wireframe: false
}))
*/
projectName.className = projectName.className + "stereo";
title.className = title.className + "fadeOut";
logo.className = logo.className + "fadeOut";
fullscreen.className = "fadeIn";
logo.addEventListener( "animationend", proceed, false );
function proceed() {
title.className = title.className + " hidden";
logo.className = logo.className + " hidden";
init();
start();
animate(lastTimestamp);
}
/*
//Some Tween.js stuff I'm not going to use
var tween = new TWEEN.Tween( sphere.material ).to( { opacity: 0 }, 1000 );
tween.easing(TWEEN.Easing.Cubic.In);
tween.start();
var tween = new TWEEN.Tween( { x: 50, y: 0 } )
.to( { x: 400 }, 2000 )
.easing( TWEEN.Easing.Elastic.InOut )
.onUpdate( function () {
output.innerHTML = 'x == ' + Math.round( this.x );
var transform = 'translateX(' + this.x + 'px)';
output.style.webkitTransform = transform;
output.style.transform = transform;
} )
.start();
*/
} );
}
//function, adds DoubleSide to all materials found in scene
var applyDoubleSide = function (node) {
if (node.material) {
node.material.side = THREE.DoubleSide;
}
if ( node.children ) {
for (var i = 0; i < node.children.length; i++) {
applyDoubleSide( node.children[i] );
}
}
}
var setMaterial = function(node, material) {
node.material = material;
node.material.side = THREE.DoubleSide;
if (node.children) {
for (var i = 0; i < node.children.length; i++) {
setMaterial(node.children[i], material);
}
}
}
function init() {
//setup event handlers
fullscreen.onclick = function() {
vrEffect.setFullScreen( true );
};
window.addEventListener( "keypress", onkey, true);
window.addEventListener( "resize", onWindowResize, false );
//setup container, renderer, scene
container = document.getElementById("container");
renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
});
renderer.antialias = true;
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild(renderer.domElement);
scene = new THREE.Scene();
//setup VR rendere and controls
vrEffect = new THREE.VREffect(renderer, VREffectLoaded);
vrControls = new THREE.VRControls(camera);
function VREffectLoaded(error) {
if (error) {
//fullscreen.innerHTML = error;
//fullscreen.classList.add('error');
}
}
//setup lights
/**/
var light = new THREE.DirectionalLight( 0xffffff, .8 );
light.position.set( 2, 2, 20 );
scene.add( light );
//setup floor
/*
var floor = new THREE.Mesh(
new THREE.PlaneGeometry(20,20,1,1),
new THREE.MeshBasicMaterial( {color: 0x0099ff} )
);
floor.material.side = THREE.DoubleSide;
floor.position.set(0,0,0)
floor.rotation.set(1, 0, 0);
scene.add(floor);
*/
//setup collada model
scene.add(model);
//setup inner white sphere
/*
sphere = new THREE.Mesh(
new THREE.SphereGeometry(100,32,32),
new THREE.MeshBasicMaterial( { color: 0xffffff, transparent: true, opacity: 1 } )
//new THREE.MeshBasicMaterial( {color: 0xffffff} )
);
sphere.material.side = THREE.DoubleSide;
scene.add(sphere);
*/
//setup background image sphere
var bg = new THREE.Mesh(
new THREE.SphereGeometry( 2000, 60, 40 ),
new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/bg3.jpg' ) } )
);
bg.material.side = THREE.DoubleSide;
scene.add(bg);
// setup KeyFrame Animations
for ( var i = 0; i < kfAnimationsLength; ++i ) {
var animation = animations[ i ];
THREE.AnimationHandler.add( animation );
var kfAnimation = new THREE.KeyFrameAnimation( animation );
kfAnimation.timeScale = 1;
kfAnimations.push( kfAnimation );
}
//setup user
user = new THREE.Object3D();
user.position.set(0,1.7,0)
scene.add(user)
//setup camera
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.set(0,0,0)
user.add(camera);
//setup HUD
hud = new THREE.Object3D();
hud.position.set(0,0,-100);
user.add(hud);
var map = new THREE.Mesh(
new THREE.PlaneGeometry(20,20,1,1),
new THREE.MeshBasicMaterial( {color: 0xff03f1, transparent: true, opacity: 1} )
);
map.material.side = THREE.DoubleSide;
map.position.set(-44,44,0)
//hud.add(map);
}
function onkey(event) {
if (event.charCode == 'f'.charCodeAt(0)) {
vrEffect.setFullScreen( true );
} else if (event.charCode == 'v'.charCodeAt(0)) {
vrEffect.setFullScreen( true );
} else {
return;
}
event.preventDefault();
event.stopPropagation();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
//Collada-related
var timestamp = Date.now();
var frameTime = ( timestamp - lastTimestamp ) * 0.001; // seconds
if ( progress >= 0 && progress < 9 ) {
for ( var i = 0; i < kfAnimationsLength; ++i ) {
kfAnimations[ i ].update( frameTime );
}
} else if ( progress >= 9 ) {
for ( var i = 0; i < kfAnimationsLength; ++i ) {
kfAnimations[ i ].stop();
}
progress = 0;
start();
}
progress += frameTime;
lastTimestamp = timestamp;
//TWEEN.update();
render();
requestAnimationFrame( animate );
}
function render() {
vrControls.update();
//vrEffect.render( scene, camera );
renderer.render( scene, camera );
}
//start
//starts animation loop. Is called by animate() when playhead advances to X, to restart loop.
function start() {
//start keyframes
for ( var i = 0; i < kfAnimationsLength; ++i ) {
var animation = kfAnimations[i];
for ( var h = 0, hl = animation.hierarchy.length; h < hl; h++ ) {
var keys = animation.data.hierarchy[ h ].keys;
var sids = animation.data.hierarchy[ h ].sids;
var obj = animation.hierarchy[ h ];
if ( keys.length && sids ) {
for ( var s = 0; s < sids.length; s++ ) {
var sid = sids[ s ];
var next = animation.getNextKeyWith( sid, h, 0 );
if ( next ) next.apply( sid );
}
obj.matrixAutoUpdate = false;
animation.data.hierarchy[ h ].node.updateMatrix();
obj.matrixWorldNeedsUpdate = true;
}
}
animation.loop = false;
animation.play();
lastTimestamp = Date.now();
}
}
window.addEventListener("load", makePretty, false);
function makePretty() {
projectName.className = projectName.className + "stereo";
title.className = title.className + "fadeOut";
logo.className = logo.className + "fadeOut";
fullscreen.className = "fadeIn";
logo.addEventListener( "animationend", proceed, false );
function proceed() {
title.className = title.className + " hidden";
logo.className = logo.className + " hidden";
//init();
//start();
//animate(lastTimestamp);
}
}
</script>
</body>
</html>