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