Put a shader on a text and draw pixel fonts sharp (#169)
A text could not wear a shader, and a pixel font was always smoothed. Pixi's Text takes no custom shader. It also shares one texture between texts of the same content and style, and the filter is not part of that key, so two equal texts with different filters would overwrite each other. - TextPicture rasters a text into a texture of its own and draws it like a sprite. Shader, time animation and edge mode work without a special path. A new text, font or size rasters again and keeps the shader. - The old texture is dropped only after the shader holds the new one. Before, Pixi warned about a destroyed texture that was still bound. - A text has a Shader row with the shader's property rows below, as a sprite has. Overrides, saving, animation and the Properties node (type Text) know them. A shader parameter named like an own row of the text loses to that row, as on the particle source. - The font asset has a Filter row, Linear or Nearest. The row existed three times already (texture, vector art, pixel art) and is now one helper filterRow. - Nearest rasters the text 8 times finer and cuts every 8x8 block to one texel: at least half covered is full, else empty. Chrome on Windows smooths canvas text sideways through DirectWrite whatever filter we set later. The cut removes those half pixels on every system, and the texture stays one texel per world unit. Found on the way: - On a font that is no pixel font thin strokes break apart under Nearest. For a pixel font at its own size this does not matter. Not done: - The preview in the Shader tab does not take a selected text as its picture, it shows the test picture then. Tests: - ./check.sh: 189 files, 2759 tests, CHECK EXIT 0. - text-shader.test.ts with 10 tests: rows and name precedence, a clamped override, save and load, animation, the font filter, and sharpCoverage on a smoothed edge and at the texture border. - e2e 30 has 7 new checks: red glyphs through the shader picked with the mouse, the shader survives a new text, red in the game, soft edges under Linear, clean 4x4 blocks under Nearest, and half covered texels (Linear 238, Nearest 0). - Full local run before the Nearest cut: all passed, 1543 checks. GM_STEPS=1,30 on the final bundle: all passed, 32 checks. The sum 1544 in the README is counted from these two runs, not measured. - Counter-checks fail as expected: in the browser no shader on a text fails 30h-30j, an always linear sampler fails the Nearest check, the old 1:1 raster leaves 107 half covered texels and fails 30k-ii. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ef2600493f
commit
89aa92d2e3
13 files changed
+578
-101
No files matched your search
@@ -95,10 +95,10 @@ export function buildPropertyPortSection(
|
||||
}
|
||||
if (target !== 'behavior') {
|
||||
const owner = `a ${targetLabel(target)}`;
|
||||
// A sprite's rows go on with the parameters of the shader it wears – any shader, the node cannot
|
||||
// know which, so each entry says whose it is. Keyed by the def itself: a shader parameter and a
|
||||
// sprite property may share a name, and only the shader's entry earns the note.
|
||||
const shaderRows = target === 'sprite' ? allShaderParamRows(host.state.project) : [];
|
||||
// A sprite's or text's rows go on with the parameters of the shader it wears – any shader, the node
|
||||
// cannot know which, so each entry says whose it is. Keyed by the def itself: a shader parameter and
|
||||
// a property of the type may share a name, and only the shader's entry earns the note.
|
||||
const shaderRows = target === 'sprite' || target === 'text' ? allShaderParamRows(host.state.project) : [];
|
||||
const from = new Map(shaderRows.map((row) => [row.def, row.shaders.join(', ')]));
|
||||
buildPickedPortSection(host, assetId, node, {
|
||||
noun: 'Property',
|
||||
|
||||
@@ -1,19 +1,11 @@
|
||||
import {
|
||||
Container,
|
||||
type FederatedPointerEvent,
|
||||
Graphics,
|
||||
type Mesh,
|
||||
Text,
|
||||
Texture,
|
||||
TextStyle,
|
||||
} from 'pixi.js';
|
||||
import { Container, type FederatedPointerEvent, Graphics, type Mesh, Texture } from 'pixi.js';
|
||||
import { isSymbolType } from '../../core/model/AssetTypes';
|
||||
import { type Extent, scaleOf } from '../../core/model/matrix';
|
||||
import { type Vec2, textOf } from '../../core/model/Properties';
|
||||
import { footprintExtent, footprintOf, placementOf, textAnchor } from '../../core/properties/bounds';
|
||||
import type { Vec2 } from '../../core/model/Properties';
|
||||
import { footprintExtent, footprintOf, placementOf } from '../../core/properties/bounds';
|
||||
import { rectsIntersect } from '../../core/properties/camera';
|
||||
import type { EditorState } from '../state';
|
||||
import { type Size, Theme, Widget } from '../../ui';
|
||||
import { type Size, Widget } from '../../ui';
|
||||
import { fontLibrary } from '../../viewport/FontLibrary';
|
||||
import { drawSlots } from '../../viewport/drawOrder';
|
||||
import type { DrawItem } from '../../viewport/items';
|
||||
@@ -23,13 +15,8 @@ import { particleLibrary } from '../../viewport/ParticleLibrary';
|
||||
import { SHEET_EDGE } from '../../core/shader/nodes';
|
||||
import { shaderLibrary } from '../../viewport/ShaderLibrary';
|
||||
import { type ShaderSource, wornShader } from '../../viewport/shaders';
|
||||
import {
|
||||
disposeSpriteMesh,
|
||||
drawItem,
|
||||
newSpriteMesh,
|
||||
placeNode,
|
||||
syncSpriteMesh,
|
||||
} from '../../viewport/symbols';
|
||||
import { disposeSpriteMesh, drawItem, newSpriteMesh, syncSpriteMesh } from '../../viewport/symbols';
|
||||
import { TextPicture } from '../../viewport/textPicture';
|
||||
import { cellCentre, type LayerFocus, TilemapField } from '../../viewport/tilemap';
|
||||
import { textureLibrary } from '../../viewport/TextureLibrary';
|
||||
import { gameClock } from '../../core/time';
|
||||
@@ -51,7 +38,7 @@ export abstract class WorldViewport extends Widget {
|
||||
protected readonly contentMask = new Graphics();
|
||||
protected readonly nodes = new Map<string, Graphics>();
|
||||
/** Text content of text items: a sibling of the item's node, kept next to it in z. */
|
||||
private readonly texts = new Map<string, Text>();
|
||||
private readonly texts = new Map<string, TextPicture>();
|
||||
/** Pictures of sprite items: siblings as well, each right under its node. */
|
||||
private readonly sprites = new Map<string, Mesh>();
|
||||
/** The drawn particles of each source, one container – and one draw call – per source. */
|
||||
@@ -322,7 +309,7 @@ export abstract class WorldViewport extends Widget {
|
||||
if (item.enabled && !hidden) culled.push({ node, extent: item.type === 'particles' ? null : extent });
|
||||
node.eventMode = this.itemsInteractive && node.visible ? 'static' : 'none';
|
||||
this.syncDecorations(item, onScreen, view.scale, extent);
|
||||
this.syncText(item, onScreen, view.scale);
|
||||
this.syncText(item, onScreen, view.scale, shaders);
|
||||
this.syncSprite(item, onScreen, size, shaders);
|
||||
this.syncParticles(item, onScreen, shaders);
|
||||
this.syncTilemap(item, onScreen);
|
||||
@@ -378,7 +365,7 @@ export abstract class WorldViewport extends Widget {
|
||||
const node = this.nodes.get(id);
|
||||
if (node) this.objects.addChild(node);
|
||||
const text = this.texts.get(id);
|
||||
if (text) this.objects.addChild(text);
|
||||
if (text) this.objects.addChild(text.mesh);
|
||||
const field = this.fields.get(id);
|
||||
if (field) this.objects.addChild(field.container);
|
||||
}
|
||||
@@ -491,41 +478,24 @@ export abstract class WorldViewport extends Widget {
|
||||
* The typeface a text is drawn in and how wide that makes it. Both are what a font asset is
|
||||
* for, and both arrive asynchronously, so a test cannot read them off the model.
|
||||
*/
|
||||
textInfo(id: string): { family: string; width: number } | null {
|
||||
textInfo(id: string): ReturnType<TextPicture['info']> | null {
|
||||
const text = this.texts.get(id);
|
||||
if (!text?.visible) return null;
|
||||
return { family: String(text.style.fontFamily), width: Math.round(text.width) };
|
||||
return text?.mesh.visible ? text.info() : null;
|
||||
}
|
||||
|
||||
/** Resolution follows the zoom in coarse steps: re-rendering the glyphs per wheel notch is wasteful. */
|
||||
private syncText(item: DrawItem, onScreen: boolean, scale: number): void {
|
||||
private syncText(item: DrawItem, onScreen: boolean, scale: number, shaders: ShaderSource): void {
|
||||
let text = this.texts.get(item.id);
|
||||
if (item.type !== 'text' || !onScreen) {
|
||||
if (text) text.visible = false;
|
||||
if (text) text.mesh.visible = false;
|
||||
return;
|
||||
}
|
||||
if (!text) {
|
||||
text = new Text({
|
||||
text: '',
|
||||
style: new TextStyle({ fontFamily: Theme.font, fontSize: 32, fill: 0xffffff }),
|
||||
});
|
||||
text.eventMode = 'none';
|
||||
text = new TextPicture();
|
||||
this.texts.set(item.id, text);
|
||||
this.objects.addChild(text);
|
||||
this.objects.addChild(text.mesh);
|
||||
}
|
||||
text.visible = true;
|
||||
placeNode(text, item.m);
|
||||
text.text = textOf(item.props.text ?? '');
|
||||
const size = Math.max(1, Number(item.props.fontSize ?? 32));
|
||||
if (text.style.fontSize !== size) text.style.fontSize = size;
|
||||
// An uploaded face that has not arrived yet reads as `''`; until it does, the editor's own
|
||||
// typeface stands in, and the library's change event brings the redraw that swaps it.
|
||||
const family = fontLibrary.family(item.font) || Theme.font;
|
||||
if (text.style.fontFamily !== family) text.style.fontFamily = family;
|
||||
const color = Number(item.props.color ?? 0xffffff);
|
||||
if (text.style.fill !== color) text.style.fill = color;
|
||||
text.anchor.set(textAnchor(item.props), 0.5);
|
||||
const resolution = Math.min(4, Math.max(1, (Math.round(scale * 2) / 2) * 2));
|
||||
if (text.resolution !== resolution) text.resolution = resolution;
|
||||
text.mesh.visible = true;
|
||||
if (wornShader(item.props, shaders)?.time) this.animated = true;
|
||||
text.sync(item, scale, shaders);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user