forked from bqplot/bqplot-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScatterGLView.ts
More file actions
1322 lines (1153 loc) · 40.5 KB
/
Copy pathScatterGLView.ts
File metadata and controls
1322 lines (1153 loc) · 40.5 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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2015 Bloomberg Finance L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { ColorScale } from 'bqscales';
import { Mark, symbol as bqSymbol, deepCopy, Scale } from 'bqplot';
import * as d3 from 'd3';
import * as _ from 'underscore';
import { ScatterGLModel } from './ScatterGLModel';
import * as THREE from 'three';
import { ScaleType, initializeBqplotFigure } from './utils';
type TypedArray =
| Int8Array
| Uint8Array
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Uint8ClampedArray
| Float32Array
| Float64Array;
const to_float_array = function (value: any) {
if (value instanceof Float32Array) {
return value;
}
if (typeof value[Symbol.iterator] === 'function') {
const N = value.length;
const array32 = new Float32Array(N);
for (let i = 0; i < N; i++) {
array32[i] = value[i];
}
return array32;
}
return new Float32Array(value);
};
const color_to_array_rgba = function (color: any, default_color?: any) {
const color_name = color || default_color || [0, 0, 0, 0];
if (color_name == 'none') {
return [0, 0, 0, 0];
} else {
const color = new THREE.Color(color_name);
return [color.r, color.g, color.b, 1.0];
}
};
const create_colormap = function (scale: ColorScale) {
// convert the d3 color scale to a texture
const colors = scale ? scale.model.colorRange : [0, 1];
const color_scale = d3
.scaleLinear()
.range(colors)
.domain(_.range(colors.length).map((i) => i / (colors.length - 1)));
const colormap_array: any = [];
const N = 256;
_.map(_.range(N), (i) => {
const index = i / (N - 1);
const rgb = d3.color(String(color_scale(index))).hex();
const rgb_str = String(rgb);
const rgb_arr = [
parseInt('0x' + rgb_str.substring(1, 3)),
parseInt('0x' + rgb_str.substring(3, 5)),
parseInt('0x' + rgb_str.substring(5, 7)),
];
colormap_array.push(rgb_arr[0], rgb_arr[1], rgb_arr[2]);
});
const colormap_arr = new Uint8Array(colormap_array);
const colormap_texture = new THREE.DataTexture(
colormap_arr,
N,
1,
THREE.RGBFormat,
THREE.UnsignedByteType
);
colormap_texture.needsUpdate = true;
return colormap_texture;
};
class AttributeParameters {
constructor(
array: TypedArray,
item_size = 1,
mesh_per_attribute = 1,
normalized = false
) {
this.array = array;
this.item_size = item_size;
this.mesh_per_attribute = mesh_per_attribute;
this.normalized = normalized;
}
array: TypedArray;
item_size: number;
mesh_per_attribute: number;
normalized: boolean;
}
class ColorAttributeParameters extends AttributeParameters {
constructor(
array: TypedArray,
item_size = 1,
mesh_per_attribute = 1,
normalized = false,
use_colormap = true
) {
super(array, item_size, mesh_per_attribute, normalized);
this.use_colormap = use_colormap;
}
use_colormap: boolean;
}
class SelectionAttributeParameters extends AttributeParameters {
constructor(
array: TypedArray,
item_size = 1,
mesh_per_attribute = 1,
normalized = false,
use_selection = true
) {
super(array, item_size, mesh_per_attribute, normalized);
this.use_selection = use_selection;
}
use_selection: boolean;
}
export class ScatterGLView extends Mark {
async render() {
const base_render_promise = super.render();
initializeBqplotFigure(this.parent);
this.transitions = [];
this.invalidated_pixel_position = true;
// only used for the legend
this.dot = bqSymbol().type(this.model.get('marker'));
// Create square geometry (two triangles) for markers
this.instanced_geometry = new THREE.InstancedBufferGeometry();
const vertices = new Float32Array([
-0.5, 0.5, 0, 0.5, 0.5, 0, -0.5, -0.5, 0, 0.5, -0.5, 0,
]);
this.instanced_geometry.addAttribute(
'position',
new THREE.Float32BufferAttribute(vertices, 3)
);
const uv = new Float32Array([0, 1, 1, 1, 0, 0, 1, 0]);
this.instanced_geometry.addAttribute(
'uv',
new THREE.Float32BufferAttribute(uv, 2)
);
const indices = new Uint16Array([0, 2, 1, 2, 3, 1]);
this.instanced_geometry.index = new THREE.Uint16BufferAttribute(indices, 1);
// Create material for markers
this.material = new THREE.RawShaderMaterial({
uniforms: {
domain_x: { type: '2f', value: [0, 10] },
domain_y: { type: '2f', value: [-12, 12] },
domain_z: { type: '2f', value: [0, 1] },
domain_size: { type: '2f', value: [0, 1] },
domain_color: { type: '2f', value: [0, 1] },
domain_rotation: { type: '2f', value: [0, 180] },
domain_opacity: { type: '2f', value: [0, 1] },
range_x: { type: '2f', value: [0, 1] },
range_y: { type: '2f', value: [0, 1] },
range_z: { type: '2f', value: [0, 1] },
range_size: { type: '2f', value: [0, 1] },
range_rotation: { type: '2f', value: [0, Math.PI] },
range_opacity: { type: '2f', value: [0, 1] },
animation_time_x: { type: 'f', value: 1 },
animation_time_y: { type: 'f', value: 1 },
animation_time_z: { type: 'f', value: 1 },
animation_time_size: { type: 'f', value: 1 },
animation_time_color: { type: 'f', value: 1 },
animation_time_rotation: { type: 'f', value: 1 },
animation_time_opacity: { type: 'f', value: 1 },
has_selection: { type: 'b', value: false },
has_hover: { type: 'b', value: false },
has_selected_fill: { type: 'b', value: false },
has_selected_stroke: { type: 'b', value: false },
has_selected_opacity: { type: 'b', value: false },
has_unselected_fill: { type: 'b', value: false },
has_unselected_stroke: { type: 'b', value: false },
has_unselected_opacity: { type: 'b', value: false },
has_hovered_fill: { type: 'b', value: false },
has_hovered_stroke: { type: 'b', value: false },
has_hovered_opacity: { type: 'b', value: false },
has_unhovered_fill: { type: 'b', value: false },
has_unhovered_stroke: { type: 'b', value: false },
has_unhovered_opacity: { type: 'b', value: false },
selected_fill: { type: '4f', value: [1, 0, 0, 1.0] },
selected_stroke: { type: '4f', value: [1, 0, 0, 1.0] },
selected_opacity: { value: 1.0 },
unselected_fill: { type: '4f', value: [1, 0, 0, 1.0] },
unselected_stroke: { type: '4f', value: [1, 0, 0, 1.0] },
unselected_opacity: { value: 1.0 },
hovered_fill: { type: '4f', value: [0, 1, 0, 1.0] },
hovered_opacity: { value: 1.0 },
hovered_stroke: { type: '4f', value: [0, 1, 0, 1.0] },
unhovered_fill: { type: '4f', value: [0, 1, 0, 1.0] },
unhovered_stroke: { type: '4f', value: [0, 1, 0, 1.0] },
unhovered_opacity: { value: 1.0 },
default_stroke_color: { type: '4f', value: [0, 0, 0, 0] },
colormap: { type: 't', value: null },
fill: { type: 'b', value: true },
stroke_width: { type: 'f', value: 1.5 },
marker_scale: { type: 'f', value: 1.0 },
},
vertexShader: require('raw-loader!../shaders/scatter-vertex.glsl')
.default,
fragmentShader: require('raw-loader!../shaders/scatter-fragment.glsl')
.default,
transparent: true,
depthTest: false,
depthWrite: false,
// background reading for blending:
// https://limnu.com/webgl-blending-youre-probably-wrong/
blending: THREE.CustomBlending,
blendEquation: THREE.AddEquation,
blendSrc: THREE.OneFactor,
blendDst: THREE.OneMinusSrcAlphaFactor,
blendEquationAlpha: THREE.AddEquation,
blendSrcAlpha: THREE.OneFactor,
blendDstAlpha: THREE.OneMinusSrcAlphaFactor,
});
// Create mesh
this.mesh = new THREE.Mesh(this.instanced_geometry, this.material);
await base_render_promise;
this.scene = new THREE.Scene();
const x_parameters = new AttributeParameters(
to_float_array(this.model.get('x'))
);
this.x = this.initializeAttribute('x', x_parameters);
this.x_previous = this.initializeAttribute('x_previous', x_parameters);
const y_parameters = new AttributeParameters(
to_float_array(this.model.get('y'))
);
this.y = this.initializeAttribute('y', y_parameters);
this.y_previous = this.initializeAttribute('y_previous', y_parameters);
this.markers_number = Math.min(this.x.array.length, this.y.array.length);
this.instanced_geometry.maxInstancedCount = this.markers_number;
const color_parameters = this.getColorAttributeParameters();
this.color = this.initializeAttribute('color', color_parameters);
this.material.defines['USE_COLORMAP'] = color_parameters.use_colormap;
this.material.defines['USE_SCALE_X'] = true;
this.material.defines['USE_SCALE_Y'] = true;
const scaleTypeMap = {
date: ScaleType.SCALE_TYPE_LINEAR,
linear: ScaleType.SCALE_TYPE_LINEAR,
log: ScaleType.SCALE_TYPE_LOG,
};
const x_scale = this.scales.x ? this.scales.x : this.parent.scale_x;
const y_scale = this.scales.y ? this.scales.y : this.parent.scale_y;
this.material.defines[`SCALE_TYPE_X`] = scaleTypeMap[x_scale.model.type];
this.material.defines[`SCALE_TYPE_Y`] = scaleTypeMap[y_scale.model.type];
const opacity_parameters = this.getOpacityAttributeParameters();
this.opacity = this.initializeAttribute('opacity', opacity_parameters);
this.opacity_previous = this.initializeAttribute(
'opacity_previous',
opacity_parameters
);
const size_parameters = this.getSizeAttributeParameters();
this.size = this.initializeAttribute('size', size_parameters);
this.size_previous = this.initializeAttribute(
'size_previous',
size_parameters
);
const rotation_parameters = this.getRotationAttributeParameters();
this.rotation = this.initializeAttribute('rotation', rotation_parameters);
this.rotation_previous = this.initializeAttribute(
'rotation_previous',
rotation_parameters
);
const selected_parameters = this.getSelectedAttributeParameters();
this.selected = this.initializeAttribute('selected', selected_parameters);
this.material.uniforms['has_selection'].value =
selected_parameters.use_selection;
this.material.needsUpdate = true;
this.scene.add(this.mesh);
this.create_listeners();
this.compute_view_padding();
this.parent.extras.webGLMarks.push(this);
this.parent.extras.webGLRequestRender();
}
initializeAttribute(name: string, parameters: AttributeParameters) {
const attribute = new THREE.InstancedBufferAttribute(
parameters.array,
parameters.item_size,
parameters.mesh_per_attribute
);
attribute.normalized = parameters.normalized;
attribute.dynamic = true;
this.instanced_geometry.addAttribute(name, attribute);
return attribute;
}
getColorAttributeParameters() {
if (this.model.get('color')) {
const color = this.model.get('color');
// TODO Use `default_color`?
let array: Float32Array;
if (color.length < this.markers_number) {
array = to_float_array(this.markers_number);
array.set(color);
} else {
array = to_float_array(color);
}
return new ColorAttributeParameters(array, 1, 1, true, true);
} else {
let colors = this.model.get('colors');
if (!colors) {
const color =
(this.model.get('unselected_style') || {})['fill'] || 'orange';
colors = [color];
}
let array: Float32Array;
let mesh_per_attribute: number;
if (colors.length == 1) {
const color = new THREE.Color(colors[0]);
array = to_float_array([color.r, color.g, color.b]);
mesh_per_attribute = this.markers_number;
} else {
array = to_float_array(this.markers_number * 3);
_.each(_.range(this.markers_number), (i) => {
const color = new THREE.Color(colors[i % colors.length]);
array[i * 3 + 0] = color.r;
array[i * 3 + 1] = color.g;
array[i * 3 + 2] = color.b;
});
mesh_per_attribute = 1;
}
return new ColorAttributeParameters(
array,
3,
mesh_per_attribute,
false,
false
);
}
}
getOpacityAttributeParameters() {
let opacities = this.model.get('opacities') || [1];
if (opacities.length == 0) {
opacities = [1];
}
if (this.model.get('opacity')) {
const opacity = this.model.get('opacity');
let array: Float32Array;
if (opacity.length < this.markers_number) {
array = to_float_array(this.markers_number);
array.set(opacity);
_.each(_.range(this.markers_number - opacity.length), (i) => {
i = i + opacity.length;
array[i] = opacities[i % opacities.length];
});
} else {
array = to_float_array(opacity);
}
return new AttributeParameters(array, 1, 1);
} else {
let array: Float32Array;
let mesh_per_attribute: number;
if (opacities.length == 1) {
array = to_float_array(opacities);
mesh_per_attribute = this.markers_number;
} else {
array = to_float_array(this.markers_number);
mesh_per_attribute = 1;
_.each(_.range(this.markers_number), (i) => {
array[i] = opacities[i % opacities.length];
});
}
return new AttributeParameters(array, 1, mesh_per_attribute);
}
}
getSizeAttributeParameters() {
if (this.model.get('size')) {
// One size per marker
const size = this.model.get('size');
let array: Float32Array;
if (size.length < this.markers_number) {
array = to_float_array(this.markers_number);
array.fill(this.model.get('default_size'));
array.set(size);
} else {
array = to_float_array(size);
}
return new AttributeParameters(array, 1, 1);
} else {
// Same size for all the markers
return new AttributeParameters(
to_float_array([this.model.get('default_size')]),
1,
this.markers_number
);
}
}
getRotationAttributeParameters() {
if (this.model.get('rotation')) {
const rotation = this.model.get('rotation');
let array: Float32Array;
if (rotation.length < this.markers_number) {
array = to_float_array(this.markers_number);
array.set(rotation);
} else {
array = to_float_array(rotation);
}
return new AttributeParameters(array, 1, 1);
} else {
return new AttributeParameters(to_float_array(1), 1, this.markers_number);
}
}
getSelectedAttributeParameters() {
if (this.model.get('selected')) {
const selected = this.model.get('selected');
const array = to_float_array(this.markers_number);
for (let i = 0; i < selected.length; i++) {
if (selected[i] < array.length) {
array[selected[i]] = 1;
}
}
return new SelectionAttributeParameters(array, 1, 1, false, true);
} else {
return new SelectionAttributeParameters(
to_float_array(1),
1,
this.markers_number,
false,
false
);
}
}
renderGL() {
this.set_ranges();
const fig = this.parent;
const x_scale = this.scales.x ? this.scales.x : this.parent.scale_x;
const y_scale = this.scales.y ? this.scales.y : this.parent.scale_y;
const range_x = this.parent.padded_range('x', x_scale.model);
const range_y = this.parent.padded_range('y', y_scale.model);
_.each(['selected', 'hovered'], (style_type) => {
_.each(['stroke', 'fill', 'opacity'], (style_property) => {
this.material.uniforms[`has_${style_type}_${style_property}`].value =
Boolean(this.model.get(`${style_type}_style`)[style_property]);
this.material.uniforms[`has_un${style_type}_${style_property}`].value =
Boolean(this.model.get(`un${style_type}_style`)[style_property]);
if (_.contains(['opacity'], style_property)) {
this.material.uniforms[`${style_type}_${style_property}`].value =
this.model.get(`${style_type}_style`)[style_property];
this.material.uniforms[`un${style_type}_${style_property}`].value =
this.model.get(`un${style_type}_style`)[style_property];
} else {
this.material.uniforms[`${style_type}_${style_property}`].value =
color_to_array_rgba(
this.model.get(`${style_type}_style`)[style_property],
'green'
);
this.material.uniforms[`un${style_type}_${style_property}`].value =
color_to_array_rgba(
this.model.get(`un${style_type}_style`)[style_property],
'green'
);
}
});
});
this.material.uniforms['range_x'].value = range_x;
this.material.uniforms['range_y'].value = [range_y[1], range_y[0]]; // flipped coordinates in WebGL
this.material.uniforms['domain_x'].value = x_scale.scale.domain();
this.material.uniforms['domain_y'].value = y_scale.scale.domain();
if (this.scales.size) {
this.material.uniforms['range_size'].value =
this.scales.size.scale.range();
this.material.uniforms['domain_size'].value =
this.scales.size.scale.domain();
} else {
const size = this.model.get('default_size');
this.material.uniforms['range_size'].value = [0, size];
this.material.uniforms['domain_size'].value = [0, size];
}
if (this.scales.rotation) {
this.material.uniforms['range_rotation'].value =
this.scales.rotation.scale.range();
this.material.uniforms['domain_rotation'].value =
this.scales.rotation.scale.domain();
}
if (this.scales.opacity) {
this.material.uniforms['range_opacity'].value =
this.scales.opacity.scale.range();
this.material.uniforms['domain_opacity'].value =
this.scales.opacity.scale.domain();
}
const { renderer, camera } = fig.extras.webGLRenderer;
renderer.render(this.scene, camera);
const transitions_todo = [];
for (let i = 0; i < this.transitions.length; i++) {
const t = this.transitions[i];
if (!t.is_done()) {
transitions_todo.push(t);
}
t.update();
}
this.transitions = transitions_todo;
if (this.transitions.length > 0) {
this.parent.extras.webGLRequestRender();
}
}
create_listeners() {
super.create_listeners();
this.listenTo(this.model, 'change:x', this.updateX);
this.listenTo(this.model, 'change:y', this.updateY);
this.listenTo(
this.model,
'change:color change:colors change:unselected_style',
this.updateColor
);
this.listenTo(
this.model,
'change:opacity change:opacities',
this.updateOpacity
);
this.listenTo(
this.model,
'change:size change:default_size',
this.updateSize
);
this.listenTo(this.model, 'change:rotation', this.updateRotation);
this.listenTo(this.model, 'change:selected', this.updateSelected);
this.listenTo(this.model, 'change:marker', this.updateMarker);
this.updateMarker();
this.listenTo(this.model, 'change:stroke', this.updateStroke);
this.updateStroke();
this.listenTo(this.model, 'change:stroke_width', this.updateStrokeWidth);
this.updateStrokeWidth();
const sync_visible = () => {
this.mesh.visible = this.model.get('visible');
this.parent.extras.webGLRequestRender();
};
this.listenTo(this.model, 'change:visible', sync_visible);
sync_visible();
const sync_fill = () => {
this.material.defines['FILL'] = this.model.get('fill') ? 1 : 0;
this.material.needsUpdate = true;
this.parent.extras.webGLRequestRender();
};
this.listenTo(this.model, 'change:fill', sync_fill);
sync_fill();
this.listenTo(this.model, 'change', this.update_legend);
}
updateAttribute(
name: string,
value: THREE.InstancedBufferAttribute,
new_parameters: AttributeParameters
) {
// Workaround, updating `meshPerAttribute` does not work in ThreeJS and can result in a buffer overflow
// so we need to re-initialize the attribute. Same for an array size increase.
if (value.meshPerAttribute != new_parameters.mesh_per_attribute) {
value = this.initializeAttribute(name, new_parameters);
} else if (value.array.length < new_parameters.array.length) {
value = this.initializeAttribute(name, new_parameters);
} else {
value.itemSize = new_parameters.item_size;
value.setArray(new_parameters.array);
}
value.needsUpdate = true;
return value;
}
updateAttributes(
name: string,
value: THREE.InstancedBufferAttribute,
value_previous: THREE.InstancedBufferAttribute,
new_parameters: AttributeParameters,
animate = true,
after_animation: Function = () => {}
) {
const animation_duration = this.parent.model.get('animation_duration');
if (animate && animation_duration > 0) {
// `value_previous.array` must have at least the same length as `value.array` in order for animation to work
if (
value.array.length < new_parameters.array.length &&
value.meshPerAttribute == new_parameters.mesh_per_attribute
) {
const array = deepCopy(new_parameters.array);
// array.fill(default_value);
array.set(value.array);
value_previous = this.updateAttribute(
name + '_previous',
value_previous,
new AttributeParameters(
array,
value.itemSize,
value.meshPerAttribute,
value.normalized
)
);
}
// meshPerAttribute changed but not the array size, it is very likely that the number of markers has changed, value_previous must
// have the right meshPerAttribute
else if (
value.array.length == new_parameters.array.length &&
value.meshPerAttribute != new_parameters.mesh_per_attribute
) {
value_previous = this.updateAttribute(
name + '_previous',
value_previous,
new AttributeParameters(
value.array,
value.itemSize,
new_parameters.mesh_per_attribute,
value.normalized
)
);
} else {
value_previous = this.updateAttribute(
name + '_previous',
value_previous,
new AttributeParameters(
value.array,
value.itemSize,
value.meshPerAttribute,
value.normalized
)
);
}
value = this.updateAttribute(name, value, new_parameters);
this.animateAttribute(name, after_animation);
} else {
value_previous = this.updateAttribute(
name + '_previous',
value_previous,
new_parameters
);
value = this.updateAttribute(name, value, new_parameters);
}
return [value, value_previous];
}
animateAttribute(name: string, after_animation: Function = () => {}) {
this.material.uniforms['animation_time_' + name]['value'] = 0;
const set = (value) => {
this.material.uniforms['animation_time_' + name]['value'] = value;
if (value == 1) {
const updater =
{
x: this.updateX,
y: this.updateY,
size: this.updateSize,
opacity: this.updateOpacity,
}[name] || null;
if (updater !== null) {
// we update once without animation, otherwise we might
// have dynamic range issues https://github.com/bqplot/bqplot/issues/1661
// when the previous values are in a completely different d3-domain
// the issue resides in vec3 center = mix(vec3(x_previous, y_previous, 0), vec3(x, y, 0), animation_time);
// in scatter-vertex.glsl where x/y_previous is of a different magnitude, which somehow affects
// the precision in center, causing aliasing (on some hardware)
updater.apply(this, [true, false]);
}
}
};
this.transition(set, after_animation, this);
}
updateX(rerender = true, animate: boolean = true) {
const x_array = to_float_array(this.model.get('x'));
const new_markers_number = Math.min(x_array.length, this.y.array.length);
// If the `markers_number` changes
if (this.markers_number != new_markers_number) {
this.updateMarkersNumber(new_markers_number);
}
[this.x, this.x_previous] = this.updateAttributes(
'x',
this.x,
this.x_previous,
new AttributeParameters(x_array, 1, 1),
animate
);
if (rerender) {
this.parent.extras.webGLRequestRender();
}
}
updateY(rerender = true, animate: boolean = true) {
const y_array = to_float_array(this.model.get('y'));
const new_markers_number = Math.min(this.x.array.length, y_array.length);
// If the `markers_number` changes
if (this.markers_number != new_markers_number) {
this.updateMarkersNumber(new_markers_number);
}
[this.y, this.y_previous] = this.updateAttributes(
'y',
this.y,
this.y_previous,
new AttributeParameters(y_array, 1, 1),
animate
);
if (rerender) {
this.parent.extras.webGLRequestRender();
}
}
updateMarkersNumber(n: number) {
const old_markers_number = this.markers_number;
this.markers_number = n;
this.instanced_geometry.maxInstancedCount = n;
// Increasing the number of markers, we need to update buffer sizes in
// order to prevent buffer overflows. We assume here that
// `updateMarkersNumber` is called from `updateX` or `updateY` hence they
// are already correctly sized
// If the number of markers has decreased, no need to change the buffer sizes
if (this.markers_number > old_markers_number) {
this.updateColor(false);
this.updateOpacity(false);
this.updateSize(false);
this.updateRotation(false);
this.updateSelected(false);
}
}
updateColor(rerender = true) {
const color_parameters = this.getColorAttributeParameters();
this.color = this.updateAttribute('color', this.color, color_parameters);
this.color.normalized = color_parameters.normalized;
this.material.defines['USE_COLORMAP'] = color_parameters.use_colormap;
this.material.needsUpdate = true;
if (rerender) {
this.parent.extras.webGLRequestRender();
}
}
updateOpacity(rerender = true, animate: boolean = true) {
const opacity_parameters = this.getOpacityAttributeParameters();
[this.opacity, this.opacity_previous] = this.updateAttributes(
'opacity',
this.opacity,
this.opacity_previous,
opacity_parameters,
animate
);
if (rerender) {
this.parent.extras.webGLRequestRender();
}
}
updateSize(rerender = true, animate: boolean = true) {
const size_parameters = this.getSizeAttributeParameters();
[this.size, this.size_previous] = this.updateAttributes(
'size',
this.size,
this.size_previous,
size_parameters,
animate
);
if (rerender) {
this.parent.extras.webGLRequestRender();
}
}
updateRotation(rerender = true, animate: boolean = true) {
const rotation_parameters = this.getRotationAttributeParameters();
[this.rotation, this.rotation_previous] = this.updateAttributes(
'rotation',
this.rotation,
this.rotation_previous,
rotation_parameters,
animate
);
if (rerender) {
this.parent.extras.webGLRequestRender();
}
}
updateSelected(rerender = true) {
const selected_parameters = this.getSelectedAttributeParameters();
this.selected = this.updateAttribute(
'selected',
this.selected,
selected_parameters
);
this.material.uniforms['has_selection'].value =
selected_parameters.use_selection;
if (rerender) {
this.parent.extras.webGLRequestRender();
}
}
updateMarker() {
const FAST_CIRCLE = 1;
const FAST_SQUARE = 2;
const FAST_ARROW = 3;
const FAST_CROSS = 4;
const FAST_TRIANGLE_UP = 5;
const FAST_TRIANGLE_DOWN = 6;
const marker = this.model.get('marker');
this.dot.type(marker);
if (marker === 'circle') {
// same as in ./Markers.js
this.material.uniforms.marker_scale.value = 1 / Math.sqrt(Math.PI);
this.material.defines['FAST_DRAW'] = FAST_CIRCLE;
}
if (marker === 'square') {
this.material.uniforms.marker_scale.value = 1 / 2;
this.material.defines['FAST_DRAW'] = FAST_SQUARE;
}
if (marker === 'arrow') {
this.material.uniforms.marker_scale.value = 2;
this.material.defines['FAST_DRAW'] = FAST_ARROW;
}
if (marker === 'cross') {
this.material.uniforms.marker_scale.value = 3 / (2 * Math.sqrt(5));
this.material.defines['FAST_DRAW'] = FAST_CROSS;
}
if (marker === 'triangle-up') {
this.material.uniforms.marker_scale.value = 2;
this.material.defines['FAST_DRAW'] = FAST_TRIANGLE_UP;
}
if (marker === 'triangle-down') {
this.material.uniforms.marker_scale.value = 2;
this.material.defines['FAST_DRAW'] = FAST_TRIANGLE_DOWN;
}
this.material.needsUpdate = true;
this.parent.extras.webGLRequestRender();
}
updateStroke() {
const stroke = this.model.get('stroke');
if (stroke) {
this.material.uniforms.default_stroke_color.value =
color_to_array_rgba(stroke);
this.material.defines['HAS_DEFAULT_STROKE_COLOR'] = true;
} else {
this.material.defines['HAS_DEFAULT_STROKE_COLOR'] = false;
}
this.material.needsUpdate = true;
this.parent.extras.webGLRequestRender();
}
updateStrokeWidth() {
this.material.uniforms.stroke_width.value = this.model.get('stroke_width');
this.parent.extras.webGLRequestRender();
}
updateColorMap() {
this.material.uniforms['colormap'].value = create_colormap(
this.scales.color
);
if (this.scales.color) {
const color = this.model.get('color');
if (color) {
let min;
let max;
if (this.scales.color.model.min !== null) {
min = this.scales.color.model.min;
} else {
min = Math.min(...color);
}
if (this.scales.color.model.max !== null) {
max = this.scales.color.model.max;
} else {
max = Math.max(...color);
}
this.material.uniforms['domain_color'].value = [min, max];
} else {
if (
this.scales.color.model.min !== null &&
this.scales.color.model.max !== null
) {
this.material.uniforms['domain_color'].value = [
this.scales.color.model.min,
this.scales.color.model.max,
];
} else {
console.warn(
'no color set, and color scale does not have a min or max'
);
}
}
}
this.parent.extras.webGLRequestRender();
}
updatePosition(animate?) {
this.parent.extras.webGLRequestRender();
this.invalidatePixelPosition();