Skip to content

Commit 358bc45

Browse files
committed
fix picking bugs
1 parent f3af689 commit 358bc45

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

lib/shaders/pick-fragment.glsl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ uniform vec4 pickOffset;
55
varying vec4 pickA, pickB;
66

77
void main() {
8-
vec4 pick = vec4(pickA.xyz, 0);
8+
vec4 fragId = vec4(pickA.xyz, 0.0);
99
if(pickB.w > pickA.w) {
10-
pick.xyz = pickB.xyz;
10+
fragId.xyz = pickB.xyz;
1111
}
1212

13-
vec4 id = pick + pickOffset;
14-
id.y += floor(id.x / 256.0);
15-
id.z += floor(id.y / 256.0);
16-
id.w += floor(id.z / 256.0);
17-
id -= 256.0 * floor(id / 256.0);
18-
gl_FragColor = id / 255.0;
13+
fragId += pickOffset;
14+
15+
fragId.y += floor(fragId.x / 256.0);
16+
fragId.x -= floor(fragId.x / 256.0) * 256.0;
17+
18+
fragId.z += floor(fragId.y / 256.0);
19+
fragId.y -= floor(fragId.y / 256.0) * 256.0;
20+
21+
fragId.w += floor(fragId.z / 256.0);
22+
fragId.z -= floor(fragId.z / 256.0) * 256.0;
23+
24+
gl_FragColor = fragId / 255.0;
1925
}

lib/shaders/pick-vertex.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ varying vec4 pickA, pickB;
1313

1414
void main() {
1515
vec3 base = matrix * vec3(a, 1);
16-
vec2 n = 0.5 * width *
16+
vec2 n = width *
1717
normalize(screenShape.yx * vec2(d.y, -d.x)) / screenShape.xy;
1818
gl_Position = vec4(base.xy/base.z + n, 0, 1);
1919
pickA = pick0;

line.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ var ndarray = require('ndarray')
99
var pool = require('typedarray-pool')
1010

1111
var SHADERS = require('./lib/shaders')
12-
function compareScale(a, b) {
13-
return b - a.pixelSize
14-
}
1512

1613
function GLLine2D(
1714
plot,
@@ -85,8 +82,6 @@ return function() {
8582
var screenX = viewBox[2] - viewBox[0]
8683
var screenY = viewBox[3] - viewBox[1]
8784

88-
var pixelSize = Math.max(dataX / screenX, dataY / screenY) / pixelRatio
89-
9085
MATRIX[0] = 2.0 * boundX / dataX
9186
MATRIX[4] = 2.0 * boundY / dataY
9287
MATRIX[6] = 2.0 * (bounds[0] - dataBox[0]) / dataX - 1.0
@@ -199,21 +194,18 @@ proto.drawPick = (function() {
199194
var bounds = this.bounds
200195
var count = this.vertCount
201196

202-
203197
var gl = plot.gl
204198
var viewBox = plot.viewBox
205199
var dataBox = plot.dataBox
206-
var pixelRatio = plot.pixelRatio
200+
var pixelRatio = plot.pickPixelRatio
207201

208-
var boundX = bounds[2] - bounds[0]
209-
var boundY = bounds[3] - bounds[1]
202+
var boundX = bounds[2] - bounds[0]
203+
var boundY = bounds[3] - bounds[1]
210204
var dataX = dataBox[2] - dataBox[0]
211205
var dataY = dataBox[3] - dataBox[1]
212206
var screenX = viewBox[2] - viewBox[0]
213207
var screenY = viewBox[3] - viewBox[1]
214208

215-
var pixelSize = Math.max(dataX / screenX, dataY / screenY) / pixelRatio
216-
217209
this.pickOffset = pickOffset
218210

219211
MATRIX[0] = 2.0 * boundX / dataX
@@ -249,7 +241,7 @@ proto.drawPick = (function() {
249241

250242
gl.drawArrays(gl.TRIANGLES, 0, count)
251243

252-
return pickOffset+numPoints
244+
return pickOffset + numPoints
253245
}
254246
})()
255247

@@ -333,6 +325,13 @@ proto.update = function(options) {
333325
bounds[3] = Math.max(bounds[3], ay)
334326
}
335327

328+
if(bounds[0] === bounds[2]) {
329+
bounds[2] += 1
330+
}
331+
if(bounds[3] === bounds[1]) {
332+
bounds[3] += 1
333+
}
334+
336335
//Generate line data
337336
var lineData = pool.mallocFloat32(24*(numPoints-1))
338337
var pickData = pool.mallocUint32(12*(numPoints-1))

0 commit comments

Comments
 (0)