-
Notifications
You must be signed in to change notification settings - Fork 514
/
viewer.js
1538 lines (1339 loc) · 52 KB
/
viewer.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2009-2013 Turbulenz Limited
/*global TurbulenzEngine: false*/
/*global TurbulenzServices: false*/
/*global Camera: false*/
/*global CameraController: false*/
/*global Floor: false*/
/*global ResourceLoader: false*/
/*global RequestHandler: false*/
/*global TextureManager: false*/
/*global ShaderManager: false*/
/*global EffectManager: false*/
/*global AnimationManager: false*/
/*global PhysicsManager: false*/
/*global Scene: false*/
/*global DefaultRendering: false*/
/*global VMath: false*/
/*global InterpolatorController: false*/
/*global PoseController: false*/
/*global NodeTransformController: false*/
/*global SkinnedNode: false*/
/*global GPUSkinController: false*/
/*global getQueryStringDict: false*/
/*global jQuery: false*/
/*global window: false*/
TurbulenzEngine.onerror = function onerrorFn(error)
{
window.console.log(error);
};
//
// Viewer
//
function Viewer() {}
Viewer.prototype =
{
version : 1
};
// Constructor function
Viewer.create = function viewerCreateFn()
{
var errorCallback = function errorCallback(msg)
{
window.alert(msg);
};
var mathDeviceParameters = {};
var md = TurbulenzEngine.createMathDevice(mathDeviceParameters);
var graphicsDeviceParameters = { };
var gd = TurbulenzEngine.createGraphicsDevice(graphicsDeviceParameters);
var inputDeviceParameters = { };
var id = TurbulenzEngine.createInputDevice(inputDeviceParameters);
var physicsDeviceParameters = { };
var pd = TurbulenzEngine.createPhysicsDevice(physicsDeviceParameters);
var dynamicsWorldParameters = { };
var dw = pd.createDynamicsWorld(dynamicsWorldParameters);
var materials =
{
debugNormalsMaterial :
{
effect : "debug_normals"
},
debugLinesNormalsMaterial :
{
effect : "debug_lines_constant",
meta:
{
constantColor : [0, 1, 0, 1]
}
},
debugTangentsMaterial :
{
effect : "debug_tangents",
meta :
{
materialcolor : true
}
},
debugLinesTangentsMaterial :
{
effect : "debug_lines_constant",
meta:
{
constantColor : [1, 0, 0, 1]
}
},
debugBinormalsMaterial :
{
effect : "debug_binormals"
},
debugLinesBinormalsMaterial :
{
effect : "debug_lines_constant",
meta:
{
constantColor : [0, 0, 1, 1]
}
}
};
var camera = Camera.create(md);
var halfFov = Math.tan(30 * Math.PI / 180);
camera.recipViewWindowX = 1.0 / halfFov;
camera.recipViewWindowY = 1.0 / halfFov;
camera.updateProjectionMatrix();
var worldUp = md.v3BuildYAxis();
camera.lookAt(md.v3Build(0.0, 1.0, 0.0), worldUp, md.v3Build(0.0, 50.0, 200.0));
camera.updateViewMatrix();
var cameraController = CameraController.create(gd, id, camera);
var floor = Floor.create(gd, md);
var rh = RequestHandler.create({});
var tm = TextureManager.create(gd, rh, null, errorCallback);
var sm = ShaderManager.create(gd, rh, null, errorCallback);
var em = EffectManager.create(gd, md, sm, null, errorCallback);
var animationManager = AnimationManager.create(errorCallback);
var physicsManager = PhysicsManager.create(md, pd, dw);
var physicsSnapshot = null;
// Add support for pressing key 'R' to reset
var keyCodes = id.keyCodes;
var onKeyUp = function onKeyUpFn(keynum)
{
// If the key R is released we reset the positions
if (keynum === keyCodes.R)
{
physicsManager.restoreSnapshot(physicsSnapshot);
}
};
var scene = Scene.create(md);
var sceneLoaded = false;
var maxSpeed = cameraController.maxSpeed;
var objectCenter = [0, 0, 0];
var lightRadius = 1000;
var lightAngle = 0;
if (typeof window.gameSlug === 'undefined')
{
window.gameSlug = 'viewer';
}
var jQuery = window.jQuery;
var vi = new Viewer();
vi.camera = camera;
vi.cameraController = cameraController;
vi.scene = scene;
vi.pathPrefix = '/play/' + window.gameSlug + '/';
var urlMapping = null;
var urlMappingReceived = false;
var requestHandler = RequestHandler.create({});
var request = function requestFn(url, onload)
{
return rh.request({
src: ((urlMapping && urlMapping[url]) || (vi.pathPrefix + url)),
onload: onload
});
};
function viewerMappingTableReceivedFn(mappingTable)
{
var urlMapping = mappingTable.urlMapping;
sm.setPathRemapping(urlMapping, mappingTable.assetPrefix);
vi.renderer = DefaultRendering.create(gd, md, sm, em);
}
var viewerMappingSettings = {
mappingTablePrefix: "staticmax/",
assetPrefix: "missing/",
mappingTableURL: vi.pathPrefix + "mapping_table.json"
};
TurbulenzServices.createMappingTable(requestHandler, null, viewerMappingTableReceivedFn,
viewerMappingSettings);
var baseMaterialsSceneData = {
version : 1,
effects :
{
"default" :
{
type : "lambert"
}
},
materials :
{
"default" :
{
effect : "default"
}
}
};
function addAnimationExtentsToScene(scene)
{
// no extents found so far so try using the animations to calculate them
var maxValue = Number.MAX_VALUE;
var animExtentMin0 = maxValue;
var animExtentMin1 = maxValue;
var animExtentMin2 = maxValue;
var animExtentMax0 = -maxValue;
var animExtentMax1 = -maxValue;
var animExtentMax2 = -maxValue;
var animations = animationManager.getAll();
for (var a in animations)
{
if (animations.hasOwnProperty(a))
{
var anim = animations[a];
var bounds = anim.bounds;
var numFrames = anim.bounds.length;
for (var i = 0; i < numFrames; i = i + 1)
{
var bound = bounds[i];
var center = bound.center;
var halfExtent = bound.halfExtent;
var c0 = center[0];
var c1 = center[1];
var c2 = center[2];
var h0 = halfExtent[0];
var h1 = halfExtent[1];
var h2 = halfExtent[2];
var min0 = (c0 - h0);
var min1 = (c1 - h1);
var min2 = (c2 - h2);
var max0 = (c0 + h0);
var max1 = (c1 + h1);
var max2 = (c2 + h2);
animExtentMin0 = (animExtentMin0 < min0 ? animExtentMin0 : min0);
animExtentMin1 = (animExtentMin1 < min1 ? animExtentMin1 : min1);
animExtentMin2 = (animExtentMin2 < min2 ? animExtentMin2 : min2);
animExtentMax0 = (animExtentMax0 > max0 ? animExtentMax0 : max0);
animExtentMax1 = (animExtentMax1 > max1 ? animExtentMax1 : max1);
animExtentMax2 = (animExtentMax2 > max2 ? animExtentMax2 : max2);
}
}
}
// If any of the min values are less than or equal to the max values we found animations
if (animExtentMin0 <= animExtentMax0)
{
scene.extents[0] = animExtentMin0 < scene.extents[0] ? animExtentMin0 : scene.extents[0];
scene.extents[1] = animExtentMin1 < scene.extents[1] ? animExtentMin1 : scene.extents[1];
scene.extents[2] = animExtentMin2 < scene.extents[2] ? animExtentMin2 : scene.extents[2];
scene.extents[3] = animExtentMax0 > scene.extents[3] ? animExtentMax0 : scene.extents[3];
scene.extents[4] = animExtentMax1 > scene.extents[4] ? animExtentMax1 : scene.extents[4];
scene.extents[5] = animExtentMax2 > scene.extents[5] ? animExtentMax2 : scene.extents[5];
}
}
function loadSceneFinishedFn(scene)
{
// For the viewer we include the extents of all the animations to try and get a better camera setup
addAnimationExtentsToScene(scene);
scene.update();
var sceneExtents = scene.getExtents();
var sceneMinExtent = md.v3Build(sceneExtents[0], sceneExtents[1], sceneExtents[2]);
var sceneMaxExtent = md.v3Build(sceneExtents[3], sceneExtents[4], sceneExtents[5]);
var c = md.v3ScalarMul(md.v3Add(sceneMaxExtent, sceneMinExtent), 0.5);
var e = md.v3Sub(c, sceneMinExtent);
if (VMath.v3LengthSq(e) > VMath.precision)
{
camera.lookAt(c, worldUp, md.v3Build(c[0] + e[0] * 4.0,
c[1] + Math.max(e[0], e[1], e[2]) * 2.0,
c[2] + e[2] * 4.0));
camera.updateViewMatrix();
var len = VMath.v3Length(e);
if (len < 4.0)
{
camera.nearPlane = len * 0.1;
}
else
{
camera.nearPlane = 1.0;
}
camera.farPlane = Math.ceil(len) * 100.0;
camera.updateProjectionMatrix();
maxSpeed = (len < 100 ? (len * 2) : (len * 0.5));
objectCenter = c;
lightRadius = (len * 4);
}
scene.skinnedNodes = [];
var nodeHasSkeleton = animationManager.nodeHasSkeleton;
var animations = animationManager.getAll();
var sceneNodes = scene.rootNodes;
var numNodes = sceneNodes.length;
var a, anim, interp, skinnedNode, nodeController;
for (var n = 0; n < numNodes; n += 1)
{
var node = sceneNodes[n];
var skeleton = nodeHasSkeleton(node);
if (skeleton && skeleton.numNodes)
{
interp = InterpolatorController.create(skeleton);
skinnedNode = SkinnedNode.create(gd, md, node, skeleton, interp);
scene.skinnedNodes.push(skinnedNode);
skinnedNode.animController = interp;
for (a in animations)
{
if (animations.hasOwnProperty(a))
{
anim = animations[a];
if (anim.numNodes === skeleton.numNodes)
{
interp.setAnimation(anim, true);
break;
}
}
}
// Build a pose controller to allow the viewer to render bind poses
var poseController = PoseController.create(skeleton);
var numJoints = skeleton.numNodes;
var parents = skeleton.parents;
var bindPoses = skeleton.bindPoses;
var hasScale = false;
// Check if the bind pose includes any scale
var j;
for (j = 0; j < numJoints; j += 1)
{
var poseMatrix = bindPoses[j];
if (!VMath.v3Equal([md.v3LengthSq(md.m43Right(poseMatrix)),
md.v3LengthSq(md.m43Up(poseMatrix)),
md.v3LengthSq(md.m43At(poseMatrix))],
VMath.v3Build(1, 1, 1)))
{
hasScale = true;
break;
}
}
hasScale = false;
// Dependent on whether we have scale setup the output channels on the pose controller
if (hasScale)
{
poseController.setOutputChannels({rotation: true, translation: true, scale: true});
}
else
{
poseController.setOutputChannels({rotation: true, translation: true});
}
for (j = 0; j < numJoints; j += 1)
{
var joint = bindPoses[j];
var parentIndex = parents[j];
if (parentIndex !== -1)
{
var parentJoint = bindPoses[parentIndex];
var invParent = md.m43Inverse(parentJoint);
joint = md.m43Mul(joint, invParent);
}
var sx, sy, sz;
if (hasScale)
{
sx = md.v3Length(md.m43Right(joint));
sy = md.v3Length(md.m43Up(joint));
sz = md.v3Length(md.m43At(joint));
md.m43SetRight(joint, md.v3ScalarMul(md.m43Right(joint), 1 / sx));
md.m43SetUp(joint, md.v3ScalarMul(md.m43Up(joint), 1 / sy));
md.m43SetAt(joint, md.v3ScalarMul(md.m43At(joint), 1 / sz));
}
var quat = md.quatFromM43(joint);
var pos = md.v3Build(joint[9], joint[10], joint[11]);
var scale;
if (hasScale)
{
scale = md.v3Build(sx, sy, sz);
}
poseController.setJointPose(j, quat, pos, scale);
}
skinnedNode.poseController = poseController;
// If no suitable animation was found swap to poseController and drop reference to interp
if (!interp.currentAnim)
{
delete skinnedNode.animController;
skinnedNode.setInputController(skinnedNode.poseController);
}
}
}
scene.nodeControllers = [];
// If there are no skinned nodes try creating some controllers to update nodes. Note these will work
// to drive the animations even when no nodes are present
if (scene.skinnedNodes.length === 0)
{
for (a in animations)
{
if (animations.hasOwnProperty(a))
{
anim = animations[a];
interp = InterpolatorController.create(anim.hierarchy);
interp.setAnimation(anim, true);
nodeController = NodeTransformController.create(anim.hierarchy, scene);
nodeController.setInputController(interp);
scene.nodeControllers.push(nodeController);
}
}
}
var globalLights = scene.getGlobalLights();
var numGlobalLights = globalLights.length;
for (var g = 0; g < numGlobalLights; g += 1)
{
var globalLight = globalLights[g];
if (globalLight.ambient)
{
vi.renderer.setAmbientColor(globalLight.color);
break;
}
}
vi.renderer.setDefaultTexture(tm.get("default"));
if (scene.loadMaterial(gd, tm, em, "debugNormalsMaterial", materials.debugNormalsMaterial))
{
scene.getMaterial("debugNormalsMaterial").reference.add();
materials.debugNormalsMaterial.loaded = true;
}
if (scene.loadMaterial(gd, tm, em, "debugLinesNormalsMaterial", materials.debugLinesNormalsMaterial))
{
scene.getMaterial("debugLinesNormalsMaterial").reference.add();
materials.debugLinesNormalsMaterial.loaded = true;
}
if (scene.loadMaterial(gd, tm, em, "debugTangentsMaterial", materials.debugTangentsMaterial))
{
scene.getMaterial("debugTangentsMaterial").reference.add();
materials.debugTangentsMaterial.loaded = true;
}
if (scene.loadMaterial(gd, tm, em, "debugLinesTangentsMaterial", materials.debugLinesTangentsMaterial))
{
scene.getMaterial("debugLinesTangentsMaterial").reference.add();
materials.debugLinesTangentsMaterial.loaded = true;
}
if (scene.loadMaterial(gd, tm, em, "debugBinormalsMaterial", materials.debugBinormalsMaterial))
{
scene.getMaterial("debugBinormalsMaterial").reference.add();
materials.debugBinormalsMaterial.loaded = true;
}
if (scene.loadMaterial(gd, tm, em, "debugLinesBinormalsMaterial", materials.debugLinesBinormalsMaterial))
{
scene.getMaterial("debugLinesBinormalsMaterial").reference.add();
materials.debugLinesBinormalsMaterial.loaded = true;
}
// If there are physics objects then prevent them from falling
if (physicsManager.physicsNodes.length > 0)
{
// Floor is represented by a plane shape
var floorShape = pd.createPlaneShape({
normal : md.v3Build(0, 1, 0),
distance : 0,
margin : 0.001
});
var extents = scene.getExtents();
var floorObject = pd.createCollisionObject({
shape : floorShape,
transform : md.m43BuildTranslation(extents[0], Math.min(extents[1], 0), extents[2]),
friction : 0.5,
restitution : 0.3,
group: pd.FILTER_STATIC,
mask: pd.FILTER_ALL
});
// Adds the floor collision object to the world
dw.addCollisionObject(floorObject);
// Create snapshot for reset functionality
physicsSnapshot = physicsManager.createSnapshot();
id.addEventListener("keyup", onKeyUp);
jQuery('.engine-control').find('tbody')
.append(jQuery('<tr>').append(jQuery('<td>').attr('class', 'key').text('R'))
.append(jQuery('<td>').text('Reset physics objects'))
);
}
sceneLoaded = true;
jQuery(function ($j) {
$j('#loading').toggle();
});
}
vi.load = function viewerLoadFn(assetPath, urlBase, mappingTable, append, onload)
{
jQuery(function ($j) {
$j('#loading').toggle();
});
sceneLoaded = false;
if (!append)
{
scene.clear();
scene.nodeControllers = [];
scene.skinnedNodes = [];
physicsManager.clear();
physicsSnapshot = null;
dw.flush();
id.removeEventListener("keyup", onKeyUp);
animationManager = AnimationManager.create(errorCallback);
}
urlMappingReceived = false;
urlMapping = null;
var checkMapping = function checkMappingFn()
{
if (urlMappingReceived)
{
var loadSceneFinished;
if (onload)
{
loadSceneFinished = function (scene)
{
loadSceneFinishedFn(scene);
onload(scene);
};
}
else
{
loadSceneFinished = loadSceneFinishedFn;
}
var sceneReceived = function sceneReceivedFn(sceneData)
{
// If we were supplied an animation manager let that load any animations from the resolved data
animationManager.loadData(sceneData);
var imageArchiveName = assetPath + ".images.tar";
if (urlMapping[imageArchiveName])
{
tm.loadArchive(imageArchiveName);
}
var yieldFn = function sceneLoadYieldFn(callback)
{
TurbulenzEngine.setTimeout(callback, 0);
};
var baseMaterialsScene = Scene.create(md);
baseMaterialsScene.load({
data : baseMaterialsSceneData,
graphicsDevice : gd,
keepVertexData : true
});
scene.load({
data : sceneData,
graphicsDevice : gd,
mathDevice : md,
physicsDevice : pd,
textureManager : tm,
effectManager : em,
baseScene : baseMaterialsScene,
append : append,
keepLights : true,
yieldFn : yieldFn,
onload : loadSceneFinished,
physicsManager : physicsManager,
keepVertexData : true
});
};
var loadResolve = function loadResolveFn(text)
{
var sceneData = {};
if (text)
{
sceneData = JSON.parse(text);
}
var resourceLoader = ResourceLoader.create();
resourceLoader.resolve({
data : sceneData,
append : false,
requestHandler : rh,
onload : sceneReceived
});
};
request(assetPath, loadResolve);
}
else
{
TurbulenzEngine.setTimeout(checkMappingFn, 100);
}
};
if (mappingTable && mappingTable !== '')
{
var mappingReceived = function mappingReceivedFn(mappingTable)
{
tm.setPathRemapping(mappingTable.urlMapping, mappingTable.assetPrefix);
//sm.setPathRemapping(mappingTable.urlMapping, mappingTable.assetPrefix); // viewer doesn't load any more shaders
animationManager.setPathRemapping(mappingTable.urlMapping, mappingTable.assetPrefix);
urlMapping = mappingTable.urlMapping;
urlMappingReceived = true;
};
var mappingError = function mappingErrorFn()
{
window.alert("Error loading asset mapping table: " + mappingTable +
" will attempt to continue without it");
urlMapping = {};
urlMappingReceived = true;
};
var mappingSettings = {
mappingTablePrefix: urlBase + "staticmax/",
assetPrefix: urlBase,
mappingTableURL: urlBase + mappingTable
};
TurbulenzServices.createMappingTable(requestHandler, null, mappingReceived,
mappingSettings, mappingError);
}
else
{
urlMapping = {};
urlMappingReceived = true;
}
checkMapping();
};
var clearColor = [0.95, 0.95, 1.0, 1.0];
var previousFrameTime = 0;
var nextUpdateTime = 0;
vi.doUpdate = true;
vi.drawInterpolators = true;
vi.drawSkeleton = true;
vi.drawLights = true;
vi.drawPortals = false;
vi.drawPhysicsGeometry = false;
vi.drawPhysicsExtents = false;
vi.drawOpaqueNodesExtents = false;
vi.drawSceneNodeHierarchy = false;
vi.drawNormals = false;
vi.drawTangents = false;
vi.drawBinormals = false;
vi.isDefaultWireframeOn = false;
vi.isBlueprintWireframeOn = false;
vi.isDebugTangentsOn = false;
vi.isDebugNormalsOn = false;
vi.isDebugBinormalsOn = false;
vi.drawAreasExtents = false;
vi.animScale = 1;
vi.movementScale = 1;
vi.normalsScale = 1;
if (gd.beginFrame())
{
gd.clear(clearColor, 1.0, 0);
gd.endFrame();
}
function drawDecalsCB()
{
// Draw floor during decals pass to avoid artifacts with transparent materials
floor.render(gd, camera);
}
function drawDebugCB()
{
if (vi.prevDrawNormals !== vi.drawNormals ||
vi.prevDrawTangents !== vi.drawTangents ||
vi.prevDrawBinormals !== vi.drawBinormals ||
vi.prevNormalsScale !== vi.normalsScale)
{
vi.prevDrawNormals = vi.drawNormals;
vi.prevDrawTangents = vi.drawTangents;
vi.prevDrawBinormals = vi.drawBinormals;
vi.prevNormalsScale = vi.normalsScale;
scene.updateNormals(gd,
vi.normalsScale,
vi.drawNormals, scene.getMaterial("debugLinesNormalsMaterial"),
vi.drawTangents, scene.getMaterial("debugLinesTangentsMaterial"),
vi.drawBinormals, scene.getMaterial("debugLinesBinormalsMaterial"));
}
// TODO: draw the hierarchies at the correct positions
if (vi.drawInterpolators)
{
var nodeControllers = scene.nodeControllers;
var numNodeControllers = nodeControllers.length;
for (var i = 0; i < numNodeControllers; i += 1)
{
var nodeController = nodeControllers[i];
var interp = nodeController.inputController;
var hierarchy = interp.currentAnim.hierarchy;
scene.drawAnimationHierarchy(gd, sm, camera,
hierarchy,
hierarchy.numNodes,
interp);
}
}
if (vi.drawSkeleton)
{
var skinnedNodes = scene.skinnedNodes;
var numSkins = skinnedNodes.length;
for (var skin = 0; skin < numSkins; skin += 1)
{
var skinnedNode = skinnedNodes[skin];
var controller = skinnedNode.skinController;
var nodeTM = skinnedNode.node.world;
scene.drawAnimationHierarchy(gd, sm, camera,
controller.skeleton,
controller.skeleton.numNodes,
controller.inputController,
nodeTM);
}
}
if (vi.drawLights)
{
scene.drawLights(gd, sm, camera);
}
if (vi.drawPortals)
{
scene.drawPortals(gd, sm, camera);
}
if (vi.drawAreasExtents)
{
scene.drawAreas(gd, sm, camera);
}
if (vi.drawPhysicsGeometry)
{
scene.drawPhysicsGeometry(gd, sm, camera, physicsManager);
}
if (vi.drawPhysicsExtents)
{
scene.drawPhysicsNodes(gd, sm, camera, physicsManager);
}
if (vi.drawOpaqueNodesExtents)
{
scene.drawOpaqueNodesExtents(gd, sm, camera);
}
if (vi.drawSceneNodeHierarchy)
{
scene.drawSceneNodeHierarchy(gd, sm, camera);
}
}
function renderFrameFn()
{
var nodeControllers, numNodeControllers, nodeController;
var skinnedNodes, numSkins, skinnedNode, skin;
var i;
var currentTime = TurbulenzEngine.time;
var deltaTime = (currentTime - previousFrameTime);
if (deltaTime > 1)
{
deltaTime = 1;
}
cameraController.maxSpeed = (deltaTime * maxSpeed * vi.movementScale);
id.update();
cameraController.update();
var deviceWidth = gd.width;
var deviceHeight = gd.height;
var aspectRatio = (deviceWidth / deviceHeight);
if (aspectRatio !== camera.aspectRatio)
{
camera.aspectRatio = aspectRatio;
camera.updateProjectionMatrix();
}
camera.updateViewProjectionMatrix();
var renderer = vi.renderer;
if (vi.doUpdate)
{
renderer.setGlobalLightPosition(md.v3Build((objectCenter[0] + lightRadius * Math.cos(lightAngle)),
(objectCenter[1] + lightRadius),
(objectCenter[2] + lightRadius * Math.sin(lightAngle))));
lightAngle += deltaTime;
if (lightAngle >= (2 * Math.PI))
{
lightAngle -= (2 * Math.PI);
}
dw.update();
physicsManager.update(scene);
// Update all the animations in the scene
nodeControllers = scene.nodeControllers;
numNodeControllers = nodeControllers.length;
for (i = 0; i < numNodeControllers; i += 1)
{
nodeController = nodeControllers[i];
nodeController.addTime(deltaTime * vi.animScale);
nodeController.update();
}
skinnedNodes = scene.skinnedNodes;
numSkins = skinnedNodes.length;
for (skin = 0; skin < numSkins; skin += 1)
{
skinnedNode = skinnedNodes[skin];
if (vi.drawSkinBindPose || !skinnedNode.animController ||
vi.drawNormals ||
vi.drawTangents ||
vi.drawBinormals)
{
skinnedNode.skinController.setInputController(skinnedNode.poseController);
}
else
{
skinnedNode.skinController.setInputController(skinnedNode.animController);
}
skinnedNode.addTime(deltaTime * vi.animScale);
skinnedNode.update();
}
if (currentTime >= nextUpdateTime)
{
nextUpdateTime = (currentTime + 1.0);
renderer.updateShader(sm);
}
scene.update();
renderer.update(gd, camera, scene, currentTime);
}
if (gd.beginFrame())
{
if (renderer.updateBuffers(gd, deviceWidth, deviceHeight))
{
renderer.draw(gd, clearColor, drawDecalsCB, null, drawDebugCB);
}
gd.endFrame();
}
vi.fps = gd.fps;
previousFrameTime = currentTime;
}
var intervalID;
function loadingLoopFn()
{
var deviceWidth = gd.width;
var deviceHeight = gd.height;
var aspectRatio = (deviceWidth / deviceHeight);
if (aspectRatio !== camera.aspectRatio)
{
camera.aspectRatio = aspectRatio;
camera.updateProjectionMatrix();
}
camera.updateViewProjectionMatrix();
if (gd.beginFrame())
{
gd.clear(clearColor, 1.0, 0);
floor.render(gd, camera);
gd.endFrame();
}
if (sceneLoaded)
{
TurbulenzEngine.clearInterval(intervalID);
intervalID = TurbulenzEngine.setInterval(renderFrameFn, 1000 / 60);
}
}
intervalID = TurbulenzEngine.setInterval(loadingLoopFn, 1000 / 30);
vi.reloadTextures = function viewerreloadTexturesFn()
{
nextUpdateTime = 0;
tm.reloadAll();
};
vi.reloadShaders = function viewerreloadShadersFn()
{
nextUpdateTime = 0;
sm.reloadAll();
};
vi.destroy = function destroyFn()
{
TurbulenzEngine.clearInterval(intervalID);
sceneLoaded = false;
if (physicsManager)
{
physicsManager.clear();
physicsManager = null;
physicsSnapshot = null;
}
vi.renderer = null;
vi.scene = null;
if (scene)
{
scene.destroy();
scene = null;
}
em = null;
sm = null;
tm = null;
floor = null;
cameraController = null;
camera = null;
TurbulenzEngine.flush();
pd = null;
id = null;
gd = null;
md = null;
};
vi.hasNodeSemantic = function hasNodeSemantic(node, semanticCondition)
{
if (node.hasRenderables())
{
var numRenderables = node.renderables.length;
for (var r = 0; r < numRenderables; r += 1)
{
var renderable = node.renderables[r];
if (renderable.geometry)
{
var geometry = renderable.geometry;
var semantics = geometry.semantics;
var numSemantics = semantics.length;
for (var i = 0; i < numSemantics; i += 1)
{
if (semantics[i] === semanticCondition)
{
return true;
}
}
}
}
}
return vi.hasChildrenNodesSemantic(node, semanticCondition);
};
vi.hasChildrenNodesSemantic = function hasChildrenNodesSemantic(node, semanticCondition)