@@ -217,8 +217,8 @@ class PenSkin extends Skin {
217
217
218
218
this . _drawLineOnBuffer (
219
219
penAttributes ,
220
- this . _rotationCenter [ 0 ] + x0 + offset , this . _rotationCenter [ 1 ] - y0 + offset ,
221
- this . _rotationCenter [ 0 ] + x1 + offset , this . _rotationCenter [ 1 ] - y1 + offset
220
+ x0 + offset , - y0 + offset ,
221
+ x1 + offset , - y1 + offset
222
222
) ;
223
223
224
224
this . _silhouetteDirty = true ;
@@ -230,17 +230,6 @@ class PenSkin extends Skin {
230
230
_createLineGeometry ( ) {
231
231
const quads = {
232
232
a_position : {
233
- numComponents : 2 ,
234
- data : [
235
- - 1 , - 1 ,
236
- 1 , - 1 ,
237
- - 1 , 1 ,
238
- - 1 , 1 ,
239
- 1 , - 1 ,
240
- 1 , 1
241
- ]
242
- } ,
243
- a_texCoord : {
244
233
numComponents : 2 ,
245
234
data : [
246
235
1 , 0 ,
@@ -293,6 +282,8 @@ class PenSkin extends Skin {
293
282
294
283
/**
295
284
* 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).
296
287
* @param {PenAttributes } penAttributes - how the line should be drawn.
297
288
* @param {number } x0 - the X coordinate of the beginning of the line.
298
289
* @param {number } y0 - the Y coordinate of the beginning of the line.
@@ -306,31 +297,26 @@ class PenSkin extends Skin {
306
297
307
298
this . _renderer . enterDrawRegion ( this . _lineOnBufferDrawRegionId ) ;
308
299
309
- const radius = penAttributes . diameter / 2 ;
300
+ const radius = ( penAttributes . diameter || DefaultPenAttributes . diameter ) / 2 ;
301
+ // Expand line bounds by sqrt(2) / 2 each side-- this ensures that all antialiased pixels
302
+ // fall within the quad, even at a 45-degree diagonal
303
+ const expandedRadius = radius + 1.4142135623730951 ;
310
304
311
- // Clip drawn polygon to line's AABB.
312
- const transformMatrix = __modelMatrix ;
305
+ const lineLength = Math . hypot ( x1 - x0 , y1 - y0 ) ;
306
+ const lineAngle = Math . atan2 ( y1 - y0 , x1 - x0 ) ;
313
307
314
- const left = Math . floor ( Math . min ( x0 , x1 ) - radius ) - 1 ;
315
- const right = Math . ceil ( Math . max ( x0 , x1 ) + radius ) + 1 ;
316
- const top = Math . floor ( Math . min ( y0 , y1 ) - radius ) - 1 ;
317
- const bottom = Math . floor ( Math . max ( y0 , y1 ) + radius ) + 1 ;
308
+ const halfWidth = this . _bounds . width * 0.5 ;
309
+ const halfHeight = this . _bounds . height * 0.5 ;
318
310
319
- const width = this . _bounds . width ;
320
- const height = this . _bounds . height ;
321
-
322
- const translationVector = __modelTranslationVector ;
323
- translationVector [ 0 ] = ( left / ( width * 0.5 ) ) - 1 ;
324
- translationVector [ 1 ] = ( top / ( height * 0.5 ) ) - 1 ;
325
-
326
- const scalingVector = __modelScalingVector ;
327
- scalingVector [ 0 ] = ( right - left ) / width ;
328
- scalingVector [ 1 ] = ( bottom - top ) / height ;
311
+ const transformMatrix = __modelMatrix ;
312
+ twgl . m4 . identity ( transformMatrix ) ;
313
+ // Apply view transform to matrix
314
+ twgl . m4 . scale ( transformMatrix , [ 1 / halfWidth , 1 / halfHeight , 1 ] , transformMatrix ) ;
329
315
330
- transformMatrix [ 0 ] = scalingVector [ 0 ] ;
331
- transformMatrix [ 5 ] = scalingVector [ 1 ] ;
332
- transformMatrix [ 12 ] = translationVector [ 0 ] + scalingVector [ 0 ] ;
333
- transformMatrix [ 13 ] = translationVector [ 1 ] + scalingVector [ 1 ] ;
316
+ twgl . m4 . translate ( transformMatrix , [ x0 , y0 , 0 ] , transformMatrix ) ;
317
+ twgl . m4 . rotateZ ( transformMatrix , lineAngle , transformMatrix ) ;
318
+ twgl . m4 . translate ( transformMatrix , [ - expandedRadius , - expandedRadius , 0 ] , transformMatrix ) ;
319
+ twgl . m4 . scale ( transformMatrix , [ lineLength + ( expandedRadius * 2 ) , ( expandedRadius * 2 ) , 1 ] , transformMatrix ) ;
334
320
335
321
// Premultiply pen color by pen transparency
336
322
const penColor = penAttributes . color4f || DefaultPenAttributes . color4f ;
@@ -343,8 +329,7 @@ class PenSkin extends Skin {
343
329
u_modelMatrix : transformMatrix ,
344
330
u_lineColor : __premultipliedColor ,
345
331
u_lineThickness : penAttributes . diameter ,
346
- u_p1 : [ x0 , y0 ] ,
347
- u_p2 : [ x1 , y1 ] ,
332
+ u_penPoints : [ x0 + halfWidth , y0 + halfHeight , x1 + halfWidth , y1 + halfHeight ] ,
348
333
u_stageSize : this . size
349
334
} ;
350
335
0 commit comments