@@ -44,11 +44,6 @@ const __projectionMatrix = twgl.m4.identity();
44
44
*/
45
45
const __modelTranslationMatrix = twgl . m4 . identity ( ) ;
46
46
47
- /**
48
- * Reused memory location for rotation matrix for building a model matrix.
49
- * @type {FloatArray }
50
- */
51
- const __modelRotationMatrix = twgl . m4 . identity ( ) ;
52
47
53
48
/**
54
49
* Reused memory location for scaling matrix for building a model matrix.
@@ -135,7 +130,7 @@ class PenSkin extends Skin {
135
130
this . _stampShader = this . _renderer . _shaderManager . getShader ( ShaderManager . DRAW_MODE . default , NO_EFFECTS ) ;
136
131
137
132
/** @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 ) ;
139
134
140
135
this . _createLineGeometry ( ) ;
141
136
@@ -221,10 +216,15 @@ class PenSkin extends Skin {
221
216
* @param {number } y1 - the Y coordinate of the end of the line.
222
217
*/
223
218
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
+
224
224
this . _drawLineOnBuffer (
225
225
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
228
228
) ;
229
229
230
230
this . _silhouetteDirty = true ;
@@ -234,72 +234,16 @@ class PenSkin extends Skin {
234
234
* Create 2D geometry for drawing lines to a framebuffer.
235
235
*/
236
236
_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.
248
237
const quads = {
249
238
a_position : {
250
239
numComponents : 2 ,
251
240
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
-
284
241
1 , 0 ,
285
- 0 , 0.5 ,
286
242
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 ,
301
245
0 , 0 ,
302
- 0 , 0.5
246
+ 0 , 1
303
247
]
304
248
}
305
249
} ;
@@ -344,6 +288,8 @@ class PenSkin extends Skin {
344
288
345
289
/**
346
290
* 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).
347
293
* @param {PenAttributes } penAttributes - how the line should be drawn.
348
294
* @param {number } x0 - the X coordinate of the beginning of the line.
349
295
* @param {number } y0 - the Y coordinate of the beginning of the line.
@@ -357,26 +303,6 @@ class PenSkin extends Skin {
357
303
358
304
this . _renderer . enterDrawRegion ( this . _lineOnBufferDrawRegionId ) ;
359
305
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
-
380
306
// Premultiply pen color by pen transparency
381
307
const penColor = penAttributes . color4f || DefaultPenAttributes . color4f ;
382
308
__premultipliedColor [ 0 ] = penColor [ 0 ] * penColor [ 3 ] ;
@@ -385,19 +311,10 @@ class PenSkin extends Skin {
385
311
__premultipliedColor [ 3 ] = penColor [ 3 ] ;
386
312
387
313
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
401
318
} ;
402
319
403
320
twgl . setUniforms ( currentShader , uniforms ) ;
0 commit comments