Skip to content

Commit 6138fd7

Browse files
committed
Pass color coord instead of id
1 parent f8b71c1 commit 6138fd7

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

circle-vert.glsl

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ precision highp float;
22

33
attribute vec2 position, positionFract;
44
attribute float size, borderSize;
5-
attribute float colorId, borderColorId;
5+
attribute vec2 colorId, borderColorId;
66

77
uniform vec2 scale, scaleFract, translate, translateFract;
88
uniform float pixelRatio;
@@ -20,6 +20,12 @@ vec2 paletteCoord(float id) {
2020
(floor(id / paletteSize.x) + .5) / paletteSize.y
2121
);
2222
}
23+
vec2 paletteCoord(vec2 id) {
24+
return vec2(
25+
(id.x + .5) / paletteSize.x,
26+
(id.y + .5) / paletteSize.y
27+
);
28+
}
2329

2430
void main() {
2531
vec4 color = texture2D(palette, paletteCoord(colorId));

index.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ function Scatter (regl, options) {
165165
} : {constant: [Math.round(prop.borderSize * 255 / maxSize)]},
166166
colorId: (ctx, prop) => prop.color.length ? {
167167
buffer: colorBuffer,
168-
stride: 4,
168+
stride: 8,
169169
offset: 0
170170
} : {constant: [prop.color]},
171171
borderColorId: (ctx, prop) => prop.borderColor.length ? {
172172
buffer: colorBuffer,
173-
stride: 4,
174-
offset: 2
173+
stride: 8,
174+
offset: 4
175175
} : {constant: [prop.borderColor]}
176176
},
177177

@@ -443,7 +443,9 @@ function Scatter (regl, options) {
443443
scatter2d.groups = groups = options.map((options, i) => {
444444
let group = groups[i]
445445

446-
if (!options) return group
446+
if (options === undefined) return group
447+
448+
if (options === null) options = { positions: null }
447449
else if (typeof options === 'function') options = {after: options}
448450
else if (typeof options[0] === 'number') options = {positions: options}
449451

@@ -461,6 +463,9 @@ function Scatter (regl, options) {
461463
viewport: 'viewport viewBox',
462464
opacity: 'opacity alpha'
463465
})
466+
467+
if (options.positions === null) options.positions = []
468+
464469
if (!group) {
465470
groups[i] = group = {
466471
id: i,
@@ -702,22 +707,31 @@ function Scatter (regl, options) {
702707
}
703708

704709
if (color) {
705-
let colorData = new Uint16Array(len * 2)
710+
let colorData = new Uint16Array(len * 4)
706711

707712
groups.forEach((group, i) => {
708713
if (!group) return
709714
let {count, offset, color, borderColor} = group
710715
if (!count) return
711716

712717
if (color.length || borderColor.length) {
713-
let colorIds = new Uint16Array(count*2)
718+
let colorIds = new Uint16Array(count * 4)
714719
for (let i = 0; i < count; i++) {
715-
colorIds[i*2] = color[i] == null ? color : color[i]
716-
colorIds[i*2 + 1] = borderColor[i] == null ? borderColor : borderColor[i]
720+
if (color[i] != null) {
721+
colorIds[i*4] = color[i] % maxColors
722+
colorIds[i*4 + 1] = Math.floor(color[i] / maxColors)
723+
}
724+
if (borderColor[i] != null) {
725+
colorIds[i*4 + 2] = borderColor[i] % maxColors
726+
colorIds[i*4 + 3] = Math.floor(borderColor[i] / maxColors)
727+
}
717728
}
718-
colorData.set(colorIds, offset * 2)
729+
730+
colorData.set(colorIds, offset * 4)
719731
}
720732
})
733+
734+
721735
colorBuffer(colorData)
722736
}
723737
}

marker-vert.glsl

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ precision mediump float;
22

33
attribute vec2 position, positionFract;
44
attribute float size, borderSize;
5-
attribute float colorId, borderColorId;
5+
attribute vec2 colorId, borderColorId;
66

77
uniform vec2 scale, scaleFract, translate, translateFract, paletteSize;
88
uniform float pixelRatio;
@@ -21,6 +21,12 @@ vec2 paletteCoord(float id) {
2121
(floor(id / paletteSize.x) + .5) / paletteSize.y
2222
);
2323
}
24+
vec2 paletteCoord(vec2 id) {
25+
return vec2(
26+
(id.x + .5) / paletteSize.x,
27+
(id.y + .5) / paletteSize.y
28+
);
29+
}
2430

2531
void main() {
2632
vec4 color = texture2D(palette, paletteCoord(colorId));

test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ scatter(Array(passes).fill(null).map((x, i) => {
9898

9999
size: Array(pos.length).fill(100).map(x => Math.random() * 5 + 5),
100100
// size: 10,
101+
101102
color: Array(pos.length).fill(0).map(() => colors[Math.floor(Math.random() * colors.length)]),
102-
// color: 'rgba(0, 0, 0, .5)',
103+
// color: 'rgba(0, 0, 255, .5)',
104+
// borderColor: 'rgba(255, 0, 0, .5)',
103105

104106
// marker: markers[i],
105107
// marker: Array(pos.length).fill(0).map(() => markers[Math.floor(Math.random() * markers.length)]),

0 commit comments

Comments
 (0)