Something went wrong. Try again.
Minecraft text rendering inside the browser! www.npmjs.com/package/libmojangles
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524import type { VertexGenerator, VertexGeneratorOptions, LayoutResult, PositionedGlyph, TextMeshGroup, TextMesh, Vertex, Color,} from "../types";import { encodePickingId } from "./picker";
const SHADOW_OFFSET = 1.0;const SHADOW_DEPTH = 0.03;const EFFECT_DEPTH = 0.01;const BOLD_OFFSET = 1.0;const EXTRA_BOLD_THICKNESS = 0.1;const DEFAULT_LIGHTMAP: [number, number] = [240 / 256, 240 / 256];
const FLOATS_PER_VERTEX = 12;const PICKING_FLOATS_PER_VERTEX = 16;
function colorToArray(color: Color): [number, number, number, number] { return [color.r, color.g, color.b, color.a];}
function darkenColor(color: Color, factor: number): Color { // Minecraft uses integer math: (int)((float)color * factor) // This truncates rather than rounds, so 255 * 0.25 = 63 (0x3F), not 64 (0x40) return { r: Math.floor(color.r * 255 * factor) / 255, g: Math.floor(color.g * 255 * factor) / 255, b: Math.floor(color.b * 255 * factor) / 255, a: color.a, };}
function isColorZero(color: Color): boolean { return color.r === 0 && color.g === 0 && color.b === 0 && color.a === 0;}
export class VertexGeneratorImpl implements VertexGenerator { generate(layout: LayoutResult, options?: VertexGeneratorOptions): TextMeshGroup { const z = options?.z ?? 0; const lightmapUV = options?.lightmapUV ?? DEFAULT_LIGHTMAP; const generateShadow = options?.generateShadow ?? true; const shadowOffsetBase = options?.shadowOffset ?? [SHADOW_OFFSET, SHADOW_OFFSET]; const generatePicking = options?.generatePicking || options?.transformFeedback || false; const scale = options?.scale ?? 1;
const shadowOffset: [number, number] = [ shadowOffsetBase[0] * scale, shadowOffsetBase[1] * scale, ];
const meshesByKey = new Map<string, { vertices: number[]; indices: number[]; textureId: string; componentId?: string }>();
const getMeshKey = (textureId: string, componentId?: string) => { return componentId ? `${textureId}:${componentId}` : textureId; };
const getMeshData = (textureId: string, componentId?: string) => { const key = getMeshKey(textureId, componentId); if (!meshesByKey.has(key)) { meshesByKey.set(key, { vertices: [], indices: [], textureId, componentId }); } return meshesByKey.get(key)!; }; const pickingByComponent = new Map<string, { vertices: number[]; indices: number[]; componentId?: string }>(); const getPickingData = (componentId?: string) => { const key = componentId ?? ""; if (!pickingByComponent.has(key)) { pickingByComponent.set(key, { vertices: [], indices: [], componentId }); } return pickingByComponent.get(key)!; };
for (const line of layout.lines) { for (const glyph of line.glyphs) { this.generateGlyphQuads( glyph, z, lightmapUV, generateShadow, shadowOffset, options?.shadowColor, getMeshData, scale );
if (generatePicking && glyph.glyph.textureId) { this.generatePickingQuad(glyph, z, getPickingData(glyph.componentId), scale); }
if (glyph.style.underlined) { this.generateEffectQuad( glyph, 8 * scale, 9 * scale, z + EFFECT_DEPTH, lightmapUV, generateShadow, shadowOffset, options?.shadowColor, getMeshData, scale ); }
if (glyph.style.strikethrough) { this.generateEffectQuad( glyph, 3.5 * scale, 4.5 * scale, z + EFFECT_DEPTH, lightmapUV, generateShadow, shadowOffset, options?.shadowColor, getMeshData, scale ); } } }
const meshes: TextMesh[] = []; for (const [_key, data] of meshesByKey) { meshes.push({ vertices: new Float32Array(data.vertices), indices: new Uint16Array(data.indices), textureId: data.textureId, vertexCount: data.vertices.length / FLOATS_PER_VERTEX, indexCount: data.indices.length, componentId: data.componentId, }); }
let pickingMeshes: TextMesh[] | undefined;
if (generatePicking && pickingByComponent.size > 0) { pickingMeshes = []; for (const [_key, data] of pickingByComponent) { if (data.vertices.length > 0) { pickingMeshes.push({ vertices: new Float32Array(data.vertices), indices: new Uint16Array(data.indices), textureId: "picking", vertexCount: data.vertices.length / PICKING_FLOATS_PER_VERTEX, indexCount: data.indices.length, floatsPerVertex: PICKING_FLOATS_PER_VERTEX, componentId: data.componentId, }); } } if (pickingMeshes.length === 0) pickingMeshes = undefined; }
return { meshes, pickingMeshes }; }
generateForGlyph(glyph: PositionedGlyph, options?: VertexGeneratorOptions): Vertex[] { const z = options?.z ?? 0; const lightmapUV = options?.lightmapUV ?? DEFAULT_LIGHTMAP; const color = glyph.style.color ?? { r: 1, g: 1, b: 1, a: 1 }; const scale = options?.scale ?? 1;
const x0 = glyph.x + glyph.glyph.left * scale; const x1 = glyph.x + glyph.glyph.right * scale; const y0 = glyph.y + glyph.glyph.up * scale; const y1 = glyph.y + glyph.glyph.down * scale;
return this.createQuadVertices( x0, y0, x1, y1, z, glyph.glyph.u0, glyph.glyph.v0, glyph.glyph.u1, glyph.glyph.v1, color, lightmapUV, glyph.style.italic ?? false, glyph.style.bold ?? false, glyph.glyph.up * scale, glyph.glyph.down * scale, scale ); }
private generateGlyphQuads( glyph: PositionedGlyph, baseZ: number, lightmapUV: [number, number], generateShadow: boolean, shadowOffset: [number, number], shadowColorOverride: Color | undefined, getMeshData: (textureId: string, componentId?: string) => { vertices: number[]; indices: number[] }, scale: number ): void { const textureId = glyph.glyph.textureId; if (!textureId) return;
const meshData = getMeshData(textureId, glyph.componentId);
const color = glyph.style.color ?? { r: 1, g: 1, b: 1, a: 1 }; const italic = glyph.style.italic ?? false; const bold = glyph.style.bold ?? false; const shadowColor = glyph.style.shadowColor ?? shadowColorOverride ?? darkenColor(color, 0.25); const hasShadow = generateShadow && (glyph.style.shadow ?? true) && !isColorZero(shadowColor);
const left = glyph.glyph.left * scale; const right = glyph.glyph.right * scale; const up = glyph.glyph.up * scale; const down = glyph.glyph.down * scale;
const x0 = glyph.x + left; const x1 = glyph.x + right; const y0 = glyph.y + up; const y1 = glyph.y + down;
if (hasShadow) { this.addQuad( meshData, x0 + shadowOffset[0], y0 + shadowOffset[1], x1 + shadowOffset[0], y1 + shadowOffset[1], baseZ, glyph.glyph.u0, glyph.glyph.v0, glyph.glyph.u1, glyph.glyph.v1, shadowColor, lightmapUV, italic, bold, up, down, scale );
if (bold) { this.addQuad( meshData, x0 + shadowOffset[0] + BOLD_OFFSET * scale, y0 + shadowOffset[1], x1 + shadowOffset[0] + BOLD_OFFSET * scale, y1 + shadowOffset[1], baseZ + 0.001, glyph.glyph.u0, glyph.glyph.v0, glyph.glyph.u1, glyph.glyph.v1, shadowColor, lightmapUV, italic, true, up, down, scale ); } }
const glyphZ = hasShadow ? baseZ + SHADOW_DEPTH : baseZ; this.addQuad( meshData, x0, y0, x1, y1, glyphZ, glyph.glyph.u0, glyph.glyph.v0, glyph.glyph.u1, glyph.glyph.v1, color, lightmapUV, italic, bold, up, down, scale );
if (bold) { this.addQuad( meshData, x0 + BOLD_OFFSET * scale, y0, x1 + BOLD_OFFSET * scale, y1, glyphZ + 0.001, glyph.glyph.u0, glyph.glyph.v0, glyph.glyph.u1, glyph.glyph.v1, color, lightmapUV, italic, true, up, down, scale ); } }
private generateEffectQuad( glyph: PositionedGlyph, effectY0: number, effectY1: number, z: number, lightmapUV: [number, number], generateShadow: boolean, shadowOffset: [number, number], shadowColorOverride: Color | undefined, getMeshData: (textureId: string, componentId?: string) => { vertices: number[]; indices: number[] }, scale: number ): void { // Use dedicated white texture for solid color effects (like Minecraft's SpecialGlyphs.WHITE) const textureId = "__white__"; const meshData = getMeshData(textureId, glyph.componentId); const color = glyph.style.color ?? { r: 1, g: 1, b: 1, a: 1 }; const shadowColor = glyph.style.shadowColor ?? shadowColorOverride ?? darkenColor(color, 0.25); const hasShadow = generateShadow && (glyph.style.shadow ?? true) && !isColorZero(shadowColor);
const bold = glyph.style.bold ?? false; const advance = (glyph.glyph.advance + (bold ? BOLD_OFFSET : 0)) * scale;
const x0 = glyph.x - (glyph.sourceIndex === 0 ? scale : 0); const x1 = glyph.x + advance; const y0 = glyph.y + effectY0; const y1 = glyph.y + effectY1;
if (hasShadow) { this.addRectQuad( meshData, x0 + shadowOffset[0], y0 + shadowOffset[1], x1 + shadowOffset[0], y1 + shadowOffset[1], z, shadowColor, lightmapUV ); }
const effectZ = hasShadow ? z + SHADOW_DEPTH : z; this.addRectQuad(meshData, x0, y0, x1, y1, effectZ, color, lightmapUV); }
private generatePickingQuad( glyph: PositionedGlyph, z: number, pickingData: { vertices: number[]; indices: number[] }, scale: number ): void { const pickColor = encodePickingId(glyph.sourceIndex); const lightmapUV: [number, number] = [1, 1];
const bold = glyph.style.bold ?? false; const advance = (glyph.glyph.advance + (bold ? BOLD_OFFSET : 0)) * scale;
const x0 = glyph.x; const x1 = glyph.x + advance; const y0 = glyph.y + glyph.glyph.up * scale; const y1 = glyph.y + glyph.glyph.down * scale;
const color = glyph.style.color ?? { r: 1, g: 1, b: 1, a: 1 }; this.addPickingRectQuad(pickingData, x0, y0, x1, y1, z, color, lightmapUV, pickColor); }
private addPickingRectQuad( meshData: { vertices: number[]; indices: number[] }, x0: number, y0: number, x1: number, y1: number, z: number, color: Color, lightmapUV: [number, number], pickColor: Color ): void { const baseVertex = meshData.vertices.length / PICKING_FLOATS_PER_VERTEX; const rgba = colorToArray(color); const pickRgba = colorToArray(pickColor);
meshData.vertices.push( x0, y0, z, rgba[0], rgba[1], rgba[2], rgba[3], 0, 0, lightmapUV[0], lightmapUV[1], 0, pickRgba[0], pickRgba[1], pickRgba[2], pickRgba[3], x0, y1, z, rgba[0], rgba[1], rgba[2], rgba[3], 0, 1, lightmapUV[0], lightmapUV[1], 0, pickRgba[0], pickRgba[1], pickRgba[2], pickRgba[3], x1, y1, z, rgba[0], rgba[1], rgba[2], rgba[3], 1, 1, lightmapUV[0], lightmapUV[1], 0, pickRgba[0], pickRgba[1], pickRgba[2], pickRgba[3], x1, y0, z, rgba[0], rgba[1], rgba[2], rgba[3], 1, 0, lightmapUV[0], lightmapUV[1], 0, pickRgba[0], pickRgba[1], pickRgba[2], pickRgba[3] );
meshData.indices.push( baseVertex, baseVertex + 1, baseVertex + 2, baseVertex, baseVertex + 2, baseVertex + 3 ); }
private addQuad( meshData: { vertices: number[]; indices: number[] }, x0: number, y0: number, x1: number, y1: number, z: number, u0: number, v0: number, u1: number, v1: number, color: Color, lightmapUV: [number, number], italic: boolean, bold: boolean, up: number, down: number, scale: number ): void { const extraThickness = bold ? EXTRA_BOLD_THICKNESS * scale : 0;
const shearTop = italic ? (1.0 - 0.25 * (up / scale)) * scale : 0; const shearBottom = italic ? (1.0 - 0.25 * (down / scale)) * scale : 0;
const finalX0 = x0 - extraThickness; const finalX1 = x1 + extraThickness; const finalY0 = y0 - extraThickness; const finalY1 = y1 + extraThickness;
const baseVertex = meshData.vertices.length / FLOATS_PER_VERTEX; const rgba = colorToArray(color);
meshData.vertices.push( finalX0 + shearTop, finalY0, z, rgba[0], rgba[1], rgba[2], rgba[3], u0, v0, lightmapUV[0], lightmapUV[1], 0, finalX0 + shearBottom, finalY1, z, rgba[0], rgba[1], rgba[2], rgba[3], u0, v1, lightmapUV[0], lightmapUV[1], 0, finalX1 + shearBottom, finalY1, z, rgba[0], rgba[1], rgba[2], rgba[3], u1, v1, lightmapUV[0], lightmapUV[1], 0, finalX1 + shearTop, finalY0, z, rgba[0], rgba[1], rgba[2], rgba[3], u1, v0, lightmapUV[0], lightmapUV[1], 0 );
meshData.indices.push( baseVertex, baseVertex + 1, baseVertex + 2, baseVertex, baseVertex + 2, baseVertex + 3 ); }
private addRectQuad( meshData: { vertices: number[]; indices: number[] }, x0: number, y0: number, x1: number, y1: number, z: number, color: Color, lightmapUV: [number, number] ): void { const baseVertex = meshData.vertices.length / FLOATS_PER_VERTEX; const rgba = colorToArray(color);
meshData.vertices.push( x0, y0, z, rgba[0], rgba[1], rgba[2], rgba[3], 0, 0, lightmapUV[0], lightmapUV[1], 0, x0, y1, z, rgba[0], rgba[1], rgba[2], rgba[3], 0, 1, lightmapUV[0], lightmapUV[1], 0, x1, y1, z, rgba[0], rgba[1], rgba[2], rgba[3], 1, 1, lightmapUV[0], lightmapUV[1], 0, x1, y0, z, rgba[0], rgba[1], rgba[2], rgba[3], 1, 0, lightmapUV[0], lightmapUV[1], 0 );
meshData.indices.push( baseVertex, baseVertex + 1, baseVertex + 2, baseVertex, baseVertex + 2, baseVertex + 3 ); }
private createQuadVertices( x0: number, y0: number, x1: number, y1: number, z: number, u0: number, v0: number, u1: number, v1: number, color: Color, lightmapUV: [number, number], italic: boolean, bold: boolean, up: number, down: number, scale: number ): Vertex[] { const extraThickness = bold ? EXTRA_BOLD_THICKNESS * scale : 0; const shearTop = italic ? (1.0 - 0.25 * (up / scale)) * scale : 0; const shearBottom = italic ? (1.0 - 0.25 * (down / scale)) * scale : 0;
const finalX0 = x0 - extraThickness; const finalX1 = x1 + extraThickness; const finalY0 = y0 - extraThickness; const finalY1 = y1 + extraThickness; const rgba = colorToArray(color);
return [ { position: [finalX0 + shearTop, finalY0, z], color: rgba, uv0: [u0, v0], uv2: lightmapUV }, { position: [finalX0 + shearBottom, finalY1, z], color: rgba, uv0: [u0, v1], uv2: lightmapUV }, { position: [finalX1 + shearBottom, finalY1, z], color: rgba, uv0: [u1, v1], uv2: lightmapUV }, { position: [finalX1 + shearTop, finalY0, z], color: rgba, uv0: [u1, v0], uv2: lightmapUV }, ]; }}
export function createVertexGenerator(): VertexGenerator { return new VertexGeneratorImpl();}