Skip to content

Commit a6aef98

Browse files
committed
Fix #42
1 parent c82b498 commit a6aef98

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export const DEFAULT_KEY_MAP = {
7777
export const DEFAULT_DATA_ASPECT_RATIO = 1;
7878
export const DEFAULT_WIDTH = 'auto';
7979
export const DEFAULT_HEIGHT = 'auto';
80+
export const DEFAULT_GAMMA = 1;
8081

8182
// Default styles
8283
export const DEFAULT_POINT_SIZE = 6;

src/index.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import {
9191
LONG_CLICK_TIME,
9292
DEFAULT_OPACITY,
9393
DEFAULT_OPACITY_BY,
94+
DEFAULT_GAMMA,
9495
} from './constants';
9596

9697
import {
@@ -222,6 +223,7 @@ const createScatterplot = (initialProperties = {}) => {
222223
sizeBy = DEFAULT_SIZE_BY,
223224
height = DEFAULT_HEIGHT,
224225
width = DEFAULT_WIDTH,
226+
gamma = DEFAULT_GAMMA,
225227
} = initialProperties;
226228

227229
let currentWidth = width === 'auto' ? 1 : width;
@@ -1165,6 +1167,7 @@ const createScatterplot = (initialProperties = {}) => {
11651167
count: 3,
11661168
});
11671169

1170+
// From https://observablehq.com/@rreusser/selecting-the-right-opacity-for-2d-point-clouds
11681171
const copyToScreen = regl({
11691172
vert: `
11701173
precision highp float;
@@ -1176,16 +1179,23 @@ const createScatterplot = (initialProperties = {}) => {
11761179
precision highp float;
11771180
uniform vec2 srcRes;
11781181
uniform sampler2D src;
1182+
uniform float gamma;
1183+
1184+
vec3 approxLinearToSRGB (vec3 rgb, float gamma) {
1185+
return pow(clamp(rgb, vec3(0), vec3(1)), vec3(1.0 / gamma));
1186+
}
11791187
11801188
void main () {
1181-
gl_FragColor = texture2D(src, gl_FragCoord.xy / srcRes);
1189+
vec4 color = texture2D(src, gl_FragCoord.xy / srcRes);
1190+
gl_FragColor = vec4(approxLinearToSRGB(color.rgb, gamma), 1);
11821191
}`,
11831192
attributes: {
11841193
xy: [-4, -4, 4, -4, 0, 4],
11851194
},
11861195
uniforms: {
11871196
src: () => fbo,
11881197
srcRes: () => fboRes,
1198+
gamma: () => gamma,
11891199
},
11901200
count: 3,
11911201
depth: { enable: false },
@@ -1683,16 +1693,16 @@ const createScatterplot = (initialProperties = {}) => {
16831693
const draw = (showRecticleOnce) => {
16841694
if (!isInit || !regl) return;
16851695

1696+
// Update camera
1697+
isViewChanged = camera.tick();
1698+
16861699
fbo.use(() => {
16871700
regl.clear({
16881701
// background color (transparent)
16891702
color: [0, 0, 0, 0],
16901703
depth: 1,
16911704
});
16921705

1693-
// Update camera
1694-
isViewChanged = camera.tick();
1695-
16961706
// eslint-disable-next-line no-underscore-dangle
16971707
if (backgroundImage && backgroundImage._reglType) {
16981708
drawBackgroundImage();
@@ -1711,16 +1721,16 @@ const createScatterplot = (initialProperties = {}) => {
17111721
if (!mouseDown && (showRecticle || showRecticleOnce)) drawRecticle();
17121722
if (hoveredPoint >= 0) drawHoveredPoint();
17131723
if (selection.length) drawSelectedPoint();
1714-
1715-
lasso.draw({
1716-
projection: getProjection(),
1717-
model: getModel(),
1718-
view: getView(),
1719-
});
17201724
});
17211725

17221726
copyToScreen();
17231727

1728+
lasso.draw({
1729+
projection: getProjection(),
1730+
model: getModel(),
1731+
view: getView(),
1732+
});
1733+
17241734
// Publish camera change
17251735
if (isViewChanged) {
17261736
updateScales();
@@ -2136,6 +2146,10 @@ const createScatterplot = (initialProperties = {}) => {
21362146
computePointSizeMouseDetection();
21372147
};
21382148

2149+
const setGamma = (newGamma) => {
2150+
gamma = +newGamma;
2151+
};
2152+
21392153
/**
21402154
* Update Regl's viewport, drawingBufferWidth, and drawingBufferHeight
21412155
*
@@ -2233,6 +2247,7 @@ const createScatterplot = (initialProperties = {}) => {
22332247
if (property === 'xScale') return xScale;
22342248
if (property === 'yScale') return yScale;
22352249
if (property === 'performanceMode') return performanceMode;
2250+
if (property === 'gamma') return gamma;
22362251

22372252
return undefined;
22382253
};
@@ -2433,6 +2448,10 @@ const createScatterplot = (initialProperties = {}) => {
24332448
setDeselectOnEscape(properties.deselectOnEscape);
24342449
}
24352450

2451+
if (properties.gamma !== undefined) {
2452+
setGamma(properties.gamma);
2453+
}
2454+
24362455
// setWidth and setHeight can be async when width or height are set to
24372456
// 'auto'. And since draw() would have anyway been async we can just make
24382457
// all calls async.

0 commit comments

Comments
 (0)