forked from jcarpenter/vr-experiments-july2014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-alpha-test.html
301 lines (215 loc) · 7.27 KB
/
3-alpha-test.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
<!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>Josh HUD Alpha</title>
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container"></div>
<div id="about">
<h1>C4D NitroBake Experiment</h1>
<h2><a href="mailto:jcarpenter@mozilla.com">Josh Carpenter</a>, July 2 2014</h2>
<p>Testing baking keyframes in Cinema4D using NitroBake plugin, exporting to DAE, and importing into Three.js</p>
</div>
<!-- <div id="loading">LOADING SCENE</div> -->
<div id="fullscreen">Fullscreen</div>
<img id="logo" src="images/mozilla.png" width="100" height="26" alt="Mozilla">
<script src="js/three.min.js"></script>
<script src="js/VRPlugin.js"></script>
<script src="js/loaders/ColladaLoader.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script type="text/javascript">
//scene variables
var container, scene, renderer;
var camera, cameraLeft, cameraRight
var user, hud;
var geometry, material, mesh, objects, sphere;
//vr and resize variables
var vrPlugin = new THREE.VRPlugin();
var radius = 100, theta = 0;
//collada variables
var model;
var modelURL = "models/test-transparency-export-4.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 = -1000;
//setMaterial(model, new THREE.MeshBasicMaterial({color: 0xffffff, transparent: true, opacity: 0.5 }))
init();
start();
animate(lastTimestamp);
} );
}
var setMaterial = function(node, material) {
node.material = material;
if (node.children) {
for (var i = 0; i < node.children.length; i++) {
setMaterial(node.children[i], material);
}
}
}
function init() {
//setup event handlers
var fullScreenButton = document.getElementById("fullscreen");;
fullScreenButton.onclick = function() {
vrPlugin.enableVRMode(true);
};
window.addEventListener("keypress", onkey, true);
window.addEventListener( 'resize', onWindowResize, false );
//setup container, renderer, scene
container = document.getElementById("container");
renderer = new THREE.WebGLRenderer({
alpha: false,
});
renderer.antialias = true;
vrPlugin.init(renderer);
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild(renderer.domElement);
scene = new THREE.Scene();
//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/bg1.png' ) } )
);
bg.material.side = THREE.DoubleSide;
scene.add(bg);
// setup KeyFrame Animations
var animHandler = THREE.AnimationHandler;
for ( var i = 0; i < kfAnimationsLength; ++i ) {
var animation = animations[ i ];
animHandler.add( animation );
var kfAnimation = new THREE.KeyFrameAnimation( animation.node, animation.name );
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)) {
vrPlugin.enableVRMode(true);
} else if (event.charCode == 'v'.charCodeAt(0)) {
vrPlugin.enableVRMode(false);
} 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 < 6 ) {
for ( var i = 0; i < kfAnimationsLength; ++i ) {
kfAnimations[ i ].update( frameTime );
}
} else if ( progress >= 6 ) {
for ( var i = 0; i < kfAnimationsLength; ++i ) {
kfAnimations[ i ].stop();
}
progress = 0;
start();
}
progress += frameTime;
lastTimestamp = timestamp;
render();
requestAnimationFrame( animate );
}
function render() {
vrPlugin.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", loadModel, false);
</script>
</body>
</html>