diff --git a/lib/shaders/pick-vertex.glsl b/lib/shaders/pick-vertex.glsl index 98e8162..9de1d24 100644 --- a/lib/shaders/pick-vertex.glsl +++ b/lib/shaders/pick-vertex.glsl @@ -1,11 +1,10 @@ -precision mediump float; +precision highp float; -attribute vec2 position; +attribute vec2 positionHi, positionLo; attribute vec2 offset; attribute vec4 id; -uniform mat3 viewTransform; -uniform vec2 pixelScale; +uniform vec2 scaleHi, scaleLo, translateHi, translateLo, pixelScale; uniform vec4 pickOffset; varying vec4 fragColor; @@ -27,8 +26,8 @@ void main() { fragColor = fragId / 255.0; gl_Position = computePosition( - position, - offset, - viewTransform, - pixelScale); + positionHi, positionLo, + scaleHi, scaleLo, + translateHi, translateLo, + pixelScale, offset); } diff --git a/lib/shaders/vertex.glsl b/lib/shaders/vertex.glsl index 59deb39..ce073f9 100644 --- a/lib/shaders/vertex.glsl +++ b/lib/shaders/vertex.glsl @@ -1,11 +1,10 @@ -precision mediump float; +precision highp float; -attribute vec2 position; +attribute vec2 positionHi, positionLo; attribute vec2 offset; attribute vec4 color; -uniform mat3 viewTransform; -uniform vec2 pixelScale; +uniform vec2 scaleHi, scaleLo, translateHi, translateLo, pixelScale; varying vec4 fragColor; @@ -15,8 +14,8 @@ void main() { fragColor = color; gl_Position = computePosition( - position, - offset, - viewTransform, - pixelScale); + positionHi, positionLo, + scaleHi, scaleLo, + translateHi, translateLo, + pixelScale, offset); } diff --git a/lib/shaders/xform.glsl b/lib/shaders/xform.glsl index baa04e6..9770da4 100644 --- a/lib/shaders/xform.glsl +++ b/lib/shaders/xform.glsl @@ -1,9 +1,8 @@ -vec4 computePosition(vec2 position, vec2 offset, mat3 view, vec2 scale) { - vec3 xposition = view * vec3(position, 1.0); - return vec4( - xposition.xy + scale * offset * xposition.z, - 0, - xposition.z); -} - #pragma glslify: export(computePosition) +vec4 computePosition(vec2 posHi, vec2 posLo, vec2 scHi, vec2 scLo, vec2 trHi, vec2 trLo, vec2 screenScale, vec2 screenOffset) { + return vec4(scHi * posHi + trHi + + scLo * posHi + trLo + + scHi * posLo + + scLo * posLo + + screenScale * screenOffset, 0, 1); +} \ No newline at end of file diff --git a/package.json b/package.json index ce58d08..d847b71 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gl-scatter2d-fancy", - "version": "1.1.3", + "version": "1.2.0", "description": "Fancy and slow scatter plots", "main": "scatter-fancy.js", "scripts": { @@ -15,6 +15,7 @@ "plot" ], "author": "Mikola Lysenko", + "contributors": ["Étienne Tétreault-Pinard", "Robert Monfera"], "license": "MIT", "bugs": { "url": "https://github.com/gl-vis/gl-scatter2d-fancy/issues" diff --git a/scatter-fancy.js b/scatter-fancy.js index 86e44e8..ae2563b 100644 --- a/scatter-fancy.js +++ b/scatter-fancy.js @@ -28,7 +28,7 @@ function getBoundary(glyph) { polys.forEach(function(loops) { loops.forEach(function(loop) { - for(var i=0; i> (i * 8)) & 0xff + } - proto.drawPick = function(offset) { - var plot = this.plot - var shader = this.pickShader - var numVertices = this.numVertices + shader.uniforms.pickOffset = PICK_OFFSET - var gl = plot.gl + this.idBuffer.bind() + shader.attributes.id.pointer(gl.UNSIGNED_BYTE, false) - this.pickOffset = offset + } else { - if(!numVertices) { - return offset - } + this.colorBuffer.bind() + shader.attributes.color.pointer(gl.UNSIGNED_BYTE, true) - for(var i=0; i<4; ++i) { - PICK_OFFSET[i] = ((offset>>(i*8)) & 0xff) } - calcScales.call(this) - - shader.bind() - - shader.uniforms.pixelScale = PIXEL_SCALE - shader.uniforms.viewTransform = MATRIX - shader.uniforms.pickOffset = PICK_OFFSET + this.posHiBuffer.bind() + shader.attributes.positionHi.pointer() - this.positionBuffer.bind() - shader.attributes.position.pointer() + this.posLoBuffer.bind() + shader.attributes.positionLo.pointer() this.offsetBuffer.bind() shader.attributes.offset.pointer() - this.idBuffer.bind() - shader.attributes.id.pointer(gl.UNSIGNED_BYTE, false) + shader.uniforms.pixelScale = PIXEL_SCALE + shader.uniforms.scaleHi = SCALE_HI + shader.uniforms.scaleLo = SCALE_LO + shader.uniforms.translateHi = TRANSLATE_HI + shader.uniforms.translateLo = TRANSLATE_LO gl.drawArrays(gl.TRIANGLES, 0, numVertices) - return offset + this.numPoints + if(pick) return offset + this.numPoints } })() +proto.draw = proto.drawPick + proto.pick = function(x, y, value) { var pickOffset = this.pickOffset var pointCount = this.numPoints @@ -233,11 +230,11 @@ proto.pick = function(x, y, value) { return null } var pointId = value - pickOffset - var points = this.points + var points = this.points return { - object: this, - pointId: pointId, - dataCoord: [ points[2*pointId], points[2*pointId+1] ] + object: this, + pointId: pointId, + dataCoord: [points[2 * pointId], points[2 * pointId + 1]] } } @@ -250,19 +247,26 @@ proto.update = function(options) { var sizes = options.sizes || [] var borderWidths = options.borderWidths || [] var borderColors = options.borderColors || [] + var i, j this.points = positions var bounds = this.bounds = [Infinity, Infinity, -Infinity, -Infinity] var numVertices = 0 - for(var i=0; i> 1 - for(var j=0; j<2; ++j) { - bounds[j] = Math.min(bounds[j], positions[2*i+j]) - bounds[2+j] = Math.max(bounds[2+j], positions[2*i+j]) + + var glyphMeshes = [] + var glyphBoundaries = [] + var glyph, border + + for(i = 0; i < glyphs.length; ++i) { + glyph = textCache('sans-serif', glyphs[i]) + border = getBoundary(glyphs[i]) + glyphMeshes.push(glyph) + glyphBoundaries.push(border) + numVertices += (glyph.data.length + border.coords.length) >> 1 + for(j = 0; j < 2; ++j) { + bounds[j] = Math.min(bounds[j], positions[2 * i + j]) + bounds[2 + j] = Math.max(bounds[2 + j], positions[2 * i + j]) } } @@ -273,61 +277,63 @@ proto.update = function(options) { bounds[3] += 1 } - var sx = 1/(bounds[2] - bounds[0]) - var sy = 1/(bounds[3] - bounds[1]) + var sx = 1 / (bounds[2] - bounds[0]) + var sy = 1 / (bounds[3] - bounds[1]) var tx = bounds[0] var ty = bounds[1] - var v_position = pool.mallocFloat32(2 * numVertices) + var v_position = pool.mallocFloat64(2 * numVertices) + var v_posHi = pool.mallocFloat32(2 * numVertices) + var v_posLo = pool.mallocFloat32(2 * numVertices) var v_offset = pool.mallocFloat32(2 * numVertices) var v_color = pool.mallocUint8(4 * numVertices) var v_ids = pool.mallocUint32(numVertices) var ptr = 0 - for(var i=0; i