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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(text): a label's shadow and outline fade with its own colour alpha
yhh4baaebc4362b
diff --git a/docs/astro/src/content/docs/ui/text.mdx b/docs/astro/src/content/docs/ui/text.mdxindex 4373fd11f..6a6ab2a0e 100644--- a/docs/astro/src/content/docs/ui/text.mdx+++ b/docs/astro/src/content/docs/ui/text.mdx@@ -89,7 +89,7 @@ Defaults below are the component defaults (what the inspector shows).| `bold` / `italic` | boolean | `false` | Base font style (rich-text `<b>`/`<i>` layer on top). || `strokeColor` | Color | `{0,0,0,1}` | Outline color. || `strokeWidth` | number | `0` | Outline width in px; `0` disables. On an SDF atlas the glyph's own edge is pushed outward, so it stays the glyph's shape at any width (see [below](#outlines)). |-| `shadowColor` | Color | `{0,0,0,1}` | Drop-shadow color. |+| `shadowColor` | Color | `{0,0,0,1}` | Drop-shadow color. The shadow and the outline fade with the text's own `color` alpha, so a label faded out leaves neither behind. || `shadowOffsetX` / `shadowOffsetY` | number | `0` | Shadow offset in px. || `shadowBlur` | number | `0` | Softens the shadow, in px. `0` is a hard offset copy; above that the shadow spreads into a ring, so it reads as a soft mass under the glyphs rather than a second stamp of them. A blur with no offset is a halo. || `richText` | boolean | `false` | Parse inline [rich-text markup](#rich-text). |diff --git a/docs/astro/src/content/docs/zh-cn/ui/text.mdx b/docs/astro/src/content/docs/zh-cn/ui/text.mdxindex 42a6b0b13..5d503e65d 100644--- a/docs/astro/src/content/docs/zh-cn/ui/text.mdx+++ b/docs/astro/src/content/docs/zh-cn/ui/text.mdx@@ -83,7 +83,7 @@ const pulseBanner = defineSystem([Query(Mut(Transform), Text), Res(Time)], (q, t| `bold` / `italic` | boolean | `false` | 基础字体样式(富文本 `<b>`/`<i>` 在其上叠加)。 || `strokeColor` | Color | `{0,0,0,1}` | 描边颜色。 || `strokeWidth` | number | `0` | 描边宽度(px);`0` 关闭。SDF 图集上是把字形自己的边缘向外推,所以任意宽度下描出来的仍是这个字的形状([见下文](#描边))。 |-| `shadowColor` | Color | `{0,0,0,1}` | 投影颜色。 |+| `shadowColor` | Color | `{0,0,0,1}` | 投影颜色。投影和描边会随文字自身 `color` 的 alpha 一起淡出,文字淡到透明时不会留下投影。 || `shadowOffsetX` / `shadowOffsetY` | number | `0` | 投影偏移(px)。 || `shadowBlur` | number | `0` | 投影柔度(px)。`0` 是硬边偏移副本;大于 0 时投影摊成一圈,读起来是字下面一团柔和的暗,而不是又盖了一遍字。只给柔度不给偏移就是一圈光晕。 || `richText` | boolean | `false` | 解析内联[富文本标记](#富文本)。 |diff --git a/sdk/src/ui/text/text-renderer.ts b/sdk/src/ui/text/text-renderer.tsindex 7bf044ca7..62a325455 100644--- a/sdk/src/ui/text/text-renderer.ts+++ b/sdk/src/ui/text/text-renderer.ts@@ -273,10 +273,17 @@ export function drawTextWith(atlas: GlyphAtlas, sink: GlyphBatchSink, p: DrawTex}};+ // A label's shadow and outline belong to it: they fade with its own alpha, so+ // a text faded out by its colour leaves no shadow behind (Unity's rule; CSS+ // keeps the shadow, which every fade then had to undo by hand).+ const fillAlpha = Math.max(0, Math.min(1, p.color[3]));+ const decoration = (c: RGBA): RGBA => [c[0], c[1], c[2], c[3] * fillAlpha];+// 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) {- const { color, dx, dy } = p.shadow;+ if (p.shadow && p.shadow.color[3] * fillAlpha > 0) {+ const { dx, dy } = p.shadow;+ const color = decoration(p.shadow.color);const blur = p.shadow.blur ?? 0;if (blur > 0) {const a = tapAlpha(color[3], SHADOW_RING.length);@@ -292,14 +299,15 @@ export function drawTextWith(atlas: GlyphAtlas, sink: GlyphBatchSink, p: DrawTex// 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.+ const outlineColor = p.outline ? decoration(p.outline.color) : null;if (baked) {// The page has the ring already: it is drawn, not grown, so the width only says whether there is one.- if (p.outline && p.outline.width > 0 && p.outline.color[3] > 0) emitPass(p.outline.color, 0, 0, 0, false, TextGlyphMode.Mask);- } else if (p.outline && p.outline.width > 0 && p.outline.color[3] > 0) {+ if (p.outline && p.outline.width > 0 && outlineColor![3] > 0) emitPass(outlineColor!, 0, 0, 0, false, TextGlyphMode.Mask);+ } else if (p.outline && p.outline.width > 0 && outlineColor![3] > 0) {const w = p.outline.width;const bias = outlineBias(atlas, 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);+ if (bias > 0) emitPass(outlineColor!, 0, 0, bias);+ else for (const [ox, oy] of OUTLINE_OFFSETS) emitPass(outlineColor!, ox * w, oy * w);}// Fill (on top).emitPass(p.color, 0, 0, 0, true, baked ? TextGlyphMode.Fill : undefined);diff --git a/sdk/tests/native-text.test.ts b/sdk/tests/native-text.test.tsindex ffc8e26b9..b8cfc7e16 100644--- a/sdk/tests/native-text.test.ts+++ b/sdk/tests/native-text.test.ts@@ -373,8 +373,9 @@ describe('a frame on the native core', () => {});await app.tick(1 / 60);expect(submitSpy).toHaveBeenCalledTimes(3);- expect((submitSpy.mock.calls[0][0] as Float32Array)[7]).toBeCloseTo(0.3);- expect((submitSpy.mock.calls[1][0] as Float32Array)[7]).toBeCloseTo(0.2);+ // Shadow and outline fade with the fill's own alpha (0.8) as well as the subtree's (0.5).+ expect((submitSpy.mock.calls[0][0] as Float32Array)[7]).toBeCloseTo(0.24);+ expect((submitSpy.mock.calls[1][0] as Float32Array)[7]).toBeCloseTo(0.16);expect((submitSpy.mock.calls[2][0] as Float32Array)[7]).toBeCloseTo(0.4);alpha = 0;submitSpy.mockClear();diff --git a/sdk/tests/ui-text-renderer.test.ts b/sdk/tests/ui-text-renderer.test.tsindex 44ec159d5..974be07a1 100644--- a/sdk/tests/ui-text-renderer.test.ts+++ b/sdk/tests/ui-text-renderer.test.ts@@ -64,7 +64,8 @@ describe('REARCH_GUI P1.3b: drawTextWith', () => {outline: { color: [0, 1, 0, 0.4], width: 1 },});expect(sink).toHaveBeenCalledTimes(6);- const alpha = [0.3, 0.3, 0.2, 0.2, 0.4, 0.4];+ // Shadow and outline fade with the fill's own alpha (0.8), then opacity (0.5).+ const alpha = [0.24, 0.24, 0.16, 0.16, 0.4, 0.4];for (let i = 0; i < 6; i++) {const vertices = sink.mock.calls[i][0] as Float32Array;const indices = sink.mock.calls[i][1] as Uint16Array;@@ -74,6 +75,18 @@ describe('REARCH_GUI P1.3b: drawTextWith', () => {}});+ it('draws no shadow or outline for a label faded out by its colour', () => {+ const sink = vi.fn();+ drawTextWith(makeAtlas(1024), sink, {+ text: 'Hi', fontFamily: 'Arial', fontSizePx: 24, color: [1, 1, 1, 0],+ shadow: { color: [0, 0, 0, 0.6], dx: 2, dy: 2 },+ outline: { color: [0, 1, 0, 0.4], width: 1 },+ });+ for (const [vertices] of sink.mock.calls as Array<[Float32Array]>) {+ for (let i = 7; i < vertices.length; i += TEXT_VERTEX_FLOATS) expect(vertices[i]).toBe(0);+ }+ });+it('keeps rich fill colors out of shadow and outline passes on both atlas modes', () => {for (const spread of [0, 6]) {for (const blur of [0, 2]) {