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(ui): render SDF text outline + shadow from the Text stroke/shadow fields (REARCH_GUI F8)

esengine5be300f57d34
diff --git a/desktop/public/scenes/ui-text-effects.esscene b/desktop/public/scenes/ui-text-effects.esscene
new file mode 100644
index 000000000..5639ad104
--- /dev/null
+++ b/desktop/public/scenes/ui-text-effects.esscene
@@ -0,0 +1,77 @@
+{
+ "version": "1.0",
+ "name": "SDF text outline + shadow (REARCH_GUI F8)",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 10 } } },
+ { "type": "Camera", "data": { "projectionType": 1, "orthoSize": 300, "isActive": true, "priority": 0 } }
+ ],
+ "visible": true
+ },
+ {
+ "id": 1,
+ "name": "Canvas",
+ "parent": null,
+ "children": [2, 3],
+ "components": [
+ { "type": "Transform", "data": {} },
+ { "type": "Canvas", "data": { "designResolution": { "x": 800, "y": 600 }, "scaleMode": 2 } },
+ { "type": "UINode", "data": {} }
+ ],
+ "visible": true
+ },
+ {
+ "id": 2,
+ "name": "Backdrop",
+ "parent": 1,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": {} },
+ { "type": "UINode", "data": { "position": 1, "insetLeft": { "value": 0, "unit": 0 }, "insetTop": { "value": 0, "unit": 0 }, "insetRight": { "value": 0, "unit": 0 }, "insetBottom": { "value": 0, "unit": 0 } } },
+ { "type": "UIVisual", "data": { "visualType": 1, "color": { "r": 0.32, "g": 0.34, "b": 0.40, "a": 1 }, "enabled": true } }
+ ],
+ "visible": true
+ },
+ {
+ "id": 3,
+ "name": "EffectText",
+ "parent": 1,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": {} },
+ {
+ "type": "UINode",
+ "data": {
+ "position": 1,
+ "insetLeft": { "value": 100, "unit": 0 },
+ "insetTop": { "value": 240, "unit": 0 },
+ "width": { "value": 600, "unit": 0 },
+ "height": { "value": 120, "unit": 0 }
+ }
+ },
+ {
+ "type": "Text",
+ "data": {
+ "content": "Outline + Shadow 描边阴影",
+ "fontFamily": "sans-serif",
+ "fontSize": 64,
+ "color": { "r": 1, "g": 0.9, "b": 0.2, "a": 1 },
+ "align": 1,
+ "verticalAlign": 1,
+ "strokeColor": { "r": 0, "g": 0, "b": 0, "a": 1 },
+ "strokeWidth": 3,
+ "shadowColor": { "r": 0, "g": 0, "b": 0, "a": 0.6 },
+ "shadowOffsetX": 7,
+ "shadowOffsetY": 7
+ }
+ }
+ ],
+ "visible": true
+ }
+ ]
+}
diff --git a/sdk/src/ui/text/plugin.ts b/sdk/src/ui/text/plugin.ts
index dd9ca9696..86464826e 100644
--- a/sdk/src/ui/text/plugin.ts
+++ b/sdk/src/ui/text/plugin.ts
@@ -15,6 +15,7 @@ import { defineSystem, Schedule } from '../../system';
import type { ESEngineModule, CppRegistry } from '../../wasm';
import type { Entity } from '../../types';
import { SdfTextRenderer } from './text-renderer';
+import type { RGBA } from './layout';
import { composeTRS, rectTextBox, UI_TEXT_BOLD, UI_TEXT_ITALIC } from './text-transform';
import { Text, type TextData } from '../core/text';
import { UINode } from '../core/ui-node';
@@ -94,6 +95,20 @@ export class TextPlugin implements Plugin {
layer = order >= 0 ? UI_BASE_LAYER + order : UI_BASE_LAYER;
}
+ const shadow = t.shadowColor.a > 0 && (t.shadowOffsetX !== 0 || t.shadowOffsetY !== 0)
+ ? {
+ color: [t.shadowColor.r, t.shadowColor.g, t.shadowColor.b, t.shadowColor.a] as RGBA,
+ dx: t.shadowOffsetX,
+ dy: t.shadowOffsetY,
+ }
+ : undefined;
+ const outline = t.strokeWidth > 0 && t.strokeColor.a > 0
+ ? {
+ color: [t.strokeColor.r, t.strokeColor.g, t.strokeColor.b, t.strokeColor.a] as RGBA,
+ width: t.strokeWidth,
+ }
+ : undefined;
+
this.renderer_.drawText(
{
text: t.content,
@@ -109,6 +124,8 @@ export class TextPlugin implements Plugin {
boxHeight,
originX,
originY,
+ shadow,
+ outline,
},
this.matrix_,
entity as number,
diff --git a/sdk/src/ui/text/text-renderer.ts b/sdk/src/ui/text/text-renderer.ts
index 6546bb9fb..fa6093fc8 100644
--- a/sdk/src/ui/text/text-renderer.ts
+++ b/sdk/src/ui/text/text-renderer.ts
@@ -42,8 +42,19 @@ export interface DrawTextParams {
verticalAlign?: number;
/** Box height (px) for vertical alignment; omit for top-anchored. */
boxHeight?: number;
+ /** Drop shadow: an offset, recolored copy of the glyphs drawn behind the fill. */
+ shadow?: { color: RGBA; dx: number; dy: number };
+ /** Outline: recolored glyph copies fanned out by `width` px around the fill. */
+ outline?: { color: RGBA; width: number };
}
+// 8-direction offsets (unit) for the outline fan — scaled by the outline width.
+const OUTLINE_OFFSETS: ReadonlyArray<readonly [number, number]> = [
+ [-1, -1], [0, -1], [1, -1],
+ [-1, 0], [1, 0],
+ [-1, 1], [0, 1], [1, 1],
+];
+
/**
* Lay out `text` against `atlas` and emit one quad batch per atlas page to
* `sink`. Pure given the atlas — no engine/Canvas dependency — so the grouping
@@ -77,10 +88,30 @@ export function drawTextWith(atlas: GlyphAtlas, sink: GlyphBatchSink, p: DrawTex
if (!arr) { arr = []; byPage.set(g.pageId, arr); }
arr.push(g);
}
- for (const [pageId, glyphs] of byPage) {
- const { vertices, indices } = buildGlyphVertices(glyphs, p.color, p.originX ?? 0, originY);
- sink(vertices, indices, pageId);
+
+ const baseX = p.originX ?? 0;
+ // 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 => {
+ for (const [pageId, glyphs] of byPage) {
+ const { vertices, indices } = buildGlyphVertices(glyphs, color, baseX + dx, originY + dy);
+ sink(vertices, indices, pageId);
+ }
+ };
+
+ // Shadow (offset drop copy). y-up local space: a positive screen-down offset
+ // moves the copy toward -y.
+ if (p.shadow && p.shadow.color[3] > 0) {
+ emitPass(p.shadow.color, p.shadow.dx, -p.shadow.dy);
+ }
+ // Outline (8-direction fan around the glyph).
+ 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);
}
+ // Fill (on top).
+ emitPass(p.color, 0, 0);
}
export interface TextRendererOptions extends CanvasGlyphRasterizerOptions {
diff --git a/sdk/tests/ui-text-renderer.test.ts b/sdk/tests/ui-text-renderer.test.ts
index e0505d45c..e747eaec5 100644
--- a/sdk/tests/ui-text-renderer.test.ts
+++ b/sdk/tests/ui-text-renderer.test.ts
@@ -49,6 +49,33 @@ describe('REARCH_GUI P1.3b: drawTextWith', () => {
expect(totalVerts).toBe(6 * 4 * 8); // all 6 glyphs emitted
});
+ it('emits shadow + outline passes behind the fill (REARCH_GUI F8)', () => {
+ const atlas = makeAtlas(1024);
+ const sink = vi.fn();
+ drawTextWith(atlas, sink, {
+ text: 'AB', fontFamily: 'Arial', fontSizePx: 24, color: [1, 1, 1, 1],
+ 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);
+ // 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];
+ expect(shadowX - fillX).toBeCloseTo(2, 5);
+ });
+
+ it('skips shadow/outline passes when transparent or zero-width', () => {
+ const atlas = makeAtlas(1024);
+ const sink = vi.fn();
+ drawTextWith(atlas, sink, {
+ text: 'AB', fontFamily: 'Arial', fontSizePx: 24, color: [1, 1, 1, 1],
+ shadow: { color: [0, 0, 0, 0], dx: 2, dy: 2 }, // transparent → skip
+ outline: { color: [0, 0, 0, 1], width: 0 }, // zero width → skip
+ });
+ expect(sink).toHaveBeenCalledTimes(1); // fill only
+ });
+
it('emits nothing for empty / whitespace-only text', () => {
const atlas = makeAtlas(1024);
const sink = vi.fn();