Skip to content

Commit d7e6888

Browse files
feat: add glDrawUntextured function for optimized untextured rectangle drawing
1 parent f85b035 commit d7e6888

3 files changed

Lines changed: 67 additions & 6 deletions

File tree

src/engineDraw.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,12 @@ function drawTile(pos, size=vec2(1), tileInfo, color=WHITE,
318318
}
319319
else
320320
{
321-
// if no tile info, force untextured by zeroing rgba (so whatever
322-
// texture is bound doesn't leak in) and folding color+additive
323-
// into the additive slot — matches the Canvas2D path's
324-
// color.add(additiveColor) on line ~337.
321+
// untextured: glDrawUntextured picks the optimal path (poly
322+
// tristrip if already in poly mode, otherwise instanced with
323+
// uvs/rgba zeroed). Color+additive are folded together to match
324+
// the Canvas2D path's color.add(additiveColor) on line ~337.
325325
const combined = additiveColor ? color.add(additiveColor) : color;
326-
glDraw(pos.x, pos.y, size.x, size.y, angle, 0, 0, 0, 0,
327-
0, combined.rgbaInt());
326+
glDrawUntextured(pos.x, pos.y, size.x, size.y, angle, combined.rgbaInt());
328327
}
329328
}
330329
else

src/engineExport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ export
286286
glCopyToContext,
287287
glSetAntialias,
288288
glDraw,
289+
glDrawUntextured,
289290
glDrawPointsTransform,
290291
glDrawOutlineTransform,
291292
glDrawPoints,

src/engineWebGL.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,67 @@ function glDraw(x, y, sizeX, sizeY, angle=0, uv0X=0, uv0Y=0, uv1X=1, uv1Y=1, rgb
567567
glPositionData[offset++] = angle;
568568
}
569569

570+
/** Add an untextured rect to the gl draw list
571+
* Picks the optimal path: if already in poly mode, emits a tristrip rect
572+
* so it batches with surrounding polys; otherwise uses the instanced path
573+
* with uvs and rgba zeroed so the color falls through the additive slot.
574+
* @param {number} x
575+
* @param {number} y
576+
* @param {number} sizeX
577+
* @param {number} sizeY
578+
* @param {number} angle
579+
* @param {number} rgba - color as 32-bit integer
580+
* @memberof WebGL */
581+
function glDrawUntextured(x, y, sizeX, sizeY, angle, rgba)
582+
{
583+
if (glPolyMode)
584+
{
585+
// batch with surrounding polys as a 4-vertex tristrip rect
586+
const vertCount = 6; // 4 corners + 2 degenerate verts
587+
if (glBatchCount+vertCount >= gl_MAX_POLY_VERTEXES || glBatchAdditive !== glAdditive)
588+
glFlush();
589+
590+
// compute rotated corners in world space (matches glDrawPointsTransform rotation)
591+
const hx = sizeX*.5, hy = sizeY*.5;
592+
const c = cos(angle), s = sin(angle);
593+
const chx = c*hx, shx = s*hx, chy = c*hy, shy = s*hy;
594+
const x0 = x - chx - shy, y0 = y + shx - chy; // (-hx,-hy)
595+
const x1 = x + chx - shy, y1 = y - shx - chy; // ( hx,-hy)
596+
const x2 = x - chx + shy, y2 = y + shx + chy; // (-hx, hy)
597+
const x3 = x + chx + shy, y3 = y - shx + chy; // ( hx, hy)
598+
599+
// write tristrip with leading/trailing degenerate verts
600+
let offset = glBatchCount * gl_INDICES_PER_POLY_VERTEX;
601+
glPositionData[offset++] = x0; glPositionData[offset++] = y0; glColorData[offset++] = rgba;
602+
glPositionData[offset++] = x0; glPositionData[offset++] = y0; glColorData[offset++] = rgba;
603+
glPositionData[offset++] = x1; glPositionData[offset++] = y1; glColorData[offset++] = rgba;
604+
glPositionData[offset++] = x2; glPositionData[offset++] = y2; glColorData[offset++] = rgba;
605+
glPositionData[offset++] = x3; glPositionData[offset++] = y3; glColorData[offset++] = rgba;
606+
glPositionData[offset++] = x3; glPositionData[offset++] = y3; glColorData[offset++] = rgba;
607+
glBatchCount += vertCount;
608+
return;
609+
}
610+
611+
// instanced path: zero uvs and rgba so the texture contribution is killed,
612+
// then carry the real color in the additive slot
613+
if (glBatchCount >= gl_MAX_INSTANCES || glBatchAdditive !== glAdditive)
614+
glFlush();
615+
glSetInstancedMode();
616+
617+
let offset = glBatchCount++ * gl_INDICES_PER_INSTANCE;
618+
glPositionData[offset++] = x;
619+
glPositionData[offset++] = y;
620+
glPositionData[offset++] = sizeX;
621+
glPositionData[offset++] = sizeY;
622+
glPositionData[offset++] = 0;
623+
glPositionData[offset++] = 0;
624+
glPositionData[offset++] = 0;
625+
glPositionData[offset++] = 0;
626+
glColorData[offset++] = 0;
627+
glColorData[offset++] = rgba;
628+
glPositionData[offset++] = angle;
629+
}
630+
570631
/** Transform and add a polygon to the gl draw list
571632
* @param {Array<Vector2>} points - Array of Vector2 points
572633
* @param {number} rgba - Color of the polygon as a 32-bit integer

0 commit comments

Comments
 (0)