Skip to content

Commit

Permalink
🔧 improve picking selection with overplotting
Browse files Browse the repository at this point in the history
  • Loading branch information
casperhart committed Jul 14, 2024
1 parent 4954040 commit e62e71c
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Imports:
purrr,
viridisLite,
grDevices
RoxygenNote: 7.2.0
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Suggests:
testthat (>= 3.0.0),
Expand Down
2 changes: 1 addition & 1 deletion inst/htmlwidgets/lib/show_sage_2d.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/lib/show_sage_3d.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/lib/show_scatter_2d.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/lib/show_scatter_3d.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/lib/show_slice_2d.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/htmlwidgets/lib/show_slice_3d.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion man/reexports.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions srcts/show_scatter/shaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ varying float vSize;
varying vec3 vColor;
varying float vAlpha;
varying float vPicking;
varying float vShrink; // shrink points when picking
void main(){
// 1.0 if picking else vAlpha
Expand Down
24 changes: 18 additions & 6 deletions srcts/show_scatter/show_scatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ export abstract class DisplayScatter {

const pixelBuffer = new Uint8Array(4 * width * height);

// re-render the picking scene with smaller points to prevent overplotting
// when picking
this.points.geometry.setAttribute("color", this.pickingColours);
(this.points.material as THREE.ShaderMaterial).uniforms.picking.value = 1;
(this.points.material as THREE.ShaderMaterial).uniforms.shrink.value = 1;
this.renderer.setRenderTarget(this.pickingTexture);
this.renderer.render(this.scene, this.camera);

renderer.readRenderTargetPixels(
pickingTexture,
x,
Expand All @@ -584,6 +592,12 @@ export abstract class DisplayScatter {
pixelBuffer
);

// reset from picking scene
this.renderer.setRenderTarget(null);
this.points.geometry.setAttribute("color", this.pointColours);
(this.points.material as THREE.ShaderMaterial).uniforms.picking.value = 0;
(this.points.material as THREE.ShaderMaterial).uniforms.shrink.value = 0;

const selectedPointSet = new Set<number>();
let id;
for (let i = 0; i < width * height; i++) {
Expand Down Expand Up @@ -706,12 +720,10 @@ export abstract class DisplayScatter {
this.filteredPointIndices.includes(id - 1)
) {
const toolTipCoords = this.toolTip.getBoundingClientRect();
this.toolTip.style.left = `${
Math.floor(x / dpr) - toolTipCoords.width
}px`;
this.toolTip.style.top = `${
Math.floor(y / dpr) - toolTipCoords.height
}px`;
this.toolTip.style.left = `${Math.floor(x / dpr) - toolTipCoords.width
}px`;
this.toolTip.style.top = `${Math.floor(y / dpr) - toolTipCoords.height
}px`;
this.toolTip.className = "detourrTooltip visible";
const span = this.toolTip.querySelector("span");
span.innerHTML = `${this.mapping.label[id - 1]}`;
Expand Down
1 change: 1 addition & 0 deletions srcts/show_scatter_2d/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class DisplayScatter2d extends DisplayScatter {
size: { value: Math.max(pointSize, this.minPointSize) },
zoom: { value: this.camera.zoom },
picking: { value: 0 },
shrink: { value: 0 }
},
vertexShader: VERTEX_SHADER_2D,
fragmentShader: FRAGMENT_SHADER,
Expand Down
5 changes: 4 additions & 1 deletion srcts/show_scatter_2d/shaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const VERTEX_SHADER_2D = `
uniform float size;
uniform float zoom;
uniform float picking;
uniform float shrink;
attribute float alpha;
attribute vec3 color;
Expand All @@ -10,14 +11,16 @@ attribute vec3 color;
varying vec3 vColor;
varying float vAlpha;
varying float vPicking;
varying float vShrink;
void main(){
vColor = color;
vAlpha = alpha;
vPicking = picking;
vShrink = shrink;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0);
gl_Position = projectionMatrix * mvPosition;
gl_PointSize = 100.0 * size * sqrt(zoom);
gl_PointSize = 2.0 * shrink + (100.0 * size * sqrt(zoom)) * (1.0 - shrink);
}
`;
1 change: 1 addition & 0 deletions srcts/show_scatter_3d/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class DisplayScatter3d extends DisplayScatter {
uniforms: {
size: { value: Math.max(pointSize, this.minPointSize) },
picking: { value: 0 },
shrink: { value: 0 }
},
vertexShader: VERTEX_SHADER_3D,
fragmentShader: FRAGMENT_SHADER,
Expand Down
6 changes: 5 additions & 1 deletion srcts/show_scatter_3d/shaders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const VERTEX_SHADER_3D = `
uniform float size;
uniform float picking;
// shrink when picking with box, but not on hover
uniform float shrink;
attribute vec3 color;
attribute float alpha;
Expand All @@ -9,14 +11,16 @@ attribute float alpha;
varying vec3 vColor;
varying float vAlpha;
varying float vPicking;
varying float vShrink;
void main(){
vColor = color;
vAlpha = alpha;
vPicking = picking;
vShrink = shrink;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0);
gl_Position = projectionMatrix * mvPosition;
gl_PointSize = 200.0 * size / -mvPosition.z;
gl_PointSize = 2.0 * shrink + (200.0 * size / -mvPosition.z) * (1.0 - shrink);
}
`;

0 comments on commit e62e71c

Please sign in to comment.