From 2592d50c735e6f65388f1a1751df31cfa3aa06fa Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Tue, 19 Nov 2024 15:21:13 +0100 Subject: [PATCH] feat: add basic rendering for text displays --- prismarine-viewer/viewer/lib/entities.ts | 15 ++++++++++++--- prismarine-viewer/viewer/lib/entity/EntityMesh.js | 4 ++-- prismarine-viewer/viewer/lib/entity/entities.json | 4 ++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/prismarine-viewer/viewer/lib/entities.ts b/prismarine-viewer/viewer/lib/entities.ts index 6afd1c342..be074de12 100644 --- a/prismarine-viewer/viewer/lib/entities.ts +++ b/prismarine-viewer/viewer/lib/entities.ts @@ -29,17 +29,25 @@ function getUsernameTexture (username: string, { fontFamily = 'sans-serif' }: an const padding = 5 ctx.font = `${fontSize}px ${fontFamily}` - const textWidth = ctx.measureText(username).width + padding * 2 + const lines = String(username).split("\n") + + let textWidth = 0 + for (let line of lines) { + const width = ctx.measureText(line).width + padding * 2 + if (width > textWidth) textWidth = width + } canvas.width = textWidth - canvas.height = fontSize + padding * 2 + canvas.height = (fontSize + padding * 2) * lines.length ctx.fillStyle = 'rgba(0, 0, 0, 0.3)' ctx.fillRect(0, 0, canvas.width, canvas.height) ctx.font = `${fontSize}px ${fontFamily}` ctx.fillStyle = 'white' - ctx.fillText(username, padding, fontSize) + for (let i = 0; i < lines.length; i++) { + ctx.fillText(lines[i], padding + (textWidth - ctx.measureText(lines[i]).width) / 2, fontSize + fontSize * i) + } return canvas } @@ -454,6 +462,7 @@ export class Entities extends EventEmitter { // --- // not player const displayText = entity.metadata?.[3] && this.parseEntityLabel(entity.metadata[2]) + || entity.metadata?.[23] && this.parseEntityLabel(entity.metadata[23]) // text displays if (entity.name !== 'player' && displayText) { addNametag({ ...entity, username: displayText }, this.entitiesOptions, this.entities[entity.id].children.find(c => c.name === 'mesh')) } diff --git a/prismarine-viewer/viewer/lib/entity/EntityMesh.js b/prismarine-viewer/viewer/lib/entity/EntityMesh.js index a518171ff..1350dcf0d 100644 --- a/prismarine-viewer/viewer/lib/entity/EntityMesh.js +++ b/prismarine-viewer/viewer/lib/entity/EntityMesh.js @@ -224,8 +224,8 @@ export const knownNotHandled = [ 'item_display', 'item_frame', 'lightning_bolt', 'marker', 'painting', 'spawner_minecart', - 'spectral_arrow', 'text_display', - 'tnt', 'trader_llama', 'zombie_horse' + 'spectral_arrow', 'tnt', + 'trader_llama', 'zombie_horse' ] export const temporaryMap = { diff --git a/prismarine-viewer/viewer/lib/entity/entities.json b/prismarine-viewer/viewer/lib/entity/entities.json index f005f5d15..e084a5cd3 100644 --- a/prismarine-viewer/viewer/lib/entity/entities.json +++ b/prismarine-viewer/viewer/lib/entity/entities.json @@ -16390,6 +16390,10 @@ }, "render_controllers": ["controller.render.strider"] }, + "text_display": { + "identifier": "minecraft:text_display", + "geometry": {} + }, "trident": { "identifier": "minecraft:thrown_trident", "textures": {