@@ -44,11 +44,6 @@ const __projectionMatrix = twgl.m4.identity();
4444 */
4545const __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 ) ;
0 commit comments