Skip to content

Commit e1a9c11

Browse files
committed
[sigma] Fixes #1397
The solution is to encode item IDs in only 3 channels (R, G and B), but sending a full 4 channels color, but with a hard-coded alpha at 1. It is non-optimal (we encode a 3 bytes data as a 4 bytes integer in the ByteArray), but it solves the issue quite well. Details: - Updates indexToColor and colorToIndex to fit our solution - Updates all vertex shaders so that the `v_color.a *= bias;` correction is also applied to the picking layer colors - Updates program.ts to enable alpha blending for the picking layer
1 parent a58eccb commit e1a9c11

File tree

12 files changed

+49
-21
lines changed

12 files changed

+49
-21
lines changed

packages/examples/custom-rendering/node.border.vert.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void main() {
3232
#else
3333
// For normal mode, we use the color:
3434
v_color = a_color;
35-
v_color.a *= bias;
3635
#endif
36+
37+
v_color.a *= bias;
3738
}

packages/sigma/src/rendering/program.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,15 @@ export abstract class Program<Uniform extends string = string> implements Abstra
324324
abstract setUniforms(params: RenderParams, programInfo: ProgramInfo): void;
325325

326326
protected renderProgram(params: RenderParams, programInfo: ProgramInfo): void {
327-
const { gl, program, isPicking } = programInfo;
327+
const { gl, program } = programInfo;
328328

329-
if (!isPicking) gl.enable(gl.BLEND);
330-
else gl.disable(gl.BLEND);
329+
// With the current fix for #1397, the alpha blending is enabled for the
330+
// picking layer:
331+
gl.enable(gl.BLEND);
332+
333+
// Original code:
334+
// if (!isPicking) gl.enable(gl.BLEND);
335+
// else gl.disable(gl.BLEND);
331336

332337
gl.useProgram(program);
333338
this.setUniforms(params, programInfo);

packages/sigma/src/rendering/programs/edge-arrow-head/vert.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void main() {
5757
#else
5858
// For normal mode, we use the color:
5959
v_color = a_color;
60-
v_color.a *= bias;
6160
#endif
61+
62+
v_color.a *= bias;
6263
}

packages/sigma/src/rendering/programs/edge-clamped/vert.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void main() {
5454
#else
5555
// For normal mode, we use the color:
5656
v_color = a_color;
57-
v_color.a *= bias;
5857
#endif
58+
59+
v_color.a *= bias;
5960
}

packages/sigma/src/rendering/programs/edge-line/vert.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void main() {
2222
#else
2323
// For normal mode, we use the color:
2424
v_color = a_color;
25-
v_color.a *= bias;
2625
#endif
26+
27+
v_color.a *= bias;
2728
}

packages/sigma/src/rendering/programs/edge-rectangle/vert.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void main() {
5151
#else
5252
// For normal mode, we use the color:
5353
v_color = a_color;
54-
v_color.a *= bias;
5554
#endif
55+
56+
v_color.a *= bias;
5657
}

packages/sigma/src/rendering/programs/edge-triangle/vert.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void main() {
3636
#else
3737
// For normal mode, we use the color:
3838
v_color = a_color;
39-
v_color.a *= bias;
4039
#endif
40+
41+
v_color.a *= bias;
4142
}

packages/sigma/src/rendering/programs/node-circle/vert.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void main() {
3636
#else
3737
// For normal mode, we use the color:
3838
v_color = a_color;
39-
v_color.a *= bias;
4039
#endif
40+
41+
v_color.a *= bias;
4142
}

packages/sigma/src/rendering/programs/node-image/vert.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ void main() {
3434
#else
3535
// For normal mode, we use the color:
3636
v_color = a_color;
37-
v_color.a *= bias;
3837

3938
// Pass the texture coordinates:
4039
v_texture = a_texture;
4140
#endif
41+
42+
v_color.a *= bias;
4243
}

packages/sigma/src/rendering/programs/node-point/vert.glsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void main() {
3232
#else
3333
// For normal mode, we use the color:
3434
v_color = a_color;
35-
v_color.a *= bias;
3635
#endif
36+
37+
v_color.a *= bias;
3738
}

packages/sigma/src/sigma.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,9 @@ export default class Sigma<GraphType extends Graph = Graph> extends TypedEventEm
799799
// Add data to programs
800800
for (let i = 0, l = nodes.length; i < l; i++) {
801801
const node = nodes[i];
802-
itemIDsIndex[incrID] = { type: "node", id: node };
802+
803803
nodeIndices[node] = incrID;
804+
itemIDsIndex[nodeIndices[node]] = { type: "node", id: node };
804805
incrID++;
805806

806807
const data = this.nodeDataCache[node];
@@ -842,8 +843,8 @@ export default class Sigma<GraphType extends Graph = Graph> extends TypedEventEm
842843
for (let i = 0, l = edges.length; i < l; i++) {
843844
const edge = edges[i];
844845

845-
itemIDsIndex[incrID] = { type: "edge", id: edge };
846846
edgeIndices[edge] = incrID;
847+
itemIDsIndex[edgeIndices[edge]] = { type: "edge", id: edge };
847848
incrID++;
848849

849850
const data = this.edgeDataCache[edge];

packages/sigma/src/utils/index.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,18 +314,31 @@ export function indexToColor(index: number): number {
314314
// If the index is already computed, we yield it
315315
if (typeof FLOAT_INDEX_CACHE[index] !== "undefined") return FLOAT_INDEX_CACHE[index];
316316

317-
const r = (index & 0xff000000) >>> 24;
318-
const g = (index & 0x00ff0000) >>> 16;
319-
const b = (index & 0x0000ff00) >>> 8;
320-
const a = index & 0x000000ff;
317+
// To address issue #1397, one strategy is to keep encoding 4 bytes colors,
318+
// but with alpha hard-set to 1.0 (or 255):
319+
const r = (index & 0x00ff0000) >>> 16;
320+
const g = (index & 0x0000ff00) >>> 8;
321+
const b = index & 0x000000ff;
322+
const a = 0x000000ff;
323+
324+
// The original 4 bytes color encoding was the following:
325+
// const r = (index & 0xff000000) >>> 24;
326+
// const g = (index & 0x00ff0000) >>> 16;
327+
// const b = (index & 0x0000ff00) >>> 8;
328+
// const a = index & 0x000000ff;
321329

322-
const color = rgbaToFloat(r, g, b, a);
330+
const color = rgbaToFloat(r, g, b, a, true);
323331
FLOAT_INDEX_CACHE[index] = color;
324332

325333
return color;
326334
}
327-
export function colorToIndex(r: number, g: number, b: number, a: number): number {
328-
return a + (b << 8) + (g << 16) + (r << 24);
335+
export function colorToIndex(r: number, g: number, b: number, _a: number): number {
336+
// As for the function indexToColor, because of #1397 and the "alpha is always
337+
// 1.0" strategy, we need to fix this function as well:
338+
return b + (g << 8) + (r << 16);
339+
340+
// The original 4 bytes color decoding is the following:
341+
// return a + (b << 8) + (g << 16) + (r << 24);
329342
}
330343

331344
/**

0 commit comments

Comments
 (0)