-
Notifications
You must be signed in to change notification settings - Fork 31
/
scatter-plot.ts
793 lines (701 loc) · 24.1 KB
/
scatter-plot.ts
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
import * as THREE from 'three';
import { OrbitControls } from 'three-orbitcontrols-ts';
import * as vector from './vector';
import * as util from './util';
import { ProjectorEventContext } from './projector-event-context';
import { ScatterPlotVisualizer } from './scatter-plot-visualizer';
import {
ScatterBoundingBox,
ScatterPlotRectangleSelector,
} from './scatter-plot-rectangle-selector';
import { CameraType, LabelRenderParams, RenderContext } from './render';
const BACKGROUND_COLOR = 0xffffff;
/**
* The length of the cube (diameter of the circumscribing sphere) where all the
* points live.
*/
const CUBE_LENGTH = 2;
const MAX_ZOOM = 5 * CUBE_LENGTH;
const MIN_ZOOM = 0.025 * CUBE_LENGTH;
// Constants relating to the camera parameters.
const PERSP_CAMERA_FOV_VERTICAL = 70;
const PERSP_CAMERA_NEAR_CLIP_PLANE = 0.01;
const PERSP_CAMERA_FAR_CLIP_PLANE = 100;
const ORTHO_CAMERA_FRUSTUM_HALF_EXTENT = 1.2;
// Key presses.
const SHIFT_KEY = 16;
const CTRL_KEY = 17;
const START_CAMERA_POS_3D = new THREE.Vector3(0.45, 0.9, 1.6);
const START_CAMERA_TARGET_3D = new THREE.Vector3(0, 0, 0);
const START_CAMERA_POS_2D = new THREE.Vector3(0, 0, 4);
const START_CAMERA_TARGET_2D = new THREE.Vector3(0, 0, 0);
const ORBIT_MOUSE_ROTATION_SPEED = 1;
const ORBIT_ANIMATION_ROTATION_CYCLE_IN_SECONDS = 7;
export type OnCameraMoveListener = (
cameraPosition: THREE.Vector3,
cameraTarget: THREE.Vector3
) => void;
/** Supported modes of interaction. */
export enum MouseMode {
AREA_SELECT,
CAMERA_AND_CLICK_SELECT,
}
/** Defines a camera, suitable for serialization. */
export class CameraDef {
orthographic: boolean = false;
position: vector.Point3D;
target: vector.Point3D;
zoom: number;
}
/**
* Maintains a three.js instantiation and context,
* animation state, and all other logic that's
* independent of how a 3D scatter plot is actually rendered. Also holds an
* array of visualizers and dispatches application events to them.
*/
export class ScatterPlot {
private visualizers: ScatterPlotVisualizer[] = [];
private onCameraMoveListeners: OnCameraMoveListener[] = [];
private height: number;
private width: number;
private mouseMode: MouseMode;
private backgroundColor: number = BACKGROUND_COLOR;
private dimensionality: number = 3;
private renderer: THREE.WebGLRenderer;
private scene: THREE.Scene;
private pickingTexture: THREE.WebGLRenderTarget;
private light: THREE.PointLight;
private cameraDef: CameraDef;
private camera: THREE.Camera;
private orbitAnimationOnNextCameraCreation: boolean = false;
private orbitCameraControls: any;
private orbitAnimationId: number | null;
private worldSpacePointPositions: Float32Array;
private pointColors: Float32Array;
private pointScaleFactors: Float32Array;
private labels: LabelRenderParams;
private polylineColors: { [polylineIndex: number]: Float32Array };
private polylineOpacities: Float32Array;
private polylineWidths: Float32Array;
private selecting = false;
private nearestPoint: number | null;
private mouseIsDown = false;
private isDragSequence = false;
private rectangleSelector: ScatterPlotRectangleSelector;
constructor(
private container: HTMLElement,
private projectorEventContext: ProjectorEventContext
) {
this.getLayoutValues();
this.scene = new THREE.Scene();
this.renderer = new THREE.WebGLRenderer({
alpha: true,
premultipliedAlpha: false,
antialias: false,
});
this.renderer.setClearColor(BACKGROUND_COLOR, 1);
this.container.appendChild(this.renderer.domElement);
this.light = new THREE.PointLight(0xffecbf, 1, 0);
this.scene.add(this.light);
this.setDimensions(3);
this.recreateCamera(this.makeDefaultCameraDef(this.dimensionality));
this.renderer.render(this.scene, this.camera);
this.rectangleSelector = new ScatterPlotRectangleSelector(
this.container,
(boundingBox: ScatterBoundingBox) => this.selectBoundingBox(boundingBox)
);
this.addInteractionListeners();
this.resize();
}
private addInteractionListeners() {
this.container.addEventListener('mousemove', this.onMouseMove.bind(this));
this.container.addEventListener('mousedown', this.onMouseDown.bind(this));
this.container.addEventListener('mouseup', this.onMouseUp.bind(this));
this.container.addEventListener('click', this.onClick.bind(this));
window.addEventListener('keydown', this.onKeyDown.bind(this), false);
window.addEventListener('keyup', this.onKeyUp.bind(this), false);
}
private addCameraControlsEventListeners(cameraControls: any) {
// Start is called when the user stars interacting with
// controls.
cameraControls.addEventListener('start', () => {
this.stopOrbitAnimation();
this.onCameraMoveListeners.forEach(l =>
l(this.camera.position, cameraControls.target)
);
});
// Change is called everytime the user interacts with the controls.
cameraControls.addEventListener('change', () => {
this.render();
});
// End is called when the user stops interacting with the
// controls (e.g. on mouse up, after dragging).
cameraControls.addEventListener('end', () => {});
}
private makeOrbitControls(
camera: THREE.Camera,
cameraDef: CameraDef,
cameraIs3D: boolean
) {
if (this.orbitCameraControls != null) {
this.orbitCameraControls.dispose();
}
const occ = new OrbitControls(camera, this.renderer.domElement);
(occ as any).target0 = new THREE.Vector3(
cameraDef.target[0],
cameraDef.target[1],
cameraDef.target[2]
);
(occ as any).position0 = new THREE.Vector3().copy(camera.position);
(occ as any).zoom0 = cameraDef.zoom;
occ.enableRotate = cameraIs3D;
occ.autoRotate = false;
occ.rotateSpeed = ORBIT_MOUSE_ROTATION_SPEED;
if (cameraIs3D) {
occ.mouseButtons.ORBIT = THREE.MOUSE.LEFT;
occ.mouseButtons.PAN = THREE.MOUSE.RIGHT;
} else {
(occ.mouseButtons as any).ORBIT = null;
occ.mouseButtons.PAN = THREE.MOUSE.LEFT;
}
occ.reset();
this.camera = camera;
this.orbitCameraControls = occ;
this.addCameraControlsEventListeners(this.orbitCameraControls);
}
private makeCamera3D(cameraDef: CameraDef, w: number, h: number) {
let camera: THREE.PerspectiveCamera;
{
const aspectRatio = w / h;
camera = new THREE.PerspectiveCamera(
PERSP_CAMERA_FOV_VERTICAL,
aspectRatio,
PERSP_CAMERA_NEAR_CLIP_PLANE,
PERSP_CAMERA_FAR_CLIP_PLANE
);
camera.position.set(
cameraDef.position[0],
cameraDef.position[1],
cameraDef.position[2]
);
const at = new THREE.Vector3(
cameraDef.target[0],
cameraDef.target[1],
cameraDef.target[2]
);
camera.lookAt(at);
camera.zoom = cameraDef.zoom;
camera.updateProjectionMatrix();
}
this.camera = camera;
this.makeOrbitControls(camera, cameraDef, true);
}
private makeCamera2D(cameraDef: CameraDef, w: number, h: number) {
let camera: THREE.OrthographicCamera;
const target = new THREE.Vector3(
cameraDef.target[0],
cameraDef.target[1],
cameraDef.target[2]
);
{
const aspectRatio = w / h;
let left = -ORTHO_CAMERA_FRUSTUM_HALF_EXTENT;
let right = ORTHO_CAMERA_FRUSTUM_HALF_EXTENT;
let bottom = -ORTHO_CAMERA_FRUSTUM_HALF_EXTENT;
let top = ORTHO_CAMERA_FRUSTUM_HALF_EXTENT;
// Scale up the larger of (w, h) to match the aspect ratio.
if (aspectRatio > 1) {
left *= aspectRatio;
right *= aspectRatio;
} else {
top /= aspectRatio;
bottom /= aspectRatio;
}
camera = new THREE.OrthographicCamera(
left,
right,
top,
bottom,
-1000,
1000
);
camera.position.set(
cameraDef.position[0],
cameraDef.position[1],
cameraDef.position[2]
);
camera.up = new THREE.Vector3(0, 1, 0);
camera.lookAt(target);
camera.zoom = cameraDef.zoom;
camera.updateProjectionMatrix();
}
this.camera = camera;
this.makeOrbitControls(camera, cameraDef, false);
}
private makeDefaultCameraDef(dimensionality: number): CameraDef {
const def = new CameraDef();
def.orthographic = dimensionality === 2;
def.zoom = 1.0;
if (def.orthographic) {
def.position = [
START_CAMERA_POS_2D.x,
START_CAMERA_POS_2D.y,
START_CAMERA_POS_2D.z,
];
def.target = [
START_CAMERA_TARGET_2D.x,
START_CAMERA_TARGET_2D.y,
START_CAMERA_TARGET_2D.z,
];
} else {
def.position = [
START_CAMERA_POS_3D.x,
START_CAMERA_POS_3D.y,
START_CAMERA_POS_3D.z,
];
def.target = [
START_CAMERA_TARGET_3D.x,
START_CAMERA_TARGET_3D.y,
START_CAMERA_TARGET_3D.z,
];
}
return def;
}
/** Recreate the scatter plot camera from a definition structure. */
recreateCamera(cameraDef: CameraDef) {
if (cameraDef.orthographic) {
this.makeCamera2D(cameraDef, this.width, this.height);
} else {
this.makeCamera3D(cameraDef, this.width, this.height);
}
this.orbitCameraControls.minDistance = MIN_ZOOM;
this.orbitCameraControls.maxDistance = MAX_ZOOM;
this.orbitCameraControls.update();
if (this.orbitAnimationOnNextCameraCreation) {
this.startOrbitAnimation();
}
}
private onClick(e: MouseEvent | null, notify = true) {
if (e && this.selecting) {
return;
}
// Only call event handlers if the click originated from the scatter plot.
if (!this.isDragSequence && notify) {
const selection = this.nearestPoint != null ? [this.nearestPoint] : [];
this.projectorEventContext.notifySelectionChanged(selection);
}
this.isDragSequence = false;
this.render();
}
private onMouseDown(e: MouseEvent) {
this.isDragSequence = false;
this.mouseIsDown = true;
if (this.selecting) {
this.orbitCameraControls.enabled = false;
this.rectangleSelector.onMouseDown(e.offsetX, e.offsetY);
this.setNearestPointToMouse(e);
} else if (
!e.ctrlKey &&
this.sceneIs3D() &&
this.orbitCameraControls.mouseButtons.ORBIT === THREE.MOUSE.RIGHT
) {
// The user happened to press the ctrl key when the tab was active,
// unpressed the ctrl when the tab was inactive, and now he/she
// is back to the projector tab.
this.orbitCameraControls.mouseButtons.ORBIT = THREE.MOUSE.LEFT;
this.orbitCameraControls.mouseButtons.PAN = THREE.MOUSE.RIGHT;
} else if (
e.ctrlKey &&
this.sceneIs3D() &&
this.orbitCameraControls.mouseButtons.ORBIT === THREE.MOUSE.LEFT
) {
// Similarly to the situation above.
this.orbitCameraControls.mouseButtons.ORBIT = THREE.MOUSE.RIGHT;
this.orbitCameraControls.mouseButtons.PAN = THREE.MOUSE.LEFT;
}
}
/** When we stop dragging/zooming, return to normal behavior. */
private onMouseUp(e: any) {
if (this.selecting) {
this.orbitCameraControls.enabled = true;
this.rectangleSelector.onMouseUp();
this.render();
}
this.mouseIsDown = false;
}
/**
* When the mouse moves, find the nearest point (if any) and send it to the
* hoverlisteners (usually called from embedding.ts)
*/
private onMouseMove(e: MouseEvent) {
this.isDragSequence = this.mouseIsDown;
// Depending if we're selecting or just navigating, handle accordingly.
if (this.selecting && this.mouseIsDown) {
this.rectangleSelector.onMouseMove(e.offsetX, e.offsetY);
this.render();
} else if (!this.mouseIsDown) {
this.setNearestPointToMouse(e);
if (this.nearestPoint !== null) {
this.projectorEventContext.notifyHoverOverPoint(this.nearestPoint);
}
}
}
/** For using ctrl + left click as right click, and for circle select */
private onKeyDown(e: any) {
// If ctrl is pressed, use left click to orbit
if (e.keyCode === CTRL_KEY && this.sceneIs3D()) {
this.orbitCameraControls.mouseButtons.ORBIT = THREE.MOUSE.RIGHT;
this.orbitCameraControls.mouseButtons.PAN = THREE.MOUSE.LEFT;
}
// If shift is pressed, start selecting
if (e.keyCode === SHIFT_KEY) {
this.selecting = true;
this.container.style.cursor = 'crosshair';
}
}
/** For using ctrl + left click as right click, and for circle select */
private onKeyUp(e: any) {
if (e.keyCode === CTRL_KEY && this.sceneIs3D()) {
this.orbitCameraControls.mouseButtons.ORBIT = THREE.MOUSE.LEFT;
this.orbitCameraControls.mouseButtons.PAN = THREE.MOUSE.RIGHT;
}
// If shift is released, stop selecting
if (e.keyCode === SHIFT_KEY) {
this.selecting = this.getMouseMode() === MouseMode.AREA_SELECT;
if (!this.selecting) {
this.container.style.cursor = 'default';
}
this.render();
}
}
/**
* Returns a list of indices of points in a bounding box from the picking
* texture.
* @param boundingBox The bounding box to select from.
*/
private getPointIndicesFromPickingTexture(
boundingBox: ScatterBoundingBox
): number[] {
if (this.worldSpacePointPositions == null) {
return [];
}
const pointCount = this.worldSpacePointPositions.length / 3;
const dpr = window.devicePixelRatio || 1;
const x = Math.floor(boundingBox.x * dpr);
const y = Math.floor(boundingBox.y * dpr);
const width = Math.floor(boundingBox.width * dpr);
const height = Math.floor(boundingBox.height * dpr);
// Create buffer for reading all of the pixels from the texture.
let pixelBuffer = new Uint8Array(width * height * 4);
// Read the pixels from the bounding box.
this.renderer.readRenderTargetPixels(
this.pickingTexture,
x,
this.pickingTexture.height - y,
width,
height,
pixelBuffer
);
// Keep a flat list of each point and whether they are selected or not. This
// approach is more efficient than using an object keyed by the index.
let pointIndicesSelection = new Uint8Array(
this.worldSpacePointPositions.length
);
for (let i = 0; i < width * height; i++) {
const id =
(pixelBuffer[i * 4] << 16) |
(pixelBuffer[i * 4 + 1] << 8) |
pixelBuffer[i * 4 + 2];
if (id !== 0xffffff && id < pointCount) {
pointIndicesSelection[id] = 1;
}
}
let pointIndices: number[] = [];
for (let i = 0; i < pointIndicesSelection.length; i++) {
if (pointIndicesSelection[i] === 1) {
pointIndices.push(i);
}
}
return pointIndices;
}
private selectBoundingBox(boundingBox: ScatterBoundingBox) {
let pointIndices = this.getPointIndicesFromPickingTexture(boundingBox);
this.projectorEventContext.notifySelectionChanged(pointIndices);
}
private setNearestPointToMouse(e: MouseEvent) {
if (this.pickingTexture == null) {
this.nearestPoint = null;
return;
}
const boundingBox: ScatterBoundingBox = {
x: e.offsetX,
y: e.offsetY,
width: 1,
height: 1,
};
const pointIndices = this.getPointIndicesFromPickingTexture(boundingBox);
this.nearestPoint = pointIndices != null ? pointIndices[0] : null;
}
private getLayoutValues(): vector.Point2D {
this.width = this.container.offsetWidth;
this.height = Math.max(1, this.container.offsetHeight);
return [this.width, this.height];
}
private sceneIs3D(): boolean {
return this.dimensionality === 3;
}
private remove3dAxisFromScene(): THREE.Object3D | undefined {
const axes = this.scene.getObjectByName('axes');
if (axes != null) {
this.scene.remove(axes);
}
return axes;
}
private add3dAxis() {
const axes = new THREE.AxesHelper();
axes.name = 'axes';
this.scene.add(axes);
}
/** Set 2d vs 3d mode. */
setDimensions(dimensionality: number) {
if (dimensionality !== 2 && dimensionality !== 3) {
throw new RangeError('dimensionality must be 2 or 3');
}
this.dimensionality = dimensionality;
const def = this.cameraDef || this.makeDefaultCameraDef(dimensionality);
this.recreateCamera(def);
this.remove3dAxisFromScene();
if (dimensionality === 3) {
this.add3dAxis();
}
}
/** Gets the current camera information, suitable for serialization. */
getCameraDef(): CameraDef {
const def = new CameraDef();
const pos = this.camera.position;
const tgt = this.orbitCameraControls.target;
def.orthographic = !this.sceneIs3D();
def.position = [pos.x, pos.y, pos.z];
def.target = [tgt.x, tgt.y, tgt.z];
def.zoom = (this.camera as any).zoom;
return def;
}
/** Sets parameters for the next camera recreation. */
setCameraParametersForNextCameraCreation(
def: CameraDef | null,
orbitAnimation: boolean
) {
(this.cameraDef as any) = def;
this.orbitAnimationOnNextCameraCreation = orbitAnimation;
}
/** Gets the current camera position. */
getCameraPosition(): vector.Point3D {
const currPos = this.camera.position;
return [currPos.x, currPos.y, currPos.z];
}
/** Gets the current camera target. */
getCameraTarget(): vector.Point3D {
let currTarget = this.orbitCameraControls.target;
return [currTarget.x, currTarget.y, currTarget.z];
}
/** Sets up the camera from given position and target coordinates. */
setCameraPositionAndTarget(position: vector.Point3D, target: vector.Point3D) {
this.stopOrbitAnimation();
this.camera.position.set(position[0], position[1], position[2]);
this.orbitCameraControls.target.set(target[0], target[1], target[2]);
this.orbitCameraControls.update();
this.render();
}
/** Starts orbiting the camera around its current lookat target. */
startOrbitAnimation() {
if (!this.sceneIs3D()) {
return;
}
if (this.orbitAnimationId != null) {
this.stopOrbitAnimation();
}
this.orbitCameraControls.autoRotate = true;
this.orbitCameraControls.rotateSpeed = ORBIT_ANIMATION_ROTATION_CYCLE_IN_SECONDS;
this.updateOrbitAnimation();
}
private updateOrbitAnimation() {
this.orbitCameraControls.update();
this.orbitAnimationId = requestAnimationFrame(() =>
this.updateOrbitAnimation()
);
}
/** Stops the orbiting animation on the camera. */
stopOrbitAnimation() {
this.orbitCameraControls.autoRotate = false;
this.orbitCameraControls.rotateSpeed = ORBIT_MOUSE_ROTATION_SPEED;
if (this.orbitAnimationId != null) {
cancelAnimationFrame(this.orbitAnimationId);
this.orbitAnimationId = null;
}
}
/** Adds a visualizer to the set, will start dispatching events to it */
addVisualizer(visualizer: ScatterPlotVisualizer) {
if (this.scene) {
visualizer.setScene(this.scene);
}
visualizer.onResize(this.width, this.height);
visualizer.onPointPositionsChanged(this.worldSpacePointPositions);
this.visualizers.push(visualizer);
}
/** Removes all visualizers attached to this scatter plot. */
removeAllVisualizers() {
this.visualizers.forEach(v => v.dispose());
this.visualizers = [];
}
/** Update scatter plot with a new array of packed xyz point positions. */
setPointPositions(worldSpacePointPositions: Float32Array) {
this.worldSpacePointPositions = worldSpacePointPositions;
this.visualizers.forEach(v =>
v.onPointPositionsChanged(worldSpacePointPositions)
);
}
render() {
{
const lightPos = this.camera.position.clone();
lightPos.x += 1;
lightPos.y += 1;
this.light.position.set(lightPos.x, lightPos.y, lightPos.z);
}
const cameraType =
this.camera instanceof THREE.PerspectiveCamera
? CameraType.Perspective
: CameraType.Orthographic;
let cameraSpacePointExtents: [number, number] = [0, 0];
if (this.worldSpacePointPositions != null) {
cameraSpacePointExtents = util.getNearFarPoints(
this.worldSpacePointPositions,
this.camera.position,
this.orbitCameraControls.target
);
}
const rc = new RenderContext(
this.camera,
cameraType,
this.orbitCameraControls.target,
this.width,
this.height,
cameraSpacePointExtents[0],
cameraSpacePointExtents[1],
this.backgroundColor,
this.pointColors,
this.pointScaleFactors,
this.labels,
this.polylineColors,
this.polylineOpacities,
this.polylineWidths
);
// Render first pass to picking target. This render fills pickingTexture
// with colors that are actually point ids, so that sampling the texture at
// the mouse's current x,y coordinates will reveal the data point that the
// mouse is over.
this.visualizers.forEach(v => v.onPickingRender(rc));
{
const axes = this.remove3dAxisFromScene();
this.renderer.render(this.scene, this.camera /* this.pickingTexture */);
if (axes != null) {
this.scene.add(axes);
}
}
// Render second pass to color buffer, to be displayed on the canvas.
this.visualizers.forEach(v => v.onRender(rc));
this.renderer.render(this.scene, this.camera);
}
setMouseMode(mouseMode: MouseMode) {
this.mouseMode = mouseMode;
if (mouseMode === MouseMode.AREA_SELECT) {
this.selecting = true;
this.container.style.cursor = 'crosshair';
} else {
this.selecting = false;
this.container.style.cursor = 'default';
}
}
/** Set the colors for every data point. (RGB triplets) */
setPointColors(colors: Float32Array) {
this.pointColors = colors;
}
/** Set the scale factors for every data point. (scalars) */
setPointScaleFactors(scaleFactors: Float32Array) {
this.pointScaleFactors = scaleFactors;
}
/** Set the labels to rendered */
setLabels(labels: LabelRenderParams) {
this.labels = labels;
}
/** Set the colors for every data polyline. (RGB triplets) */
setPolylineColors(colors: { [polylineIndex: number]: Float32Array }) {
this.polylineColors = colors;
}
setPolylineOpacities(opacities: Float32Array) {
this.polylineOpacities = opacities;
}
setPolylineWidths(widths: Float32Array) {
this.polylineWidths = widths;
}
getMouseMode(): MouseMode {
return this.mouseMode;
}
resetZoom() {
this.recreateCamera(this.makeDefaultCameraDef(this.dimensionality));
this.render();
}
setDayNightMode(isNight: boolean) {
const canvases = this.container.querySelectorAll('canvas');
const filterValue = isNight ? 'invert(100%)' : null;
for (let i = 0; i < canvases.length; i++) {
canvases[i].style.filter = filterValue;
}
}
resize(render = true) {
const [oldW, oldH] = [this.width, this.height];
const [newW, newH] = this.getLayoutValues();
if (this.dimensionality === 3) {
const camera = this.camera as THREE.PerspectiveCamera;
camera.aspect = newW / newH;
camera.updateProjectionMatrix();
} else {
const camera = this.camera as THREE.OrthographicCamera;
// Scale the ortho frustum by however much the window changed.
const scaleW = newW / oldW;
const scaleH = newH / oldH;
const newCamHalfWidth = ((camera.right - camera.left) * scaleW) / 2;
const newCamHalfHeight = ((camera.top - camera.bottom) * scaleH) / 2;
camera.top = newCamHalfHeight;
camera.bottom = -newCamHalfHeight;
camera.left = -newCamHalfWidth;
camera.right = newCamHalfWidth;
camera.updateProjectionMatrix();
}
// Accouting for retina displays.
const dpr = window.devicePixelRatio || 1;
this.renderer.setPixelRatio(dpr);
this.renderer.setSize(newW, newH);
// the picking texture needs to be exactly the same as the render texture.
{
const renderCanvasSize = new THREE.Vector2();
this.renderer.getSize(renderCanvasSize);
const pixelRatio = this.renderer.getPixelRatio();
this.pickingTexture = new THREE.WebGLRenderTarget(
renderCanvasSize.width * pixelRatio,
renderCanvasSize.height * pixelRatio
);
this.pickingTexture.texture.minFilter = THREE.LinearFilter;
}
this.visualizers.forEach(v => v.onResize(newW, newH));
if (render) {
this.render();
}
}
onCameraMove(listener: OnCameraMoveListener) {
this.onCameraMoveListeners.push(listener);
}
clickOnPoint(pointIndex: number) {
this.nearestPoint = pointIndex;
this.onClick(null, false);
}
}