Skip to content

Commit 0ff0092

Browse files
committed
Move pen line bounds calculation to vertex shader
1 parent 6bc1c32 commit 0ff0092

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

src/PenSkin.js

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ class PenSkin extends Skin {
217217

218218
this._drawLineOnBuffer(
219219
penAttributes,
220-
x0 + offset, -y0 + offset,
221-
x1 + offset, -y1 + offset
220+
x0 + offset, y0 + offset,
221+
x1 + offset, y1 + offset
222222
);
223223

224224
this._silhouetteDirty = true;
@@ -297,28 +297,6 @@ class PenSkin extends Skin {
297297

298298
this._renderer.enterDrawRegion(this._lineOnBufferDrawRegionId);
299299

300-
const diameter = penAttributes.diameter || DefaultPenAttributes.diameter;
301-
const radius = diameter / 2;
302-
// Expand line bounds by sqrt(2) / 2 each side-- this ensures that all antialiased pixels
303-
// fall within the quad, even at a 45-degree diagonal
304-
const expandedRadius = radius + 1.4142135623730951;
305-
306-
const lineLength = Math.hypot(x1 - x0, y1 - y0);
307-
const lineAngle = Math.atan2(y1 - y0, x1 - x0);
308-
309-
const halfWidth = this._bounds.width * 0.5;
310-
const halfHeight = this._bounds.height * 0.5;
311-
312-
const transformMatrix = __modelMatrix;
313-
twgl.m4.identity(transformMatrix);
314-
// Apply view transform to matrix
315-
twgl.m4.scale(transformMatrix, [1 / halfWidth, 1 / halfHeight, 1], transformMatrix);
316-
317-
twgl.m4.translate(transformMatrix, [x0, y0, 0], transformMatrix);
318-
twgl.m4.rotateZ(transformMatrix, lineAngle, transformMatrix);
319-
twgl.m4.translate(transformMatrix, [-expandedRadius, -expandedRadius, 0], transformMatrix);
320-
twgl.m4.scale(transformMatrix, [lineLength + (expandedRadius * 2), (expandedRadius * 2), 1], transformMatrix);
321-
322300
// Premultiply pen color by pen transparency
323301
const penColor = penAttributes.color4f || DefaultPenAttributes.color4f;
324302
__premultipliedColor[0] = penColor[0] * penColor[3];
@@ -327,10 +305,9 @@ class PenSkin extends Skin {
327305
__premultipliedColor[3] = penColor[3];
328306

329307
const uniforms = {
330-
u_modelMatrix: transformMatrix,
331308
u_lineColor: __premultipliedColor,
332-
u_lineThickness: diameter,
333-
u_penPoints: [x0 + halfWidth, y0 + halfHeight, x1 + halfWidth, y1 + halfHeight],
309+
u_lineThickness: penAttributes.diameter || DefaultPenAttributes.diameter,
310+
u_penPoints: [x0, -y0, x1, -y1],
334311
u_stageSize: this.size
335312
};
336313

src/shaders/sprite.vert

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
uniform mat4 u_modelMatrix;
1+
precision mediump float;
22

33
#ifdef DRAW_MODE_line
44
uniform vec2 u_stageSize;
5+
uniform float u_lineThickness;
6+
uniform vec4 u_penPoints;
7+
8+
// Add this to divisors to prevent division by 0, which results in NaNs propagating through calculations.
9+
// Smaller values can cause problems on some mobile devices.
10+
const float epsilon = 1e-3;
511
#endif
612

713
#ifndef DRAW_MODE_line
814
uniform mat4 u_projectionMatrix;
15+
uniform mat4 u_modelMatrix;
916
attribute vec2 a_texCoord;
1017
#endif
1118

@@ -14,13 +21,37 @@ attribute vec2 a_position;
1421
varying vec2 v_texCoord;
1522

1623
void main() {
17-
#ifdef DRAW_MODE_line
18-
vec4 screenCoord = u_modelMatrix * vec4(a_position, 0, 1);
19-
20-
gl_Position = screenCoord;
21-
v_texCoord = ((screenCoord.xy * 0.5) + 0.5) * u_stageSize;
22-
#else
23-
gl_Position = u_projectionMatrix * u_modelMatrix * vec4(a_position, 0, 1);
24-
v_texCoord = a_texCoord;
25-
#endif
24+
#ifdef DRAW_MODE_line
25+
// Calculate a rotated ("tight") bounding box around the two pen points.
26+
// Yes, we're doing this 6 times (once per vertex), but on actual GPU hardware,
27+
// it's still faster than doing it in JS combined with the cost of uniformMatrix4fv.
28+
29+
// Expand line bounds by sqrt(2) / 2 each side-- this ensures that all antialiased pixels
30+
// fall within the quad, even at a 45-degree diagonal
31+
vec2 position = a_position;
32+
float expandedRadius = (u_lineThickness * 0.5) + 1.4142135623730951;
33+
34+
float lineLength = length(u_penPoints.zw - u_penPoints.xy);
35+
36+
position.x *= lineLength + (2.0 * expandedRadius);
37+
position.y *= 2.0 * expandedRadius;
38+
39+
// Center around first pen point
40+
position -= expandedRadius;
41+
42+
// Rotate quad to line angle
43+
vec2 normalized = (u_penPoints.zw - u_penPoints.xy) / (lineLength + epsilon);
44+
position = mat2(normalized.x, normalized.y, -normalized.y, normalized.x) * position;
45+
// Translate quad
46+
position += u_penPoints.xy;
47+
48+
// Apply view transform
49+
position *= 2.0 / u_stageSize;
50+
51+
gl_Position = vec4(position, 0, 1);
52+
v_texCoord = position * 0.5 * u_stageSize;
53+
#else
54+
gl_Position = u_projectionMatrix * u_modelMatrix * vec4(a_position, 0, 1);
55+
v_texCoord = a_texCoord;
56+
#endif
2657
}

0 commit comments

Comments
 (0)