Skip to content

Commit a94f238

Browse files
authored
Merge pull request #580 from LLK/revert-559-revert-438-pen-shader
Revert "Revert "Draw pen lines via fragment shader""
2 parents d17066b + 4a52150 commit a94f238

File tree

4 files changed

+95
-131
lines changed

4 files changed

+95
-131
lines changed

src/PenSkin.js

Lines changed: 17 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ const __projectionMatrix = twgl.m4.identity();
4444
*/
4545
const __modelTranslationMatrix = twgl.m4.identity();
4646

47-
/**
48-
* Reused memory location for rotation matrix for building a model matrix.
49-
* @type {FloatArray}
50-
*/
51-
const __modelRotationMatrix = twgl.m4.identity();
5247

5348
/**
5449
* Reused memory location for scaling matrix for building a model matrix.
@@ -135,7 +130,7 @@ class PenSkin extends Skin {
135130
this._stampShader = this._renderer._shaderManager.getShader(ShaderManager.DRAW_MODE.default, NO_EFFECTS);
136131

137132
/** @type {twgl.ProgramInfo} */
138-
this._lineShader = this._renderer._shaderManager.getShader(ShaderManager.DRAW_MODE.lineSample, NO_EFFECTS);
133+
this._lineShader = this._renderer._shaderManager.getShader(ShaderManager.DRAW_MODE.line, NO_EFFECTS);
139134

140135
this._createLineGeometry();
141136

@@ -221,10 +216,15 @@ class PenSkin extends Skin {
221216
* @param {number} y1 - the Y coordinate of the end of the line.
222217
*/
223218
drawLine (penAttributes, x0, y0, x1, y1) {
219+
// For compatibility with Scratch 2.0, offset pen lines of width 1 and 3 so they're pixel-aligned.
220+
// See https://github.com/LLK/scratch-render/pull/314
221+
const diameter = penAttributes.diameter || DefaultPenAttributes.diameter;
222+
const offset = (diameter === 1 || diameter === 3) ? 0.5 : 0;
223+
224224
this._drawLineOnBuffer(
225225
penAttributes,
226-
this._rotationCenter[0] + x0, this._rotationCenter[1] - y0,
227-
this._rotationCenter[0] + x1, this._rotationCenter[1] - y1
226+
x0 + offset, y0 + offset,
227+
x1 + offset, y1 + offset
228228
);
229229

230230
this._silhouetteDirty = true;
@@ -234,72 +234,16 @@ class PenSkin extends Skin {
234234
* Create 2D geometry for drawing lines to a framebuffer.
235235
*/
236236
_createLineGeometry () {
237-
// Create a set of triangulated quads that break up a line into 3 parts:
238-
// 2 caps and a body. The y component of these position vertices are
239-
// divided to bring a value of 1 down to 0.5 to 0. The large y values
240-
// are set so they will still be at least 0.5 after division. The
241-
// divisor is scaled based on the length of the line and the lines
242-
// width.
243-
//
244-
// Texture coordinates are based on a "generated" texture whose general
245-
// shape is a circle. The line caps set their texture values to define
246-
// there roundedness with the texture. The body has all of its texture
247-
// values set to the center of the texture so it's a solid block.
248237
const quads = {
249238
a_position: {
250239
numComponents: 2,
251240
data: [
252-
-0.5, 1,
253-
0.5, 1,
254-
-0.5, 100000,
255-
256-
-0.5, 100000,
257-
0.5, 1,
258-
0.5, 100000,
259-
260-
-0.5, 1,
261-
0.5, 1,
262-
-0.5, -1,
263-
264-
-0.5, -1,
265-
0.5, 1,
266-
0.5, -1,
267-
268-
-0.5, -100000,
269-
0.5, -100000,
270-
-0.5, -1,
271-
272-
-0.5, -1,
273-
0.5, -100000,
274-
0.5, -1
275-
]
276-
},
277-
a_texCoord: {
278-
numComponents: 2,
279-
data: [
280-
1, 0.5,
281-
0, 0.5,
282-
1, 0,
283-
284241
1, 0,
285-
0, 0.5,
286242
0, 0,
287-
288-
0.5, 0,
289-
0.5, 1,
290-
0.5, 0,
291-
292-
0.5, 0,
293-
0.5, 1,
294-
0.5, 1,
295-
296-
1, 0,
297-
0, 0,
298-
1, 0.5,
299-
300-
1, 0.5,
243+
1, 1,
244+
1, 1,
301245
0, 0,
302-
0, 0.5
246+
0, 1
303247
]
304248
}
305249
};
@@ -344,6 +288,8 @@ class PenSkin extends Skin {
344288

345289
/**
346290
* Draw a line on the framebuffer.
291+
* Note that the point coordinates are in the following coordinate space:
292+
* +y is down, (0, 0) is the center, and the coords range from (-width / 2, -height / 2) to (height / 2, width / 2).
347293
* @param {PenAttributes} penAttributes - how the line should be drawn.
348294
* @param {number} x0 - the X coordinate of the beginning of the line.
349295
* @param {number} y0 - the Y coordinate of the beginning of the line.
@@ -357,26 +303,6 @@ class PenSkin extends Skin {
357303

358304
this._renderer.enterDrawRegion(this._lineOnBufferDrawRegionId);
359305

360-
const diameter = penAttributes.diameter || DefaultPenAttributes.diameter;
361-
const length = Math.hypot(Math.abs(x1 - x0) - 0.001, Math.abs(y1 - y0) - 0.001);
362-
const avgX = (x0 + x1) / 2;
363-
const avgY = (y0 + y1) / 2;
364-
const theta = Math.atan2(y0 - y1, x0 - x1);
365-
const alias = 1;
366-
367-
// The line needs a bit of aliasing to look smooth. Add a small offset
368-
// and a small size boost to scaling to give a section to alias.
369-
const translationVector = __modelTranslationVector;
370-
translationVector[0] = avgX - (alias / 2);
371-
translationVector[1] = avgY + (alias / 4);
372-
373-
const scalingVector = __modelScalingVector;
374-
scalingVector[0] = diameter + alias;
375-
scalingVector[1] = length + diameter - (alias / 2);
376-
377-
const radius = diameter / 2;
378-
const yScalar = (0.50001 - (radius / (length + diameter)));
379-
380306
// Premultiply pen color by pen transparency
381307
const penColor = penAttributes.color4f || DefaultPenAttributes.color4f;
382308
__premultipliedColor[0] = penColor[0] * penColor[3];
@@ -385,19 +311,10 @@ class PenSkin extends Skin {
385311
__premultipliedColor[3] = penColor[3];
386312

387313
const uniforms = {
388-
u_positionScalar: yScalar,
389-
u_capScale: diameter,
390-
u_aliasAmount: alias,
391-
u_modelMatrix: twgl.m4.multiply(
392-
twgl.m4.multiply(
393-
twgl.m4.translation(translationVector, __modelTranslationMatrix),
394-
twgl.m4.rotationZ(theta - (Math.PI / 2), __modelRotationMatrix),
395-
__modelMatrix
396-
),
397-
twgl.m4.scaling(scalingVector, __modelScalingMatrix),
398-
__modelMatrix
399-
),
400-
u_lineColor: __premultipliedColor
314+
u_lineColor: __premultipliedColor,
315+
u_lineThickness: penAttributes.diameter || DefaultPenAttributes.diameter,
316+
u_penPoints: [x0, -y0, x1, -y1],
317+
u_stageSize: this.size
401318
};
402319

403320
twgl.setUniforms(currentShader, uniforms);

src/ShaderManager.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ ShaderManager.DRAW_MODE = {
174174
colorMask: 'colorMask',
175175

176176
/**
177-
* Sample a "texture" to draw a line with caps.
177+
* Draw a line with caps.
178178
*/
179-
lineSample: 'lineSample'
179+
line: 'line'
180180
};
181181

182182
module.exports = ShaderManager;

src/shaders/sprite.frag

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ uniform float u_mosaic;
3333
uniform float u_ghost;
3434
#endif // ENABLE_ghost
3535

36-
#ifdef DRAW_MODE_lineSample
36+
#ifdef DRAW_MODE_line
3737
uniform vec4 u_lineColor;
38-
uniform float u_capScale;
39-
uniform float u_aliasAmount;
40-
#endif // DRAW_MODE_lineSample
38+
uniform float u_lineThickness;
39+
uniform vec4 u_penPoints;
40+
#endif // DRAW_MODE_line
4141

4242
uniform sampler2D u_skin;
4343

@@ -109,7 +109,7 @@ const vec2 kCenter = vec2(0.5, 0.5);
109109

110110
void main()
111111
{
112-
#ifndef DRAW_MODE_lineSample
112+
#ifndef DRAW_MODE_line
113113
vec2 texcoord0 = v_texCoord;
114114

115115
#ifdef ENABLE_mosaic
@@ -214,15 +214,27 @@ void main()
214214
// Un-premultiply alpha.
215215
gl_FragColor.rgb /= gl_FragColor.a + epsilon;
216216
#endif
217-
218-
#else // DRAW_MODE_lineSample
219-
gl_FragColor = u_lineColor * clamp(
220-
// Scale the capScale a little to have an aliased region.
221-
(u_capScale + u_aliasAmount -
222-
u_capScale * 2.0 * distance(v_texCoord, vec2(0.5, 0.5))
223-
) / (u_aliasAmount + 1.0),
224-
0.0,
225-
1.0
226-
);
227-
#endif // DRAW_MODE_lineSample
217+
218+
#else // DRAW_MODE_line
219+
// Maaaaagic antialiased-line-with-round-caps shader.
220+
// Adapted from Inigo Quilez' 2D distance function cheat sheet
221+
// https://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
222+
223+
// The xy component of u_penPoints is the first point; the zw is the second point.
224+
// This is done to minimize the number of gl.uniform calls, which can add up.
225+
vec2 pa = v_texCoord - u_penPoints.xy, ba = u_penPoints.zw - u_penPoints.xy;
226+
// Magnitude of vector projection of this fragment onto the line (both relative to the line's start point).
227+
// This results in a "linear gradient" which goes from 0.0 at the start point to 1.0 at the end point.
228+
float projMagnitude = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
229+
230+
float lineDistance = length(pa - (ba * projMagnitude));
231+
232+
// The distance to the line allows us to create lines of any thickness.
233+
// Instead of checking whether this fragment's distance < the line thickness,
234+
// utilize the distance field to get some antialiasing. Fragments far away from the line are 0,
235+
// fragments close to the line are 1, and fragments that are within a 1-pixel border of the line are in between.
236+
float cappedLine = clamp((u_lineThickness + 1.0) * 0.5 - lineDistance, 0.0, 1.0);
237+
238+
gl_FragColor = u_lineColor * cappedLine;
239+
#endif // DRAW_MODE_line
228240
}

src/shaders/sprite.vert

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,57 @@
1+
precision mediump float;
2+
3+
#ifdef DRAW_MODE_line
4+
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;
11+
#endif
12+
13+
#ifndef DRAW_MODE_line
114
uniform mat4 u_projectionMatrix;
215
uniform mat4 u_modelMatrix;
16+
attribute vec2 a_texCoord;
17+
#endif
318

419
attribute vec2 a_position;
5-
attribute vec2 a_texCoord;
620

721
varying vec2 v_texCoord;
822

9-
#ifdef DRAW_MODE_lineSample
10-
uniform float u_positionScalar;
11-
#endif
12-
1323
void main() {
14-
#ifdef DRAW_MODE_lineSample
15-
vec2 position = a_position;
16-
position.y = clamp(position.y * u_positionScalar, -0.5, 0.5);
17-
gl_Position = u_projectionMatrix * u_modelMatrix * vec4(position, 0, 1);
18-
#else
19-
gl_Position = u_projectionMatrix * u_modelMatrix * vec4(a_position, 0, 1);
20-
#endif
21-
v_texCoord = a_texCoord;
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 + epsilon) / (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
2257
}

0 commit comments

Comments
 (0)