Skip to content

Commit 1a4a57a

Browse files
committed
Update palette mode error
1 parent ce221ed commit 1a4a57a

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

scatter.js

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ function Scatter (regl, options) {
3636

3737
// persistent variables
3838
let gl = regl._gl,
39-
drawMarker, drawCircle,
4039
sizeBuffer, positionBuffer, positionFractBuffer, colorBuffer,
4140
paletteTexture, palette = [], paletteIds = {},
4241

@@ -132,11 +131,11 @@ function Scatter (regl, options) {
132131

133132
// create marker textures
134133
options.clone.markers.forEach(markers => {
135-
addMarker(markers)
134+
this.addMarker(markers)
136135
})
137136
// clone palette texture
138-
updatePalette(options.clone.palette)
139-
updateBuffers({point: true, color: true, size: true})
137+
this.updatePalette(options.clone.palette)
138+
this.updateBuffers({point: true, color: true, size: true})
140139
}
141140
// full create from options
142141
else {
@@ -201,15 +200,18 @@ function Scatter (regl, options) {
201200
buffer: colorBuffer,
202201
stride: tooManyColors ? 8 : 4,
203202
offset: 0
204-
} : {constant: tooManyColors ? palette.slice(prop.color * 4, prop.color * 4 + 4) : [prop.color]},
203+
} : {
204+
constant: tooManyColors ? palette.slice(prop.color * 4, prop.color * 4 + 4) : [prop.color]
205+
},
205206
borderColorId: (ctx, prop) => prop.borderColor.length ? {
206207
buffer: colorBuffer,
207208
stride: tooManyColors ? 8 : 4,
208209
offset: tooManyColors ? 4 : 2
209-
} : {constant: tooManyColors ? palette.slice(prop.borderColor * 4, prop.borderColor * 4 + 4) : [prop.borderColor]}
210+
} : {
211+
constant: tooManyColors ? palette.slice(prop.borderColor * 4, prop.borderColor * 4 + 4) : [prop.borderColor]
212+
}
210213
},
211214

212-
213215
blend: {
214216
enable: true,
215217
color: [0,0,0,1],
@@ -330,7 +332,6 @@ Scatter.prototype.drawItem = function (id, els) {
330332
whitelist[els[i]] = true
331333
}
332334
}
333-
334335
// draw circles
335336
if (group.markerIds[0]) {
336337
let opts = this.getMarkerDrawOptions(group.markerIds[0], group, whitelist)
@@ -361,12 +362,12 @@ Scatter.prototype.getMarkerDrawOptions = function(ids, group, whitelist) {
361362
if (!ids.snap) {
362363
let elements = whitelist ? filter(ids.data, whitelist) : ids.elements;
363364

364-
return [extend({}, group, {
365+
return [ extend({}, group, {
365366
elements: elements,
366367
offset: 0,
367368
count: whitelist ? elements.length : ids.length,
368369
marker: markerTextures[ids.id]
369-
})]
370+
}) ]
370371
}
371372

372373
// scales batch
@@ -714,7 +715,7 @@ Scatter.prototype.update = function (...args) {
714715

715716
// update buffers data based on existing groups
716717
Scatter.prototype.updateBuffers = function({point, size, color}) {
717-
let { paletteIds, palette, groups, tooManyColors, colorBuffer, positionBuffer, positionFractBuffer, maxColors, maxSize, sizeBuffer } = this
718+
let { palette, groups, tooManyColors, colorBuffer, positionBuffer, positionFractBuffer, maxColors, maxSize, sizeBuffer } = this
718719

719720
// put point/color data into buffers, if updated any of them
720721
let len = groups.reduce((acc, group, i) => {
@@ -733,8 +734,14 @@ Scatter.prototype.updateBuffers = function({point, size, color}) {
733734
positionFractData.set(fract32(positions), offset * 2)
734735
})
735736

736-
positionBuffer(positionData)
737-
positionFractBuffer(positionFractData)
737+
positionBuffer({
738+
data: positionData,
739+
usage: 'dynamic'
740+
})
741+
positionFractBuffer({
742+
data: positionFractData,
743+
usage: 'dynamic'
744+
})
738745
}
739746

740747
if (size) {
@@ -755,7 +762,10 @@ Scatter.prototype.updateBuffers = function({point, size, color}) {
755762
sizeData.set(sizes, offset * 2)
756763
}
757764
})
758-
sizeBuffer(sizeData)
765+
sizeBuffer({
766+
data: sizeData,
767+
usage: 'dynamic'
768+
})
759769
}
760770

761771
if (color) {
@@ -794,7 +804,8 @@ Scatter.prototype.updateBuffers = function({point, size, color}) {
794804
// if limited amount of colors - keep palette color picking
795805
// that saves significant memory
796806
else {
797-
colorData = new Uint8Array(len * 4)
807+
// we need slight data increase by 2 due to vec4 borderId in shader
808+
colorData = new Uint8Array(len * 4 + 2)
798809

799810
groups.forEach((group, i) => {
800811
if (!group) return
@@ -819,8 +830,10 @@ Scatter.prototype.updateBuffers = function({point, size, color}) {
819830
}
820831
})
821832
}
822-
823-
colorBuffer(colorData)
833+
colorBuffer({
834+
data: colorData,
835+
usage: 'dynamic'
836+
})
824837
}
825838
}
826839

@@ -948,11 +961,13 @@ Scatter.prototype.updatePalette = function(palette) {
948961

949962
// remove unused stuff
950963
Scatter.prototype.destroy = function () {
951-
groups.length = 0
964+
this.groups.length = 0
952965

953-
sizeBuffer.destroy()
954-
positionBuffer.destroy()
955-
positionFractBuffer.destroy()
956-
colorBuffer.destroy()
957-
paletteTexture.destroy()
966+
this.sizeBuffer.destroy()
967+
this.positionBuffer.destroy()
968+
this.positionFractBuffer.destroy()
969+
this.colorBuffer.destroy()
970+
this.paletteTexture.destroy()
971+
972+
return this
958973
}

test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function show (arr) {
7676

7777

7878

79-
let N = 1e4
79+
let N = 1e5
8080
let ratio = window.innerWidth / window.innerHeight
8181
let range = [-10 * ratio, -10, 10 * ratio, 10]
8282
let colors = palettes[Math.floor(Math.random() * palettes.length)]
@@ -102,19 +102,18 @@ scatter(...Array(passes).fill(null).map((x, i) => {
102102

103103
// color: 'red',
104104
// color: ['red', 'green', 'blue', 'black', 'red', 'red', 'red', 'gray'],
105-
color: Array(pos.length).fill(0).map(() => colors[Math.floor(Math.random() * colors.length)]),
105+
color: Array(N).fill(0).map(() => colors[Math.floor(Math.random() * colors.length)]),
106106
// color: 'rgba(0, 0, 255, .5)',
107-
// color: Array(pos.length * 4).fill(0).map(Math.random),
107+
// color: Array(N * 4).fill(0).map(Math.random),
108108
// borderColor: 'rgba(0, 255, 0, .5)',
109109

110110
// marker: markers[i],
111111
marker: Array(N).fill(0).map(() => markers[Math.floor(Math.random() * markers.length)]),
112112

113113
range: range,
114-
// borderSize: 1,
115-
// borderColor: [[.5,.5,.5,1]],
114+
borderSize: 3,
115+
borderColor: Array(N).fill(0).map(() => colors[Math.floor(Math.random() * colors.length)]),
116116
snap: true,
117-
precise: true,
118117

119118
// viewport: [0,100,300,300]
120119
}

0 commit comments

Comments
 (0)