-
Notifications
You must be signed in to change notification settings - Fork 19
/
view.js
611 lines (560 loc) · 20.7 KB
/
view.js
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// three.js global variables
var logElement;
var scene;
var renderer;
var camera;
var controls;
var loadedMeshes = [];
var loadedLights = [];
var loadedCameras = [];
var gridLines;
var light;
var lightSphere;
var lastTimestamp;
var timers = {};
var imageCache = {};
var modelRadius = 1;
var lightTime = 0;
var keyframesPerSecond = 10;
var keyframeMin = 0;
var keyframeMax = 999;
var statisticsElement;
var contentNode;
var useLights = false;
var useCamera = false;
var animations = [];
var messages = [];
var msgFilter = {};
// implementation
function initApplication() {
// Drag and drop does not work without this
jQuery.event.props.push('dataTransfer');
// jQuery UI Slider
$( "#kps" ).slider({max:100, min:0.1, value:10, change:onKpsChange, slide:onKpsChange});
$( "#frameRange" ).slider({range: true, min: 0, max: 5000, values: [ 0, 5000 ], slide: onFrameRangeChange});
// Events
$( "#view_container" ).on("drop", onMeshDrop);
$( "#view_container" ).on("dragover", onDragOver);
$( "#images" ).on("drop", onImageDrop);
$( "#images" ).on("dragover", onDragOver);
$( "#use_lights" ).change( onUseCameraAndLightsChange );
$( "#use_camera" ).change( onUseCameraAndLightsChange );
$( "#filterErrors" ).click( onMessageFilterClicked );
$( "#filterWarnings" ).click( onMessageFilterClicked );
$( "#filterInfo" ).click( onMessageFilterClicked );
$( "#filterTrace" ).click( onMessageFilterClicked );
$( "#clearLog" ).click( clearMessageLog );
$( "#load" ).click( loadFile );
$( "#inputfile" ).change( readFile );
// Pop-overs
var popover = {
trigger:"hover",
placement:"left",
delay: { show: 800, hide: 0 }
};
popover.title = "Load animations";
popover.content = "If checked, animated meshes will be loaded. Otherwise, a static version of animated meshes will be loaded. Does not affect already loaded meshes.";
$('#load_animations_label').popover(popover);
popover.title = "Skins as morphs";
popover.content = "If checked, all skin animated meshes will be converted to morph animated meshes upon loading. Does not affect already loaded meshes.";
$('#skin_to_morph_label').popover(popover);
popover.title = "Use loaded lights";
popover.content = "If checked, lights from the collada file will be used for rendering. Otherwise, a static light setup with a moving point light will be used. Can be toggled at any time.";
$('#use_lights_label').popover(popover);
popover.title = "Use loaded camera";
popover.content = "If checked, the first camera from the collada file will be used for rendering. Otherwise, a user-controlled, interactive camera will be used. Can be toggled at any time. Has no effect if there is no camera in the collada file.";
$('#use_camera_label').popover(popover);
// Misc
statisticsElement = document.getElementById( 'statistics' );
updateMessageFilter();
initCanvas();
animateCanvas(Date.now());
}
function logMessage(type, msg) {
messages.push({type:type, desc:msg});
console.log(msg);
addMessageToLog(type, msg);
}
function addMessageToLog(type, msg) {
if (msgFilter[type]) {
// Escape HTML characters
html = msg.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
html = '<tr><td>' + type + '</td><td>' + html + '</td></tr>'
$('#log > tbody:last').append(html);
}
}
function rebuildLog() {
$('#log tbody > tr').remove();
for(var i=0;i<messages.length;++i) {
msg = messages[i];
addMessageToLog(msg.type, msg.desc);
}
}
function clearMessageLog() {
messages = [];
rebuildLog();
}
function onMessageFilterClicked() {
$(this).toggleClass("active");
updateMessageFilter();
}
function updateMessageFilter() {
msgFilter["ERROR"] = $( "#filterErrors" ).hasClass("active");
msgFilter["WARNING"] = $( "#filterWarnings" ).hasClass("active");
msgFilter["INFO"] = $( "#filterInfo" ).hasClass("active");
msgFilter["TRACE"] = $( "#filterTrace" ).hasClass("active");
rebuildLog();
}
function logActionStart(action) {
logMessage("TRACE", action + " started.");
timers[action] = Date.now();
}
function logActionEnd(action) {
var start = timers[action];
var duration = Date.now() - start;
logMessage("TRACE", action + " finished (" + duration + "ms).");
}
function onKpsChange(ev, ui) {
keyframesPerSecond = parseFloat(ui.value);
$( "#kpsLabel" ).text( '' + keyframesPerSecond.toPrecision(3) + ' keyframes per second' );
}
function onFrameRangeChange(ev, ui) {
keyframeMin = parseInt(ui.values[0], 10);
keyframeMax = parseInt(ui.values[1], 10);
$( "#frameRangeLabel" ).text( 'Keyframe playback range: ' + keyframeMin + ' - ' + keyframeMax + '' );
}
function onUseCameraAndLightsChange(ev) {
useLights = $( "#use_lights" ).is(":checked");
useCamera = $( "#use_camera" ).is(":checked");
light.visible = !useLights;
lightSphere.visible = !useLights;
for(var i=0; i<loadedLights.length; i++){
loadedLights[i].visible = useLights;
}
}
function onDragOver(ev) {
//ev.stopPropagation();
ev.preventDefault();
//ev.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
function onMeshDrop(ev) {
setModels();
ev.preventDefault();
var dt = ev.dataTransfer;
var files = dt.files;
if (files.length == 0) {
logMessage("ERROR", "You did not drop a file. Try dragging and dropping a file instead.");
return;
}
if (files.length > 1) {
logMessage("ERROR", "You dropped multiple files. Please only drop a single file.");
return;
}
var file = files[0];
var reader = new FileReader();
reader.onload = onFileLoaded;
reader.onerror = onFileError;
logActionStart("File reading");
reader.readAsText(file);
}
function loadFile(ev) {
$("#inputfile").click();
}
function readFile(ev) {
var files = ev.target.files;
var file = files[0];
var reader = new FileReader();
reader.onload = onFileLoaded;
reader.onerror = onFileError;
logActionStart("File reading");
reader.readAsText(file);
}
function onImageDrop(ev) {
ev.preventDefault();
var dt = ev.dataTransfer;
var files = dt.files;
for(var i=0; i<files.length; ++i) {
loadImage(files[i]);
}
}
function loadImage(file) {
var reader = new FileReader();
reader.onload = function(ev) { onImageFileRead(reader, file); }
reader.onerror = onFileError;
reader.readAsDataURL(file);
}
function loadCOLLADAFile(data, loader) {
var parser = new DOMParser();
logActionStart("XML parsing");
var xmlDoc = parser.parseFromString(data,"text/xml");
logActionEnd("XML parsing");
logActionStart("COLLADA parsing");
loader.parse( xmlDoc, function ( collada ) {
logActionEnd("COLLADA parsing");
console.log(collada);
var enableDocumentInfoOutput = false
if (enableDocumentInfoOutput && collada.getInfo) {
console.log(collada.getInfo(0,""));
}
setModels(collada.scene);
console.profileEnd();
parseProfiles();
}, document.URL);
}
function loadJSONFile(data, loader) {
logActionStart("JSON parsing");
var json = JSON.parse( data );
logActionEnd("JSON parsing");
logActionStart("JSON model parsing");
result = loader.parse( json, "./");
logActionEnd("JSON model parsing");
logActionStart("JSON mesh creation");
var geometry = result.geometry;
var materials = result.materials;
var material = null;
var mesh = null;
if (materials.length > 1) {
material = new THREE.MeshFaceMaterial( materials );
} else if (materials.length > 0) {
material = materials[0];
} else {
material = new THREE.MeshPhongMaterial();
}
if (geometry.skinIndices && geometry.skinIndices.length > 0) {
mesh = new THREE.SkinnedMesh( geometry, material );
for(var i=0; i<materials.length; ++i) {
materials[i].skinning = true;
}
} else {
mesh = new THREE.Mesh( geometry, material );
}
logActionEnd("JSON mesh creation");
setModels(mesh);
}
function onFileLoaded(ev) {
console.profile("onFileLoaded");
var data = this.result;
logActionEnd("File reading");
var selectElement = document.getElementById("loader_type");
switch (selectElement.selectedIndex) {
case 0:
var loader = new THREE.ColladaLoader();
loadCOLLADAFile(data, loader);
break;
case 1:
var loader = new ColladaLoader2();
loader.options.localImageMode = true;
loader.options.verboseMessages = true;
loader.options.convertSkinsToMorphs = document.getElementById( 'skin_to_morph' ).checked;
loader.options.useAnimations = document.getElementById( 'load_animations' ).checked;
loader.addCachedTextures(imageCache)
loader.setLog(function(msg, type) {logMessage(ColladaLoader2.messageTypes[type], msg); } );
loadCOLLADAFile(data, loader);
break;
case 2:
var loader = new THREE.JSONLoader();
loadJSONFile(data, loader);
break;
default:
throw new Error("Unknown loader type");
}
}
function parseProfiles(node, depth) {
if (!console.profiles) {
return;
}
if (depth > 10) {
return;
}
if (node===undefined) {
var profile = console.profiles[0];
if (profile != undefined) {
var head = profile.head;
if (head != undefined) {
profileData = [];
parseProfiles(head, 0);
var profileDataStr = profileData.join("\n");
//document.getElementById( 'profile' ).value = profileDataStr;
}
}
return;
}
var functionName = node.functionName;
for(var i=functionName.length;i<40;++i) functionName += " ";
profileData.push(functionName + "\t" + node.selfTime.toFixed(2) + "\t" + node.totalTime.toFixed(2));
var children = node.children();
for(var i in children) {
parseProfiles(children[i], depth+1);
}
}
function onImageFileRead(reader, file) {
var dataURI = reader.result;
var image = new Image;
image.name = file.name;
image.onload = function () { onImageLoaded(image, file.name); };
image.src = dataURI
}
function onImageLoaded(image, name) {
var texture = new THREE.Texture( image, new THREE.UVMapping() );
texture.flipY = false;
// HACK: Set the repeat mode to repeat
texture.wrapS = THREE.RepeatWrapping
texture.wrapT = THREE.RepeatWrapping
texture.needsUpdate = true;
imageCache[name] = texture;
imagesLog = document.getElementById( 'images' );
imagesLog.value += name;
imagesLog.value += "\n";
}
function onFileError(ev) {
logMessage("ERROR", "Can not read the file. Most likely, reading of files is disabled in your browser for security reasons. Error code: " + this.error.code);
}
function initCanvas() {
logActionStart("WebGL initialization");
var webglInitStarted = Date.now();
var container = document.getElementById( 'view_container' );
// Scene
scene = new THREE.Scene();
// Camera
camera = new THREE.PerspectiveCamera( 20, container.clientWidth / container.clientHeight, 0.1, 10000 );
camera.up.set( 0, 0, 1 );
camera.position.set( -7, 3, 5 );
camera.lookAt( new THREE.Vector3( 0, 0, 0 ) );
scene.add( camera );
// Controller
controls = new THREE.TrackballControls( camera, container );
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.keys = [ 65, 83, 68 ];
controls.screen = {width: container.offsetWidth, height: container.offsetHeight, offsetLeft: container.offsetLeft, offsetTop: container.offsetTop };
// Light
scene.add( new THREE.AmbientLight( 0x606060 ) );
light = new THREE.PointLight( 0xeeeeee );
light.position.set(3,2,3);
scene.add( light );
lightSphere = new THREE.Mesh( new THREE.SphereGeometry(0.1, 16, 16), new THREE.MeshBasicMaterial({color:0xffffff, ambient:0xffffff}));
lightSphere.position.set(3,2,3);
scene.add(lightSphere);
// Grid
var material = new THREE.LineBasicMaterial( { color: 0xcccccc, opacity: 0.2 } );
var geometry = new THREE.Geometry();
var floor = -0.04, step = 1, size = 10;
for ( var i = 0; i <= size / step * 2; i ++ ) {
geometry.vertices.push( new THREE.Vector3( - size, i * step - size, floor ) );
geometry.vertices.push( new THREE.Vector3( size, i * step - size, floor ) );
geometry.vertices.push( new THREE.Vector3( i * step - size, -size, floor ) );
geometry.vertices.push( new THREE.Vector3( i * step - size, size, floor ) );
}
gridLines = new THREE.Line( geometry, material, THREE.LinePieces );
gridLines.scale.x = 0.2
gridLines.scale.y = 0.2
gridLines.scale.z = 0.2
scene.add( gridLines );
// Content root node
contentNode = new THREE.Object3D;
scene.add( contentNode );
// Renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( container.clientWidth, container.clientHeight );
container.appendChild( renderer.domElement );
logActionEnd("WebGL initialization");
}
function enforceRange(value, minVal, maxVal) {
if (value < minVal) {
return minVal;
}
else if (minVal >= maxVal) {
return minVal;
}
else if (value > maxVal) {
range = maxVal - minVal
while (value > maxVal) {
value -= range;
}
return value;
}
else return value;
}
function updateAnimation(timestamp) {
// Get the elapsed time
if (!lastTimestamp) {
lastTimestamp = timestamp;
}
var frameTime = ( timestamp - lastTimestamp ) * 0.001; // seconds
// Animate all meshes
for(var m=0; m<loadedMeshes.length; m++)
{
model = loadedMeshes[m];
if (model && model.morphTargetInfluences)
{
model.animstate.progress += frameTime * keyframesPerSecond;
var morphTargets = model.morphTargetInfluences.length;
var maxProgress = Math.min(morphTargets, keyframeMax);
model.animstate.progress = enforceRange(model.animstate.progress, keyframeMin, maxProgress);
for ( var i = 0; i < morphTargets; i++ ) {
model.morphTargetInfluences[ i ] = 0;
}
progress_l = Math.floor( model.animstate.progress );
progress_h = progress_l + 1;
progress_f = model.animstate.progress - progress_l;
model.morphTargetInfluences[ progress_l % morphTargets] = 1 - progress_f;
model.morphTargetInfluences[ progress_h % morphTargets] = progress_f;
}
else if (model && model.geometry.animation && model.geometry.animation.length > 0)
{
model.animstate.animation.currentTime += frameTime * keyframesPerSecond;
var maxProgress = Math.min(model.geometry.animation.length, keyframeMax);
model.animstate.animation.update(0);
// cannot enforce the range for skinned meshes due to the implementaiton of animation.update()
// if you mess with the animation.currentTime member, the playback will get corrupted and output wrong values
// model.animstate.animation.currentTime = enforceRange(model.animstate.animation.currentTime, keyframeMin, maxProgress);
}
}
// Animate the light
if (modelRadius > 0)
{
lightTime += frameTime;
var x0 = -1.0*modelRadius;
var y0 = 1.0*modelRadius;
var z0 = 1.0*modelRadius;
var t = lightTime*6.2831;
var th = t / 6.0;
var tv = t / 15.43;
var x = Math.cos(th)*x0 + Math.sin(th)*y0;
var y = -Math.sin(th)*x0 + Math.cos(th)*y0;
var z = (2.00 + 0.0*Math.sin(tv))*z0;
light.position.set(x,y,z);
lightSphere.position.set(x,y,z);
}
lastTimestamp = timestamp;
}
function appendMeshStatistics(mesh, index) {
var vertexCount = mesh.geometry.vertices.length;
var faceCount = mesh.geometry.faces.length;
statisticsElement.value += "Model #" + index + "\n"
statisticsElement.value += "=================\n"
if (mesh instanceof THREE.MorphAnimMesh) {
statisticsElement.value += "Class: MorphAnimMesh\n"
} else if (mesh instanceof THREE.SkinnedMesh) {
statisticsElement.value += "Class: SkinnedMesh\n"
} else if (mesh instanceof THREE.Mesh) {
statisticsElement.value += "Class: Mesh\n"
} else {
statisticsElement.value += "Class: Unknown\n"
}
if (!mesh.geometry.boundingSphere) {
mesh.geometry.computeBoundingSphere();
}
statisticsElement.value += "Radius: "+mesh.geometry.boundingSphere.radius.toFixed(3)+"\n";
statisticsElement.value += "Vertices: "+vertexCount+"\n";
statisticsElement.value += "Faces: "+faceCount+"\n";
statisticsElement.value += "\n";
var morph = mesh.geometry.morphTargets;
var bones = mesh.geometry.bones;
var anims = mesh.geometry.animation;
if (morph && morph.length > 0) {
statisticsElement.value += "Morph target animation\n";
statisticsElement.value += "Keyframes: "+morph.length+"\n";
statisticsElement.value += "\n";
}
if (bones && bones.length > 0 || anims && anims.length > 0) {
statisticsElement.value += "Skinning animation\n";
statisticsElement.value += "Bones: "+(bones?bones.length:"none")+"\n";
statisticsElement.value += "Animation: "+(anims?"yes":"no")+"\n";
statisticsElement.value += "\n";
}
}
function appendLightStatistics(light, index) {
statisticsElement.value += "Light #" + index + "\n"
statisticsElement.value += "=================\n"
statisticsElement.value += "\n";
}
function appendCameraStatistics(light, index) {
statisticsElement.value += "Camera #" + index + "\n"
statisticsElement.value += "=================\n"
statisticsElement.value += "\n";
}
function setModels(loadedNode) {
// Clear the scene
for(var i=0;i<contentNode.children.length;i++) {
contentNode.remove( contentNode.children[i] );
}
for(var i=0;i<animations.length;i++) {
animations[i].stop();
}
animations = [];
// Unfortunately, there is no way to remove animations from the animation handler
// THREE.AnimationHandler.removeAll();
statisticsElement.value = "";
loadedMeshes = [];
loadedLights = [];
loadedCameras = [];
if (!loadedNode) {
return;
}
// Add the new content
contentNode.add( loadedNode );
// Collect statistics about the content
var r = 0;
var i = 0;
loadedNode.traverse( function(node) {
if (node instanceof THREE.Mesh) {
var vertexCount = node.geometry.vertices.length;
var faceCount = node.geometry.faces.length;
node.animstate = {};
node.animstate.progress = 0;
loadedMeshes.push( node );
appendMeshStatistics(node, i);
node.geometry.computeBoundingSphere();
r += node.geometry.boundingSphere.radius;
}
if (node instanceof THREE.SkinnedMesh && node.geometry.animation) {
THREE.AnimationHandler.add( node.geometry.animation );
var animation = new THREE.Animation( node, node.geometry.animation.name );
animation.JITCompile = false;
animation.interpolationType = THREE.AnimationHandler.LINEAR;
animation.play();
animations.push(animation);
node.animstate.animation = animation;
}
if (node instanceof THREE.Light) {
loadedLights.push( node );
appendLightStatistics(node, i);
}
if (node instanceof THREE.Camera) {
loadedCameras.push( node );
appendCameraStatistics(node, i);
}
i++;
});
// Scale the light, camera, and grid position/size
if (r < 0.001 || loadedMeshes.length === 0) {
r = 1.0;
} else {
r /= loadedMeshes.length;
}
camera.position.set( -7.0*r, 3.0*r, 5.0*r );
camera.lookAt( new THREE.Vector3( 0, 0, 0 ) );
gridLines.scale.x = 0.2*r;
gridLines.scale.y = 0.2*r;
gridLines.scale.z = 0.2*r;
modelRadius = r;
lightTime = 0;
// Reset the animations
lastTimestamp = null;
}
function animateCanvas(timestamp) {
controls.update();
updateAnimation(timestamp);
if (loadedCameras.length > 0 && useCamera) {
renderer.render( scene, loadedCameras[ 0 ] );
} else {
renderer.render( scene, camera );
}
requestAnimationFrame( animateCanvas );
}