@ohuoy/easymap 1.0.19 → 1.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/bundle.js +318 -290
  2. package/dist/example - /345/211/257/346/234/254/bundle.js" +318 -290
  3. package/dist/example - /345/211/257/346/234/254/index.html" +11 -11
  4. package/index.js +4 -0
  5. package/lib/threebox-plugin/CHANGELOG.md +665 -0
  6. package/lib/threebox-plugin/LICENSE.txt +97 -0
  7. package/lib/threebox-plugin/README.md +199 -0
  8. package/lib/threebox-plugin/exports.js +2 -0
  9. package/lib/threebox-plugin/main.js +8 -0
  10. package/lib/threebox-plugin/package.json +44 -0
  11. package/lib/threebox-plugin/server.stop.js +13 -0
  12. package/lib/threebox-plugin/src/Threebox.js +1216 -0
  13. package/lib/threebox-plugin/src/animation/AnimationManager.js +483 -0
  14. package/lib/threebox-plugin/src/camera/CameraSync.js +302 -0
  15. package/lib/threebox-plugin/src/objects/CSS2DRenderer.js +245 -0
  16. package/lib/threebox-plugin/src/objects/LabelRenderer.js +71 -0
  17. package/lib/threebox-plugin/src/objects/Object3D.js +34 -0
  18. package/lib/threebox-plugin/src/objects/effects/BuildingShadows.js +115 -0
  19. package/lib/threebox-plugin/src/objects/extrusion.js +61 -0
  20. package/lib/threebox-plugin/src/objects/fflate.min.js +15 -0
  21. package/lib/threebox-plugin/src/objects/label.js +29 -0
  22. package/lib/threebox-plugin/src/objects/line.js +1386 -0
  23. package/lib/threebox-plugin/src/objects/loadObj.js +142 -0
  24. package/lib/threebox-plugin/src/objects/loaders/ColladaLoader.js +3751 -0
  25. package/lib/threebox-plugin/src/objects/loaders/FBXLoader.js +3864 -0
  26. package/lib/threebox-plugin/src/objects/loaders/GLTFLoader.js +3857 -0
  27. package/lib/threebox-plugin/src/objects/loaders/MTLLoader.js +498 -0
  28. package/lib/threebox-plugin/src/objects/loaders/OBJLoader.js +818 -0
  29. package/lib/threebox-plugin/src/objects/objects.js +1113 -0
  30. package/lib/threebox-plugin/src/objects/sphere.js +28 -0
  31. package/lib/threebox-plugin/src/objects/tooltip.js +27 -0
  32. package/lib/threebox-plugin/src/objects/tube.js +35 -0
  33. package/lib/threebox-plugin/src/three.js +6 -0
  34. package/lib/threebox-plugin/src/three.module.js +54571 -0
  35. package/lib/threebox-plugin/src/utils/ValueGenerator.js +11 -0
  36. package/lib/threebox-plugin/src/utils/constants.js +21 -0
  37. package/lib/threebox-plugin/src/utils/material.js +52 -0
  38. package/lib/threebox-plugin/src/utils/suncalc.js +322 -0
  39. package/lib/threebox-plugin/src/utils/utils.js +424 -0
  40. package/lib/threebox-plugin/src/utils/validate.js +115 -0
  41. package/package.json +18 -18
  42. package/src/components/EasyMapMarker.js +8 -0
  43. package/src/components/control/DrawBar.js +5 -0
  44. package/src/components/control/TilesBar.js +116 -27
  45. package/src/components/control/Toobars.js +20 -1
  46. package/src/components/layer/AlarmLayer.js +4 -1
  47. package/src/components/layer/AnimationBarbsLayer.js +1 -1
  48. package/src/components/layer/AnimationLayer copy.js +1 -1
  49. package/src/components/layer/AnimationLayer.js +11 -3
  50. package/src/components/layer/CustomIconLayer.js +1 -1
  51. package/src/components/layer/ExtrusionLayer.js +1 -1
  52. package/src/components/layer/ExtrusionLayerold.js +2 -1
  53. package/src/components/layer/MarkerAreaLayer.js +1 -1
  54. package/src/components/layer/PathLineLayer.js +1 -1
  55. package/src/components/layer/ThreeScanLayer.js +51 -14
  56. package/src/components/layer/ThreeWallLayer.js +1 -1
  57. package/webpack.config.js +2 -1
@@ -0,0 +1,3751 @@
1
+ import * as THREE from '../../three.module.js'
2
+
3
+ /**
4
+ * @author mrdoob / http://mrdoob.com/
5
+ * @author Mugen87 / https://github.com/Mugen87
6
+ */
7
+
8
+ // (function () {
9
+
10
+ class ColladaLoader extends THREE.Loader {
11
+
12
+ constructor(manager) {
13
+
14
+ super(manager);
15
+
16
+ }
17
+
18
+ load(url, onLoad, onProgress, onError) {
19
+
20
+ const scope = this;
21
+ const path = scope.path === '' ? THREE.LoaderUtils.extractUrlBase(url) : scope.path;
22
+ const loader = new THREE.FileLoader(scope.manager);
23
+ loader.setPath(scope.path);
24
+ loader.setRequestHeader(scope.requestHeader);
25
+ loader.setWithCredentials(scope.withCredentials);
26
+ loader.load(url, function (text) {
27
+
28
+ try {
29
+
30
+ onLoad(scope.parse(text, path));
31
+
32
+ } catch (e) {
33
+
34
+ if (onError) {
35
+
36
+ onError(e);
37
+
38
+ } else {
39
+
40
+ console.error(e);
41
+
42
+ }
43
+
44
+ scope.manager.itemError(url);
45
+
46
+ }
47
+
48
+ }, onProgress, onError);
49
+
50
+ }
51
+
52
+ parse(text, path) {
53
+
54
+ function getElementsByTagName(xml, name) {
55
+
56
+ // Non recursive xml.getElementsByTagName() ...
57
+ const array = [];
58
+ const childNodes = xml.childNodes;
59
+
60
+ for (let i = 0, l = childNodes.length; i < l; i++) {
61
+
62
+ const child = childNodes[i];
63
+
64
+ if (child.nodeName === name) {
65
+
66
+ array.push(child);
67
+
68
+ }
69
+
70
+ }
71
+
72
+ return array;
73
+
74
+ }
75
+
76
+ function parseStrings(text) {
77
+
78
+ if (text.length === 0) return [];
79
+ const parts = text.trim().split(/\s+/);
80
+ const array = new Array(parts.length);
81
+
82
+ for (let i = 0, l = parts.length; i < l; i++) {
83
+
84
+ array[i] = parts[i];
85
+
86
+ }
87
+
88
+ return array;
89
+
90
+ }
91
+
92
+ function parseFloats(text) {
93
+
94
+ if (text.length === 0) return [];
95
+ const parts = text.trim().split(/\s+/);
96
+ const array = new Array(parts.length);
97
+
98
+ for (let i = 0, l = parts.length; i < l; i++) {
99
+
100
+ array[i] = parseFloat(parts[i]);
101
+
102
+ }
103
+
104
+ return array;
105
+
106
+ }
107
+
108
+ function parseInts(text) {
109
+
110
+ if (text.length === 0) return [];
111
+ const parts = text.trim().split(/\s+/);
112
+ const array = new Array(parts.length);
113
+
114
+ for (let i = 0, l = parts.length; i < l; i++) {
115
+
116
+ array[i] = parseInt(parts[i]);
117
+
118
+ }
119
+
120
+ return array;
121
+
122
+ }
123
+
124
+ function parseId(text) {
125
+
126
+ return text.substring(1);
127
+
128
+ }
129
+
130
+ function generateId() {
131
+
132
+ return 'three_default_' + count++;
133
+
134
+ }
135
+
136
+ function isEmpty(object) {
137
+
138
+ return Object.keys(object).length === 0;
139
+
140
+ } // asset
141
+
142
+
143
+ function parseAsset(xml) {
144
+
145
+ return {
146
+ unit: parseAssetUnit(getElementsByTagName(xml, 'unit')[0]),
147
+ upAxis: parseAssetUpAxis(getElementsByTagName(xml, 'up_axis')[0])
148
+ };
149
+
150
+ }
151
+
152
+ function parseAssetUnit(xml) {
153
+
154
+ if (xml !== undefined && xml.hasAttribute('meter') === true) {
155
+
156
+ return parseFloat(xml.getAttribute('meter'));
157
+
158
+ } else {
159
+
160
+ return 1; // default 1 meter
161
+
162
+ }
163
+
164
+ }
165
+
166
+ function parseAssetUpAxis(xml) {
167
+
168
+ return xml !== undefined ? xml.textContent : 'Y_UP';
169
+
170
+ } // library
171
+
172
+
173
+ function parseLibrary(xml, libraryName, nodeName, parser) {
174
+
175
+ const library = getElementsByTagName(xml, libraryName)[0];
176
+
177
+ if (library !== undefined) {
178
+
179
+ const elements = getElementsByTagName(library, nodeName);
180
+
181
+ for (let i = 0; i < elements.length; i++) {
182
+
183
+ parser(elements[i]);
184
+
185
+ }
186
+
187
+ }
188
+
189
+ }
190
+
191
+ function buildLibrary(data, builder) {
192
+
193
+ for (const name in data) {
194
+
195
+ const object = data[name];
196
+ object.build = builder(data[name]);
197
+
198
+ }
199
+
200
+ } // get
201
+
202
+
203
+ function getBuild(data, builder) {
204
+
205
+ if (data.build !== undefined) return data.build;
206
+ data.build = builder(data);
207
+ return data.build;
208
+
209
+ } // animation
210
+
211
+
212
+ function parseAnimation(xml) {
213
+
214
+ const data = {
215
+ sources: {},
216
+ samplers: {},
217
+ channels: {}
218
+ };
219
+ let hasChildren = false;
220
+
221
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
222
+
223
+ const child = xml.childNodes[i];
224
+ if (child.nodeType !== 1) continue;
225
+ let id;
226
+
227
+ switch (child.nodeName) {
228
+
229
+ case 'source':
230
+ id = child.getAttribute('id');
231
+ data.sources[id] = parseSource(child);
232
+ break;
233
+
234
+ case 'sampler':
235
+ id = child.getAttribute('id');
236
+ data.samplers[id] = parseAnimationSampler(child);
237
+ break;
238
+
239
+ case 'channel':
240
+ id = child.getAttribute('target');
241
+ data.channels[id] = parseAnimationChannel(child);
242
+ break;
243
+
244
+ case 'animation':
245
+ // hierarchy of related animations
246
+ parseAnimation(child);
247
+ hasChildren = true;
248
+ break;
249
+
250
+ default:
251
+ console.log(child);
252
+
253
+ }
254
+
255
+ }
256
+
257
+ if (hasChildren === false) {
258
+
259
+ // since 'id' attributes can be optional, it's necessary to generate a UUID for unqiue assignment
260
+ library.animations[xml.getAttribute('id') || THREE.MathUtils.generateUUID()] = data;
261
+
262
+ }
263
+
264
+ }
265
+
266
+ function parseAnimationSampler(xml) {
267
+
268
+ const data = {
269
+ inputs: {}
270
+ };
271
+
272
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
273
+
274
+ const child = xml.childNodes[i];
275
+ if (child.nodeType !== 1) continue;
276
+
277
+ switch (child.nodeName) {
278
+
279
+ case 'input':
280
+ const id = parseId(child.getAttribute('source'));
281
+ const semantic = child.getAttribute('semantic');
282
+ data.inputs[semantic] = id;
283
+ break;
284
+
285
+ }
286
+
287
+ }
288
+
289
+ return data;
290
+
291
+ }
292
+
293
+ function parseAnimationChannel(xml) {
294
+
295
+ const data = {};
296
+ const target = xml.getAttribute('target'); // parsing SID Addressing Syntax
297
+
298
+ let parts = target.split('/');
299
+ const id = parts.shift();
300
+ let sid = parts.shift(); // check selection syntax
301
+
302
+ const arraySyntax = sid.indexOf('(') !== - 1;
303
+ const memberSyntax = sid.indexOf('.') !== - 1;
304
+
305
+ if (memberSyntax) {
306
+
307
+ // member selection access
308
+ parts = sid.split('.');
309
+ sid = parts.shift();
310
+ data.member = parts.shift();
311
+
312
+ } else if (arraySyntax) {
313
+
314
+ // array-access syntax. can be used to express fields in one-dimensional vectors or two-dimensional matrices.
315
+ const indices = sid.split('(');
316
+ sid = indices.shift();
317
+
318
+ for (let i = 0; i < indices.length; i++) {
319
+
320
+ indices[i] = parseInt(indices[i].replace(/\)/, ''));
321
+
322
+ }
323
+
324
+ data.indices = indices;
325
+
326
+ }
327
+
328
+ data.id = id;
329
+ data.sid = sid;
330
+ data.arraySyntax = arraySyntax;
331
+ data.memberSyntax = memberSyntax;
332
+ data.sampler = parseId(xml.getAttribute('source'));
333
+ return data;
334
+
335
+ }
336
+
337
+ function buildAnimation(data) {
338
+
339
+ const tracks = [];
340
+ const channels = data.channels;
341
+ const samplers = data.samplers;
342
+ const sources = data.sources;
343
+
344
+ for (const target in channels) {
345
+
346
+ if (channels.hasOwnProperty(target)) {
347
+
348
+ const channel = channels[target];
349
+ const sampler = samplers[channel.sampler];
350
+ const inputId = sampler.inputs.INPUT;
351
+ const outputId = sampler.inputs.OUTPUT;
352
+ const inputSource = sources[inputId];
353
+ const outputSource = sources[outputId];
354
+ const animation = buildAnimationChannel(channel, inputSource, outputSource);
355
+ createKeyframeTracks(animation, tracks);
356
+
357
+ }
358
+
359
+ }
360
+
361
+ return tracks;
362
+
363
+ }
364
+
365
+ function getAnimation(id) {
366
+
367
+ return getBuild(library.animations[id], buildAnimation);
368
+
369
+ }
370
+
371
+ function buildAnimationChannel(channel, inputSource, outputSource) {
372
+
373
+ const node = library.nodes[channel.id];
374
+ const object3D = getNode(node.id);
375
+ const transform = node.transforms[channel.sid];
376
+ const defaultMatrix = node.matrix.clone().transpose();
377
+ let time, stride;
378
+ let i, il, j, jl;
379
+ const data = {}; // the collada spec allows the animation of data in various ways.
380
+ // depending on the transform type (matrix, translate, rotate, scale), we execute different logic
381
+
382
+ switch (transform) {
383
+
384
+ case 'matrix':
385
+ for (i = 0, il = inputSource.array.length; i < il; i++) {
386
+
387
+ time = inputSource.array[i];
388
+ stride = i * outputSource.stride;
389
+ if (data[time] === undefined) data[time] = {};
390
+
391
+ if (channel.arraySyntax === true) {
392
+
393
+ const value = outputSource.array[stride];
394
+ const index = channel.indices[0] + 4 * channel.indices[1];
395
+ data[time][index] = value;
396
+
397
+ } else {
398
+
399
+ for (j = 0, jl = outputSource.stride; j < jl; j++) {
400
+
401
+ data[time][j] = outputSource.array[stride + j];
402
+
403
+ }
404
+
405
+ }
406
+
407
+ }
408
+
409
+ break;
410
+
411
+ case 'translate':
412
+ console.warn('THREE.ColladaLoader: Animation transform type "%s" not yet implemented.', transform);
413
+ break;
414
+
415
+ case 'rotate':
416
+ console.warn('THREE.ColladaLoader: Animation transform type "%s" not yet implemented.', transform);
417
+ break;
418
+
419
+ case 'scale':
420
+ console.warn('THREE.ColladaLoader: Animation transform type "%s" not yet implemented.', transform);
421
+ break;
422
+
423
+ }
424
+
425
+ const keyframes = prepareAnimationData(data, defaultMatrix);
426
+ const animation = {
427
+ name: object3D.uuid,
428
+ keyframes: keyframes
429
+ };
430
+ return animation;
431
+
432
+ }
433
+
434
+ function prepareAnimationData(data, defaultMatrix) {
435
+
436
+ const keyframes = []; // transfer data into a sortable array
437
+
438
+ for (const time in data) {
439
+
440
+ keyframes.push({
441
+ time: parseFloat(time),
442
+ value: data[time]
443
+ });
444
+
445
+ } // ensure keyframes are sorted by time
446
+
447
+
448
+ keyframes.sort(ascending); // now we clean up all animation data, so we can use them for keyframe tracks
449
+
450
+ for (let i = 0; i < 16; i++) {
451
+
452
+ transformAnimationData(keyframes, i, defaultMatrix.elements[i]);
453
+
454
+ }
455
+
456
+ return keyframes; // array sort function
457
+
458
+ function ascending(a, b) {
459
+
460
+ return a.time - b.time;
461
+
462
+ }
463
+
464
+ }
465
+
466
+ const position = new THREE.Vector3();
467
+ const scale = new THREE.Vector3();
468
+ const quaternion = new THREE.Quaternion();
469
+
470
+ function createKeyframeTracks(animation, tracks) {
471
+
472
+ const keyframes = animation.keyframes;
473
+ const name = animation.name;
474
+ const times = [];
475
+ const positionData = [];
476
+ const quaternionData = [];
477
+ const scaleData = [];
478
+
479
+ for (let i = 0, l = keyframes.length; i < l; i++) {
480
+
481
+ const keyframe = keyframes[i];
482
+ const time = keyframe.time;
483
+ const value = keyframe.value;
484
+ matrix.fromArray(value).transpose();
485
+ matrix.decompose(position, quaternion, scale);
486
+ times.push(time);
487
+ positionData.push(position.x, position.y, position.z);
488
+ quaternionData.push(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
489
+ scaleData.push(scale.x, scale.y, scale.z);
490
+
491
+ }
492
+
493
+ if (positionData.length > 0) tracks.push(new THREE.VectorKeyframeTrack(name + '.position', times, positionData));
494
+ if (quaternionData.length > 0) tracks.push(new THREE.QuaternionKeyframeTrack(name + '.quaternion', times, quaternionData));
495
+ if (scaleData.length > 0) tracks.push(new THREE.VectorKeyframeTrack(name + '.scale', times, scaleData));
496
+ return tracks;
497
+
498
+ }
499
+
500
+ function transformAnimationData(keyframes, property, defaultValue) {
501
+
502
+ let keyframe;
503
+ let empty = true;
504
+ let i, l; // check, if values of a property are missing in our keyframes
505
+
506
+ for (i = 0, l = keyframes.length; i < l; i++) {
507
+
508
+ keyframe = keyframes[i];
509
+
510
+ if (keyframe.value[property] === undefined) {
511
+
512
+ keyframe.value[property] = null; // mark as missing
513
+
514
+ } else {
515
+
516
+ empty = false;
517
+
518
+ }
519
+
520
+ }
521
+
522
+ if (empty === true) {
523
+
524
+ // no values at all, so we set a default value
525
+ for (i = 0, l = keyframes.length; i < l; i++) {
526
+
527
+ keyframe = keyframes[i];
528
+ keyframe.value[property] = defaultValue;
529
+
530
+ }
531
+
532
+ } else {
533
+
534
+ // filling gaps
535
+ createMissingKeyframes(keyframes, property);
536
+
537
+ }
538
+
539
+ }
540
+
541
+ function createMissingKeyframes(keyframes, property) {
542
+
543
+ let prev, next;
544
+
545
+ for (let i = 0, l = keyframes.length; i < l; i++) {
546
+
547
+ const keyframe = keyframes[i];
548
+
549
+ if (keyframe.value[property] === null) {
550
+
551
+ prev = getPrev(keyframes, i, property);
552
+ next = getNext(keyframes, i, property);
553
+
554
+ if (prev === null) {
555
+
556
+ keyframe.value[property] = next.value[property];
557
+ continue;
558
+
559
+ }
560
+
561
+ if (next === null) {
562
+
563
+ keyframe.value[property] = prev.value[property];
564
+ continue;
565
+
566
+ }
567
+
568
+ interpolate(keyframe, prev, next, property);
569
+
570
+ }
571
+
572
+ }
573
+
574
+ }
575
+
576
+ function getPrev(keyframes, i, property) {
577
+
578
+ while (i >= 0) {
579
+
580
+ const keyframe = keyframes[i];
581
+ if (keyframe.value[property] !== null) return keyframe;
582
+ i--;
583
+
584
+ }
585
+
586
+ return null;
587
+
588
+ }
589
+
590
+ function getNext(keyframes, i, property) {
591
+
592
+ while (i < keyframes.length) {
593
+
594
+ const keyframe = keyframes[i];
595
+ if (keyframe.value[property] !== null) return keyframe;
596
+ i++;
597
+
598
+ }
599
+
600
+ return null;
601
+
602
+ }
603
+
604
+ function interpolate(key, prev, next, property) {
605
+
606
+ if (next.time - prev.time === 0) {
607
+
608
+ key.value[property] = prev.value[property];
609
+ return;
610
+
611
+ }
612
+
613
+ key.value[property] = (key.time - prev.time) * (next.value[property] - prev.value[property]) / (next.time - prev.time) + prev.value[property];
614
+
615
+ } // animation clips
616
+
617
+
618
+ function parseAnimationClip(xml) {
619
+
620
+ const data = {
621
+ name: xml.getAttribute('id') || 'default',
622
+ start: parseFloat(xml.getAttribute('start') || 0),
623
+ end: parseFloat(xml.getAttribute('end') || 0),
624
+ animations: []
625
+ };
626
+
627
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
628
+
629
+ const child = xml.childNodes[i];
630
+ if (child.nodeType !== 1) continue;
631
+
632
+ switch (child.nodeName) {
633
+
634
+ case 'instance_animation':
635
+ data.animations.push(parseId(child.getAttribute('url')));
636
+ break;
637
+
638
+ }
639
+
640
+ }
641
+
642
+ library.clips[xml.getAttribute('id')] = data;
643
+
644
+ }
645
+
646
+ function buildAnimationClip(data) {
647
+
648
+ const tracks = [];
649
+ const name = data.name;
650
+ const duration = data.end - data.start || - 1;
651
+ const animations = data.animations;
652
+
653
+ for (let i = 0, il = animations.length; i < il; i++) {
654
+
655
+ const animationTracks = getAnimation(animations[i]);
656
+
657
+ for (let j = 0, jl = animationTracks.length; j < jl; j++) {
658
+
659
+ tracks.push(animationTracks[j]);
660
+
661
+ }
662
+
663
+ }
664
+
665
+ return new THREE.AnimationClip(name, duration, tracks);
666
+
667
+ }
668
+
669
+ function getAnimationClip(id) {
670
+
671
+ return getBuild(library.clips[id], buildAnimationClip);
672
+
673
+ } // controller
674
+
675
+
676
+ function parseController(xml) {
677
+
678
+ const data = {};
679
+
680
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
681
+
682
+ const child = xml.childNodes[i];
683
+ if (child.nodeType !== 1) continue;
684
+
685
+ switch (child.nodeName) {
686
+
687
+ case 'skin':
688
+ // there is exactly one skin per controller
689
+ data.id = parseId(child.getAttribute('source'));
690
+ data.skin = parseSkin(child);
691
+ break;
692
+
693
+ case 'morph':
694
+ data.id = parseId(child.getAttribute('source'));
695
+ console.warn('THREE.ColladaLoader: Morph target animation not supported yet.');
696
+ break;
697
+
698
+ }
699
+
700
+ }
701
+
702
+ library.controllers[xml.getAttribute('id')] = data;
703
+
704
+ }
705
+
706
+ function parseSkin(xml) {
707
+
708
+ const data = {
709
+ sources: {}
710
+ };
711
+
712
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
713
+
714
+ const child = xml.childNodes[i];
715
+ if (child.nodeType !== 1) continue;
716
+
717
+ switch (child.nodeName) {
718
+
719
+ case 'bind_shape_matrix':
720
+ data.bindShapeMatrix = parseFloats(child.textContent);
721
+ break;
722
+
723
+ case 'source':
724
+ const id = child.getAttribute('id');
725
+ data.sources[id] = parseSource(child);
726
+ break;
727
+
728
+ case 'joints':
729
+ data.joints = parseJoints(child);
730
+ break;
731
+
732
+ case 'vertex_weights':
733
+ data.vertexWeights = parseVertexWeights(child);
734
+ break;
735
+
736
+ }
737
+
738
+ }
739
+
740
+ return data;
741
+
742
+ }
743
+
744
+ function parseJoints(xml) {
745
+
746
+ const data = {
747
+ inputs: {}
748
+ };
749
+
750
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
751
+
752
+ const child = xml.childNodes[i];
753
+ if (child.nodeType !== 1) continue;
754
+
755
+ switch (child.nodeName) {
756
+
757
+ case 'input':
758
+ const semantic = child.getAttribute('semantic');
759
+ const id = parseId(child.getAttribute('source'));
760
+ data.inputs[semantic] = id;
761
+ break;
762
+
763
+ }
764
+
765
+ }
766
+
767
+ return data;
768
+
769
+ }
770
+
771
+ function parseVertexWeights(xml) {
772
+
773
+ const data = {
774
+ inputs: {}
775
+ };
776
+
777
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
778
+
779
+ const child = xml.childNodes[i];
780
+ if (child.nodeType !== 1) continue;
781
+
782
+ switch (child.nodeName) {
783
+
784
+ case 'input':
785
+ const semantic = child.getAttribute('semantic');
786
+ const id = parseId(child.getAttribute('source'));
787
+ const offset = parseInt(child.getAttribute('offset'));
788
+ data.inputs[semantic] = {
789
+ id: id,
790
+ offset: offset
791
+ };
792
+ break;
793
+
794
+ case 'vcount':
795
+ data.vcount = parseInts(child.textContent);
796
+ break;
797
+
798
+ case 'v':
799
+ data.v = parseInts(child.textContent);
800
+ break;
801
+
802
+ }
803
+
804
+ }
805
+
806
+ return data;
807
+
808
+ }
809
+
810
+ function buildController(data) {
811
+
812
+ const build = {
813
+ id: data.id
814
+ };
815
+ const geometry = library.geometries[build.id];
816
+
817
+ if (data.skin !== undefined) {
818
+
819
+ build.skin = buildSkin(data.skin); // we enhance the 'sources' property of the corresponding geometry with our skin data
820
+
821
+ geometry.sources.skinIndices = build.skin.indices;
822
+ geometry.sources.skinWeights = build.skin.weights;
823
+
824
+ }
825
+
826
+ return build;
827
+
828
+ }
829
+
830
+ function buildSkin(data) {
831
+
832
+ const BONE_LIMIT = 4;
833
+ const build = {
834
+ joints: [],
835
+ // this must be an array to preserve the joint order
836
+ indices: {
837
+ array: [],
838
+ stride: BONE_LIMIT
839
+ },
840
+ weights: {
841
+ array: [],
842
+ stride: BONE_LIMIT
843
+ }
844
+ };
845
+ const sources = data.sources;
846
+ const vertexWeights = data.vertexWeights;
847
+ const vcount = vertexWeights.vcount;
848
+ const v = vertexWeights.v;
849
+ const jointOffset = vertexWeights.inputs.JOINT.offset;
850
+ const weightOffset = vertexWeights.inputs.WEIGHT.offset;
851
+ const jointSource = data.sources[data.joints.inputs.JOINT];
852
+ const inverseSource = data.sources[data.joints.inputs.INV_BIND_MATRIX];
853
+ const weights = sources[vertexWeights.inputs.WEIGHT.id].array;
854
+ let stride = 0;
855
+ let i, j, l; // procces skin data for each vertex
856
+
857
+ for (i = 0, l = vcount.length; i < l; i++) {
858
+
859
+ const jointCount = vcount[i]; // this is the amount of joints that affect a single vertex
860
+
861
+ const vertexSkinData = [];
862
+
863
+ for (j = 0; j < jointCount; j++) {
864
+
865
+ const skinIndex = v[stride + jointOffset];
866
+ const weightId = v[stride + weightOffset];
867
+ const skinWeight = weights[weightId];
868
+ vertexSkinData.push({
869
+ index: skinIndex,
870
+ weight: skinWeight
871
+ });
872
+ stride += 2;
873
+
874
+ } // we sort the joints in descending order based on the weights.
875
+ // this ensures, we only procced the most important joints of the vertex
876
+
877
+
878
+ vertexSkinData.sort(descending); // now we provide for each vertex a set of four index and weight values.
879
+ // the order of the skin data matches the order of vertices
880
+
881
+ for (j = 0; j < BONE_LIMIT; j++) {
882
+
883
+ const d = vertexSkinData[j];
884
+
885
+ if (d !== undefined) {
886
+
887
+ build.indices.array.push(d.index);
888
+ build.weights.array.push(d.weight);
889
+
890
+ } else {
891
+
892
+ build.indices.array.push(0);
893
+ build.weights.array.push(0);
894
+
895
+ }
896
+
897
+ }
898
+
899
+ } // setup bind matrix
900
+
901
+
902
+ if (data.bindShapeMatrix) {
903
+
904
+ build.bindMatrix = new THREE.Matrix4().fromArray(data.bindShapeMatrix).transpose();
905
+
906
+ } else {
907
+
908
+ build.bindMatrix = new THREE.Matrix4().identity();
909
+
910
+ } // process bones and inverse bind matrix data
911
+
912
+
913
+ for (i = 0, l = jointSource.array.length; i < l; i++) {
914
+
915
+ const name = jointSource.array[i];
916
+ const boneInverse = new THREE.Matrix4().fromArray(inverseSource.array, i * inverseSource.stride).transpose();
917
+ build.joints.push({
918
+ name: name,
919
+ boneInverse: boneInverse
920
+ });
921
+
922
+ }
923
+
924
+ return build; // array sort function
925
+
926
+ function descending(a, b) {
927
+
928
+ return b.weight - a.weight;
929
+
930
+ }
931
+
932
+ }
933
+
934
+ function getController(id) {
935
+
936
+ return getBuild(library.controllers[id], buildController);
937
+
938
+ } // image
939
+
940
+
941
+ function parseImage(xml) {
942
+
943
+ const data = {
944
+ init_from: getElementsByTagName(xml, 'init_from')[0].textContent
945
+ };
946
+ library.images[xml.getAttribute('id')] = data;
947
+
948
+ }
949
+
950
+ function buildImage(data) {
951
+
952
+ if (data.build !== undefined) return data.build;
953
+ return data.init_from;
954
+
955
+ }
956
+
957
+ function getImage(id) {
958
+
959
+ const data = library.images[id];
960
+
961
+ if (data !== undefined) {
962
+
963
+ return getBuild(data, buildImage);
964
+
965
+ }
966
+
967
+ console.warn('THREE.ColladaLoader: Couldn\'t find image with ID:', id);
968
+ return null;
969
+
970
+ } // effect
971
+
972
+
973
+ function parseEffect(xml) {
974
+
975
+ const data = {};
976
+
977
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
978
+
979
+ const child = xml.childNodes[i];
980
+ if (child.nodeType !== 1) continue;
981
+
982
+ switch (child.nodeName) {
983
+
984
+ case 'profile_COMMON':
985
+ data.profile = parseEffectProfileCOMMON(child);
986
+ break;
987
+
988
+ }
989
+
990
+ }
991
+
992
+ library.effects[xml.getAttribute('id')] = data;
993
+
994
+ }
995
+
996
+ function parseEffectProfileCOMMON(xml) {
997
+
998
+ const data = {
999
+ surfaces: {},
1000
+ samplers: {}
1001
+ };
1002
+
1003
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1004
+
1005
+ const child = xml.childNodes[i];
1006
+ if (child.nodeType !== 1) continue;
1007
+
1008
+ switch (child.nodeName) {
1009
+
1010
+ case 'newparam':
1011
+ parseEffectNewparam(child, data);
1012
+ break;
1013
+
1014
+ case 'technique':
1015
+ data.technique = parseEffectTechnique(child);
1016
+ break;
1017
+
1018
+ case 'extra':
1019
+ data.extra = parseEffectExtra(child);
1020
+ break;
1021
+
1022
+ }
1023
+
1024
+ }
1025
+
1026
+ return data;
1027
+
1028
+ }
1029
+
1030
+ function parseEffectNewparam(xml, data) {
1031
+
1032
+ const sid = xml.getAttribute('sid');
1033
+
1034
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1035
+
1036
+ const child = xml.childNodes[i];
1037
+ if (child.nodeType !== 1) continue;
1038
+
1039
+ switch (child.nodeName) {
1040
+
1041
+ case 'surface':
1042
+ data.surfaces[sid] = parseEffectSurface(child);
1043
+ break;
1044
+
1045
+ case 'sampler2D':
1046
+ data.samplers[sid] = parseEffectSampler(child);
1047
+ break;
1048
+
1049
+ }
1050
+
1051
+ }
1052
+
1053
+ }
1054
+
1055
+ function parseEffectSurface(xml) {
1056
+
1057
+ const data = {};
1058
+
1059
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1060
+
1061
+ const child = xml.childNodes[i];
1062
+ if (child.nodeType !== 1) continue;
1063
+
1064
+ switch (child.nodeName) {
1065
+
1066
+ case 'init_from':
1067
+ data.init_from = child.textContent;
1068
+ break;
1069
+
1070
+ }
1071
+
1072
+ }
1073
+
1074
+ return data;
1075
+
1076
+ }
1077
+
1078
+ function parseEffectSampler(xml) {
1079
+
1080
+ const data = {};
1081
+
1082
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1083
+
1084
+ const child = xml.childNodes[i];
1085
+ if (child.nodeType !== 1) continue;
1086
+
1087
+ switch (child.nodeName) {
1088
+
1089
+ case 'source':
1090
+ data.source = child.textContent;
1091
+ break;
1092
+
1093
+ }
1094
+
1095
+ }
1096
+
1097
+ return data;
1098
+
1099
+ }
1100
+
1101
+ function parseEffectTechnique(xml) {
1102
+
1103
+ const data = {};
1104
+
1105
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1106
+
1107
+ const child = xml.childNodes[i];
1108
+ if (child.nodeType !== 1) continue;
1109
+
1110
+ switch (child.nodeName) {
1111
+
1112
+ case 'constant':
1113
+ case 'lambert':
1114
+ case 'blinn':
1115
+ case 'phong':
1116
+ data.type = child.nodeName;
1117
+ data.parameters = parseEffectParameters(child);
1118
+ break;
1119
+
1120
+ }
1121
+
1122
+ }
1123
+
1124
+ return data;
1125
+
1126
+ }
1127
+
1128
+ function parseEffectParameters(xml) {
1129
+
1130
+ const data = {};
1131
+
1132
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1133
+
1134
+ const child = xml.childNodes[i];
1135
+ if (child.nodeType !== 1) continue;
1136
+
1137
+ switch (child.nodeName) {
1138
+
1139
+ case 'emission':
1140
+ case 'diffuse':
1141
+ case 'specular':
1142
+ case 'bump':
1143
+ case 'ambient':
1144
+ case 'shininess':
1145
+ case 'transparency':
1146
+ data[child.nodeName] = parseEffectParameter(child);
1147
+ break;
1148
+
1149
+ case 'transparent':
1150
+ data[child.nodeName] = {
1151
+ opaque: child.getAttribute('opaque'),
1152
+ data: parseEffectParameter(child)
1153
+ };
1154
+ break;
1155
+
1156
+ }
1157
+
1158
+ }
1159
+
1160
+ return data;
1161
+
1162
+ }
1163
+
1164
+ function parseEffectParameter(xml) {
1165
+
1166
+ const data = {};
1167
+
1168
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1169
+
1170
+ const child = xml.childNodes[i];
1171
+ if (child.nodeType !== 1) continue;
1172
+
1173
+ switch (child.nodeName) {
1174
+
1175
+ case 'color':
1176
+ data[child.nodeName] = parseFloats(child.textContent);
1177
+ break;
1178
+
1179
+ case 'float':
1180
+ data[child.nodeName] = parseFloat(child.textContent);
1181
+ break;
1182
+
1183
+ case 'texture':
1184
+ data[child.nodeName] = {
1185
+ id: child.getAttribute('texture'),
1186
+ extra: parseEffectParameterTexture(child)
1187
+ };
1188
+ break;
1189
+
1190
+ }
1191
+
1192
+ }
1193
+
1194
+ return data;
1195
+
1196
+ }
1197
+
1198
+ function parseEffectParameterTexture(xml) {
1199
+
1200
+ const data = {
1201
+ technique: {}
1202
+ };
1203
+
1204
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1205
+
1206
+ const child = xml.childNodes[i];
1207
+ if (child.nodeType !== 1) continue;
1208
+
1209
+ switch (child.nodeName) {
1210
+
1211
+ case 'extra':
1212
+ parseEffectParameterTextureExtra(child, data);
1213
+ break;
1214
+
1215
+ }
1216
+
1217
+ }
1218
+
1219
+ return data;
1220
+
1221
+ }
1222
+
1223
+ function parseEffectParameterTextureExtra(xml, data) {
1224
+
1225
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1226
+
1227
+ const child = xml.childNodes[i];
1228
+ if (child.nodeType !== 1) continue;
1229
+
1230
+ switch (child.nodeName) {
1231
+
1232
+ case 'technique':
1233
+ parseEffectParameterTextureExtraTechnique(child, data);
1234
+ break;
1235
+
1236
+ }
1237
+
1238
+ }
1239
+
1240
+ }
1241
+
1242
+ function parseEffectParameterTextureExtraTechnique(xml, data) {
1243
+
1244
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1245
+
1246
+ const child = xml.childNodes[i];
1247
+ if (child.nodeType !== 1) continue;
1248
+
1249
+ switch (child.nodeName) {
1250
+
1251
+ case 'repeatU':
1252
+ case 'repeatV':
1253
+ case 'offsetU':
1254
+ case 'offsetV':
1255
+ data.technique[child.nodeName] = parseFloat(child.textContent);
1256
+ break;
1257
+
1258
+ case 'wrapU':
1259
+ case 'wrapV':
1260
+ // some files have values for wrapU/wrapV which become NaN via parseInt
1261
+ if (child.textContent.toUpperCase() === 'TRUE') {
1262
+
1263
+ data.technique[child.nodeName] = 1;
1264
+
1265
+ } else if (child.textContent.toUpperCase() === 'FALSE') {
1266
+
1267
+ data.technique[child.nodeName] = 0;
1268
+
1269
+ } else {
1270
+
1271
+ data.technique[child.nodeName] = parseInt(child.textContent);
1272
+
1273
+ }
1274
+
1275
+ break;
1276
+
1277
+ }
1278
+
1279
+ }
1280
+
1281
+ }
1282
+
1283
+ function parseEffectExtra(xml) {
1284
+
1285
+ const data = {};
1286
+
1287
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1288
+
1289
+ const child = xml.childNodes[i];
1290
+ if (child.nodeType !== 1) continue;
1291
+
1292
+ switch (child.nodeName) {
1293
+
1294
+ case 'technique':
1295
+ data.technique = parseEffectExtraTechnique(child);
1296
+ break;
1297
+
1298
+ }
1299
+
1300
+ }
1301
+
1302
+ return data;
1303
+
1304
+ }
1305
+
1306
+ function parseEffectExtraTechnique(xml) {
1307
+
1308
+ const data = {};
1309
+
1310
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1311
+
1312
+ const child = xml.childNodes[i];
1313
+ if (child.nodeType !== 1) continue;
1314
+
1315
+ switch (child.nodeName) {
1316
+
1317
+ case 'double_sided':
1318
+ data[child.nodeName] = parseInt(child.textContent);
1319
+ break;
1320
+
1321
+ }
1322
+
1323
+ }
1324
+
1325
+ return data;
1326
+
1327
+ }
1328
+
1329
+ function buildEffect(data) {
1330
+
1331
+ return data;
1332
+
1333
+ }
1334
+
1335
+ function getEffect(id) {
1336
+
1337
+ return getBuild(library.effects[id], buildEffect);
1338
+
1339
+ } // material
1340
+
1341
+
1342
+ function parseMaterial(xml) {
1343
+
1344
+ const data = {
1345
+ name: xml.getAttribute('name')
1346
+ };
1347
+
1348
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1349
+
1350
+ const child = xml.childNodes[i];
1351
+ if (child.nodeType !== 1) continue;
1352
+
1353
+ switch (child.nodeName) {
1354
+
1355
+ case 'instance_effect':
1356
+ data.url = parseId(child.getAttribute('url'));
1357
+ break;
1358
+
1359
+ }
1360
+
1361
+ }
1362
+
1363
+ library.materials[xml.getAttribute('id')] = data;
1364
+
1365
+ }
1366
+
1367
+ function getTextureLoader(image) {
1368
+
1369
+ let loader;
1370
+ let extension = image.slice((image.lastIndexOf('.') - 1 >>> 0) + 2); // http://www.jstips.co/en/javascript/get-file-extension/
1371
+
1372
+ extension = extension.toLowerCase();
1373
+
1374
+ switch (extension) {
1375
+
1376
+ case 'tga':
1377
+ loader = tgaLoader;
1378
+ break;
1379
+
1380
+ default:
1381
+ loader = textureLoader;
1382
+
1383
+ }
1384
+
1385
+ return loader;
1386
+
1387
+ }
1388
+
1389
+ function buildMaterial(data) {
1390
+
1391
+ const effect = getEffect(data.url);
1392
+ const technique = effect.profile.technique;
1393
+ const extra = effect.profile.extra;
1394
+ let material;
1395
+
1396
+ switch (technique.type) {
1397
+
1398
+ case 'phong':
1399
+ case 'blinn':
1400
+ material = new THREE.MeshPhongMaterial();
1401
+ break;
1402
+
1403
+ case 'lambert':
1404
+ material = new THREE.MeshLambertMaterial();
1405
+ break;
1406
+
1407
+ default:
1408
+ material = new THREE.MeshBasicMaterial();
1409
+ break;
1410
+
1411
+ }
1412
+
1413
+ material.name = data.name || '';
1414
+
1415
+ function getTexture(textureObject) {
1416
+
1417
+ const sampler = effect.profile.samplers[textureObject.id];
1418
+ let image = null; // get image
1419
+
1420
+ if (sampler !== undefined) {
1421
+
1422
+ const surface = effect.profile.surfaces[sampler.source];
1423
+ image = getImage(surface.init_from);
1424
+
1425
+ } else {
1426
+
1427
+ console.warn('THREE.ColladaLoader: Undefined sampler. Access image directly (see #12530).');
1428
+ image = getImage(textureObject.id);
1429
+
1430
+ } // create texture if image is avaiable
1431
+
1432
+
1433
+ if (image !== null) {
1434
+
1435
+ const loader = getTextureLoader(image);
1436
+
1437
+ if (loader !== undefined) {
1438
+
1439
+ const texture = loader.load(image);
1440
+ const extra = textureObject.extra;
1441
+
1442
+ if (extra !== undefined && extra.technique !== undefined && isEmpty(extra.technique) === false) {
1443
+
1444
+ const technique = extra.technique;
1445
+ texture.wrapS = technique.wrapU ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
1446
+ texture.wrapT = technique.wrapV ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
1447
+ texture.offset.set(technique.offsetU || 0, technique.offsetV || 0);
1448
+ texture.repeat.set(technique.repeatU || 1, technique.repeatV || 1);
1449
+
1450
+ } else {
1451
+
1452
+ texture.wrapS = THREE.RepeatWrapping;
1453
+ texture.wrapT = THREE.RepeatWrapping;
1454
+
1455
+ }
1456
+
1457
+ return texture;
1458
+
1459
+ } else {
1460
+
1461
+ console.warn('THREE.ColladaLoader: THREE.Loader for texture %s not found.', image);
1462
+ return null;
1463
+
1464
+ }
1465
+
1466
+ } else {
1467
+
1468
+ console.warn('THREE.ColladaLoader: Couldn\'t create texture with ID:', textureObject.id);
1469
+ return null;
1470
+
1471
+ }
1472
+
1473
+ }
1474
+
1475
+ const parameters = technique.parameters;
1476
+
1477
+ for (const key in parameters) {
1478
+
1479
+ const parameter = parameters[key];
1480
+
1481
+ switch (key) {
1482
+
1483
+ case 'diffuse':
1484
+ if (parameter.color) material.color.fromArray(parameter.color);
1485
+ if (parameter.texture) material.map = getTexture(parameter.texture);
1486
+ break;
1487
+
1488
+ case 'specular':
1489
+ if (parameter.color && material.specular) material.specular.fromArray(parameter.color);
1490
+ if (parameter.texture) material.specularMap = getTexture(parameter.texture);
1491
+ break;
1492
+
1493
+ case 'bump':
1494
+ if (parameter.texture) material.normalMap = getTexture(parameter.texture);
1495
+ break;
1496
+
1497
+ case 'ambient':
1498
+ if (parameter.texture) material.lightMap = getTexture(parameter.texture);
1499
+ break;
1500
+
1501
+ case 'shininess':
1502
+ if (parameter.float && material.shininess) material.shininess = parameter.float;
1503
+ break;
1504
+
1505
+ case 'emission':
1506
+ if (parameter.color && material.emissive) material.emissive.fromArray(parameter.color);
1507
+ if (parameter.texture) material.emissiveMap = getTexture(parameter.texture);
1508
+ break;
1509
+
1510
+ }
1511
+
1512
+ } //
1513
+
1514
+
1515
+ let transparent = parameters['transparent'];
1516
+ let transparency = parameters['transparency']; // <transparency> does not exist but <transparent>
1517
+
1518
+ if (transparency === undefined && transparent) {
1519
+
1520
+ transparency = {
1521
+ float: 1
1522
+ };
1523
+
1524
+ } // <transparent> does not exist but <transparency>
1525
+
1526
+
1527
+ if (transparent === undefined && transparency) {
1528
+
1529
+ transparent = {
1530
+ opaque: 'A_ONE',
1531
+ data: {
1532
+ color: [1, 1, 1, 1]
1533
+ }
1534
+ };
1535
+
1536
+ }
1537
+
1538
+ if (transparent && transparency) {
1539
+
1540
+ // handle case if a texture exists but no color
1541
+ if (transparent.data.texture) {
1542
+
1543
+ // we do not set an alpha map (see #13792)
1544
+ material.transparent = true;
1545
+
1546
+ } else {
1547
+
1548
+ const color = transparent.data.color;
1549
+
1550
+ switch (transparent.opaque) {
1551
+
1552
+ case 'A_ONE':
1553
+ material.opacity = color[3] * transparency.float;
1554
+ break;
1555
+
1556
+ case 'RGB_ZERO':
1557
+ material.opacity = 1 - color[0] * transparency.float;
1558
+ break;
1559
+
1560
+ case 'A_ZERO':
1561
+ material.opacity = 1 - color[3] * transparency.float;
1562
+ break;
1563
+
1564
+ case 'RGB_ONE':
1565
+ material.opacity = color[0] * transparency.float;
1566
+ break;
1567
+
1568
+ default:
1569
+ console.warn('THREE.ColladaLoader: Invalid opaque type "%s" of transparent tag.', transparent.opaque);
1570
+
1571
+ }
1572
+
1573
+ if (material.opacity < 1) material.transparent = true;
1574
+
1575
+ }
1576
+
1577
+ } //
1578
+
1579
+
1580
+ if (extra !== undefined && extra.technique !== undefined && extra.technique.double_sided === 1) {
1581
+
1582
+ material.side = THREE.DoubleSide;
1583
+
1584
+ }
1585
+
1586
+ return material;
1587
+
1588
+ }
1589
+
1590
+ function getMaterial(id) {
1591
+
1592
+ return getBuild(library.materials[id], buildMaterial);
1593
+
1594
+ } // camera
1595
+
1596
+
1597
+ function parseCamera(xml) {
1598
+
1599
+ const data = {
1600
+ name: xml.getAttribute('name')
1601
+ };
1602
+
1603
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1604
+
1605
+ const child = xml.childNodes[i];
1606
+ if (child.nodeType !== 1) continue;
1607
+
1608
+ switch (child.nodeName) {
1609
+
1610
+ case 'optics':
1611
+ data.optics = parseCameraOptics(child);
1612
+ break;
1613
+
1614
+ }
1615
+
1616
+ }
1617
+
1618
+ library.cameras[xml.getAttribute('id')] = data;
1619
+
1620
+ }
1621
+
1622
+ function parseCameraOptics(xml) {
1623
+
1624
+ for (let i = 0; i < xml.childNodes.length; i++) {
1625
+
1626
+ const child = xml.childNodes[i];
1627
+
1628
+ switch (child.nodeName) {
1629
+
1630
+ case 'technique_common':
1631
+ return parseCameraTechnique(child);
1632
+
1633
+ }
1634
+
1635
+ }
1636
+
1637
+ return {};
1638
+
1639
+ }
1640
+
1641
+ function parseCameraTechnique(xml) {
1642
+
1643
+ const data = {};
1644
+
1645
+ for (let i = 0; i < xml.childNodes.length; i++) {
1646
+
1647
+ const child = xml.childNodes[i];
1648
+
1649
+ switch (child.nodeName) {
1650
+
1651
+ case 'perspective':
1652
+ case 'orthographic':
1653
+ data.technique = child.nodeName;
1654
+ data.parameters = parseCameraParameters(child);
1655
+ break;
1656
+
1657
+ }
1658
+
1659
+ }
1660
+
1661
+ return data;
1662
+
1663
+ }
1664
+
1665
+ function parseCameraParameters(xml) {
1666
+
1667
+ const data = {};
1668
+
1669
+ for (let i = 0; i < xml.childNodes.length; i++) {
1670
+
1671
+ const child = xml.childNodes[i];
1672
+
1673
+ switch (child.nodeName) {
1674
+
1675
+ case 'xfov':
1676
+ case 'yfov':
1677
+ case 'xmag':
1678
+ case 'ymag':
1679
+ case 'znear':
1680
+ case 'zfar':
1681
+ case 'aspect_ratio':
1682
+ data[child.nodeName] = parseFloat(child.textContent);
1683
+ break;
1684
+
1685
+ }
1686
+
1687
+ }
1688
+
1689
+ return data;
1690
+
1691
+ }
1692
+
1693
+ function buildCamera(data) {
1694
+
1695
+ let camera;
1696
+
1697
+ switch (data.optics.technique) {
1698
+
1699
+ case 'perspective':
1700
+ camera = new THREE.PerspectiveCamera(data.optics.parameters.yfov, data.optics.parameters.aspect_ratio, data.optics.parameters.znear, data.optics.parameters.zfar);
1701
+ break;
1702
+
1703
+ case 'orthographic':
1704
+ let ymag = data.optics.parameters.ymag;
1705
+ let xmag = data.optics.parameters.xmag;
1706
+ const aspectRatio = data.optics.parameters.aspect_ratio;
1707
+ xmag = xmag === undefined ? ymag * aspectRatio : xmag;
1708
+ ymag = ymag === undefined ? xmag / aspectRatio : ymag;
1709
+ xmag *= 0.5;
1710
+ ymag *= 0.5;
1711
+ camera = new THREE.OrthographicCamera(- xmag, xmag, ymag, - ymag, // left, right, top, bottom
1712
+ data.optics.parameters.znear, data.optics.parameters.zfar);
1713
+ break;
1714
+
1715
+ default:
1716
+ camera = new THREE.PerspectiveCamera();
1717
+ break;
1718
+
1719
+ }
1720
+
1721
+ camera.name = data.name || '';
1722
+ return camera;
1723
+
1724
+ }
1725
+
1726
+ function getCamera(id) {
1727
+
1728
+ const data = library.cameras[id];
1729
+
1730
+ if (data !== undefined) {
1731
+
1732
+ return getBuild(data, buildCamera);
1733
+
1734
+ }
1735
+
1736
+ console.warn('THREE.ColladaLoader: Couldn\'t find camera with ID:', id);
1737
+ return null;
1738
+
1739
+ } // light
1740
+
1741
+
1742
+ function parseLight(xml) {
1743
+
1744
+ let data = {};
1745
+
1746
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1747
+
1748
+ const child = xml.childNodes[i];
1749
+ if (child.nodeType !== 1) continue;
1750
+
1751
+ switch (child.nodeName) {
1752
+
1753
+ case 'technique_common':
1754
+ data = parseLightTechnique(child);
1755
+ break;
1756
+
1757
+ }
1758
+
1759
+ }
1760
+
1761
+ library.lights[xml.getAttribute('id')] = data;
1762
+
1763
+ }
1764
+
1765
+ function parseLightTechnique(xml) {
1766
+
1767
+ const data = {};
1768
+
1769
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1770
+
1771
+ const child = xml.childNodes[i];
1772
+ if (child.nodeType !== 1) continue;
1773
+
1774
+ switch (child.nodeName) {
1775
+
1776
+ case 'directional':
1777
+ case 'point':
1778
+ case 'spot':
1779
+ case 'ambient':
1780
+ data.technique = child.nodeName;
1781
+ data.parameters = parseLightParameters(child);
1782
+
1783
+ }
1784
+
1785
+ }
1786
+
1787
+ return data;
1788
+
1789
+ }
1790
+
1791
+ function parseLightParameters(xml) {
1792
+
1793
+ const data = {};
1794
+
1795
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1796
+
1797
+ const child = xml.childNodes[i];
1798
+ if (child.nodeType !== 1) continue;
1799
+
1800
+ switch (child.nodeName) {
1801
+
1802
+ case 'color':
1803
+ const array = parseFloats(child.textContent);
1804
+ data.color = new THREE.Color().fromArray(array);
1805
+ break;
1806
+
1807
+ case 'falloff_angle':
1808
+ data.falloffAngle = parseFloat(child.textContent);
1809
+ break;
1810
+
1811
+ case 'quadratic_attenuation':
1812
+ const f = parseFloat(child.textContent);
1813
+ data.distance = f ? Math.sqrt(1 / f) : 0;
1814
+ break;
1815
+
1816
+ }
1817
+
1818
+ }
1819
+
1820
+ return data;
1821
+
1822
+ }
1823
+
1824
+ function buildLight(data) {
1825
+
1826
+ let light;
1827
+
1828
+ switch (data.technique) {
1829
+
1830
+ case 'directional':
1831
+ light = new THREE.DirectionalLight();
1832
+ break;
1833
+
1834
+ case 'point':
1835
+ light = new THREE.PointLight();
1836
+ break;
1837
+
1838
+ case 'spot':
1839
+ light = new THREE.SpotLight();
1840
+ break;
1841
+
1842
+ case 'ambient':
1843
+ light = new THREE.AmbientLight();
1844
+ break;
1845
+
1846
+ }
1847
+
1848
+ if (data.parameters.color) light.color.copy(data.parameters.color);
1849
+ if (data.parameters.distance) light.distance = data.parameters.distance;
1850
+ return light;
1851
+
1852
+ }
1853
+
1854
+ function getLight(id) {
1855
+
1856
+ const data = library.lights[id];
1857
+
1858
+ if (data !== undefined) {
1859
+
1860
+ return getBuild(data, buildLight);
1861
+
1862
+ }
1863
+
1864
+ console.warn('THREE.ColladaLoader: Couldn\'t find light with ID:', id);
1865
+ return null;
1866
+
1867
+ } // geometry
1868
+
1869
+
1870
+ function parseGeometry(xml) {
1871
+
1872
+ const data = {
1873
+ name: xml.getAttribute('name'),
1874
+ sources: {},
1875
+ vertices: {},
1876
+ primitives: []
1877
+ };
1878
+ const mesh = getElementsByTagName(xml, 'mesh')[0]; // the following tags inside geometry are not supported yet (see https://github.com/mrdoob/three.js/pull/12606): convex_mesh, spline, brep
1879
+
1880
+ if (mesh === undefined) return;
1881
+
1882
+ for (let i = 0; i < mesh.childNodes.length; i++) {
1883
+
1884
+ const child = mesh.childNodes[i];
1885
+ if (child.nodeType !== 1) continue;
1886
+ const id = child.getAttribute('id');
1887
+
1888
+ switch (child.nodeName) {
1889
+
1890
+ case 'source':
1891
+ data.sources[id] = parseSource(child);
1892
+ break;
1893
+
1894
+ case 'vertices':
1895
+ // data.sources[ id ] = data.sources[ parseId( getElementsByTagName( child, 'input' )[ 0 ].getAttribute( 'source' ) ) ];
1896
+ data.vertices = parseGeometryVertices(child);
1897
+ break;
1898
+
1899
+ case 'polygons':
1900
+ console.warn('THREE.ColladaLoader: Unsupported primitive type: ', child.nodeName);
1901
+ break;
1902
+
1903
+ case 'lines':
1904
+ case 'linestrips':
1905
+ case 'polylist':
1906
+ case 'triangles':
1907
+ data.primitives.push(parseGeometryPrimitive(child));
1908
+ break;
1909
+
1910
+ default:
1911
+ console.log(child);
1912
+
1913
+ }
1914
+
1915
+ }
1916
+
1917
+ library.geometries[xml.getAttribute('id')] = data;
1918
+
1919
+ }
1920
+
1921
+ function parseSource(xml) {
1922
+
1923
+ const data = {
1924
+ array: [],
1925
+ stride: 3
1926
+ };
1927
+
1928
+ for (let i = 0; i < xml.childNodes.length; i++) {
1929
+
1930
+ const child = xml.childNodes[i];
1931
+ if (child.nodeType !== 1) continue;
1932
+
1933
+ switch (child.nodeName) {
1934
+
1935
+ case 'float_array':
1936
+ data.array = parseFloats(child.textContent);
1937
+ break;
1938
+
1939
+ case 'Name_array':
1940
+ data.array = parseStrings(child.textContent);
1941
+ break;
1942
+
1943
+ case 'technique_common':
1944
+ const accessor = getElementsByTagName(child, 'accessor')[0];
1945
+
1946
+ if (accessor !== undefined) {
1947
+
1948
+ data.stride = parseInt(accessor.getAttribute('stride'));
1949
+
1950
+ }
1951
+
1952
+ break;
1953
+
1954
+ }
1955
+
1956
+ }
1957
+
1958
+ return data;
1959
+
1960
+ }
1961
+
1962
+ function parseGeometryVertices(xml) {
1963
+
1964
+ const data = {};
1965
+
1966
+ for (let i = 0; i < xml.childNodes.length; i++) {
1967
+
1968
+ const child = xml.childNodes[i];
1969
+ if (child.nodeType !== 1) continue;
1970
+ data[child.getAttribute('semantic')] = parseId(child.getAttribute('source'));
1971
+
1972
+ }
1973
+
1974
+ return data;
1975
+
1976
+ }
1977
+
1978
+ function parseGeometryPrimitive(xml) {
1979
+
1980
+ const primitive = {
1981
+ type: xml.nodeName,
1982
+ material: xml.getAttribute('material'),
1983
+ count: parseInt(xml.getAttribute('count')),
1984
+ inputs: {},
1985
+ stride: 0,
1986
+ hasUV: false
1987
+ };
1988
+
1989
+ for (let i = 0, l = xml.childNodes.length; i < l; i++) {
1990
+
1991
+ const child = xml.childNodes[i];
1992
+ if (child.nodeType !== 1) continue;
1993
+
1994
+ switch (child.nodeName) {
1995
+
1996
+ case 'input':
1997
+ const id = parseId(child.getAttribute('source'));
1998
+ const semantic = child.getAttribute('semantic');
1999
+ const offset = parseInt(child.getAttribute('offset'));
2000
+ const set = parseInt(child.getAttribute('set'));
2001
+ const inputname = set > 0 ? semantic + set : semantic;
2002
+ primitive.inputs[inputname] = {
2003
+ id: id,
2004
+ offset: offset
2005
+ };
2006
+ primitive.stride = Math.max(primitive.stride, offset + 1);
2007
+ if (semantic === 'TEXCOORD') primitive.hasUV = true;
2008
+ break;
2009
+
2010
+ case 'vcount':
2011
+ primitive.vcount = parseInts(child.textContent);
2012
+ break;
2013
+
2014
+ case 'p':
2015
+ primitive.p = parseInts(child.textContent);
2016
+ break;
2017
+
2018
+ }
2019
+
2020
+ }
2021
+
2022
+ return primitive;
2023
+
2024
+ }
2025
+
2026
+ function groupPrimitives(primitives) {
2027
+
2028
+ const build = {};
2029
+
2030
+ for (let i = 0; i < primitives.length; i++) {
2031
+
2032
+ const primitive = primitives[i];
2033
+ if (build[primitive.type] === undefined) build[primitive.type] = [];
2034
+ build[primitive.type].push(primitive);
2035
+
2036
+ }
2037
+
2038
+ return build;
2039
+
2040
+ }
2041
+
2042
+ function checkUVCoordinates(primitives) {
2043
+
2044
+ let count = 0;
2045
+
2046
+ for (let i = 0, l = primitives.length; i < l; i++) {
2047
+
2048
+ const primitive = primitives[i];
2049
+
2050
+ if (primitive.hasUV === true) {
2051
+
2052
+ count++;
2053
+
2054
+ }
2055
+
2056
+ }
2057
+
2058
+ if (count > 0 && count < primitives.length) {
2059
+
2060
+ primitives.uvsNeedsFix = true;
2061
+
2062
+ }
2063
+
2064
+ }
2065
+
2066
+ function buildGeometry(data) {
2067
+
2068
+ const build = {};
2069
+ const sources = data.sources;
2070
+ const vertices = data.vertices;
2071
+ const primitives = data.primitives;
2072
+ if (primitives.length === 0) return {}; // our goal is to create one buffer geometry for a single type of primitives
2073
+ // first, we group all primitives by their type
2074
+
2075
+ const groupedPrimitives = groupPrimitives(primitives);
2076
+
2077
+ for (const type in groupedPrimitives) {
2078
+
2079
+ const primitiveType = groupedPrimitives[type]; // second, ensure consistent uv coordinates for each type of primitives (polylist,triangles or lines)
2080
+
2081
+ checkUVCoordinates(primitiveType); // third, create a buffer geometry for each type of primitives
2082
+
2083
+ build[type] = buildGeometryType(primitiveType, sources, vertices);
2084
+
2085
+ }
2086
+
2087
+ return build;
2088
+
2089
+ }
2090
+
2091
+ function buildGeometryType(primitives, sources, vertices) {
2092
+
2093
+ const build = {};
2094
+ const position = {
2095
+ array: [],
2096
+ stride: 0
2097
+ };
2098
+ const normal = {
2099
+ array: [],
2100
+ stride: 0
2101
+ };
2102
+ const uv = {
2103
+ array: [],
2104
+ stride: 0
2105
+ };
2106
+ const uv2 = {
2107
+ array: [],
2108
+ stride: 0
2109
+ };
2110
+ const color = {
2111
+ array: [],
2112
+ stride: 0
2113
+ };
2114
+ const skinIndex = {
2115
+ array: [],
2116
+ stride: 4
2117
+ };
2118
+ const skinWeight = {
2119
+ array: [],
2120
+ stride: 4
2121
+ };
2122
+ const geometry = new THREE.BufferGeometry();
2123
+ const materialKeys = [];
2124
+ let start = 0;
2125
+
2126
+ for (let p = 0; p < primitives.length; p++) {
2127
+
2128
+ const primitive = primitives[p];
2129
+ const inputs = primitive.inputs; // groups
2130
+
2131
+ let count = 0;
2132
+
2133
+ switch (primitive.type) {
2134
+
2135
+ case 'lines':
2136
+ case 'linestrips':
2137
+ count = primitive.count * 2;
2138
+ break;
2139
+
2140
+ case 'triangles':
2141
+ count = primitive.count * 3;
2142
+ break;
2143
+
2144
+ case 'polylist':
2145
+ for (let g = 0; g < primitive.count; g++) {
2146
+
2147
+ const vc = primitive.vcount[g];
2148
+
2149
+ switch (vc) {
2150
+
2151
+ case 3:
2152
+ count += 3; // single triangle
2153
+
2154
+ break;
2155
+
2156
+ case 4:
2157
+ count += 6; // quad, subdivided into two triangles
2158
+
2159
+ break;
2160
+
2161
+ default:
2162
+ count += (vc - 2) * 3; // polylist with more than four vertices
2163
+
2164
+ break;
2165
+
2166
+ }
2167
+
2168
+ }
2169
+
2170
+ break;
2171
+
2172
+ default:
2173
+ console.warn('THREE.ColladaLoader: Unknow primitive type:', primitive.type);
2174
+
2175
+ }
2176
+
2177
+ geometry.addGroup(start, count, p);
2178
+ start += count; // material
2179
+
2180
+ if (primitive.material) {
2181
+
2182
+ materialKeys.push(primitive.material);
2183
+
2184
+ } // geometry data
2185
+
2186
+
2187
+ for (const name in inputs) {
2188
+
2189
+ const input = inputs[name];
2190
+
2191
+ switch (name) {
2192
+
2193
+ case 'VERTEX':
2194
+ for (const key in vertices) {
2195
+
2196
+ const id = vertices[key];
2197
+
2198
+ switch (key) {
2199
+
2200
+ case 'POSITION':
2201
+ const prevLength = position.array.length;
2202
+ buildGeometryData(primitive, sources[id], input.offset, position.array);
2203
+ position.stride = sources[id].stride;
2204
+
2205
+ if (sources.skinWeights && sources.skinIndices) {
2206
+
2207
+ buildGeometryData(primitive, sources.skinIndices, input.offset, skinIndex.array);
2208
+ buildGeometryData(primitive, sources.skinWeights, input.offset, skinWeight.array);
2209
+
2210
+ } // see #3803
2211
+
2212
+
2213
+ if (primitive.hasUV === false && primitives.uvsNeedsFix === true) {
2214
+
2215
+ const count = (position.array.length - prevLength) / position.stride;
2216
+
2217
+ for (let i = 0; i < count; i++) {
2218
+
2219
+ // fill missing uv coordinates
2220
+ uv.array.push(0, 0);
2221
+
2222
+ }
2223
+
2224
+ }
2225
+
2226
+ break;
2227
+
2228
+ case 'NORMAL':
2229
+ buildGeometryData(primitive, sources[id], input.offset, normal.array);
2230
+ normal.stride = sources[id].stride;
2231
+ break;
2232
+
2233
+ case 'COLOR':
2234
+ buildGeometryData(primitive, sources[id], input.offset, color.array);
2235
+ color.stride = sources[id].stride;
2236
+ break;
2237
+
2238
+ case 'TEXCOORD':
2239
+ buildGeometryData(primitive, sources[id], input.offset, uv.array);
2240
+ uv.stride = sources[id].stride;
2241
+ break;
2242
+
2243
+ case 'TEXCOORD1':
2244
+ buildGeometryData(primitive, sources[id], input.offset, uv2.array);
2245
+ uv.stride = sources[id].stride;
2246
+ break;
2247
+
2248
+ default:
2249
+ console.warn('THREE.ColladaLoader: Semantic "%s" not handled in geometry build process.', key);
2250
+
2251
+ }
2252
+
2253
+ }
2254
+
2255
+ break;
2256
+
2257
+ case 'NORMAL':
2258
+ buildGeometryData(primitive, sources[input.id], input.offset, normal.array);
2259
+ normal.stride = sources[input.id].stride;
2260
+ break;
2261
+
2262
+ case 'COLOR':
2263
+ buildGeometryData(primitive, sources[input.id], input.offset, color.array);
2264
+ color.stride = sources[input.id].stride;
2265
+ break;
2266
+
2267
+ case 'TEXCOORD':
2268
+ buildGeometryData(primitive, sources[input.id], input.offset, uv.array);
2269
+ uv.stride = sources[input.id].stride;
2270
+ break;
2271
+
2272
+ case 'TEXCOORD1':
2273
+ buildGeometryData(primitive, sources[input.id], input.offset, uv2.array);
2274
+ uv2.stride = sources[input.id].stride;
2275
+ break;
2276
+
2277
+ }
2278
+
2279
+ }
2280
+
2281
+ } // build geometry
2282
+
2283
+
2284
+ if (position.array.length > 0) geometry.setAttribute('position', new THREE.Float32BufferAttribute(position.array, position.stride));
2285
+ if (normal.array.length > 0) geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normal.array, normal.stride));
2286
+ if (color.array.length > 0) geometry.setAttribute('color', new THREE.Float32BufferAttribute(color.array, color.stride));
2287
+ if (uv.array.length > 0) geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uv.array, uv.stride));
2288
+ if (uv2.array.length > 0) geometry.setAttribute('uv2', new THREE.Float32BufferAttribute(uv2.array, uv2.stride));
2289
+ if (skinIndex.array.length > 0) geometry.setAttribute('skinIndex', new THREE.Float32BufferAttribute(skinIndex.array, skinIndex.stride));
2290
+ if (skinWeight.array.length > 0) geometry.setAttribute('skinWeight', new THREE.Float32BufferAttribute(skinWeight.array, skinWeight.stride));
2291
+ build.data = geometry;
2292
+ build.type = primitives[0].type;
2293
+ build.materialKeys = materialKeys;
2294
+ return build;
2295
+
2296
+ }
2297
+
2298
+ function buildGeometryData(primitive, source, offset, array) {
2299
+
2300
+ const indices = primitive.p;
2301
+ const stride = primitive.stride;
2302
+ const vcount = primitive.vcount;
2303
+
2304
+ function pushVector(i) {
2305
+
2306
+ let index = indices[i + offset] * sourceStride;
2307
+ const length = index + sourceStride;
2308
+
2309
+ for (; index < length; index++) {
2310
+
2311
+ array.push(sourceArray[index]);
2312
+
2313
+ }
2314
+
2315
+ }
2316
+
2317
+ const sourceArray = source.array;
2318
+ const sourceStride = source.stride;
2319
+
2320
+ if (primitive.vcount !== undefined) {
2321
+
2322
+ let index = 0;
2323
+
2324
+ for (let i = 0, l = vcount.length; i < l; i++) {
2325
+
2326
+ const count = vcount[i];
2327
+
2328
+ if (count === 4) {
2329
+
2330
+ const a = index + stride * 0;
2331
+ const b = index + stride * 1;
2332
+ const c = index + stride * 2;
2333
+ const d = index + stride * 3;
2334
+ pushVector(a);
2335
+ pushVector(b);
2336
+ pushVector(d);
2337
+ pushVector(b);
2338
+ pushVector(c);
2339
+ pushVector(d);
2340
+
2341
+ } else if (count === 3) {
2342
+
2343
+ const a = index + stride * 0;
2344
+ const b = index + stride * 1;
2345
+ const c = index + stride * 2;
2346
+ pushVector(a);
2347
+ pushVector(b);
2348
+ pushVector(c);
2349
+
2350
+ } else if (count > 4) {
2351
+
2352
+ for (let k = 1, kl = count - 2; k <= kl; k++) {
2353
+
2354
+ const a = index + stride * 0;
2355
+ const b = index + stride * k;
2356
+ const c = index + stride * (k + 1);
2357
+ pushVector(a);
2358
+ pushVector(b);
2359
+ pushVector(c);
2360
+
2361
+ }
2362
+
2363
+ }
2364
+
2365
+ index += stride * count;
2366
+
2367
+ }
2368
+
2369
+ } else {
2370
+
2371
+ for (let i = 0, l = indices.length; i < l; i += stride) {
2372
+
2373
+ pushVector(i);
2374
+
2375
+ }
2376
+
2377
+ }
2378
+
2379
+ }
2380
+
2381
+ function getGeometry(id) {
2382
+
2383
+ return getBuild(library.geometries[id], buildGeometry);
2384
+
2385
+ } // kinematics
2386
+
2387
+
2388
+ function parseKinematicsModel(xml) {
2389
+
2390
+ const data = {
2391
+ name: xml.getAttribute('name') || '',
2392
+ joints: {},
2393
+ links: []
2394
+ };
2395
+
2396
+ for (let i = 0; i < xml.childNodes.length; i++) {
2397
+
2398
+ const child = xml.childNodes[i];
2399
+ if (child.nodeType !== 1) continue;
2400
+
2401
+ switch (child.nodeName) {
2402
+
2403
+ case 'technique_common':
2404
+ parseKinematicsTechniqueCommon(child, data);
2405
+ break;
2406
+
2407
+ }
2408
+
2409
+ }
2410
+
2411
+ library.kinematicsModels[xml.getAttribute('id')] = data;
2412
+
2413
+ }
2414
+
2415
+ function buildKinematicsModel(data) {
2416
+
2417
+ if (data.build !== undefined) return data.build;
2418
+ return data;
2419
+
2420
+ }
2421
+
2422
+ function getKinematicsModel(id) {
2423
+
2424
+ return getBuild(library.kinematicsModels[id], buildKinematicsModel);
2425
+
2426
+ }
2427
+
2428
+ function parseKinematicsTechniqueCommon(xml, data) {
2429
+
2430
+ for (let i = 0; i < xml.childNodes.length; i++) {
2431
+
2432
+ const child = xml.childNodes[i];
2433
+ if (child.nodeType !== 1) continue;
2434
+
2435
+ switch (child.nodeName) {
2436
+
2437
+ case 'joint':
2438
+ data.joints[child.getAttribute('sid')] = parseKinematicsJoint(child);
2439
+ break;
2440
+
2441
+ case 'link':
2442
+ data.links.push(parseKinematicsLink(child));
2443
+ break;
2444
+
2445
+ }
2446
+
2447
+ }
2448
+
2449
+ }
2450
+
2451
+ function parseKinematicsJoint(xml) {
2452
+
2453
+ let data;
2454
+
2455
+ for (let i = 0; i < xml.childNodes.length; i++) {
2456
+
2457
+ const child = xml.childNodes[i];
2458
+ if (child.nodeType !== 1) continue;
2459
+
2460
+ switch (child.nodeName) {
2461
+
2462
+ case 'prismatic':
2463
+ case 'revolute':
2464
+ data = parseKinematicsJointParameter(child);
2465
+ break;
2466
+
2467
+ }
2468
+
2469
+ }
2470
+
2471
+ return data;
2472
+
2473
+ }
2474
+
2475
+ function parseKinematicsJointParameter(xml) {
2476
+
2477
+ const data = {
2478
+ sid: xml.getAttribute('sid'),
2479
+ name: xml.getAttribute('name') || '',
2480
+ axis: new THREE.Vector3(),
2481
+ limits: {
2482
+ min: 0,
2483
+ max: 0
2484
+ },
2485
+ type: xml.nodeName,
2486
+ static: false,
2487
+ zeroPosition: 0,
2488
+ middlePosition: 0
2489
+ };
2490
+
2491
+ for (let i = 0; i < xml.childNodes.length; i++) {
2492
+
2493
+ const child = xml.childNodes[i];
2494
+ if (child.nodeType !== 1) continue;
2495
+
2496
+ switch (child.nodeName) {
2497
+
2498
+ case 'axis':
2499
+ const array = parseFloats(child.textContent);
2500
+ data.axis.fromArray(array);
2501
+ break;
2502
+
2503
+ case 'limits':
2504
+ const max = child.getElementsByTagName('max')[0];
2505
+ const min = child.getElementsByTagName('min')[0];
2506
+ data.limits.max = parseFloat(max.textContent);
2507
+ data.limits.min = parseFloat(min.textContent);
2508
+ break;
2509
+
2510
+ }
2511
+
2512
+ } // if min is equal to or greater than max, consider the joint static
2513
+
2514
+
2515
+ if (data.limits.min >= data.limits.max) {
2516
+
2517
+ data.static = true;
2518
+
2519
+ } // calculate middle position
2520
+
2521
+
2522
+ data.middlePosition = (data.limits.min + data.limits.max) / 2.0;
2523
+ return data;
2524
+
2525
+ }
2526
+
2527
+ function parseKinematicsLink(xml) {
2528
+
2529
+ const data = {
2530
+ sid: xml.getAttribute('sid'),
2531
+ name: xml.getAttribute('name') || '',
2532
+ attachments: [],
2533
+ transforms: []
2534
+ };
2535
+
2536
+ for (let i = 0; i < xml.childNodes.length; i++) {
2537
+
2538
+ const child = xml.childNodes[i];
2539
+ if (child.nodeType !== 1) continue;
2540
+
2541
+ switch (child.nodeName) {
2542
+
2543
+ case 'attachment_full':
2544
+ data.attachments.push(parseKinematicsAttachment(child));
2545
+ break;
2546
+
2547
+ case 'matrix':
2548
+ case 'translate':
2549
+ case 'rotate':
2550
+ data.transforms.push(parseKinematicsTransform(child));
2551
+ break;
2552
+
2553
+ }
2554
+
2555
+ }
2556
+
2557
+ return data;
2558
+
2559
+ }
2560
+
2561
+ function parseKinematicsAttachment(xml) {
2562
+
2563
+ const data = {
2564
+ joint: xml.getAttribute('joint').split('/').pop(),
2565
+ transforms: [],
2566
+ links: []
2567
+ };
2568
+
2569
+ for (let i = 0; i < xml.childNodes.length; i++) {
2570
+
2571
+ const child = xml.childNodes[i];
2572
+ if (child.nodeType !== 1) continue;
2573
+
2574
+ switch (child.nodeName) {
2575
+
2576
+ case 'link':
2577
+ data.links.push(parseKinematicsLink(child));
2578
+ break;
2579
+
2580
+ case 'matrix':
2581
+ case 'translate':
2582
+ case 'rotate':
2583
+ data.transforms.push(parseKinematicsTransform(child));
2584
+ break;
2585
+
2586
+ }
2587
+
2588
+ }
2589
+
2590
+ return data;
2591
+
2592
+ }
2593
+
2594
+ function parseKinematicsTransform(xml) {
2595
+
2596
+ const data = {
2597
+ type: xml.nodeName
2598
+ };
2599
+ const array = parseFloats(xml.textContent);
2600
+
2601
+ switch (data.type) {
2602
+
2603
+ case 'matrix':
2604
+ data.obj = new THREE.Matrix4();
2605
+ data.obj.fromArray(array).transpose();
2606
+ break;
2607
+
2608
+ case 'translate':
2609
+ data.obj = new THREE.Vector3();
2610
+ data.obj.fromArray(array);
2611
+ break;
2612
+
2613
+ case 'rotate':
2614
+ data.obj = new THREE.Vector3();
2615
+ data.obj.fromArray(array);
2616
+ data.angle = THREE.MathUtils.degToRad(array[3]);
2617
+ break;
2618
+
2619
+ }
2620
+
2621
+ return data;
2622
+
2623
+ } // physics
2624
+
2625
+
2626
+ function parsePhysicsModel(xml) {
2627
+
2628
+ const data = {
2629
+ name: xml.getAttribute('name') || '',
2630
+ rigidBodies: {}
2631
+ };
2632
+
2633
+ for (let i = 0; i < xml.childNodes.length; i++) {
2634
+
2635
+ const child = xml.childNodes[i];
2636
+ if (child.nodeType !== 1) continue;
2637
+
2638
+ switch (child.nodeName) {
2639
+
2640
+ case 'rigid_body':
2641
+ data.rigidBodies[child.getAttribute('name')] = {};
2642
+ parsePhysicsRigidBody(child, data.rigidBodies[child.getAttribute('name')]);
2643
+ break;
2644
+
2645
+ }
2646
+
2647
+ }
2648
+
2649
+ library.physicsModels[xml.getAttribute('id')] = data;
2650
+
2651
+ }
2652
+
2653
+ function parsePhysicsRigidBody(xml, data) {
2654
+
2655
+ for (let i = 0; i < xml.childNodes.length; i++) {
2656
+
2657
+ const child = xml.childNodes[i];
2658
+ if (child.nodeType !== 1) continue;
2659
+
2660
+ switch (child.nodeName) {
2661
+
2662
+ case 'technique_common':
2663
+ parsePhysicsTechniqueCommon(child, data);
2664
+ break;
2665
+
2666
+ }
2667
+
2668
+ }
2669
+
2670
+ }
2671
+
2672
+ function parsePhysicsTechniqueCommon(xml, data) {
2673
+
2674
+ for (let i = 0; i < xml.childNodes.length; i++) {
2675
+
2676
+ const child = xml.childNodes[i];
2677
+ if (child.nodeType !== 1) continue;
2678
+
2679
+ switch (child.nodeName) {
2680
+
2681
+ case 'inertia':
2682
+ data.inertia = parseFloats(child.textContent);
2683
+ break;
2684
+
2685
+ case 'mass':
2686
+ data.mass = parseFloats(child.textContent)[0];
2687
+ break;
2688
+
2689
+ }
2690
+
2691
+ }
2692
+
2693
+ } // scene
2694
+
2695
+
2696
+ function parseKinematicsScene(xml) {
2697
+
2698
+ const data = {
2699
+ bindJointAxis: []
2700
+ };
2701
+
2702
+ for (let i = 0; i < xml.childNodes.length; i++) {
2703
+
2704
+ const child = xml.childNodes[i];
2705
+ if (child.nodeType !== 1) continue;
2706
+
2707
+ switch (child.nodeName) {
2708
+
2709
+ case 'bind_joint_axis':
2710
+ data.bindJointAxis.push(parseKinematicsBindJointAxis(child));
2711
+ break;
2712
+
2713
+ }
2714
+
2715
+ }
2716
+
2717
+ library.kinematicsScenes[parseId(xml.getAttribute('url'))] = data;
2718
+
2719
+ }
2720
+
2721
+ function parseKinematicsBindJointAxis(xml) {
2722
+
2723
+ const data = {
2724
+ target: xml.getAttribute('target').split('/').pop()
2725
+ };
2726
+
2727
+ for (let i = 0; i < xml.childNodes.length; i++) {
2728
+
2729
+ const child = xml.childNodes[i];
2730
+ if (child.nodeType !== 1) continue;
2731
+
2732
+ switch (child.nodeName) {
2733
+
2734
+ case 'axis':
2735
+ const param = child.getElementsByTagName('param')[0];
2736
+ data.axis = param.textContent;
2737
+ const tmpJointIndex = data.axis.split('inst_').pop().split('axis')[0];
2738
+ data.jointIndex = tmpJointIndex.substr(0, tmpJointIndex.length - 1);
2739
+ break;
2740
+
2741
+ }
2742
+
2743
+ }
2744
+
2745
+ return data;
2746
+
2747
+ }
2748
+
2749
+ function buildKinematicsScene(data) {
2750
+
2751
+ if (data.build !== undefined) return data.build;
2752
+ return data;
2753
+
2754
+ }
2755
+
2756
+ function getKinematicsScene(id) {
2757
+
2758
+ return getBuild(library.kinematicsScenes[id], buildKinematicsScene);
2759
+
2760
+ }
2761
+
2762
+ function setupKinematics() {
2763
+
2764
+ const kinematicsModelId = Object.keys(library.kinematicsModels)[0];
2765
+ const kinematicsSceneId = Object.keys(library.kinematicsScenes)[0];
2766
+ const visualSceneId = Object.keys(library.visualScenes)[0];
2767
+ if (kinematicsModelId === undefined || kinematicsSceneId === undefined) return;
2768
+ const kinematicsModel = getKinematicsModel(kinematicsModelId);
2769
+ const kinematicsScene = getKinematicsScene(kinematicsSceneId);
2770
+ const visualScene = getVisualScene(visualSceneId);
2771
+ const bindJointAxis = kinematicsScene.bindJointAxis;
2772
+ const jointMap = {};
2773
+
2774
+ for (let i = 0, l = bindJointAxis.length; i < l; i++) {
2775
+
2776
+ const axis = bindJointAxis[i]; // the result of the following query is an element of type 'translate', 'rotate','scale' or 'matrix'
2777
+
2778
+ const targetElement = collada.querySelector('[sid="' + axis.target + '"]');
2779
+
2780
+ if (targetElement) {
2781
+
2782
+ // get the parent of the transform element
2783
+ const parentVisualElement = targetElement.parentElement; // connect the joint of the kinematics model with the element in the visual scene
2784
+
2785
+ connect(axis.jointIndex, parentVisualElement);
2786
+
2787
+ }
2788
+
2789
+ }
2790
+
2791
+ function connect(jointIndex, visualElement) {
2792
+
2793
+ const visualElementName = visualElement.getAttribute('name');
2794
+ const joint = kinematicsModel.joints[jointIndex];
2795
+ visualScene.traverse(function (object) {
2796
+
2797
+ if (object.name === visualElementName) {
2798
+
2799
+ jointMap[jointIndex] = {
2800
+ object: object,
2801
+ transforms: buildTransformList(visualElement),
2802
+ joint: joint,
2803
+ position: joint.zeroPosition
2804
+ };
2805
+
2806
+ }
2807
+
2808
+ });
2809
+
2810
+ }
2811
+
2812
+ const m0 = new THREE.Matrix4();
2813
+ kinematics = {
2814
+ joints: kinematicsModel && kinematicsModel.joints,
2815
+ getJointValue: function (jointIndex) {
2816
+
2817
+ const jointData = jointMap[jointIndex];
2818
+
2819
+ if (jointData) {
2820
+
2821
+ return jointData.position;
2822
+
2823
+ } else {
2824
+
2825
+ console.warn('THREE.ColladaLoader: Joint ' + jointIndex + ' doesn\'t exist.');
2826
+
2827
+ }
2828
+
2829
+ },
2830
+ setJointValue: function (jointIndex, value) {
2831
+
2832
+ const jointData = jointMap[jointIndex];
2833
+
2834
+ if (jointData) {
2835
+
2836
+ const joint = jointData.joint;
2837
+
2838
+ if (value > joint.limits.max || value < joint.limits.min) {
2839
+
2840
+ console.warn('THREE.ColladaLoader: Joint ' + jointIndex + ' value ' + value + ' outside of limits (min: ' + joint.limits.min + ', max: ' + joint.limits.max + ').');
2841
+
2842
+ } else if (joint.static) {
2843
+
2844
+ console.warn('THREE.ColladaLoader: Joint ' + jointIndex + ' is static.');
2845
+
2846
+ } else {
2847
+
2848
+ const object = jointData.object;
2849
+ const axis = joint.axis;
2850
+ const transforms = jointData.transforms;
2851
+ matrix.identity(); // each update, we have to apply all transforms in the correct order
2852
+
2853
+ for (let i = 0; i < transforms.length; i++) {
2854
+
2855
+ const transform = transforms[i]; // if there is a connection of the transform node with a joint, apply the joint value
2856
+
2857
+ if (transform.sid && transform.sid.indexOf(jointIndex) !== - 1) {
2858
+
2859
+ switch (joint.type) {
2860
+
2861
+ case 'revolute':
2862
+ matrix.multiply(m0.makeRotationAxis(axis, THREE.MathUtils.degToRad(value)));
2863
+ break;
2864
+
2865
+ case 'prismatic':
2866
+ matrix.multiply(m0.makeTranslation(axis.x * value, axis.y * value, axis.z * value));
2867
+ break;
2868
+
2869
+ default:
2870
+ console.warn('THREE.ColladaLoader: Unknown joint type: ' + joint.type);
2871
+ break;
2872
+
2873
+ }
2874
+
2875
+ } else {
2876
+
2877
+ switch (transform.type) {
2878
+
2879
+ case 'matrix':
2880
+ matrix.multiply(transform.obj);
2881
+ break;
2882
+
2883
+ case 'translate':
2884
+ matrix.multiply(m0.makeTranslation(transform.obj.x, transform.obj.y, transform.obj.z));
2885
+ break;
2886
+
2887
+ case 'scale':
2888
+ matrix.scale(transform.obj);
2889
+ break;
2890
+
2891
+ case 'rotate':
2892
+ matrix.multiply(m0.makeRotationAxis(transform.obj, transform.angle));
2893
+ break;
2894
+
2895
+ }
2896
+
2897
+ }
2898
+
2899
+ }
2900
+
2901
+ object.matrix.copy(matrix);
2902
+ object.matrix.decompose(object.position, object.quaternion, object.scale);
2903
+ jointMap[jointIndex].position = value;
2904
+
2905
+ }
2906
+
2907
+ } else {
2908
+
2909
+ console.log('THREE.ColladaLoader: ' + jointIndex + ' does not exist.');
2910
+
2911
+ }
2912
+
2913
+ }
2914
+ };
2915
+
2916
+ }
2917
+
2918
+ function buildTransformList(node) {
2919
+
2920
+ const transforms = [];
2921
+ const xml = collada.querySelector('[id="' + node.id + '"]');
2922
+
2923
+ for (let i = 0; i < xml.childNodes.length; i++) {
2924
+
2925
+ const child = xml.childNodes[i];
2926
+ if (child.nodeType !== 1) continue;
2927
+ let array, vector;
2928
+
2929
+ switch (child.nodeName) {
2930
+
2931
+ case 'matrix':
2932
+ array = parseFloats(child.textContent);
2933
+ const matrix = new THREE.Matrix4().fromArray(array).transpose();
2934
+ transforms.push({
2935
+ sid: child.getAttribute('sid'),
2936
+ type: child.nodeName,
2937
+ obj: matrix
2938
+ });
2939
+ break;
2940
+
2941
+ case 'translate':
2942
+ case 'scale':
2943
+ array = parseFloats(child.textContent);
2944
+ vector = new THREE.Vector3().fromArray(array);
2945
+ transforms.push({
2946
+ sid: child.getAttribute('sid'),
2947
+ type: child.nodeName,
2948
+ obj: vector
2949
+ });
2950
+ break;
2951
+
2952
+ case 'rotate':
2953
+ array = parseFloats(child.textContent);
2954
+ vector = new THREE.Vector3().fromArray(array);
2955
+ const angle = THREE.MathUtils.degToRad(array[3]);
2956
+ transforms.push({
2957
+ sid: child.getAttribute('sid'),
2958
+ type: child.nodeName,
2959
+ obj: vector,
2960
+ angle: angle
2961
+ });
2962
+ break;
2963
+
2964
+ }
2965
+
2966
+ }
2967
+
2968
+ return transforms;
2969
+
2970
+ } // nodes
2971
+
2972
+
2973
+ function prepareNodes(xml) {
2974
+
2975
+ const elements = xml.getElementsByTagName('node'); // ensure all node elements have id attributes
2976
+
2977
+ for (let i = 0; i < elements.length; i++) {
2978
+
2979
+ const element = elements[i];
2980
+
2981
+ if (element.hasAttribute('id') === false) {
2982
+
2983
+ element.setAttribute('id', generateId());
2984
+
2985
+ }
2986
+
2987
+ }
2988
+
2989
+ }
2990
+
2991
+ const matrix = new THREE.Matrix4();
2992
+ const vector = new THREE.Vector3();
2993
+
2994
+ function parseNode(xml) {
2995
+
2996
+ const data = {
2997
+ name: xml.getAttribute('name') || '',
2998
+ type: xml.getAttribute('type'),
2999
+ id: xml.getAttribute('id'),
3000
+ sid: xml.getAttribute('sid'),
3001
+ matrix: new THREE.Matrix4(),
3002
+ nodes: [],
3003
+ instanceCameras: [],
3004
+ instanceControllers: [],
3005
+ instanceLights: [],
3006
+ instanceGeometries: [],
3007
+ instanceNodes: [],
3008
+ transforms: {}
3009
+ };
3010
+
3011
+ for (let i = 0; i < xml.childNodes.length; i++) {
3012
+
3013
+ const child = xml.childNodes[i];
3014
+ if (child.nodeType !== 1) continue;
3015
+ let array;
3016
+
3017
+ switch (child.nodeName) {
3018
+
3019
+ case 'node':
3020
+ data.nodes.push(child.getAttribute('id'));
3021
+ parseNode(child);
3022
+ break;
3023
+
3024
+ case 'instance_camera':
3025
+ data.instanceCameras.push(parseId(child.getAttribute('url')));
3026
+ break;
3027
+
3028
+ case 'instance_controller':
3029
+ data.instanceControllers.push(parseNodeInstance(child));
3030
+ break;
3031
+
3032
+ case 'instance_light':
3033
+ data.instanceLights.push(parseId(child.getAttribute('url')));
3034
+ break;
3035
+
3036
+ case 'instance_geometry':
3037
+ data.instanceGeometries.push(parseNodeInstance(child));
3038
+ break;
3039
+
3040
+ case 'instance_node':
3041
+ data.instanceNodes.push(parseId(child.getAttribute('url')));
3042
+ break;
3043
+
3044
+ case 'matrix':
3045
+ array = parseFloats(child.textContent);
3046
+ data.matrix.multiply(matrix.fromArray(array).transpose());
3047
+ data.transforms[child.getAttribute('sid')] = child.nodeName;
3048
+ break;
3049
+
3050
+ case 'translate':
3051
+ array = parseFloats(child.textContent);
3052
+ vector.fromArray(array);
3053
+ data.matrix.multiply(matrix.makeTranslation(vector.x, vector.y, vector.z));
3054
+ data.transforms[child.getAttribute('sid')] = child.nodeName;
3055
+ break;
3056
+
3057
+ case 'rotate':
3058
+ array = parseFloats(child.textContent);
3059
+ const angle = THREE.MathUtils.degToRad(array[3]);
3060
+ data.matrix.multiply(matrix.makeRotationAxis(vector.fromArray(array), angle));
3061
+ data.transforms[child.getAttribute('sid')] = child.nodeName;
3062
+ break;
3063
+
3064
+ case 'scale':
3065
+ array = parseFloats(child.textContent);
3066
+ data.matrix.scale(vector.fromArray(array));
3067
+ data.transforms[child.getAttribute('sid')] = child.nodeName;
3068
+ break;
3069
+
3070
+ case 'extra':
3071
+ break;
3072
+
3073
+ default:
3074
+ console.log(child);
3075
+
3076
+ }
3077
+
3078
+ }
3079
+
3080
+ if (hasNode(data.id)) {
3081
+
3082
+ console.warn('THREE.ColladaLoader: There is already a node with ID %s. Exclude current node from further processing.', data.id);
3083
+
3084
+ } else {
3085
+
3086
+ library.nodes[data.id] = data;
3087
+
3088
+ }
3089
+
3090
+ return data;
3091
+
3092
+ }
3093
+
3094
+ function parseNodeInstance(xml) {
3095
+
3096
+ const data = {
3097
+ id: parseId(xml.getAttribute('url')),
3098
+ materials: {},
3099
+ skeletons: []
3100
+ };
3101
+
3102
+ for (let i = 0; i < xml.childNodes.length; i++) {
3103
+
3104
+ const child = xml.childNodes[i];
3105
+
3106
+ switch (child.nodeName) {
3107
+
3108
+ case 'bind_material':
3109
+ const instances = child.getElementsByTagName('instance_material');
3110
+
3111
+ for (let j = 0; j < instances.length; j++) {
3112
+
3113
+ const instance = instances[j];
3114
+ const symbol = instance.getAttribute('symbol');
3115
+ const target = instance.getAttribute('target');
3116
+ data.materials[symbol] = parseId(target);
3117
+
3118
+ }
3119
+
3120
+ break;
3121
+
3122
+ case 'skeleton':
3123
+ data.skeletons.push(parseId(child.textContent));
3124
+ break;
3125
+
3126
+ default:
3127
+ break;
3128
+
3129
+ }
3130
+
3131
+ }
3132
+
3133
+ return data;
3134
+
3135
+ }
3136
+
3137
+ function buildSkeleton(skeletons, joints) {
3138
+
3139
+ const boneData = [];
3140
+ const sortedBoneData = [];
3141
+ let i, j, data; // a skeleton can have multiple root bones. collada expresses this
3142
+ // situtation with multiple "skeleton" tags per controller instance
3143
+
3144
+ for (i = 0; i < skeletons.length; i++) {
3145
+
3146
+ const skeleton = skeletons[i];
3147
+ let root;
3148
+
3149
+ if (hasNode(skeleton)) {
3150
+
3151
+ root = getNode(skeleton);
3152
+ buildBoneHierarchy(root, joints, boneData);
3153
+
3154
+ } else if (hasVisualScene(skeleton)) {
3155
+
3156
+ // handle case where the skeleton refers to the visual scene (#13335)
3157
+ const visualScene = library.visualScenes[skeleton];
3158
+ const children = visualScene.children;
3159
+
3160
+ for (let j = 0; j < children.length; j++) {
3161
+
3162
+ const child = children[j];
3163
+
3164
+ if (child.type === 'JOINT') {
3165
+
3166
+ const root = getNode(child.id);
3167
+ buildBoneHierarchy(root, joints, boneData);
3168
+
3169
+ }
3170
+
3171
+ }
3172
+
3173
+ } else {
3174
+
3175
+ console.error('THREE.ColladaLoader: Unable to find root bone of skeleton with ID:', skeleton);
3176
+
3177
+ }
3178
+
3179
+ } // sort bone data (the order is defined in the corresponding controller)
3180
+
3181
+
3182
+ for (i = 0; i < joints.length; i++) {
3183
+
3184
+ for (j = 0; j < boneData.length; j++) {
3185
+
3186
+ data = boneData[j];
3187
+
3188
+ if (data.bone.name === joints[i].name) {
3189
+
3190
+ sortedBoneData[i] = data;
3191
+ data.processed = true;
3192
+ break;
3193
+
3194
+ }
3195
+
3196
+ }
3197
+
3198
+ } // add unprocessed bone data at the end of the list
3199
+
3200
+
3201
+ for (i = 0; i < boneData.length; i++) {
3202
+
3203
+ data = boneData[i];
3204
+
3205
+ if (data.processed === false) {
3206
+
3207
+ sortedBoneData.push(data);
3208
+ data.processed = true;
3209
+
3210
+ }
3211
+
3212
+ } // setup arrays for skeleton creation
3213
+
3214
+
3215
+ const bones = [];
3216
+ const boneInverses = [];
3217
+
3218
+ for (i = 0; i < sortedBoneData.length; i++) {
3219
+
3220
+ data = sortedBoneData[i];
3221
+ bones.push(data.bone);
3222
+ boneInverses.push(data.boneInverse);
3223
+
3224
+ }
3225
+
3226
+ return new THREE.Skeleton(bones, boneInverses);
3227
+
3228
+ }
3229
+
3230
+ function buildBoneHierarchy(root, joints, boneData) {
3231
+
3232
+ // setup bone data from visual scene
3233
+ root.traverse(function (object) {
3234
+
3235
+ if (object.isBone === true) {
3236
+
3237
+ let boneInverse; // retrieve the boneInverse from the controller data
3238
+
3239
+ for (let i = 0; i < joints.length; i++) {
3240
+
3241
+ const joint = joints[i];
3242
+
3243
+ if (joint.name === object.name) {
3244
+
3245
+ boneInverse = joint.boneInverse;
3246
+ break;
3247
+
3248
+ }
3249
+
3250
+ }
3251
+
3252
+ if (boneInverse === undefined) {
3253
+
3254
+ // Unfortunately, there can be joints in the visual scene that are not part of the
3255
+ // corresponding controller. In this case, we have to create a dummy boneInverse matrix
3256
+ // for the respective bone. This bone won't affect any vertices, because there are no skin indices
3257
+ // and weights defined for it. But we still have to add the bone to the sorted bone list in order to
3258
+ // ensure a correct animation of the model.
3259
+ boneInverse = new THREE.Matrix4();
3260
+
3261
+ }
3262
+
3263
+ boneData.push({
3264
+ bone: object,
3265
+ boneInverse: boneInverse,
3266
+ processed: false
3267
+ });
3268
+
3269
+ }
3270
+
3271
+ });
3272
+
3273
+ }
3274
+
3275
+ function buildNode(data) {
3276
+
3277
+ const objects = [];
3278
+ const matrix = data.matrix;
3279
+ const nodes = data.nodes;
3280
+ const type = data.type;
3281
+ const instanceCameras = data.instanceCameras;
3282
+ const instanceControllers = data.instanceControllers;
3283
+ const instanceLights = data.instanceLights;
3284
+ const instanceGeometries = data.instanceGeometries;
3285
+ const instanceNodes = data.instanceNodes; // nodes
3286
+
3287
+ for (let i = 0, l = nodes.length; i < l; i++) {
3288
+
3289
+ objects.push(getNode(nodes[i]));
3290
+
3291
+ } // instance cameras
3292
+
3293
+
3294
+ for (let i = 0, l = instanceCameras.length; i < l; i++) {
3295
+
3296
+ const instanceCamera = getCamera(instanceCameras[i]);
3297
+
3298
+ if (instanceCamera !== null) {
3299
+
3300
+ objects.push(instanceCamera.clone());
3301
+
3302
+ }
3303
+
3304
+ } // instance controllers
3305
+
3306
+
3307
+ for (let i = 0, l = instanceControllers.length; i < l; i++) {
3308
+
3309
+ const instance = instanceControllers[i];
3310
+ const controller = getController(instance.id);
3311
+ const geometries = getGeometry(controller.id);
3312
+ const newObjects = buildObjects(geometries, instance.materials);
3313
+ const skeletons = instance.skeletons;
3314
+ const joints = controller.skin.joints;
3315
+ const skeleton = buildSkeleton(skeletons, joints);
3316
+
3317
+ for (let j = 0, jl = newObjects.length; j < jl; j++) {
3318
+
3319
+ const object = newObjects[j];
3320
+
3321
+ if (object.isSkinnedMesh) {
3322
+
3323
+ object.bind(skeleton, controller.skin.bindMatrix);
3324
+ object.normalizeSkinWeights();
3325
+
3326
+ }
3327
+
3328
+ objects.push(object);
3329
+
3330
+ }
3331
+
3332
+ } // instance lights
3333
+
3334
+
3335
+ for (let i = 0, l = instanceLights.length; i < l; i++) {
3336
+
3337
+ const instanceLight = getLight(instanceLights[i]);
3338
+
3339
+ if (instanceLight !== null) {
3340
+
3341
+ objects.push(instanceLight.clone());
3342
+
3343
+ }
3344
+
3345
+ } // instance geometries
3346
+
3347
+
3348
+ for (let i = 0, l = instanceGeometries.length; i < l; i++) {
3349
+
3350
+ const instance = instanceGeometries[i]; // a single geometry instance in collada can lead to multiple object3Ds.
3351
+ // this is the case when primitives are combined like triangles and lines
3352
+
3353
+ const geometries = getGeometry(instance.id);
3354
+ const newObjects = buildObjects(geometries, instance.materials);
3355
+
3356
+ for (let j = 0, jl = newObjects.length; j < jl; j++) {
3357
+
3358
+ objects.push(newObjects[j]);
3359
+
3360
+ }
3361
+
3362
+ } // instance nodes
3363
+
3364
+
3365
+ for (let i = 0, l = instanceNodes.length; i < l; i++) {
3366
+
3367
+ objects.push(getNode(instanceNodes[i]).clone());
3368
+
3369
+ }
3370
+
3371
+ let object;
3372
+
3373
+ if (nodes.length === 0 && objects.length === 1) {
3374
+
3375
+ object = objects[0];
3376
+
3377
+ } else {
3378
+
3379
+ object = type === 'JOINT' ? new THREE.Bone() : new THREE.Group();
3380
+
3381
+ for (let i = 0; i < objects.length; i++) {
3382
+
3383
+ object.add(objects[i]);
3384
+
3385
+ }
3386
+
3387
+ }
3388
+
3389
+ object.name = type === 'JOINT' ? data.sid : data.name;
3390
+ object.matrix.copy(matrix);
3391
+ object.matrix.decompose(object.position, object.quaternion, object.scale);
3392
+ return object;
3393
+
3394
+ }
3395
+
3396
+ const fallbackMaterial = new THREE.MeshBasicMaterial({
3397
+ color: 0xff00ff
3398
+ });
3399
+
3400
+ function resolveMaterialBinding(keys, instanceMaterials) {
3401
+
3402
+ const materials = [];
3403
+
3404
+ for (let i = 0, l = keys.length; i < l; i++) {
3405
+
3406
+ const id = instanceMaterials[keys[i]];
3407
+
3408
+ if (id === undefined) {
3409
+
3410
+ console.warn('THREE.ColladaLoader: Material with key %s not found. Apply fallback material.', keys[i]);
3411
+ materials.push(fallbackMaterial);
3412
+
3413
+ } else {
3414
+
3415
+ materials.push(getMaterial(id));
3416
+
3417
+ }
3418
+
3419
+ }
3420
+
3421
+ return materials;
3422
+
3423
+ }
3424
+
3425
+ function buildObjects(geometries, instanceMaterials) {
3426
+
3427
+ const objects = [];
3428
+
3429
+ for (const type in geometries) {
3430
+
3431
+ const geometry = geometries[type];
3432
+ const materials = resolveMaterialBinding(geometry.materialKeys, instanceMaterials); // handle case if no materials are defined
3433
+
3434
+ if (materials.length === 0) {
3435
+
3436
+ if (type === 'lines' || type === 'linestrips') {
3437
+
3438
+ materials.push(new THREE.LineBasicMaterial());
3439
+
3440
+ } else {
3441
+
3442
+ materials.push(new THREE.MeshPhongMaterial());
3443
+
3444
+ }
3445
+
3446
+ } // regard skinning
3447
+
3448
+
3449
+ const skinning = geometry.data.attributes.skinIndex !== undefined; // choose between a single or multi materials (material array)
3450
+
3451
+ const material = materials.length === 1 ? materials[0] : materials; // now create a specific 3D object
3452
+
3453
+ let object;
3454
+
3455
+ switch (type) {
3456
+
3457
+ case 'lines':
3458
+ object = new THREE.LineSegments(geometry.data, material);
3459
+ break;
3460
+
3461
+ case 'linestrips':
3462
+ object = new THREE.Line(geometry.data, material);
3463
+ break;
3464
+
3465
+ case 'triangles':
3466
+ case 'polylist':
3467
+ if (skinning) {
3468
+
3469
+ object = new THREE.SkinnedMesh(geometry.data, material);
3470
+
3471
+ } else {
3472
+
3473
+ object = new THREE.Mesh(geometry.data, material);
3474
+
3475
+ }
3476
+
3477
+ break;
3478
+
3479
+ }
3480
+
3481
+ objects.push(object);
3482
+
3483
+ }
3484
+
3485
+ return objects;
3486
+
3487
+ }
3488
+
3489
+ function hasNode(id) {
3490
+
3491
+ return library.nodes[id] !== undefined;
3492
+
3493
+ }
3494
+
3495
+ function getNode(id) {
3496
+
3497
+ return getBuild(library.nodes[id], buildNode);
3498
+
3499
+ } // visual scenes
3500
+
3501
+
3502
+ function parseVisualScene(xml) {
3503
+
3504
+ const data = {
3505
+ name: xml.getAttribute('name'),
3506
+ children: []
3507
+ };
3508
+ prepareNodes(xml);
3509
+ const elements = getElementsByTagName(xml, 'node');
3510
+
3511
+ for (let i = 0; i < elements.length; i++) {
3512
+
3513
+ data.children.push(parseNode(elements[i]));
3514
+
3515
+ }
3516
+
3517
+ library.visualScenes[xml.getAttribute('id')] = data;
3518
+
3519
+ }
3520
+
3521
+ function buildVisualScene(data) {
3522
+
3523
+ const group = new THREE.Group();
3524
+ group.name = data.name;
3525
+ const children = data.children;
3526
+
3527
+ for (let i = 0; i < children.length; i++) {
3528
+
3529
+ const child = children[i];
3530
+ group.add(getNode(child.id));
3531
+
3532
+ }
3533
+
3534
+ return group;
3535
+
3536
+ }
3537
+
3538
+ function hasVisualScene(id) {
3539
+
3540
+ return library.visualScenes[id] !== undefined;
3541
+
3542
+ }
3543
+
3544
+ function getVisualScene(id) {
3545
+
3546
+ return getBuild(library.visualScenes[id], buildVisualScene);
3547
+
3548
+ } // scenes
3549
+
3550
+
3551
+ function parseScene(xml) {
3552
+
3553
+ const instance = getElementsByTagName(xml, 'instance_visual_scene')[0];
3554
+ return getVisualScene(parseId(instance.getAttribute('url')));
3555
+
3556
+ }
3557
+
3558
+ function setupAnimations() {
3559
+
3560
+ const clips = library.clips;
3561
+
3562
+ if (isEmpty(clips) === true) {
3563
+
3564
+ if (isEmpty(library.animations) === false) {
3565
+
3566
+ // if there are animations but no clips, we create a default clip for playback
3567
+ const tracks = [];
3568
+
3569
+ for (const id in library.animations) {
3570
+
3571
+ const animationTracks = getAnimation(id);
3572
+
3573
+ for (let i = 0, l = animationTracks.length; i < l; i++) {
3574
+
3575
+ tracks.push(animationTracks[i]);
3576
+
3577
+ }
3578
+
3579
+ }
3580
+
3581
+ animations.push(new THREE.AnimationClip('default', - 1, tracks));
3582
+
3583
+ }
3584
+
3585
+ } else {
3586
+
3587
+ for (const id in clips) {
3588
+
3589
+ animations.push(getAnimationClip(id));
3590
+
3591
+ }
3592
+
3593
+ }
3594
+
3595
+ } // convert the parser error element into text with each child elements text
3596
+ // separated by new lines.
3597
+
3598
+
3599
+ function parserErrorToText(parserError) {
3600
+
3601
+ let result = '';
3602
+ const stack = [parserError];
3603
+
3604
+ while (stack.length) {
3605
+
3606
+ const node = stack.shift();
3607
+
3608
+ if (node.nodeType === Node.TEXT_NODE) {
3609
+
3610
+ result += node.textContent;
3611
+
3612
+ } else {
3613
+
3614
+ result += '\n';
3615
+ stack.push.apply(stack, node.childNodes);
3616
+
3617
+ }
3618
+
3619
+ }
3620
+
3621
+ return result.trim();
3622
+
3623
+ }
3624
+
3625
+ if (text.length === 0) {
3626
+
3627
+ return {
3628
+ scene: new THREE.Scene()
3629
+ };
3630
+
3631
+ }
3632
+
3633
+ const xml = new DOMParser().parseFromString(text, 'application/xml');
3634
+ const collada = getElementsByTagName(xml, 'COLLADA')[0];
3635
+ const parserError = xml.getElementsByTagName('parsererror')[0];
3636
+
3637
+ if (parserError !== undefined) {
3638
+
3639
+ // Chrome will return parser error with a div in it
3640
+ const errorElement = getElementsByTagName(parserError, 'div')[0];
3641
+ let errorText;
3642
+
3643
+ if (errorElement) {
3644
+
3645
+ errorText = errorElement.textContent;
3646
+
3647
+ } else {
3648
+
3649
+ errorText = parserErrorToText(parserError);
3650
+
3651
+ }
3652
+
3653
+ console.error('THREE.ColladaLoader: Failed to parse collada file.\n', errorText);
3654
+ return null;
3655
+
3656
+ } // metadata
3657
+
3658
+
3659
+ const version = collada.getAttribute('version');
3660
+ console.log('THREE.ColladaLoader: File version', version);
3661
+ const asset = parseAsset(getElementsByTagName(collada, 'asset')[0]);
3662
+ const textureLoader = new THREE.TextureLoader(this.manager);
3663
+ textureLoader.setPath(this.resourcePath || path).setCrossOrigin(this.crossOrigin);
3664
+ let tgaLoader;
3665
+
3666
+ if (THREE.TGALoader) {
3667
+
3668
+ tgaLoader = new THREE.TGALoader(this.manager);
3669
+ tgaLoader.setPath(this.resourcePath || path);
3670
+
3671
+ } //
3672
+
3673
+
3674
+ const animations = [];
3675
+ let kinematics = {};
3676
+ let count = 0; //
3677
+
3678
+ const library = {
3679
+ animations: {},
3680
+ clips: {},
3681
+ controllers: {},
3682
+ images: {},
3683
+ effects: {},
3684
+ materials: {},
3685
+ cameras: {},
3686
+ lights: {},
3687
+ geometries: {},
3688
+ nodes: {},
3689
+ visualScenes: {},
3690
+ kinematicsModels: {},
3691
+ physicsModels: {},
3692
+ kinematicsScenes: {}
3693
+ };
3694
+ parseLibrary(collada, 'library_animations', 'animation', parseAnimation);
3695
+ parseLibrary(collada, 'library_animation_clips', 'animation_clip', parseAnimationClip);
3696
+ parseLibrary(collada, 'library_controllers', 'controller', parseController);
3697
+ parseLibrary(collada, 'library_images', 'image', parseImage);
3698
+ parseLibrary(collada, 'library_effects', 'effect', parseEffect);
3699
+ parseLibrary(collada, 'library_materials', 'material', parseMaterial);
3700
+ parseLibrary(collada, 'library_cameras', 'camera', parseCamera);
3701
+ parseLibrary(collada, 'library_lights', 'light', parseLight);
3702
+ parseLibrary(collada, 'library_geometries', 'geometry', parseGeometry);
3703
+ parseLibrary(collada, 'library_nodes', 'node', parseNode);
3704
+ parseLibrary(collada, 'library_visual_scenes', 'visual_scene', parseVisualScene);
3705
+ parseLibrary(collada, 'library_kinematics_models', 'kinematics_model', parseKinematicsModel);
3706
+ parseLibrary(collada, 'library_physics_models', 'physics_model', parsePhysicsModel);
3707
+ parseLibrary(collada, 'scene', 'instance_kinematics_scene', parseKinematicsScene);
3708
+ buildLibrary(library.animations, buildAnimation);
3709
+ buildLibrary(library.clips, buildAnimationClip);
3710
+ buildLibrary(library.controllers, buildController);
3711
+ buildLibrary(library.images, buildImage);
3712
+ buildLibrary(library.effects, buildEffect);
3713
+ buildLibrary(library.materials, buildMaterial);
3714
+ buildLibrary(library.cameras, buildCamera);
3715
+ buildLibrary(library.lights, buildLight);
3716
+ buildLibrary(library.geometries, buildGeometry);
3717
+ buildLibrary(library.visualScenes, buildVisualScene);
3718
+ setupAnimations();
3719
+ setupKinematics();
3720
+ const scene = parseScene(getElementsByTagName(collada, 'scene')[0]);
3721
+ scene.animations = animations;
3722
+
3723
+ if (asset.upAxis === 'Z_UP') {
3724
+
3725
+ scene.quaternion.setFromEuler(new THREE.Euler(- Math.PI / 2, 0, 0));
3726
+
3727
+ }
3728
+
3729
+ scene.scale.multiplyScalar(asset.unit);
3730
+ return {
3731
+ get animations() {
3732
+
3733
+ console.warn('THREE.ColladaLoader: Please access animations over scene.animations now.');
3734
+ return animations;
3735
+
3736
+ },
3737
+
3738
+ kinematics: kinematics,
3739
+ library: library,
3740
+ scene: scene
3741
+ };
3742
+
3743
+ }
3744
+
3745
+ }
3746
+ let OBJTHREE = Object.assign({},THREE)
3747
+ OBJTHREE.ColladaLoader = ColladaLoader;
3748
+
3749
+ // })();
3750
+
3751
+ export default OBJTHREE.ColladaLoader;