Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
feat: enhance model info overlay with rich-inline rendering and additional stats
nirholas9cc199650d48
diff --git a/src/model-info.js b/src/model-info.jsindex 6ed9d27e4..60061b666 100644--- a/src/model-info.js+++ b/src/model-info.js@@ -1,19 +1,41 @@-import { prepare, layout } from '@chenglou/pretext';+import { prepare, layout, prepareWithSegments, layoutWithLines, measureLineStats } from '@chenglou/pretext';+import { prepareRichInline, walkRichInlineLineRanges, materializeRichInlineLineRange } from '@chenglou/pretext/rich-inline';-const FONT_LABEL = '600 11px Inter, sans-serif';-const FONT_VALUE = '300 13px Inter, sans-serif';+// Fontsconst FONT_TITLE = '500 14px Inter, sans-serif';+const FONT_LABEL = '600 10px Inter, sans-serif';+const FONT_VALUE = '300 13px Inter, sans-serif';+const FONT_CHIP = '600 10px Inter, sans-serif';+const FONT_DESC = '300 12px Inter, sans-serif';++// Layoutconst LINE_HEIGHT = 18;+const CHIP_HEIGHT = 16;const PADDING = 16;const ROW_GAP = 6;+const CHIP_PAD_X = 12; // horizontal padding inside chipconst DPR = Math.min(window.devicePixelRatio || 1, 2);-function countVertices(object) {+// Colors+const COL_BG = 'rgba(0, 0, 0, 0.75)';+const COL_BORDER = 'rgba(255, 255, 255, 0.08)';+const COL_TITLE = 'rgba(255, 255, 255, 0.9)';+const COL_LABEL = 'rgba(255, 255, 255, 0.30)';+const COL_VALUE = 'rgba(255, 255, 255, 0.85)';+const COL_SEP = 'rgba(255, 255, 255, 0.08)';+const COL_DESC = 'rgba(255, 255, 255, 0.45)';+const COL_CHIP_BG = 'rgba(255, 255, 255, 0.08)';+const COL_CHIP_TEXT = 'rgba(255, 255, 255, 0.7)';++function countStats(object) {let vertices = 0;let triangles = 0;let meshes = 0;const materialSet = new Set();const textureSet = new Set();+ const materialTypes = new Set();+ let hasSkinning = false;+ let hasMorphs = false;object.traverse((node) => {if (node.isMesh || node.isPoints || node.isLine) {@@ -29,10 +51,14 @@ function countVertices(object) {vertices += geo.attributes.position.count;}}+ if (node.skeleton) hasSkinning = true;+ if (node.morphTargetInfluences && node.morphTargetInfluences.length > 0) hasMorphs = true;+const mats = Array.isArray(node.material) ? node.material : [node.material];mats.forEach((mat) => {if (mat) {materialSet.add(mat.uuid);+ materialTypes.add(mat.type.replace('Material', ''));for (const key in mat) {if (mat[key] && mat[key].isTexture) {textureSet.add(mat[key].uuid);@@ -49,61 +75,130 @@ function countVertices(object) {meshes,materials: materialSet.size,textures: textureSet.size,+ materialTypes: Array.from(materialTypes),+ hasSkinning,+ hasMorphs,};}-function formatNumber(n) {+function formatNum(n) {if (n >= 1_000_000) return (n / 1_000_000).toFixed(1) + 'M';if (n >= 1_000) return (n / 1_000).toFixed(1) + 'K';return n.toString();}+function buildDescription(stats, clips) {+ const parts = [];+ if (stats.hasSkinning) parts.push('skinned');+ if (stats.hasMorphs) parts.push('morph targets');+ if (clips && clips.length > 0) parts.push(`${clips.length} animation${clips.length > 1 ? 's' : ''}`);+ if (stats.materialTypes.length > 0) parts.push(stats.materialTypes.join(', '));+ return parts.length > 0 ? parts.join(' · ') : '';+}+/**- * Creates or updates the model info overlay.- * @param {HTMLElement} container - The viewer container element- * @param {THREE.Object3D} object - The loaded 3D content- * @param {Array} clips - Animation clips- * @returns {{ el: HTMLElement, canvas: HTMLCanvasElement, update: Function, remove: Function }}+ * Creates the model info overlay using pretext rich-inline for mixed-style rendering.*/export function createModelInfo(container, object, clips) {- const stats = countVertices(object);+ const stats = countStats(object);+ const desc = buildDescription(stats, clips);+ // Build stat rowsconst rows = [- { label: 'MESHES', value: formatNumber(stats.meshes) },- { label: 'VERTICES', value: formatNumber(stats.vertices) },- { label: 'TRIANGLES', value: formatNumber(stats.triangles) },- { label: 'MATERIALS', value: formatNumber(stats.materials) },- { label: 'TEXTURES', value: formatNumber(stats.textures) },+ { label: 'MESHES', value: formatNum(stats.meshes) },+ { label: 'VERTICES', value: formatNum(stats.vertices) },+ { label: 'TRIANGLES', value: formatNum(stats.triangles) },+ { label: 'MATERIALS', value: formatNum(stats.materials) },+ { label: 'TEXTURES', value: formatNum(stats.textures) },];-if (clips && clips.length > 0) {rows.push({ label: 'ANIMATIONS', value: clips.length.toString() });}- // Use pretext to measure all text precisely- const maxWidth = 180;- const titleText = 'Model Info';- const titlePrepared = prepare(titleText, FONT_TITLE);- const titleLayout = layout(titlePrepared, maxWidth, LINE_HEIGHT + 2);+ // Build chips from material types & features+ const chips = [];+ stats.materialTypes.forEach((t) => chips.push(t));+ if (stats.hasSkinning) chips.push('Skinned');+ if (stats.hasMorphs) chips.push('Morph');+ if (clips && clips.length > 0) chips.push('Animated');++ // -- Measure everything with pretext --+ const maxWidth = 200;- const measured = rows.map((row) => {+ // Title+ const titlePrepared = prepare('Model Info', FONT_TITLE);+ const titleMeasure = layout(titlePrepared, maxWidth, LINE_HEIGHT + 2);++ // Stat rows: use rich-inline for each row (label + value in one flow)+ const rowMeasurements = rows.map((row) => {+ const items = [+ { text: row.label + ' ', font: FONT_LABEL },+ { text: row.value, font: FONT_VALUE },+ ];+ const prepared = prepareRichInline(items);+ // Get the natural width by measuring with a very wide maxconst labelPrepared = prepare(row.label, FONT_LABEL);- const labelLayout = layout(labelPrepared, maxWidth, LINE_HEIGHT);+ const labelStats = measureLineStats(labelPrepared, maxWidth);const valuePrepared = prepare(row.value, FONT_VALUE);- const valueLayout = layout(valuePrepared, maxWidth, LINE_HEIGHT);- return { ...row, labelWidth: labelLayout.width, valueWidth: valueLayout.width };+ const valueStats = measureLineStats(valuePrepared, maxWidth);+ return {+ ...row,+ labelWidth: labelStats.maxLineWidth,+ valueWidth: valueStats.maxLineWidth,+ richPrepared: prepared,+ };+ });++ // Chips: measure each chip with pretext+ const chipMeasurements = chips.map((text) => {+ const prepared = prepare(text, FONT_CHIP);+ const stats = measureLineStats(prepared, 300);+ return { text, width: stats.maxLineWidth + CHIP_PAD_X };});- // Calculate canvas dimensions from pretext measurements+ // Description: multi-line wrapping — this is where pretext really shines+ let descLines = [];+ let descHeight = 0;+ if (desc) {+ const descPrepared = prepareWithSegments(desc, FONT_DESC);+ const descLayout = layoutWithLines(descPrepared, maxWidth - PADDING, LINE_HEIGHT);+ descLines = descLayout.lines;+ descHeight = descLayout.height;+ }++ // -- Calculate canvas size from measurements --const contentWidth = Math.max(- titleLayout.width + PADDING * 2,- ...measured.map((r) => r.labelWidth + r.valueWidth + PADDING * 3),+ titleMeasure.width + PADDING * 2,+ ...rowMeasurements.map((r) => r.labelWidth + r.valueWidth + PADDING * 3),);- const canvasWidth = Math.max(contentWidth, 160);+ const canvasWidth = Math.max(contentWidth, 180);++ // Chip row height+ let chipRowHeight = 0;+ if (chipMeasurements.length > 0) {+ // Wrap chips into rows+ let chipX = 0;+ let chipRows = 1;+ chipMeasurements.forEach((chip) => {+ if (chipX > 0 && chipX + chip.width > canvasWidth - PADDING * 2) {+ chipRows++;+ chipX = 0;+ }+ chipX += chip.width + 6;+ });+ chipRowHeight = chipRows * (CHIP_HEIGHT + 6) + ROW_GAP;+ }+const canvasHeight =- PADDING + titleLayout.height + ROW_GAP + measured.length * (LINE_HEIGHT + ROW_GAP) + PADDING;+ PADDING ++ titleMeasure.height + ROW_GAP + // title+ ROW_GAP + // separator+ rowMeasurements.length * (LINE_HEIGHT + ROW_GAP) + // stat rows+ chipRowHeight + // chips+ (descHeight > 0 ? ROW_GAP + descHeight : 0) + // description+ PADDING;- // Create the canvas+ // -- Render to canvas --const canvas = document.createElement('canvas');canvas.width = canvasWidth * DPR;canvas.height = canvasHeight * DPR;@@ -113,43 +208,42 @@ export function createModelInfo(container, object, clips) {const ctx = canvas.getContext('2d');ctx.scale(DPR, DPR);- // Draw background- ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';+ // Background+ ctx.fillStyle = COL_BG;roundRect(ctx, 0, 0, canvasWidth, canvasHeight, 10);ctx.fill();- // Draw border- ctx.strokeStyle = 'rgba(255, 255, 255, 0.08)';+ // Border+ ctx.strokeStyle = COL_BORDER;ctx.lineWidth = 1;roundRect(ctx, 0.5, 0.5, canvasWidth - 1, canvasHeight - 1, 10);ctx.stroke();- // Draw title+ // Titlelet y = PADDING;ctx.font = FONT_TITLE;- ctx.fillStyle = 'rgba(255, 255, 255, 0.9)';+ ctx.fillStyle = COL_TITLE;ctx.textBaseline = 'top';- ctx.fillText(titleText, PADDING, y);- y += titleLayout.height + ROW_GAP;+ ctx.fillText('Model Info', PADDING, y);+ y += titleMeasure.height + ROW_GAP;// Separator- ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';+ ctx.strokeStyle = COL_SEP;ctx.beginPath();ctx.moveTo(PADDING, y);ctx.lineTo(canvasWidth - PADDING, y);ctx.stroke();y += ROW_GAP;- // Draw rows- measured.forEach((row) => {- // Label (left)+ // Stat rows+ rowMeasurements.forEach((row) => {ctx.font = FONT_LABEL;- ctx.fillStyle = 'rgba(255, 255, 255, 0.35)';+ ctx.fillStyle = COL_LABEL;+ ctx.textAlign = 'left';ctx.fillText(row.label, PADDING, y);- // Value (right-aligned)ctx.font = FONT_VALUE;- ctx.fillStyle = 'rgba(255, 255, 255, 0.85)';+ ctx.fillStyle = COL_VALUE;ctx.textAlign = 'right';ctx.fillText(row.value, canvasWidth - PADDING, y);ctx.textAlign = 'left';@@ -157,7 +251,52 @@ export function createModelInfo(container, object, clips) {y += LINE_HEIGHT + ROW_GAP;});- // Wrap in a container div+ // Chips+ if (chipMeasurements.length > 0) {+ let chipX = PADDING;++ chipMeasurements.forEach((chip) => {+ if (chipX > PADDING && chipX + chip.width > canvasWidth - PADDING) {+ chipX = PADDING;+ y += CHIP_HEIGHT + 6;+ }++ // Chip background+ ctx.fillStyle = COL_CHIP_BG;+ roundRect(ctx, chipX, y, chip.width, CHIP_HEIGHT, 4);+ ctx.fill();++ // Chip text (centered in chip)+ ctx.font = FONT_CHIP;+ ctx.fillStyle = COL_CHIP_TEXT;+ ctx.textBaseline = 'top';+ ctx.fillText(chip.text, chipX + CHIP_PAD_X / 2, y + 3);++ chipX += chip.width + 6;+ });++ y += CHIP_HEIGHT + ROW_GAP + 4;+ }++ // Description (multi-line, pretext-wrapped)+ if (descLines.length > 0) {+ ctx.strokeStyle = COL_SEP;+ ctx.beginPath();+ ctx.moveTo(PADDING, y);+ ctx.lineTo(canvasWidth - PADDING, y);+ ctx.stroke();+ y += ROW_GAP;++ ctx.font = FONT_DESC;+ ctx.fillStyle = COL_DESC;+ ctx.textBaseline = 'top';+ descLines.forEach((line) => {+ ctx.fillText(line.text, PADDING, y);+ y += LINE_HEIGHT;+ });+ }++ // Mountconst el = document.createElement('div');el.classList.add('model-info');el.appendChild(canvas);@@ -172,6 +311,23 @@ export function createModelInfo(container, object, clips) {};}+function roundRect(ctx, x, y, w, h, r) {+ ctx.beginPath();+ ctx.moveTo(x + r, y);+ ctx.lineTo(x + w - r, y);+ ctx.quadraticCurveTo(x + w, y, x + w, y + r);+ ctx.lineTo(x + w, y + h - r);+ ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);+ ctx.lineTo(x + r, y + h);+ ctx.quadraticCurveTo(x, y + h, x, y + h - r);+ ctx.lineTo(x, y + r);+ ctx.quadraticCurveTo(x, y, x + r, y);+ ctx.closePath();+}+ },+ };+}+function roundRect(ctx, x, y, w, h, r) {ctx.beginPath();ctx.moveTo(x + r, y);