@@ -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.
@@ -129,7 +124,7 @@ class PenSkin extends Skin {
129124 this . _stampShader = this . _renderer . _shaderManager . getShader ( ShaderManager . DRAW_MODE . default , NO_EFFECTS ) ;
130125
131126 /** @type {twgl.ProgramInfo } */
132- this . _lineShader = this . _renderer . _shaderManager . getShader ( ShaderManager . DRAW_MODE . lineSample , NO_EFFECTS ) ;
127+ this . _lineShader = this . _renderer . _shaderManager . getShader ( ShaderManager . DRAW_MODE . line , NO_EFFECTS ) ;
133128
134129 this . _createLineGeometry ( ) ;
135130
@@ -215,10 +210,15 @@ class PenSkin extends Skin {
215210 * @param {number } y1 - the Y coordinate of the end of the line.
216211 */
217212 drawLine ( penAttributes , x0 , y0 , x1 , y1 ) {
213+ // For compatibility with Scratch 2.0, offset pen lines of width 1 and 3 so they're pixel-aligned.
214+ // See https://github.com/LLK/scratch-render/pull/314
215+ const diameter = penAttributes . diameter || DefaultPenAttributes . diameter ;
216+ const offset = ( diameter === 1 || diameter === 3 ) ? 0.5 : 0 ;
217+
218218 this . _drawLineOnBuffer (
219219 penAttributes ,
220- this . _rotationCenter [ 0 ] + x0 , this . _rotationCenter [ 1 ] - y0 ,
221- this . _rotationCenter [ 0 ] + x1 , this . _rotationCenter [ 1 ] - y1
220+ x0 + offset , y0 + offset ,
221+ x1 + offset , y1 + offset
222222 ) ;
223223
224224 this . _silhouetteDirty = true ;
@@ -228,72 +228,16 @@ class PenSkin extends Skin {
228228 * Create 2D geometry for drawing lines to a framebuffer.
229229 */
230230 _createLineGeometry ( ) {
231- // Create a set of triangulated quads that break up a line into 3 parts:
232- // 2 caps and a body. The y component of these position vertices are
233- // divided to bring a value of 1 down to 0.5 to 0. The large y values
234- // are set so they will still be at least 0.5 after division. The
235- // divisor is scaled based on the length of the line and the lines
236- // width.
237- //
238- // Texture coordinates are based on a "generated" texture whose general
239- // shape is a circle. The line caps set their texture values to define
240- // there roundedness with the texture. The body has all of its texture
241- // values set to the center of the texture so it's a solid block.
242231 const quads = {
243232 a_position : {
244233 numComponents : 2 ,
245234 data : [
246- - 0.5 , 1 ,
247- 0.5 , 1 ,
248- - 0.5 , 100000 ,
249-
250- - 0.5 , 100000 ,
251- 0.5 , 1 ,
252- 0.5 , 100000 ,
253-
254- - 0.5 , 1 ,
255- 0.5 , 1 ,
256- - 0.5 , - 1 ,
257-
258- - 0.5 , - 1 ,
259- 0.5 , 1 ,
260- 0.5 , - 1 ,
261-
262- - 0.5 , - 100000 ,
263- 0.5 , - 100000 ,
264- - 0.5 , - 1 ,
265-
266- - 0.5 , - 1 ,
267- 0.5 , - 100000 ,
268- 0.5 , - 1
269- ]
270- } ,
271- a_texCoord : {
272- numComponents : 2 ,
273- data : [
274- 1 , 0.5 ,
275- 0 , 0.5 ,
276- 1 , 0 ,
277-
278235 1 , 0 ,
279- 0 , 0.5 ,
280236 0 , 0 ,
281-
282- 0.5 , 0 ,
283- 0.5 , 1 ,
284- 0.5 , 0 ,
285-
286- 0.5 , 0 ,
287- 0.5 , 1 ,
288- 0.5 , 1 ,
289-
290- 1 , 0 ,
291- 0 , 0 ,
292- 1 , 0.5 ,
293-
294- 1 , 0.5 ,
237+ 1 , 1 ,
238+ 1 , 1 ,
295239 0 , 0 ,
296- 0 , 0.5
240+ 0 , 1
297241 ]
298242 }
299243 } ;
@@ -338,6 +282,8 @@ class PenSkin extends Skin {
338282
339283 /**
340284 * Draw a line on the framebuffer.
285+ * Note that the point coordinates are in the following coordinate space:
286+ * +y is down, (0, 0) is the center, and the coords range from (-width / 2, -height / 2) to (height / 2, width / 2).
341287 * @param {PenAttributes } penAttributes - how the line should be drawn.
342288 * @param {number } x0 - the X coordinate of the beginning of the line.
343289 * @param {number } y0 - the Y coordinate of the beginning of the line.
@@ -351,26 +297,6 @@ class PenSkin extends Skin {
351297
352298 this . _renderer . enterDrawRegion ( this . _lineOnBufferDrawRegionId ) ;
353299
354- const diameter = penAttributes . diameter || DefaultPenAttributes . diameter ;
355- const length = Math . hypot ( Math . abs ( x1 - x0 ) - 0.001 , Math . abs ( y1 - y0 ) - 0.001 ) ;
356- const avgX = ( x0 + x1 ) / 2 ;
357- const avgY = ( y0 + y1 ) / 2 ;
358- const theta = Math . atan2 ( y0 - y1 , x0 - x1 ) ;
359- const alias = 1 ;
360-
361- // The line needs a bit of aliasing to look smooth. Add a small offset
362- // and a small size boost to scaling to give a section to alias.
363- const translationVector = __modelTranslationVector ;
364- translationVector [ 0 ] = avgX - ( alias / 2 ) ;
365- translationVector [ 1 ] = avgY + ( alias / 4 ) ;
366-
367- const scalingVector = __modelScalingVector ;
368- scalingVector [ 0 ] = diameter + alias ;
369- scalingVector [ 1 ] = length + diameter - ( alias / 2 ) ;
370-
371- const radius = diameter / 2 ;
372- const yScalar = ( 0.50001 - ( radius / ( length + diameter ) ) ) ;
373-
374300 // Premultiply pen color by pen transparency
375301 const penColor = penAttributes . color4f || DefaultPenAttributes . color4f ;
376302 __premultipliedColor [ 0 ] = penColor [ 0 ] * penColor [ 3 ] ;
@@ -379,19 +305,10 @@ class PenSkin extends Skin {
379305 __premultipliedColor [ 3 ] = penColor [ 3 ] ;
380306
381307 const uniforms = {
382- u_positionScalar : yScalar ,
383- u_capScale : diameter ,
384- u_aliasAmount : alias ,
385- u_modelMatrix : twgl . m4 . multiply (
386- twgl . m4 . multiply (
387- twgl . m4 . translation ( translationVector , __modelTranslationMatrix ) ,
388- twgl . m4 . rotationZ ( theta - ( Math . PI / 2 ) , __modelRotationMatrix ) ,
389- __modelMatrix
390- ) ,
391- twgl . m4 . scaling ( scalingVector , __modelScalingMatrix ) ,
392- __modelMatrix
393- ) ,
394- u_lineColor : __premultipliedColor
308+ u_lineColor : __premultipliedColor ,
309+ u_lineThickness : penAttributes . diameter || DefaultPenAttributes . diameter ,
310+ u_penPoints : [ x0 , - y0 , x1 , - y1 ] ,
311+ u_stageSize : this . size
395312 } ;
396313
397314 twgl . setUniforms ( currentShader , uniforms ) ;
0 commit comments