forked from RobotWebTools/ros3djs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathros3d.js
9257 lines (7103 loc) · 254 KB
/
ros3d.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var ROS3D = (function (exports,THREE,ROSLIB,EventEmitter2) {
'use strict';
EventEmitter2 = EventEmitter2 && EventEmitter2.hasOwnProperty('default') ? EventEmitter2['default'] : EventEmitter2;
var THREE$1 = Object.assign({}, THREE)
// Marker types
var MARKER_ARROW = 0;
var MARKER_CUBE = 1;
var MARKER_SPHERE = 2;
var MARKER_CYLINDER = 3;
var MARKER_LINE_STRIP = 4;
var MARKER_LINE_LIST = 5;
var MARKER_CUBE_LIST = 6;
var MARKER_SPHERE_LIST = 7;
var MARKER_POINTS = 8;
var MARKER_TEXT_VIEW_FACING = 9;
var MARKER_MESH_RESOURCE = 10;
var MARKER_TRIANGLE_LIST = 11;
// Interactive marker feedback types
var INTERACTIVE_MARKER_KEEP_ALIVE = 0;
var INTERACTIVE_MARKER_POSE_UPDATE = 1;
var INTERACTIVE_MARKER_MENU_SELECT = 2;
var INTERACTIVE_MARKER_BUTTON_CLICK = 3;
var INTERACTIVE_MARKER_MOUSE_DOWN = 4;
var INTERACTIVE_MARKER_MOUSE_UP = 5;
// Interactive marker control types
var INTERACTIVE_MARKER_NONE = 0;
var INTERACTIVE_MARKER_MENU = 1;
var INTERACTIVE_MARKER_BUTTON = 2;
var INTERACTIVE_MARKER_MOVE_AXIS = 3;
var INTERACTIVE_MARKER_MOVE_PLANE = 4;
var INTERACTIVE_MARKER_ROTATE_AXIS = 5;
var INTERACTIVE_MARKER_MOVE_ROTATE = 6;
var INTERACTIVE_MARKER_MOVE_3D = 7;
var INTERACTIVE_MARKER_ROTATE_3D = 8;
var INTERACTIVE_MARKER_MOVE_ROTATE_3D = 9;
// Interactive marker rotation behavior
var INTERACTIVE_MARKER_INHERIT = 0;
var INTERACTIVE_MARKER_FIXED = 1;
var INTERACTIVE_MARKER_VIEW_FACING = 2;
/**
* Create a THREE material based on the given RGBA values.
*
* @param r - the red value
* @param g - the green value
* @param b - the blue value
* @param a - the alpha value
* @returns the THREE material
*/
var makeColorMaterial = function(r, g, b, a) {
var color = new THREE$1.Color();
color.setRGB(r, g, b);
if (a <= 0.99) {
return new THREE$1.MeshBasicMaterial({
color : color.getHex(),
opacity : a + 0.1,
transparent : true,
depthWrite : true,
blendSrc : THREE$1.SrcAlphaFactor,
blendDst : THREE$1.OneMinusSrcAlphaFactor,
blendEquation : THREE$1.ReverseSubtractEquation,
blending : THREE$1.NormalBlending
});
} else {
return new THREE$1.MeshPhongMaterial({
color : color.getHex(),
opacity : a,
blending : THREE$1.NormalBlending
});
}
};
/**
* Return the intersection between the mouseray and the plane.
*
* @param mouseRay - the mouse ray
* @param planeOrigin - the origin of the plane
* @param planeNormal - the normal of the plane
* @returns the intersection point
*/
var intersectPlane = function(mouseRay, planeOrigin, planeNormal) {
var vector = new THREE$1.Vector3();
var intersectPoint = new THREE$1.Vector3();
vector.subVectors(planeOrigin, mouseRay.origin);
var dot = mouseRay.direction.dot(planeNormal);
// bail if ray and plane are parallel
if (Math.abs(dot) < mouseRay.precision) {
return undefined;
}
// calc distance to plane
var scalar = planeNormal.dot(vector) / dot;
intersectPoint.addVectors(mouseRay.origin, mouseRay.direction.clone().multiplyScalar(scalar));
return intersectPoint;
};
/**
* Find the closest point on targetRay to any point on mouseRay. Math taken from
* http://paulbourke.net/geometry/lineline3d/
*
* @param targetRay - the target ray to use
* @param mouseRay - the mouse ray
* @param the closest point between the two rays
*/
var findClosestPoint = function(targetRay, mouseRay) {
var v13 = new THREE$1.Vector3();
v13.subVectors(targetRay.origin, mouseRay.origin);
var v43 = mouseRay.direction.clone();
var v21 = targetRay.direction.clone();
var d1343 = v13.dot(v43);
var d4321 = v43.dot(v21);
var d1321 = v13.dot(v21);
var d4343 = v43.dot(v43);
var d2121 = v21.dot(v21);
var denom = d2121 * d4343 - d4321 * d4321;
// check within a delta
if (Math.abs(denom) <= 0.0001) {
return undefined;
}
var numer = d1343 * d4321 - d1321 * d4343;
var mua = numer / denom;
return mua;
};
/**
* Find the closest point between the axis and the mouse.
*
* @param axisRay - the ray from the axis
* @param camera - the camera to project from
* @param mousePos - the mouse position
* @returns the closest axis point
*/
var closestAxisPoint = function(axisRay, camera, mousePos) {
// project axis onto screen
var o = axisRay.origin.clone();
o.project(camera);
var o2 = axisRay.direction.clone().add(axisRay.origin);
o2.project(camera);
// d is the axis vector in screen space (d = o2-o)
var d = o2.clone().sub(o);
// t is the 2d ray param of perpendicular projection of mousePos onto o
var tmp = new THREE$1.Vector2();
// (t = (mousePos - o) * d / (d*d))
var t = tmp.subVectors(mousePos, o).dot(d) / d.dot(d);
// mp is the final 2d-projected mouse pos (mp = o + d*t)
var mp = new THREE$1.Vector2();
mp.addVectors(o, d.clone().multiplyScalar(t));
// go back to 3d by shooting a ray
var vector = new THREE$1.Vector3(mp.x, mp.y, 0.5);
vector.unproject(camera);
var mpRay = new THREE$1.Ray(camera.position, vector.sub(camera.position).normalize());
return findClosestPoint(axisRay, mpRay);
};
/**
* @author Julius Kammerl - jkammerl@willowgarage.com
*/
class DepthCloud extends THREE$1.Object3D {
/**
* The DepthCloud object.
*
* @constructor
* @param options - object with following keys:
*
* * url - the URL of the stream
* * streamType (optional) - the stream type: mjpeg or vp8 video (defaults to vp8)
* * f (optional) - the camera's focal length (defaults to standard Kinect calibration)
* * maxDepthPerTile (optional) - the factor with which we control the desired depth range (defaults to 1.0)
* * pointSize (optional) - point size (pixels) for rendered point cloud
* * width (optional) - width of the video stream
* * height (optional) - height of the video stream
* * whiteness (optional) - blends rgb values to white (0..100)
* * varianceThreshold (optional) - threshold for variance filter, used for compression artifact removal
*/
constructor(options) {
super();
options = options || {};
this.url = options.url;
this.streamType = options.streamType || 'vp8';
this.f = options.f || 526;
this.maxDepthPerTile = options.maxDepthPerTile || 1.0;
this.pointSize = options.pointSize || 3;
this.width = options.width || 1024;
this.height = options.height || 1024;
this.resolutionFactor = Math.max(this.width, this.height) / 1024;
this.whiteness = options.whiteness || 0;
this.varianceThreshold = options.varianceThreshold || 0.000016667;
this.isMjpeg = this.streamType.toLowerCase() === 'mjpeg';
this.video = document.createElement(this.isMjpeg ? 'img' : 'video');
this.video.addEventListener(this.isMjpeg ? 'load' : 'loadedmetadata', this.metaLoaded.bind(this), false);
if (!this.isMjpeg) {
this.video.loop = true;
}
this.video.src = this.url;
this.video.crossOrigin = 'Anonymous';
this.video.setAttribute('crossorigin', 'Anonymous');
// define custom shaders
this.vertex_shader = [
'uniform sampler2D map;',
'',
'uniform float width;',
'uniform float height;',
'uniform float nearClipping, farClipping;',
'',
'uniform float pointSize;',
'uniform float zOffset;',
'',
'uniform float focallength;',
'uniform float maxDepthPerTile;',
'uniform float resolutionFactor;',
'',
'varying vec2 vUvP;',
'varying vec2 colorP;',
'',
'varying float depthVariance;',
'varying float maskVal;',
'',
'float sampleDepth(vec2 pos)',
' {',
' float depth;',
' ',
' vec2 vUv = vec2( pos.x / (width*2.0), pos.y / (height*2.0)+0.5 );',
' vec2 vUv2 = vec2( pos.x / (width*2.0)+0.5, pos.y / (height*2.0)+0.5 );',
' ',
' vec4 depthColor = texture2D( map, vUv );',
' ',
' depth = ( depthColor.r + depthColor.g + depthColor.b ) / 3.0 ;',
' ',
' if (depth>0.99)',
' {',
' vec4 depthColor2 = texture2D( map, vUv2 );',
' float depth2 = ( depthColor2.r + depthColor2.g + depthColor2.b ) / 3.0 ;',
' depth = 0.99+depth2;',
' }',
' ',
' return depth;',
' }',
'',
'float median(float a, float b, float c)',
' {',
' float r=a;',
' ',
' if ( (a<b) && (b<c) )',
' {',
' r = b;',
' }',
' if ( (a<c) && (c<b) )',
' {',
' r = c;',
' }',
' return r;',
' }',
'',
'float variance(float d1, float d2, float d3, float d4, float d5, float d6, float d7, float d8, float d9)',
' {',
' float mean = (d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9) / 9.0;',
' float t1 = (d1-mean);',
' float t2 = (d2-mean);',
' float t3 = (d3-mean);',
' float t4 = (d4-mean);',
' float t5 = (d5-mean);',
' float t6 = (d6-mean);',
' float t7 = (d7-mean);',
' float t8 = (d8-mean);',
' float t9 = (d9-mean);',
' float v = (t1*t1+t2*t2+t3*t3+t4*t4+t5*t5+t6*t6+t7*t7+t8*t8+t9*t9)/9.0;',
' return v;',
' }',
'',
'vec2 decodeDepth(vec2 pos)',
' {',
' vec2 ret;',
' ',
' ',
' float depth1 = sampleDepth(vec2(position.x-1.0, position.y-1.0));',
' float depth2 = sampleDepth(vec2(position.x, position.y-1.0));',
' float depth3 = sampleDepth(vec2(position.x+1.0, position.y-1.0));',
' float depth4 = sampleDepth(vec2(position.x-1.0, position.y));',
' float depth5 = sampleDepth(vec2(position.x, position.y));',
' float depth6 = sampleDepth(vec2(position.x+1.0, position.y));',
' float depth7 = sampleDepth(vec2(position.x-1.0, position.y+1.0));',
' float depth8 = sampleDepth(vec2(position.x, position.y+1.0));',
' float depth9 = sampleDepth(vec2(position.x+1.0, position.y+1.0));',
' ',
' float median1 = median(depth1, depth2, depth3);',
' float median2 = median(depth4, depth5, depth6);',
' float median3 = median(depth7, depth8, depth9);',
' ',
' ret.x = median(median1, median2, median3);',
' ret.y = variance(depth1, depth2, depth3, depth4, depth5, depth6, depth7, depth8, depth9);',
' ',
' return ret;',
' ',
' }',
'',
'',
'void main() {',
' ',
' vUvP = vec2( position.x / (width*2.0), position.y / (height*2.0)+0.5 );',
' colorP = vec2( position.x / (width*2.0)+0.5 , position.y / (height*2.0) );',
' ',
' vec4 pos = vec4(0.0,0.0,0.0,0.0);',
' depthVariance = 0.0;',
' ',
' if ( (vUvP.x<0.0)|| (vUvP.x>0.5) || (vUvP.y<0.5) || (vUvP.y>0.0))',
' {',
' vec2 smp = decodeDepth(vec2(position.x, position.y));',
' float depth = smp.x;',
' depthVariance = smp.y;',
' ',
' float z = -depth;',
' ',
' pos = vec4(',
' ( position.x / width - 0.5 ) * z * 0.5 * maxDepthPerTile * resolutionFactor * (1000.0/focallength) * -1.0,',
' ( position.y / height - 0.5 ) * z * 0.5 * maxDepthPerTile * resolutionFactor * (1000.0/focallength),',
' (- z + zOffset / 1000.0) * maxDepthPerTile,',
' 1.0);',
' ',
' vec2 maskP = vec2( position.x / (width*2.0), position.y / (height*2.0) );',
' vec4 maskColor = texture2D( map, maskP );',
' maskVal = ( maskColor.r + maskColor.g + maskColor.b ) / 3.0 ;',
' }',
' ',
' gl_PointSize = pointSize;',
' gl_Position = projectionMatrix * modelViewMatrix * pos;',
' ',
'}'
].join('\n');
this.fragment_shader = [
'uniform sampler2D map;',
'uniform float varianceThreshold;',
'uniform float whiteness;',
'',
'varying vec2 vUvP;',
'varying vec2 colorP;',
'',
'varying float depthVariance;',
'varying float maskVal;',
'',
'',
'void main() {',
' ',
' vec4 color;',
' ',
' if ( (depthVariance>varianceThreshold) || (maskVal>0.5) ||(vUvP.x<0.0)|| (vUvP.x>0.5) || (vUvP.y<0.5) || (vUvP.y>1.0))',
' { ',
' discard;',
' }',
' else ',
' {',
' color = texture2D( map, colorP );',
' ',
' float fader = whiteness /100.0;',
' ',
' color.r = color.r * (1.0-fader)+ fader;',
' ',
' color.g = color.g * (1.0-fader)+ fader;',
' ',
' color.b = color.b * (1.0-fader)+ fader;',
' ',
' color.a = 1.0;//smoothstep( 20000.0, -20000.0, gl_FragCoord.z / gl_FragCoord.w );',
' }',
' ',
' gl_FragColor = vec4( color.r, color.g, color.b, color.a );',
' ',
'}'
].join('\n');
};
/**
* Callback called when video metadata is ready
*/
metaLoaded() {
this.metaLoaded = true;
this.initStreamer();
};
/**
* Callback called when video metadata is ready
*/
initStreamer() {
if (this.metaLoaded) {
this.texture = new THREE$1.Texture(this.video);
this.geometry = new THREE$1.Geometry();
for (var i = 0, l = this.width * this.height; i < l; i++) {
var vertex = new THREE$1.Vector3();
vertex.x = (i % this.width);
vertex.y = Math.floor(i / this.width);
this.geometry.vertices.push(vertex);
}
this.material = new THREE$1.ShaderMaterial({
uniforms : {
'map' : {
type : 't',
value : this.texture
},
'width' : {
type : 'f',
value : this.width
},
'height' : {
type : 'f',
value : this.height
},
'focallength' : {
type : 'f',
value : this.f
},
'pointSize' : {
type : 'f',
value : this.pointSize
},
'zOffset' : {
type : 'f',
value : 0
},
'whiteness' : {
type : 'f',
value : this.whiteness
},
'varianceThreshold' : {
type : 'f',
value : this.varianceThreshold
},
'maxDepthPerTile': {
type : 'f',
value : this.maxDepthPerTile
},
'resolutionFactor': {
type : 'f',
value : this.resolutionFactor
},
},
vertexShader : this.vertex_shader,
fragmentShader : this.fragment_shader
});
this.mesh = new THREE$1.ParticleSystem(this.geometry, this.material);
this.mesh.position.x = 0;
this.mesh.position.y = 0;
this.add(this.mesh);
var that = this;
setInterval(function() {
if (that.isMjpeg || that.video.readyState === that.video.HAVE_ENOUGH_DATA) {
that.texture.needsUpdate = true;
}
}, 1000 / 30);
}
};
/**
* Start video playback
*/
startStream() {
if (!this.isMjpeg) {
this.video.play();
}
};
/**
* Stop video playback
*/
stopStream() {
if (!this.isMjpeg) {
this.video.pause();
}
};
}
/**
* @author David Gossow - dgossow@willowgarage.com
*/
class Arrow extends THREE$1.Mesh {
/**
* A Arrow is a THREE object that can be used to display an arrow model.
*
* @constructor
* @param options - object with following keys:
*
* * origin (optional) - the origin of the arrow
* * direction (optional) - the direction vector of the arrow
* * length (optional) - the length of the arrow
* * headLength (optional) - the head length of the arrow
* * shaftDiameter (optional) - the shaft diameter of the arrow
* * headDiameter (optional) - the head diameter of the arrow
* * material (optional) - the material to use for this arrow
*/
constructor(options) {
options = options || {};
var origin = options.origin || new THREE$1.Vector3(0, 0, 0);
var direction = options.direction || new THREE$1.Vector3(1, 0, 0);
var length = options.length || 1;
var headLength = options.headLength || 0.2;
var shaftDiameter = options.shaftDiameter || 0.05;
var headDiameter = options.headDiameter || 0.1;
var material = options.material || new THREE$1.MeshBasicMaterial();
var shaftLength = length - headLength;
// create and merge geometry
var geometry = new THREE$1.CylinderGeometry(shaftDiameter * 0.5, shaftDiameter * 0.5, shaftLength,
12, 1);
var m = new THREE$1.Matrix4();
m.setPosition(new THREE$1.Vector3(0, shaftLength * 0.5, 0));
geometry.applyMatrix(m);
// create the head
var coneGeometry = new THREE$1.CylinderGeometry(0, headDiameter * 0.5, headLength, 12, 1);
m.setPosition(new THREE$1.Vector3(0, shaftLength + (headLength * 0.5), 0));
coneGeometry.applyMatrix(m);
// put the arrow together
geometry.merge(coneGeometry);
super(geometry, material);
this.position.copy(origin);
this.setDirection(direction);
};
/**
* Set the direction of this arrow to that of the given vector.
*
* @param direction - the direction to set this arrow
*/
setDirection(direction) {
var axis = new THREE$1.Vector3(0, 1, 0).cross(direction);
var radians = Math.acos(new THREE$1.Vector3(0, 1, 0).dot(direction.clone().normalize()));
this.matrix = new THREE$1.Matrix4().makeRotationAxis(axis.normalize(), radians);
this.rotation.setFromRotationMatrix(this.matrix, this.rotation.order);
};
/**
* Set this arrow to be the given length.
*
* @param length - the new length of the arrow
*/
setLength(length) {
this.scale.set(length, length, length);
};
/**
* Set the color of this arrow to the given hex value.
*
* @param hex - the hex value of the color to use
*/
setColor(hex) {
this.material.color.setHex(hex);
};
/*
* Free memory of elements in this marker.
*/
dispose() {
if (this.geometry !== undefined) {
this.geometry.dispose();
}
if (this.material !== undefined) {
this.material.dispose();
}
};
}
/**
* @author aleeper / http://adamleeper.com/
* @author mrdoob / http://mrdoob.com/
* @author gero3 / https://github.com/gero3
* @author Mugen87 / https://github.com/Mugen87
*
* Description: A THREE loader for STL ASCII files, as created by Solidworks and other CAD programs.
*
* Supports both binary and ASCII encoded files, with automatic detection of type.
*
* The loader returns a non-indexed buffer geometry.
*
* Limitations:
* Binary decoding supports "Magics" color format (http://en.wikipedia.org/wiki/STL_(file_format)#Color_in_binary_STL).
* There is perhaps some question as to how valid it is to always assume little-endian-ness.
* ASCII decoding assumes file is UTF-8.
*
* Usage:
* var loader = new THREE.STLLoader();
* loader.load( './models/stl/slotted_disk.stl', function ( geometry ) {
* scene.add( new THREE.Mesh( geometry ) );
* });
*
* For binary STLs geometry might contain colors for vertices. To use it:
* // use the same code to load STL as above
* if (geometry.hasColors) {
* material = new THREE.MeshPhongMaterial({ opacity: geometry.alpha, vertexColors: THREE.VertexColors });
* } else { .... }
* var mesh = new THREE.Mesh( geometry, material );
*/
THREE$1.STLLoader = function (manager) {
this.manager = (manager !== undefined) ? manager : THREE$1.DefaultLoadingManager;
};
THREE$1.STLLoader.prototype = {
constructor: THREE$1.STLLoader,
load: function (url, onLoad, onProgress, onError) {
var scope = this;
var loader = new THREE$1.FileLoader(scope.manager);
loader.setResponseType('arraybuffer');
loader.load(url, function (text) {
onLoad(scope.parse(text));
}, onProgress, onError);
},
parse: function (data) {
function isBinary(data) {
var expect, face_size, n_faces, reader;
reader = new DataView(data);
face_size = (32 / 8 * 3) + ((32 / 8 * 3) * 3) + (16 / 8);
n_faces = reader.getUint32(80, true);
expect = 80 + (32 / 8) + (n_faces * face_size);
if (expect === reader.byteLength) {
return true;
}
// An ASCII STL data must begin with 'solid ' as the first six bytes.
// However, ASCII STLs lacking the SPACE after the 'd' are known to be
// plentiful. So, check the first 5 bytes for 'solid'.
// US-ASCII ordinal values for 's', 'o', 'l', 'i', 'd'
var solid = [115, 111, 108, 105, 100];
for (var i = 0; i < 5; i++) {
// If solid[ i ] does not match the i-th byte, then it is not an
// ASCII STL; hence, it is binary and return true.
if (solid[i] != reader.getUint8(i, false)) return true;
}
// First 5 bytes read "solid"; declare it to be an ASCII STL
return false;
}
function parseBinary(data) {
var reader = new DataView(data);
var faces = reader.getUint32(80, true);
var r, g, b, hasColors = false, colors;
var defaultR, defaultG, defaultB, alpha;
// process STL header
// check for default color in header ("COLOR=rgba" sequence).
for (var index = 0; index < 80 - 10; index++) {
if ((reader.getUint32(index, false) == 0x434F4C4F /*COLO*/) &&
(reader.getUint8(index + 4) == 0x52 /*'R'*/) &&
(reader.getUint8(index + 5) == 0x3D /*'='*/)) {
hasColors = true;
colors = [];
defaultR = reader.getUint8(index + 6) / 255;
defaultG = reader.getUint8(index + 7) / 255;
defaultB = reader.getUint8(index + 8) / 255;
alpha = reader.getUint8(index + 9) / 255;
}
}
var dataOffset = 84;
var faceLength = 12 * 4 + 2;
var geometry = new THREE$1.BufferGeometry();
var vertices = [];
var normals = [];
for (var face = 0; face < faces; face++) {
var start = dataOffset + face * faceLength;
var normalX = reader.getFloat32(start, true);
var normalY = reader.getFloat32(start + 4, true);
var normalZ = reader.getFloat32(start + 8, true);
if (hasColors) {
var packedColor = reader.getUint16(start + 48, true);
if ((packedColor & 0x8000) === 0) {
// facet has its own unique color
r = (packedColor & 0x1F) / 31;
g = ((packedColor >> 5) & 0x1F) / 31;
b = ((packedColor >> 10) & 0x1F) / 31;
} else {
r = defaultR;
g = defaultG;
b = defaultB;
}
}
for (var i = 1; i <= 3; i++) {
var vertexstart = start + i * 12;
vertices.push(reader.getFloat32(vertexstart, true));
vertices.push(reader.getFloat32(vertexstart + 4, true));
vertices.push(reader.getFloat32(vertexstart + 8, true));
normals.push(normalX, normalY, normalZ);
if (hasColors) {
colors.push(r, g, b);
}
}
}
geometry.addAttribute('position', new THREE$1.BufferAttribute(new Float32Array(vertices), 3));
geometry.addAttribute('normal', new THREE$1.BufferAttribute(new Float32Array(normals), 3));
if (hasColors) {
geometry.addAttribute('color', new THREE$1.BufferAttribute(new Float32Array(colors), 3));
geometry.hasColors = true;
geometry.alpha = alpha;
}
return geometry;
}
function parseASCII(data) {
var geometry = new THREE$1.BufferGeometry();
var patternFace = /facet([\s\S]*?)endfacet/g;
var faceCounter = 0;
var patternFloat = /[\s]+([+-]?(?:\d+.\d+|\d+.|\d+|.\d+)(?:[eE][+-]?\d+)?)/.source;
var patternVertex = new RegExp('vertex' + patternFloat + patternFloat + patternFloat, 'g');
var patternNormal = new RegExp('normal' + patternFloat + patternFloat + patternFloat, 'g');
var vertices = [];
var normals = [];
var normal = new THREE$1.Vector3();
var result;
while ((result = patternFace.exec(data)) !== null) {
var vertexCountPerFace = 0;
var normalCountPerFace = 0;
var text = result[0];
while ((result = patternNormal.exec(text)) !== null) {
normal.x = parseFloat(result[1]);
normal.y = parseFloat(result[2]);
normal.z = parseFloat(result[3]);
normalCountPerFace++;
}
while ((result = patternVertex.exec(text)) !== null) {
vertices.push(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3]));
normals.push(normal.x, normal.y, normal.z);
vertexCountPerFace++;
}
// every face have to own ONE valid normal
if (normalCountPerFace !== 1) {
console.error('THREE.STLLoader: Something isn\'t right with the normal of face number ' + faceCounter);
}
// each face have to own THREE valid vertices
if (vertexCountPerFace !== 3) {
console.error('THREE.STLLoader: Something isn\'t right with the vertices of face number ' + faceCounter);
}
faceCounter++;
}
geometry.addAttribute('position', new THREE$1.Float32BufferAttribute(vertices, 3));
geometry.addAttribute('normal', new THREE$1.Float32BufferAttribute(normals, 3));
return geometry;
}
function ensureString(buffer) {
if (typeof buffer !== 'string') {
var array_buffer = new Uint8Array(buffer);
if (window.TextDecoder !== undefined) {
return new TextDecoder().decode(array_buffer);
}
var str = '';
for (var i = 0, il = buffer.byteLength; i < il; i++) {
str += String.fromCharCode(array_buffer[i]); // implicitly assumes little-endian
}
return str;
} else {
return buffer;
}
}
function ensureBinary(buffer) {
if (typeof buffer === 'string') {
var array_buffer = new Uint8Array(buffer.length);
for (var i = 0; i < buffer.length; i++) {
array_buffer[i] = buffer.charCodeAt(i) & 0xff; // implicitly assumes little-endian
}
return array_buffer.buffer || array_buffer;
} else {
return buffer;
}
}
// start
var binData = ensureBinary(data);
return isBinary(binData) ? parseBinary(binData) : parseASCII(ensureString(data));
}
};
/**
* @author mrdoob / http://mrdoob.com/
* @author Mugen87 / https://github.com/Mugen87
*
*
* @Modified by Jihoon Lee from ColladerLoader.js@r88
* To support rviz compatible collada viewing.
* See: #202 why it is forked.
*
* It is a fork from ColladerLoader.js in three.js. It follows three.js license.
*/
THREE$1.ColladaLoader = function (manager) {
this.manager = (manager !== undefined) ? manager : THREE$1.DefaultLoadingManager;
};
THREE$1.ColladaLoader.prototype = {
constructor: THREE$1.ColladaLoader,
crossOrigin: 'Anonymous',
load: function (url, onLoad, onProgress, onError) {
var scope = this;
var path = THREE$1.Loader.prototype.extractUrlBase(url);
var loader = new THREE$1.FileLoader(scope.manager);
loader.load(url, function (text) {
onLoad(scope.parse(text, path));
}, onProgress, onError);
},
options: {
set convertUpAxis(value) {
console.warn('THREE.ColladaLoader: options.convertUpAxis() has been removed. Up axis is converted automatically.');
}
},
setCrossOrigin: function (value) {
this.crossOrigin = value;
},
parse: function (text, path) {
function getElementsByTagName(xml, name) {
// Non recursive xml.getElementsByTagName() ...
var array = [];
var childNodes = xml.childNodes;
for (var i = 0, l = childNodes.length; i < l; i++) {
var child = childNodes[i];
if (child.nodeName === name) {
array.push(child);
}
}
return array;
}
function parseStrings(text) {