Repository navigation
Expand file tree
/
Copy pathmy3.js
More file actions
909 lines (679 loc) · 24.3 KB
/
Copy pathmy3.js
File metadata and controls
909 lines (679 loc) · 24.3 KB
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
/**
* @author Marco Sulla (marcosullaroma@gmail.com)
* @date Feb 22, 2016
*/
var my3 = { // jshint ignore:line
adaptCanvasToText: function (canvas, message, font_size, font_face) {
/**
* @author Marco Sulla (marcosullaroma@gmail.com)
* @date Feb 17, 2016
*/
"use strict";
var context = canvas.getContext('2d');
if (canvas.height > canvas.width) {
canvas.width = canvas.height;
}
while (true) {
var side = MyJS.getNextPowerOfTwo(canvas.width);
if (side < 128) {
side = 128;
}
canvas.width = canvas.height = side;
context.font = "Bold " + font_size + "pt " + font_face;
var metrics = context.measureText(message);
var text_width = metrics.width;
var text_side = MyJS.getNextPowerOfTwo(Math.max(text_width, font_size));
if (text_side >= 128) {
if (side !== text_side) {
canvas.width = text_side;
continue;
}
}
else if (side !== 128) {
canvas.width = 128;
continue;
}
break;
}
},
getVector3: function (arg1, y, z) {
/**
* @author Marco Sulla (marcosullaroma@gmail.com)
* @date Feb 17, 2016
*/
"use strict";
var x_new;
var y_new;
var z_new;
var args = (arguments.length === 1?[arguments[0]]:Array.apply(null, arguments));
args = args.map(function (v, i, arr) {
if (v === undefined) {
return "undefined";
}
return v;
});
function bad_args_num() {
/**
* @author Marco Sulla (marcosullaroma@gmail.com)
* @date Mar 1, 2016
*/
throw new Error("Arguments must be one or three. " +
"Arguments: " + args.join("; "));
}
if (arg1 === undefined) {
bad_args_num();
}
if (y !== undefined) {
if (z !== undefined) {
x_new = arg1;
y_new = y;
z_new = z;
}
else {
bad_args_num();
}
}
else {
if (! (arg1.x === undefined || arg1.y === undefined ||
arg1.z === undefined)) {
x_new = arg1.x;
y_new = arg1.y;
z_new = arg1.z;
}
else if (! (arg1[0] === undefined || arg1[1] === undefined ||
arg1[2] === undefined)) {
x_new = arg1[0];
y_new = arg1[1];
z_new = arg1[2];
}
else {
throw new Error("Malformed arguments or unsupported type. " +
"Arguments: " + args.join("; "));
}
}
return new THREE.Vector3(x_new, y_new, z_new);
},
dispose: function (obj) {
/**
* @author Marco Sulla (marcosullaroma@gmail.com)
* @date Mar 12, 2016
*/
"use strict";
var self = this;
var children = obj.children;
var child;
if (children) {
for (var i=0; i<children.length; i+=1) {
child = children[i];
self.dispose(child);
}
}
var geometry = obj.geometry;
var material = obj.material;
if (geometry) {
geometry.dispose();
}
if (material) {
var texture = material.map;
if (texture) {
texture.dispose();
}
material.dispose();
}
},
getCamera: function (scene, screen_width, screen_height, position, opts) {
/**
* @author Marco Sulla (marcosullaroma@gmail.com)
* @date Feb 17, 2016
*/
"use strict";
if (opts === undefined) {
opts = {};
}
var possible_opts = ["view_angle", "near", "far", "up", "look_at"];
for (var k in opts) {
if (opts.hasOwnProperty(k)) {
if (possible_opts.indexOf(k) < 0) {
throw new Error("Unknown option '" + k.toString() + "'");
}
}
}
if (opts["view_angle"] === undefined) {
opts["view_angle"] = 45;
}
if (opts["near"] === undefined) {
opts["near"] = 0.1;
}
if (opts["far"] === undefined) {
opts["far"] = 20000;
}
if (opts["up"] === undefined) {
opts["up"] = my3.getVector3([0, 0, 1]);
}
if (opts["look_at"] === undefined) {
opts["look_at"] = scene.position;
}
if (opts["view_angle"] <= 0) {
throw new Error("'view_angle' option must be a positive integer");
}
if (opts["near"] <= 0) {
throw new Error("'near' option must be a positive integer");
}
if (opts["far"] <= 0) {
throw new Error("'far' option must be a positive integer");
}
var aspect = screen_width / screen_height;
var camera = new THREE.PerspectiveCamera(opts["view_angle"], aspect, opts["near"], opts["far"]);
camera.position.copy(my3.getVector3(position));
camera.up = my3.getVector3(opts["up"]);
camera.lookAt(my3.getVector3(opts["look_at"]));
return camera;
},
colorLines: function (geo, z_max, z_min) {
/**
* @author Marco Sulla (marcosullaroma@gmail.com)
* @date Feb 18, 2016
*/
"use strict";
var vertices = geo.vertices;
var line_vertex;
var line_vertex_next;
var z_color;
var color;
for (var p=0; p<vertices.length; p+=2) {
line_vertex = vertices[p];
line_vertex_next = vertices[p+1];
if (line_vertex_next === undefined) {
line_vertex_next = line_vertex;
}
z_color = Math.max(line_vertex.z, line_vertex_next.z);
z_color = Math.min(z_color, z_max);
color = new THREE.Color( 0x0000ff );
color.setHSL( 0.7 * (z_max - z_color) / (z_max - z_min), 1, 0.5 );
geo.colors[p] = color;
if (line_vertex_next !== undefined) {
geo.colors[p+1] = color;
}
}
},
getAxisData: function (min, max, opts) { //jshint ignore:line
/**
* @author Marco Sulla (marcosullaroma@gmail.com)
* @date Feb 22, 2016
*/
"use strict";
opts = MyJS.defVal(opts, {});
var possible_opts = [
"step",
"data_num",
"exponential",
"precision",
"labels",
"callback",
];
for (var k in opts) {
if (opts.hasOwnProperty(k) && possible_opts.indexOf(k) < 0) {
throw new Error("Unknown option '" + k + "'");
}
}
var step = opts.step;
var data_num = opts.data_num;
var exponential = MyJS.defVal(opts.exponential, false);
var precision = opts.precision;
var labels = Boolean(opts.labels);
var callback = opts.callback;
if ($.type(min) !== "number") {
throw new Error("'min' must be a number");
}
if ($.type(max) !== "number") {
throw new Error("'max' must be a number");
}
if (max <= min) {
throw new Error("'max' must be greater than 'min'");
}
if ((step === undefined) === (data_num === undefined)) {
throw new Error("You have to specify at least one, and only " +
"one, of these parameters: 'step' and " +
"'data_num'");
}
if ($.type(exponential) !== "boolean") {
throw new Error("'exponential' option must be a boolean");
}
if (step !== undefined && step <=0) {
throw new Error("'step' must be > 0");
}
if (data_num !== undefined && (! MyJS.isInt(data_num) ||
data_num <=0)) {
throw new Error("'data_num' must be a positive integer");
}
if (precision !== undefined && (! MyJS.isInt(precision) ||
precision < 0)) {
throw new Error("Optional 'precision' parameter must be " +
"an integer >= 0");
}
var steps;
if (data_num !== undefined) {
steps = data_num;
step = (max - min) / (steps - 1);
}
else {
steps = Math.ceil((max - min) / step) + 1;
}
var res = [];
var val;
var val_str;
var step_real;
for (var i=0; i<steps; i+=1) {
if (i === steps - 1) {
val = max;
}
else {
val = min + step*i;
}
if (i === 0) {
step_real = -val;
}
else if (i === 1) {
step_real += val;
}
if (precision !== undefined) {
if (exponential) {
val_str = val.toExponential(precision);
}
else {
val_str = val.toFixed(precision);
}
}
else {
val_str = val.toString();
}
if (callback) {
val = parseFloat(val_str);
val_str = callback(val).toString();
}
if (labels) {
res.push(val_str);
}
else {
res.push(parseFloat(val_str));
}
}
return {"data": res, "step_real": step_real};
},
};
// class
my3.Grid = function (opts) { // jshint ignore:line
/**
* @author Sue Lockwood
* @author Marco Sulla (marcosullaroma@gmail.com)
*
* https://bocoup.com/weblog/learning-three-js-with-real-world-challenges-that-have-already-been-solved
*
*/
"use strict";
var self = this;
THREE.Object3D.call(self);
if (opts === undefined) {
opts = {};
}
var possible_opts = ["side", "step", "width", "height", "width_step",
"height_step", "color", "position", "rotation"];
for (var k in opts) {
if (opts.hasOwnProperty(k)) {
if (possible_opts.indexOf(k) < 0) {
throw new Error("Unknown option '" + k.toString() + "'");
}
}
}
if (opts["step"] !== undefined) {
if (opts["width_step"] !== undefined) {
throw new Error("You must set only one of these options: " +
"'step' and 'width_step'");
}
if (opts["height_step"] !== undefined) {
throw new Error("You must set only one of these options: " +
"'step' and 'height_step'");
}
}
if (opts["side"] !== undefined) {
if (opts["width"] !== undefined) {
throw new Error("You must set only one of these options: " +
"'side' and 'width'");
}
if (opts["height"] !== undefined) {
throw new Error("You must set only one of these options: " +
"'side' and 'height'");
}
}
if (opts["side"] === undefined) {
opts["side"] = 10;
}
if (opts["step"] === undefined) {
opts["step"] = 1;
}
if (opts["width"] === undefined) {
opts["width"] = opts["side"];
}
if (opts["height"] === undefined) {
opts["height"] = opts["side"];
}
if (opts["width_step"] === undefined) {
opts["width_step"] = opts["step"];
}
if (opts["height_step"] === undefined) {
opts["height_step"] = opts["step"];
}
if (opts["width"] <= 0) {
throw new Error("'width' option must be a positive integer");
}
if (opts["height"] <= 0) {
throw new Error("'height' option must be a positive integer");
}
if (opts["width_step"] <= 0) {
throw new Error("'width_step' option must be a positive integer");
}
if (opts["height_step"] <= 0) {
throw new Error("'height_step' option must be a positive integer");
}
if (opts["color"] === undefined) {
opts["color"] = 0xDD006C;
}
if (opts["position"] === undefined) {
opts["position"] = [0, 0, 0];
}
if (opts["rotation"] === undefined) {
opts["rotation"] = {};
}
var rotation = opts["rotation"];
var material = new THREE.LineBasicMaterial({
color: opts.color,
});
var grid_geo = new THREE.Geometry();
var stepw = opts["width_step"];
var steph = opts["height_step"];
//width
for (var i = 0; i <= opts.width; i += stepw) {
grid_geo.vertices.push(new THREE.Vector3(i, 0, 0));
grid_geo.vertices.push(new THREE.Vector3(i, opts.height, 0));
}
//height
for (var j = 0; j <= opts.height; j += steph) {
grid_geo.vertices.push(new THREE.Vector3(0, j, 0));
grid_geo.vertices.push(new THREE.Vector3(opts.width, j, 0));
}
var line = new THREE.LineSegments( grid_geo, material );
self.add(line);
self.position.copy(my3.getVector3(opts["position"]));
for (var axis in rotation) {
if (rotation.hasOwnProperty(axis)) {
self.rotation[axis] = rotation[axis];
}
}
};
my3.Grid.prototype = Object.create(THREE.Object3D.prototype);
my3.Grid.prototype.constructor = my3.Grid;
// class
my3.TextSprite = function (message, opts) { // jshint ignore:line
/**
* @author Lee Stemkoski
* @author Marco Sulla (marcosullaroma@gmail.com)
*
* https://stemkoski.github.io/Three.js/Sprite-Text-Labels.html
*
*/
"use strict";
var self = this;
if ( opts === undefined ) {
opts = {};
}
var possible_opts = ["font_face", "font_size", "border_thickness",
"border_color", "background_color", "text_color"];
for (var k in opts) {
if (opts.hasOwnProperty(k)) {
if (possible_opts.indexOf(k) < 0) {
throw new Error("Unknown option '" + k.toString() + "'");
}
}
}
if (opts["font_face"] === undefined) {
opts["font_face"] = "Arial";
}
if (opts["font_size"] === undefined) {
opts["font_size"] = 100;
}
var font_size = opts["font_size"];
if (font_size <= 0) {
throw new Error("'font_size' must be a positive number");
}
if (opts["border_thickness"] === undefined) {
opts["border_thickness"] = 0;
}
if (opts["border_thickness"] < 0) {
throw new Error("'border_thickness' must be >= 0");
}
if (opts["border_color"] === undefined) {
opts["border_color"] = { r:0, g:0, b:0, a:1.0 };
}
if (opts["background_color"] === undefined) {
opts["background_color"] = { r:255, g:255, b:255, a:1.0 };
}
if (opts["text_color"] === undefined) {
opts["text_color"] = { r: 0, g: 0, b: 0, a: 1 };
}
var border_color = opts["border_color"];
var background_color = opts["background_color"];
var text_color = opts["text_color"];
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
my3.adaptCanvasToText(canvas, message, font_size, opts["font_face"]);
var scale;
if (canvas.width > 128) {
scale = canvas.width / 128;
}
// background color
context.fillStyle = ("rgba(" + background_color.r + "," +
background_color.g + "," + background_color.b + "," +
background_color.a + ")");
// border color
context.strokeStyle = ("rgba(" + border_color.r + "," + border_color.g +
"," + border_color.b + "," + border_color.a + ")");
context.lineWidth = opts["border_thickness"];
// 1.4 is extra height factor for text below baseline: g,j,p,q.
// text color
context.fillStyle = ("rgba(" + text_color.r + "," + text_color.g +
"," + text_color.b + "," + text_color.a + ")");
var metrics = context.measureText( message );
var text_width = metrics.width;
context.fillText( message, (canvas.width - text_width) / 2, canvas.height / 2 + font_size / 2);
// canvas contents will be used for a texture
var texture = new THREE.Texture(canvas);
texture.needsUpdate = true;
var spriteMaterial = new THREE.SpriteMaterial({map: texture});
THREE.Sprite.call(self, spriteMaterial);
if (scale) {
self.scale.set(scale, scale, 1);
}
};
my3.TextSprite.prototype = Object.create(THREE.Sprite.prototype);
my3.TextSprite.prototype.constructor = my3.TextSprite;
// class
my3.AxisLabels = function (data, steps, position, text_opts) {
/**
* @author Sue Lockwood
* @author Marco Sulla (marcosullaroma@gmail.com)
*
* https://bocoup.com/weblog/learning-three-js-with-real-world-challenges-that-have-already-been-solved
*
*/
"use strict";
var self = this;
THREE.Object3D.call(self);
var p = {x:0, y:0, z:0};
var axes = ["x", "y", "z"];
var axis;
for ( var i = 0; i < data.length; i+=1 ) {
var label = new my3.TextSprite(data[i], text_opts);
label.position.set(p.x, p.y, p.z);
self.add( label );
for (var j=0; j<axes.length; j+=1) {
axis = axes[j];
var step = steps[axis];
if (step) {
p[axis] += step;
}
}
}
self.position.copy(my3.getVector3(position));
};
my3.AxisLabels.prototype = Object.create(THREE.Object3D.prototype);
my3.AxisLabels.prototype.constructor = my3.AxisLabels;
// class
my3.GridAxes = function (labels, opts) { // jshint ignore:line
"use strict";
var self = this;
THREE.Object3D.call(self);
if ( opts === undefined ) {
opts = {};
}
var possible_opts = ["origin", "steps", "lengths", "colors",
"text_color", "text_background_color",
"font_size", "gaps"];
for (var k in opts) {
if (opts.hasOwnProperty(k)) {
if (possible_opts.indexOf(k) < 0) {
throw new Error("Unknown option '" + k.toString() + "'");
}
}
}
opts.gaps = MyJS.defVal(opts.gaps, [false, true, false]);
if (opts["steps"] === undefined) {
opts["steps"] = [1, 1, 1];
}
if (opts["colors"] === undefined) {
opts["colors"] = ["red", "green", "blue"];
}
if (! (opts["steps"] instanceof Array)) {
opts["steps"] = [opts["steps"], opts["steps"], opts["steps"]];
}
if (! (opts["colors"] instanceof Array)) {
opts["colors"] = [opts["colors"], opts["colors"], opts["colors"]];
}
var step;
for (var i=0; i<opts["steps"].length; i+=1) {
step = opts["steps"][i];
if (step <= 0) {
throw new Error("All 'step' elements must be > 0");
}
}
var x_step = opts["steps"][0];
var y_step = opts["steps"][1];
var z_step = opts["steps"][2];
var grid_steps = {};
grid_steps.x = labels[0].length;
grid_steps.y = labels[1].length;
grid_steps.z = labels[2].length;
if (opts.gaps[0]) {
grid_steps.x += 1;
}
if (opts.gaps[1]) {
grid_steps.y += 1;
}
if (opts.gaps[2]) {
grid_steps.z += 1;
}
if (opts["lengths"] === undefined) {
opts["lengths"] = [
grid_steps.x*x_step,
grid_steps.y*y_step,
grid_steps.z*z_step,
];
}
var x_length = opts["lengths"][0];
var y_length = opts["lengths"][1];
var z_length = opts["lengths"][2];
var origin = my3.getVector3(opts["origin"]);
if (opts.gaps[0]) {
origin.x -= x_step;
}
if (! opts.gaps[2]) {
origin.z += z_step;
}
var pos_z = my3.getVector3(origin.x, origin.y + y_length, origin.z + z_length);
var xy_grid = new my3.Grid({
"width": x_length,
"height": y_length,
"width_step": x_step,
"height_step": y_step,
"color": opts["colors"][0],
"position": origin,
});
self.add(xy_grid);
var grid_yz_pos = my3.getVector3(origin.x, origin.y, origin.z + z_length);
var yz_grid = new my3.Grid({
"width": y_length,
"height": z_length,
"width_step": y_step,
"height_step": z_step,
"color": opts["colors"][1],
"position": grid_yz_pos,
"rotation": {"y": -Math.PI/2, "z": Math.PI/2},
});
self.add(yz_grid);
var zx_grid = new my3.Grid({
"width": x_length,
"height": z_length,
"width_step": x_step,
"height_step": z_step,
"color": opts["colors"][2],
"position": pos_z,
"rotation": {"x": -Math.PI/2},
});
self.add(zx_grid);
var text_opts = {
"text_color": opts["text_color"],
"background_color": opts["text_background_color"],
"font_size": opts["font_size"],
};
var labels_x_pos_x = origin.x;
if (opts.gaps[0]) {
labels_x_pos_x += x_step;
}
var labels_x_pos = my3.getVector3(labels_x_pos_x,
origin.y,
origin.z);
var labels_data_x = labels[0];
var labels_x = new my3.AxisLabels(labels_data_x, {"x": x_step},
labels_x_pos, text_opts);
labels_x.position.y -= opts["font_size"] / 150;
self.add(labels_x);
var labels_y_pos = my3.getVector3(origin.x + x_length,
origin.y + y_step,
origin.z);
var labels_data_y = labels[1];
var labels_y = new my3.AxisLabels(labels_data_y, {"y": y_step},
labels_y_pos, text_opts);
var labels_y_arr = labels_y.children;
var labels_y_last = labels_y_arr[labels_y_arr.length - 1];
var box_y = new THREE.Box3().setFromObject(labels_y_last);
labels_y.position.x += (box_y.size().x) / 2;
self.add(labels_y);
var labels_z_pos_z = origin.z;
if (opts.gaps[2]) {
labels_z_pos_z += z_step;
}
var labels_z_pos = my3.getVector3(origin.x,
origin.y + y_length,
labels_z_pos_z);
var labels_data_z = labels[2];
var labels_z = new my3.AxisLabels(labels_data_z, {"z": z_step},
labels_z_pos, text_opts);
// var labels_z_arr = labels_z.children;
// var labels_z_last = labels_z_arr[labels_z_arr.length - 1];
// var box_z = new THREE.Box3().setFromObject(labels_z_last);
// var box_z_width_offset = (box_z.size().x) / 2;
// labels_z.position.y -= box_z_width_offset;
// labels_z.position.x -= box_z_width_offset;
self.add(labels_z);
};
my3.GridAxes.prototype = Object.create(THREE.Object3D.prototype);
my3.GridAxes.prototype.constructor = my3.GridAxes;