Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(render): an outline moves the glyph's edge instead of stamping the glyph

yhha2d8dfb69936
diff --git a/sdk/src/ui/text/glyph-atlas.ts b/sdk/src/ui/text/glyph-atlas.ts
index 1ae799f23..13a05aa80 100644
--- a/sdk/src/ui/text/glyph-atlas.ts
+++ b/sdk/src/ui/text/glyph-atlas.ts
@@ -35,6 +35,9 @@ export interface RasterGlyph {
export interface GlyphRasterizer {
/** The size glyphs are rasterized at; metrics are in these units. */
readonly renderSize: number;
+ /** SDF spread in rasterized texels — the distance one half of the encoded
+ * range covers. 0 when the glyphs are plain coverage, not a distance field. */
+ readonly spread: number;
/**
* Render one glyph to an upload-ready bitmap + metrics, or null if the glyph
* cannot be produced (unknown codepoint / no font). `pixelSize` overrides the
@@ -117,6 +120,14 @@ export class GlyphAtlas {
/** The px size glyphs are rasterized at; layout scales by displaySize/renderSize. */
get renderSize(): number { return this.rasterizer.renderSize; }
+ /** Distance units per rasterized texel, or 0 when these glyphs carry coverage
+ * rather than a distance field. What turns an outline width in text px into
+ * the edge shift the shader applies. */
+ get distancePerTexel(): number {
+ const spread = this.rasterizer.spread;
+ return this.sdf && spread > 0 ? 0.5 / spread : 0;
+ }
+
/**
* On-screen device px per design px beyond DPR (the canvas design fit).
* Bitmap glyphs fold it into the rasterization size so they land 1:1 on
diff --git a/sdk/src/ui/text/glyph-rasterizer.ts b/sdk/src/ui/text/glyph-rasterizer.ts
index 37d8521c4..6c445aa87 100644
--- a/sdk/src/ui/text/glyph-rasterizer.ts
+++ b/sdk/src/ui/text/glyph-rasterizer.ts
@@ -95,6 +95,8 @@ export class CanvasGlyphRasterizer implements GlyphRasterizer {
private readonly canvas: Canvas2D;
private readonly ctx: Ctx2D | null;
+ get spread(): number { return this.sdf ? this.pad : 0; }
+
constructor(module: ESEngineModule | null, opts: CanvasGlyphRasterizerOptions = {}) {
this.module = module;
this.renderSize = opts.renderSize ?? 48;
diff --git a/sdk/src/ui/text/layout.ts b/sdk/src/ui/text/layout.ts
index 0cede459e..25ddae347 100644
--- a/sdk/src/ui/text/layout.ts
+++ b/sdk/src/ui/text/layout.ts
@@ -113,6 +113,7 @@ export function buildGlyphVertices(
color: RGBA,
originX = 0,
originY = 0,
+ sdfBias = 0,
): GlyphVertexData {
const n = glyphs.length;
const vertices = new Float32Array(n * 4 * TEXT_VERTEX_FLOATS);
@@ -125,10 +126,10 @@ export function buildGlyphVertices(
const y0 = gl.y0 + originY, y1 = gl.y1 + originY;
const vo = i * 4 * TEXT_VERTEX_FLOATS;
// BL, BR, TR, TL — UV V uses v1 at the bottom, v0 at the top.
- writeVertex(vertices, vo + 0 * TEXT_VERTEX_FLOATS, x0, y0, gl.u0, gl.v1, r, g, b, a);
- writeVertex(vertices, vo + 1 * TEXT_VERTEX_FLOATS, x1, y0, gl.u1, gl.v1, r, g, b, a);
- writeVertex(vertices, vo + 2 * TEXT_VERTEX_FLOATS, x1, y1, gl.u1, gl.v0, r, g, b, a);
- writeVertex(vertices, vo + 3 * TEXT_VERTEX_FLOATS, x0, y1, gl.u0, gl.v0, r, g, b, a);
+ writeVertex(vertices, vo + 0 * TEXT_VERTEX_FLOATS, x0, y0, gl.u0, gl.v1, r, g, b, a, sdfBias);
+ writeVertex(vertices, vo + 1 * TEXT_VERTEX_FLOATS, x1, y0, gl.u1, gl.v1, r, g, b, a, sdfBias);
+ writeVertex(vertices, vo + 2 * TEXT_VERTEX_FLOATS, x1, y1, gl.u1, gl.v0, r, g, b, a, sdfBias);
+ writeVertex(vertices, vo + 3 * TEXT_VERTEX_FLOATS, x0, y1, gl.u0, gl.v0, r, g, b, a, sdfBias);
const io = i * 6, vb = i * 4;
indices[io] = vb; indices[io + 1] = vb + 1; indices[io + 2] = vb + 2;
@@ -141,10 +142,11 @@ export function buildGlyphVertices(
function writeVertex(
out: Float32Array, o: number,
x: number, y: number, u: number, v: number,
- r: number, g: number, b: number, a: number,
+ r: number, g: number, b: number, a: number, sdfBias: number,
): void {
out[o] = x; out[o + 1] = y; out[o + 2] = u; out[o + 3] = v;
out[o + 4] = r; out[o + 5] = g; out[o + 6] = b; out[o + 7] = a;
+ out[o + 8] = sdfBias;
}
export interface RichTextLayoutOptions extends TextLayoutOptions {
diff --git a/sdk/src/ui/text/native-glyph-rasterizer.ts b/sdk/src/ui/text/native-glyph-rasterizer.ts
index e3da762ff..40c176e53 100644
--- a/sdk/src/ui/text/native-glyph-rasterizer.ts
+++ b/sdk/src/ui/text/native-glyph-rasterizer.ts
@@ -32,6 +32,8 @@ export class NativeGlyphRasterizer implements GlyphRasterizer {
private readonly pad: number;
private readonly sdf: boolean;
+ get spread(): number { return this.sdf ? this.pad : 0; }
+
constructor(opts: NativeGlyphRasterizerOptions = {}) {
this.renderSize = opts.renderSize ?? 48;
this.pad = opts.padding ?? 6;
diff --git a/sdk/src/ui/text/submit.ts b/sdk/src/ui/text/submit.ts
index 8193d8795..8b9693b1e 100644
--- a/sdk/src/ui/text/submit.ts
+++ b/sdk/src/ui/text/submit.ts
@@ -10,8 +10,8 @@
import type { ESEngineModule } from '../../wasm';
import { withScratch } from '../../wasm/wasmScratch';
-/** Floats per glyph vertex: position(2) + uv(2) + color(4). */
-export const TEXT_VERTEX_FLOATS = 8;
+/** Floats per glyph vertex: position(2) + uv(2) + color(4) + sdfBias(1). */
+export const TEXT_VERTEX_FLOATS = 9;
/**
* The same submit, without a wasm heap to marshal through: the native host takes
@@ -40,7 +40,7 @@ export function setNativeTextSubmit(submit: NativeTextBatchSubmit | null): void
/**
* Submit a batch of glyph quads. `vertices` is `TEXT_VERTEX_FLOATS` floats per
- * vertex (x,y,u,v,r,g,b,a); `indices` reference those vertices; `transform` is a
+ * vertex (x,y,u,v,r,g,b,a,sdfBias); `indices` reference those vertices; `transform` is a
* column-major mat4 applied to vertex positions (pass identity if positions are
* already world-space). No-op if the engine build lacks the binding.
*/
diff --git a/sdk/src/ui/text/text-renderer.ts b/sdk/src/ui/text/text-renderer.ts
index cd142e5ae..4a35afcba 100644
--- a/sdk/src/ui/text/text-renderer.ts
+++ b/sdk/src/ui/text/text-renderer.ts
@@ -57,19 +57,38 @@ export interface DrawTextParams {
}
// 8-direction offsets (unit) for the outline fan — scaled by the outline width.
+// The bitmap atlas's only way to widen a glyph; see outlineBias.
const OUTLINE_OFFSETS: ReadonlyArray<readonly [number, number]> = [
[-1, -1], [0, -1], [1, -1],
[-1, 0], [1, 0],
[-1, 1], [0, 1], [1, 1],
];
+/**
+ * An outline width in text px as the edge shift the SDF shader applies, or 0 when
+ * these glyphs are coverage rather than distance (no edge to shift).
+ *
+ * A glyph is rasterized at the atlas's renderSize and drawn at fontSizePx, so one
+ * atlas texel covers fontSizePx/renderSize text px; the atlas encodes `spread`
+ * texels into half its range. Both conversions are in the value, which therefore
+ * says the same thing at every font size and every zoom.
+ *
+ * Asking for more than the spread cannot dilate further — the field simply stops
+ * there — so an over-wide request degrades to the widest real outline instead of
+ * flooding the glyph's cell.
+ */
+function outlineBias(atlas: GlyphAtlas, fontSizePx: number, widthPx: number): number {
+ const perTexel = atlas.distancePerTexel;
+ if (perTexel <= 0 || fontSizePx <= 0) return 0;
+ return widthPx * (atlas.renderSize / fontSizePx) * perTexel;
+}
+
/**
* A blurred shadow is the glyphs stamped around a ring, which is the same shape
* the outline already takes — one mechanism for "spread these glyphs out", not
* two. Two rings plus the centre approximate a Gaussian closely enough at the
- * sizes UI text uses; a true one-pass soft edge wants a per-draw threshold the
- * batch path cannot carry yet (the vertex format is shared with every sprite),
- * and both effects collapse into it on the day it can.
+ * sizes UI text uses. Unlike the outline, a blur wants a SOFTER edge rather than
+ * a moved one, which the single distance the vertices carry cannot express.
*/
const SHADOW_RING: ReadonlyArray<readonly [number, number, number]> = [
// [x, y, radius fraction] — the inner ring carries most of the mass.
@@ -147,9 +166,9 @@ export function drawTextWith(atlas: GlyphAtlas, sink: GlyphBatchSink, p: DrawTex
// Emit the glyph set once per page, recolored + offset. All passes are SDF
// glyphs in the same atlas/layer, so they batch and draw in submit order —
// shadow + outline first (behind), fill last (on top).
- const emitPass = (color: RGBA, dx: number, dy: number): void => {
+ const emitPass = (color: RGBA, dx: number, dy: number, bias = 0): void => {
for (const [pageId, glyphs] of byPage) {
- const { vertices, indices } = buildGlyphVertices(glyphs, color, baseX + dx, originY + dy);
+ const { vertices, indices } = buildGlyphVertices(glyphs, color, baseX + dx, originY + dy, bias);
sink(vertices, indices, pageId);
}
};
@@ -169,10 +188,15 @@ export function drawTextWith(atlas: GlyphAtlas, sink: GlyphBatchSink, p: DrawTex
emitPass(color, dx, -dy);
}
}
- // Outline (8-direction fan around the glyph).
+ // Outline. On a distance-field atlas this is the glyph drawn once more with
+ // its edge pushed out — a real dilation, so the shape survives any width. The
+ // bitmap atlas has no distance to push, so it keeps the eight-way stamp, which
+ // is only ever asked for hairlines there.
if (p.outline && p.outline.width > 0 && p.outline.color[3] > 0) {
const w = p.outline.width;
- for (const [ox, oy] of OUTLINE_OFFSETS) emitPass(p.outline.color, ox * w, oy * w);
+ const bias = outlineBias(atlas, p.fontSizePx, w);
+ if (bias > 0) emitPass(p.outline.color, 0, 0, bias);
+ else for (const [ox, oy] of OUTLINE_OFFSETS) emitPass(p.outline.color, ox * w, oy * w);
}
// Fill (on top).
emitPass(p.color, 0, 0);
diff --git a/sdk/tests/ui-glyph-atlas.test.ts b/sdk/tests/ui-glyph-atlas.test.ts
index 1f8d3bd39..a4d59c2ec 100644
--- a/sdk/tests/ui-glyph-atlas.test.ts
+++ b/sdk/tests/ui-glyph-atlas.test.ts
@@ -26,7 +26,7 @@ function makeRasterizer() {
const w = 10, h = 12;
return { pixels: new Uint8Array(w * h * 4).fill(200), width: w, height: h, advance: 11, bearingX: 1, bearingY: 10 };
});
- return { renderSize: 48, rasterize } as GlyphRasterizer & { rasterize: typeof rasterize };
+ return { renderSize: 48, spread: 6, rasterize } as GlyphRasterizer & { rasterize: typeof rasterize };
}
function makeStore() {
diff --git a/sdk/tests/ui-text-layout.test.ts b/sdk/tests/ui-text-layout.test.ts
index b9e77e3b2..4dc247637 100644
--- a/sdk/tests/ui-text-layout.test.ts
+++ b/sdk/tests/ui-text-layout.test.ts
@@ -7,6 +7,8 @@
* verified headless.
*/
import { describe, it, expect } from 'vitest';
+
+import { TEXT_VERTEX_FLOATS } from '../src/ui/text/submit';
import {
GlyphAtlas, type GlyphRasterizer, type AtlasPageStore, type RasterGlyph,
} from '../src/ui/text/glyph-atlas';
@@ -18,6 +20,7 @@ const UNKNOWN = 0xffff;
function makeAtlas(): GlyphAtlas {
const rasterizer: GlyphRasterizer = {
renderSize: 48,
+ spread: 6,
rasterize: (cp: number): RasterGlyph | null => {
if (cp === UNKNOWN) return null;
if (cp === SPACE) return { pixels: new Uint8Array(0), width: 0, height: 0, advance: 12, bearingX: 0, bearingY: 0 };
@@ -71,7 +74,7 @@ describe('REARCH_GUI P1.3b: text layout', () => {
const atlas = makeAtlas();
const layout = layoutLine('AB', atlas, 'Arial', { fontSizePx: 24 });
const { vertices, indices } = buildGlyphVertices(layout.glyphs, [1, 0, 0, 1], 100, 200);
- expect(vertices.length).toBe(2 * 4 * 8);
+ expect(vertices.length).toBe(2 * 4 * TEXT_VERTEX_FLOATS);
expect(indices.length).toBe(2 * 6);
// first vertex = bottom-left of glyph 0, with origin applied
expect(vertices[0]).toBeCloseTo(0.5 + 100); // x
diff --git a/sdk/tests/ui-text-multiline.test.ts b/sdk/tests/ui-text-multiline.test.ts
index a7b187ddb..d74004f1b 100644
--- a/sdk/tests/ui-text-multiline.test.ts
+++ b/sdk/tests/ui-text-multiline.test.ts
@@ -13,6 +13,7 @@ import { layoutText, TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT } from '../src/ui/text/
function makeAtlas(): GlyphAtlas {
const rasterizer: GlyphRasterizer = {
renderSize: 48,
+ spread: 6,
rasterize: (): RasterGlyph => ({ pixels: new Uint8Array(10 * 12 * 4), width: 10, height: 12, advance: 11, bearingX: 1, bearingY: 10 }),
};
let next = 1;
diff --git a/sdk/tests/ui-text-renderer.test.ts b/sdk/tests/ui-text-renderer.test.ts
index edeb9668a..7bbad0b2b 100644
--- a/sdk/tests/ui-text-renderer.test.ts
+++ b/sdk/tests/ui-text-renderer.test.ts
@@ -6,6 +6,8 @@
* given the atlas, so the per-page grouping + geometry are headless-tested.
*/
import { describe, it, expect, vi } from 'vitest';
+
+import { TEXT_VERTEX_FLOATS } from '../src/ui/text/submit';
import {
GlyphAtlas, type GlyphRasterizer, type AtlasPageStore, type RasterGlyph,
} from '../src/ui/text/glyph-atlas';
@@ -15,6 +17,7 @@ import type { DrawTextParams } from '../src/ui/text/text-renderer';
function makeAtlas(pageSize: number): GlyphAtlas {
const rasterizer: GlyphRasterizer = {
renderSize: 48,
+ spread: 6,
rasterize: (cp: number): RasterGlyph | null => {
if (cp === 32) return { pixels: new Uint8Array(0), width: 0, height: 0, advance: 12, bearingX: 0, bearingY: 0 };
return { pixels: new Uint8Array(10 * 12 * 4), width: 10, height: 12, advance: 11, bearingX: 1, bearingY: 10 };
@@ -32,7 +35,7 @@ describe('REARCH_GUI P1.3b: drawTextWith', () => {
drawTextWith(atlas, sink, { text: 'AB', fontFamily: 'Arial', fontSizePx: 24, color: [1, 1, 1, 1] });
expect(sink).toHaveBeenCalledTimes(1);
const [vertices, indices, pageId] = sink.mock.calls[0];
- expect(vertices.length).toBe(2 * 4 * 8);
+ expect(vertices.length).toBe(2 * 4 * TEXT_VERTEX_FLOATS);
expect(indices.length).toBe(2 * 6);
expect(pageId).toBe(1000);
});
@@ -47,7 +50,7 @@ describe('REARCH_GUI P1.3b: drawTextWith', () => {
const pages = sink.mock.calls.map(c => c[2]);
expect(new Set(pages).size).toBe(2); // distinct pages
const totalVerts = sink.mock.calls.reduce((s, c) => s + c[0].length, 0);
- expect(totalVerts).toBe(6 * 4 * 8); // all 6 glyphs emitted
+ expect(totalVerts).toBe(6 * 4 * TEXT_VERTEX_FLOATS); // all 6 glyphs emitted
});
it('emits shadow + outline passes behind the fill (REARCH_GUI F8)', () => {
@@ -58,14 +61,52 @@ describe('REARCH_GUI P1.3b: drawTextWith', () => {
shadow: { color: [0, 0, 0, 1], dx: 2, dy: 2 },
outline: { color: [0, 0, 0, 1], width: 1 },
});
- // 1 shadow + 8 outline directions + 1 fill = 10 single-page batches.
- expect(sink).toHaveBeenCalledTimes(10);
+ // 1 shadow + 1 dilated outline + 1 fill = 3 single-page batches.
+ expect(sink).toHaveBeenCalledTimes(3);
// Shadow (first pass) is offset on x by dx; fill (last pass) is not.
const shadowX = sink.mock.calls[0][0][0];
- const fillX = sink.mock.calls[9][0][0];
+ const fillX = sink.mock.calls[2][0][0];
expect(shadowX - fillX).toBeCloseTo(2, 5);
});
+ it('outlines a distance-field glyph by moving its edge, not by stamping it', () => {
+ // One pass, sitting exactly on the fill, carrying a positive edge shift —
+ // the glyph grown by its own distance field. Eight offset copies of a wide
+ // outline stop resembling the glyph; one dilation cannot.
+ const sink = vi.fn();
+ drawTextWith(makeAtlas(1024), sink, {
+ text: 'AB', fontFamily: 'Arial', fontSizePx: 24, color: [1, 1, 1, 1],
+ outline: { color: [0, 0, 0, 1], width: 3 },
+ });
+ expect(sink).toHaveBeenCalledTimes(2); // outline + fill
+ const [outline, , ] = sink.mock.calls[0];
+ const [fill] = sink.mock.calls[1];
+ expect(outline[0]).toBeCloseTo(fill[0], 5); // not offset
+ // 3px at 24px display off a 48px/6-spread atlas: 3 × (48/24) × (0.5/6).
+ expect(outline[8]).toBeCloseTo(0.5, 6);
+ expect(fill[8]).toBe(0);
+ });
+
+ it('falls back to the stamp fan when the glyphs are coverage, not distance', () => {
+ // A bitmap atlas has no distance to move, so the only way to widen a glyph
+ // is still to draw it around itself.
+ const rasterizer: GlyphRasterizer = {
+ renderSize: 48,
+ spread: 0,
+ rasterize: (): RasterGlyph =>
+ ({ pixels: new Uint8Array(10 * 12 * 4), width: 10, height: 12, advance: 11, bearingX: 1, bearingY: 10 }),
+ };
+ let next = 2000;
+ const store: AtlasPageStore = { createPage: () => next++, uploadSubRegion: () => {} };
+ const atlas = new GlyphAtlas(rasterizer, store, { pageSize: 1024, padding: 1, sdf: false });
+ const sink = vi.fn();
+ drawTextWith(atlas, sink, {
+ text: 'AB', fontFamily: 'Arial', fontSizePx: 24, color: [1, 1, 1, 1],
+ outline: { color: [0, 0, 0, 1], width: 1 },
+ });
+ expect(sink).toHaveBeenCalledTimes(9); // 8 directions + fill
+ });
+
it('spreads a blurred shadow into a ring, still behind the fill', () => {
const atlas = makeAtlas(1024);
const sink = vi.fn();
diff --git a/sdk/tests/ui-text-richlayout.test.ts b/sdk/tests/ui-text-richlayout.test.ts
index 9ceda96e9..a60cd5a74 100644
--- a/sdk/tests/ui-text-richlayout.test.ts
+++ b/sdk/tests/ui-text-richlayout.test.ts
@@ -15,6 +15,7 @@ import { UI_TEXT_BOLD } from '../src/ui/text/text-transform';
function makeAtlas(calls: Array<{ cp: number; style: number }>): GlyphAtlas {
const rasterizer: GlyphRasterizer = {
renderSize: 48,
+ spread: 6,
rasterize: (cp: number, _f: string, style: number): RasterGlyph | null => {
calls.push({ cp, style });
return { pixels: new Uint8Array(10 * 12 * 4), width: 10, height: 12, advance: 11, bearingX: 1, bearingY: 10 };
diff --git a/sdk/tests/ui-text-wrap.test.ts b/sdk/tests/ui-text-wrap.test.ts
index 4c29245e4..9d2530d6e 100644
--- a/sdk/tests/ui-text-wrap.test.ts
+++ b/sdk/tests/ui-text-wrap.test.ts
@@ -15,6 +15,7 @@ import type { TextSegment } from '../src/ui/text/rich-text-parser';
function makeAtlas(): GlyphAtlas {
const rasterizer: GlyphRasterizer = {
renderSize: 48,
+ spread: 6,
rasterize: (cp: number): RasterGlyph =>
cp === 32
? { pixels: new Uint8Array(0), width: 0, height: 0, advance: 6, bearingX: 0, bearingY: 0 }
diff --git a/src/esengine/data/shaders/batch.esshader b/src/esengine/data/shaders/batch.esshader
index 4ca11d0aa..31e81915d 100644
--- a/src/esengine/data/shaders/batch.esshader
+++ b/src/esengine/data/shaders/batch.esshader
@@ -22,6 +22,9 @@ layout(location = 0) in vec2 a_position;
layout(location = 1) in vec4 a_color;
layout(location = 2) in vec2 a_texCoord;
layout(location = 3) in float a_texIndex;
+#ifdef SDF
+layout(location = 4) in float a_sdfBias;
+#endif
layout(std140) uniform FrameConstants {
mat4 u_projection;
@@ -30,6 +33,9 @@ layout(std140) uniform FrameConstants {
out vec4 v_color;
out vec2 v_texCoord;
flat out int v_texIndex;
+#ifdef SDF
+out float v_sdfBias;
+#endif
#ifdef LIT
out highp vec2 v_worldPos;
#endif
@@ -39,6 +45,9 @@ void main() {
v_color = a_color;
v_texCoord = a_texCoord;
v_texIndex = int(a_texIndex);
+#ifdef SDF
+ v_sdfBias = a_sdfBias;
+#endif
#ifdef LIT
v_worldPos = a_position;
#endif
@@ -53,6 +62,9 @@ precision highp float;
in vec4 v_color;
in vec2 v_texCoord;
flat in int v_texIndex;
+#ifdef SDF
+in float v_sdfBias;
+#endif
#ifdef LIT
in highp vec2 v_worldPos;
#endif
@@ -85,8 +97,11 @@ void main() {
// The glyph atlas stores a signed distance in the alpha channel (RGB = 1).
// Distance ÷ fwidth = screen px from the edge; the ±0.5px clamp is exactly
// one pixel of linear coverage ramp (the msdfgen standard).
+ // v_sdfBias moves the edge outward before the ramp, which grows the glyph's
+ // own shape by that many distance units — an outline pass draws the same
+ // quads with it raised and its colour in the vertices.
float dist = texColor.a;
- float screenPxDist = (dist - 0.5) / max(fwidth(dist), 1e-6);
+ float screenPxDist = (dist - 0.5 + v_sdfBias) / max(fwidth(dist), 1e-6);
float coverage = clamp(screenPxDist + 0.5, 0.0, 1.0);
fragColor = vec4(tint.rgb, tint.a * coverage);
#elif defined(LIT)
@@ -114,6 +129,9 @@ struct VSIn {
@location(1) a_color : vec4f,
@location(2) a_texCoord : vec2f,
@location(3) a_texIndex : f32,
+#ifdef SDF
+ @location(4) a_sdfBias : f32,
+#endif
};
struct VSOut {
@builtin(position) pos : vec4f,
@@ -123,6 +141,9 @@ struct VSOut {
#ifdef LIT
@location(3) v_worldPos : vec2f,
#endif
+#ifdef SDF
+ @location(4) v_sdfBias : f32,
+#endif
};
@vertex fn vs_main(v : VSIn) -> VSOut {
@@ -131,6 +152,9 @@ struct VSOut {
out.v_color = v.a_color;
out.v_texCoord = v.a_texCoord;
out.v_texIndex = v.a_texIndex;
+#ifdef SDF
+ out.v_sdfBias = v.a_sdfBias;
+#endif
#ifdef LIT
out.v_worldPos = v.a_position;
#endif
@@ -164,6 +188,9 @@ struct VSOut {
#ifdef LIT
@location(3) v_worldPos : vec2f,
#endif
+#ifdef SDF
+ @location(4) v_sdfBias : f32,
+#endif
};
// textureSampleLevel(…, 0) keeps sampling uniform-flow-legal inside the
@@ -188,7 +215,7 @@ struct VSOut {
// Same coverage math as the GLSL stage; fwidth here is in uniform control
// flow (main scope), which WGSL requires of derivative builtins.
let dist = texColor.a;
- let screenPxDist = (dist - 0.5) / max(fwidth(dist), 1e-6);
+ let screenPxDist = (dist - 0.5 + v.v_sdfBias) / max(fwidth(dist), 1e-6);
let coverage = clamp(screenPxDist + 0.5, 0.0, 1.0);
return vec4f(tint.rgb, tint.a * coverage);
#elif defined(LIT)
diff --git a/src/esengine/renderer/draw/BatchVertex.hpp b/src/esengine/renderer/draw/BatchVertex.hpp
index 7752fbcf7..1fa0ee18c 100644
--- a/src/esengine/renderer/draw/BatchVertex.hpp
+++ b/src/esengine/renderer/draw/BatchVertex.hpp
@@ -12,6 +12,13 @@ struct BatchVertex {
u32 color;
glm::vec2 texCoord;
f32 texIndex = 0.0f; // sampler slot within the draw's texture set; assigned at merge time
+ // How far to push the SDF text edge outward, in the atlas's own distance units
+ // (0.5 = one spread). An outline is the glyph drawn once more with this raised,
+ // which is a real dilation of the distance field — the alternative, stamping the
+ // glyph around itself, stops looking like the glyph once the ring is wide.
+ // Per-vertex rather than per-draw so labels of different styles still batch;
+ // everything that is not SDF text leaves it at 0.
+ f32 sdfBias = 0.0f;
};
inline u32 packColor(f32 r, f32 g, f32 b, f32 a) {
diff --git a/src/esengine/renderer/frame/RenderFrameSubmit.cpp b/src/esengine/renderer/frame/RenderFrameSubmit.cpp
index 52e1bacf6..64fee137a 100644
--- a/src/esengine/renderer/frame/RenderFrameSubmit.cpp
+++ b/src/esengine/renderer/frame/RenderFrameSubmit.cpp
@@ -47,7 +47,7 @@ void RenderFrame::submitTextBatch(
) {
if (vertexCount <= 0 || indexCount <= 0) return;
- constexpr i32 FLOATS_PER_VERTEX = 8; // x, y, u, v, r, g, b, a
+ constexpr i32 FLOATS_PER_VERTEX = 9; // x, y, u, v, r, g, b, a, sdfBias
glm::mat4 model = glm::make_mat4(transform16);
u32 vBytes = static_cast<u32>(vertexCount) * sizeof(BatchVertex);
@@ -58,7 +58,9 @@ void RenderFrame::submitTextBatch(
const f32* v = vertices + i * FLOATS_PER_VERTEX;
glm::vec4 worldPos = model * glm::vec4(v[0], v[1], 0.0f, 1.0f);
u32 pc = packColor(glm::vec4(v[4], v[5], v[6], v[7]));
- dst[i] = { {worldPos.x, worldPos.y}, pc, {v[2], v[3]} };
+ // texIndex is assigned at merge time; sdfBias rides in from the layout,
+ // which is where an outline pass says how far to grow the glyph.
+ dst[i] = { {worldPos.x, worldPos.y}, pc, {v[2], v[3]}, 0.0f, v[8] };
}
// sdf: the SDF variant derives coverage from the distance field; otherwise the
diff --git a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
index 66a186402..4efe8c944 100644
--- a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
+++ b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
@@ -26,6 +26,9 @@ layout(location = 0) in vec2 a_position;
layout(location = 1) in vec4 a_color;
layout(location = 2) in vec2 a_texCoord;
layout(location = 3) in float a_texIndex;
+#ifdef SDF
+layout(location = 4) in float a_sdfBias;
+#endif
layout(std140) uniform FrameConstants {
mat4 u_projection;
@@ -34,6 +37,9 @@ layout(std140) uniform FrameConstants {
out vec4 v_color;
out vec2 v_texCoord;
flat out int v_texIndex;
+#ifdef SDF
+out float v_sdfBias;
+#endif
#ifdef LIT
out highp vec2 v_worldPos;
#endif
@@ -43,6 +49,9 @@ void main() {
v_color = a_color;
v_texCoord = a_texCoord;
v_texIndex = int(a_texIndex);
+#ifdef SDF
+ v_sdfBias = a_sdfBias;
+#endif
#ifdef LIT
v_worldPos = a_position;
#endif
@@ -57,6 +66,9 @@ precision highp float;
in vec4 v_color;
in vec2 v_texCoord;
flat in int v_texIndex;
+#ifdef SDF
+in float v_sdfBias;
+#endif
#ifdef LIT
in highp vec2 v_worldPos;
#endif
@@ -89,8 +101,11 @@ void main() {
// The glyph atlas stores a signed distance in the alpha channel (RGB = 1).
// Distance ÷ fwidth = screen px from the edge; the ±0.5px clamp is exactly
// one pixel of linear coverage ramp (the msdfgen standard).
+ // v_sdfBias moves the edge outward before the ramp, which grows the glyph's
+ // own shape by that many distance units — an outline pass draws the same
+ // quads with it raised and its colour in the vertices.
float dist = texColor.a;
- float screenPxDist = (dist - 0.5) / max(fwidth(dist), 1e-6);
+ float screenPxDist = (dist - 0.5 + v_sdfBias) / max(fwidth(dist), 1e-6);
float coverage = clamp(screenPxDist + 0.5, 0.0, 1.0);
fragColor = vec4(tint.rgb, tint.a * coverage);
#elif defined(LIT)
@@ -118,6 +133,9 @@ struct VSIn {
@location(1) a_color : vec4f,
@location(2) a_texCoord : vec2f,
@location(3) a_texIndex : f32,
+#ifdef SDF
+ @location(4) a_sdfBias : f32,
+#endif
};
struct VSOut {
@builtin(position) pos : vec4f,
@@ -127,6 +145,9 @@ struct VSOut {
#ifdef LIT
@location(3) v_worldPos : vec2f,
#endif
+#ifdef SDF
+ @location(4) v_sdfBias : f32,
+#endif
};
@vertex fn vs_main(v : VSIn) -> VSOut {
@@ -135,6 +156,9 @@ struct VSOut {
out.v_color = v.a_color;
out.v_texCoord = v.a_texCoord;
out.v_texIndex = v.a_texIndex;
+#ifdef SDF
+ out.v_sdfBias = v.a_sdfBias;
+#endif
#ifdef LIT
out.v_worldPos = v.a_position;
#endif
@@ -168,6 +192,9 @@ struct VSOut {
#ifdef LIT
@location(3) v_worldPos : vec2f,
#endif
+#ifdef SDF
+ @location(4) v_sdfBias : f32,
+#endif
};
// textureSampleLevel(…, 0) keeps sampling uniform-flow-legal inside the
@@ -192,7 +219,7 @@ struct VSOut {
// Same coverage math as the GLSL stage; fwidth here is in uniform control
// flow (main scope), which WGSL requires of derivative builtins.
let dist = texColor.a;
- let screenPxDist = (dist - 0.5) / max(fwidth(dist), 1e-6);
+ let screenPxDist = (dist - 0.5 + v.v_sdfBias) / max(fwidth(dist), 1e-6);
let coverage = clamp(screenPxDist + 0.5, 0.0, 1.0);
return vec4f(tint.rgb, tint.a * coverage);
#elif defined(LIT)
diff --git a/src/esengine/renderer/rhi/TransientBufferPool.cpp b/src/esengine/renderer/rhi/TransientBufferPool.cpp
index d6d466fcc..9c1a4b640 100644
--- a/src/esengine/renderer/rhi/TransientBufferPool.cpp
+++ b/src/esengine/renderer/rhi/TransientBufferPool.cpp
@@ -230,13 +230,14 @@ void TransientBufferPool::setupStream(LayoutId layout) {
VertexLayoutDesc desc;
switch (layout) {
case LayoutId::Batch:
- // BatchVertex: pos(8) + color(4) + uv(8) + texIndex(4)
- desc.attributeCount = 4;
- desc.strides[0] = 24;
+ // BatchVertex: pos(8) + color(4) + uv(8) + texIndex(4) + sdfBias(4)
+ desc.attributeCount = 5;
+ desc.strides[0] = 28;
desc.attributes[0] = {0, 2, GfxDataType::Float, false, 0, 0};
desc.attributes[1] = {1, 4, GfxDataType::UnsignedByte, true, 8, 0};
desc.attributes[2] = {2, 2, GfxDataType::Float, false, 12, 0};
desc.attributes[3] = {3, 1, GfxDataType::Float, false, 20, 0};
+ desc.attributes[4] = {4, 1, GfxDataType::Float, false, 24, 0};
break;
case LayoutId::Shape:
desc.attributeCount = 4;