Skip to content

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.

feat: add annotation functionality for 3D meshes

nirholas60f0c5ed84e2
diff --git a/README.md b/README.md
index 7cd6a2450..e32c3e2fa 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ description: 3D Agent** is an open-source, browser-native 3D model viewer built
---
<p align="center">
- <img src="public/ddd.png" width="500" height="500">
+ <img src="public/3d.png" width="500" height="500">
</p>
<p align="center">
diff --git a/public/3d.png b/public/3d.png
new file mode 100644
index 000000000..0db6e1c8f
Binary files /dev/null and b/public/3d.png differ
diff --git a/src/annotations.js b/src/annotations.js
new file mode 100644
index 000000000..0b7fe8b27
--- /dev/null
+++ b/src/annotations.js
@@ -0,0 +1,152 @@
+import { prepare, measureLineStats } from '@chenglou/pretext';
+import {
+ prepareRichInline,
+ walkRichInlineLineRanges,
+ materializeRichInlineLineRange,
+} from '@chenglou/pretext/rich-inline';
+import { Vector3, Box3 } from 'three';
+
+const FONT_NAME = '500 11px Inter, sans-serif';
+const FONT_DETAIL = '300 10px Inter, sans-serif';
+const LABEL_PAD_X = 10;
+const LABEL_PAD_Y = 6;
+const LINE_HEIGHT = 14;
+const DPR = Math.min(window.devicePixelRatio || 1, 2);
+const MAX_LABELS = 8;
+
+/**
+ * Build annotation data for key meshes in the scene.
+ * Uses pretext rich-inline to render mixed-style labels (name + type).
+ */
+export function buildAnnotations(object) {
+ const annotations = [];
+
+ object.traverse((node) => {
+ if (annotations.length >= MAX_LABELS) return;
+ if (!node.isMesh) return;
+ if (!node.name || node.name.startsWith('_')) return;
+
+ const geo = node.geometry;
+ const mat = Array.isArray(node.material) ? node.material[0] : node.material;
+
+ const vertCount = geo && geo.attributes.position ? geo.attributes.position.count : 0;
+ const matType = mat ? mat.type.replace('Material', '') : '';
+
+ // Rich inline: mesh name (bold) + vertex count (light)
+ const items = [
+ { text: node.name, font: FONT_NAME },
+ ];
+ if (vertCount > 0) {
+ items.push({ text: ' ' + formatShort(vertCount) + ' verts', font: FONT_DETAIL });
+ }
+ if (matType) {
+ items.push({ text: ' ' + matType, font: FONT_DETAIL });
+ }
+
+ // Measure with pretext
+ const prepared = prepareRichInline(items);
+ let totalWidth = 0;
+ let lineCount = 0;
+ walkRichInlineLineRanges(prepared, 300, (range) => {
+ const line = materializeRichInlineLineRange(prepared, range);
+ totalWidth = Math.max(totalWidth, line.width);
+ lineCount++;
+ });
+
+ const labelWidth = totalWidth + LABEL_PAD_X * 2;
+ const labelHeight = lineCount * LINE_HEIGHT + LABEL_PAD_Y * 2;
+
+ // Get world position from bounding box center
+ if (geo) geo.computeBoundingBox();
+ const box = geo ? geo.boundingBox : null;
+ const pos = new Vector3();
+ if (box) {
+ box.getCenter(pos);
+ pos.applyMatrix4(node.matrixWorld);
+ } else {
+ node.getWorldPosition(pos);
+ }
+
+ annotations.push({
+ name: node.name,
+ items,
+ prepared,
+ labelWidth,
+ labelHeight,
+ position: pos,
+ vertCount,
+ matType,
+ });
+ });
+
+ return annotations;
+}
+
+/**
+ * Render a single annotation label to a canvas element.
+ */
+export function renderAnnotationCanvas(annotation) {
+ const { labelWidth, labelHeight, items, prepared } = annotation;
+
+ const canvas = document.createElement('canvas');
+ canvas.width = labelWidth * DPR;
+ canvas.height = labelHeight * DPR;
+ canvas.style.width = labelWidth + 'px';
+ canvas.style.height = labelHeight + 'px';
+
+ const ctx = canvas.getContext('2d');
+ ctx.scale(DPR, DPR);
+
+ // Background
+ ctx.fillStyle = 'rgba(0, 0, 0, 0.8)';
+ roundRect(ctx, 0, 0, labelWidth, labelHeight, 6);
+ ctx.fill();
+
+ // Border
+ ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
+ ctx.lineWidth = 1;
+ roundRect(ctx, 0.5, 0.5, labelWidth - 1, labelHeight - 1, 6);
+ ctx.stroke();
+
+ // Render rich inline text
+ ctx.textBaseline = 'top';
+ let y = LABEL_PAD_Y;
+
+ walkRichInlineLineRanges(prepared, labelWidth - LABEL_PAD_X * 2, (range) => {
+ const line = materializeRichInlineLineRange(prepared, range);
+ let x = LABEL_PAD_X;
+
+ line.fragments.forEach((frag) => {
+ x += frag.gapBefore;
+ const item = items[frag.itemIndex];
+ ctx.font = item.font;
+ ctx.fillStyle = frag.itemIndex === 0 ? 'rgba(255, 255, 255, 0.9)' : 'rgba(255, 255, 255, 0.45)';
+ ctx.fillText(frag.text, x, y);
+ x += frag.occupiedWidth;
+ });
+
+ y += LINE_HEIGHT;
+ });
+
+ return canvas;
+}
+
+function formatShort(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 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();
+}
diff --git a/src/model-info.js b/src/model-info.js
index 60061b666..4ddb9be4a 100644
--- a/src/model-info.js
+++ b/src/model-info.js
@@ -311,23 +311,6 @@ 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);
diff --git a/src/viewer.js b/src/viewer.js
index 993aeeb6a..f83e12343 100644
--- a/src/viewer.js
+++ b/src/viewer.js
@@ -34,6 +34,7 @@ import { GUI } from 'dat.gui';
import { environments } from './environments.js';
import { createModelInfo } from './model-info.js';
+import { buildAnnotations, renderAnnotationCanvas } from './annotations.js';
const DEFAULT_CAMERA = '[default]';
@@ -91,6 +92,7 @@ export class Viewer {
// Info overlay
showInfo: true,
+ showLabels: false,
};
this.prevTime = 0;
@@ -135,6 +137,7 @@ export class Viewer {
this.gridHelper = null;
this.axesHelper = null;
this.modelInfo = null;
+ this.annotationEls = [];
this.addAxesHelper();
this.addGUI();
@@ -173,6 +176,7 @@ export class Viewer {
this.axesCamera.lookAt(this.axesScene.position);
this.axesRenderer.render(this.axesScene, this.axesCamera);
}
+ this.projectAnnotations();
}
takeScreenshot() {
@@ -340,6 +344,7 @@ export class Viewer {
this.updateEnvironment();
this.updateDisplay();
this.updateModelInfo(object, clips);
+ this.updateAnnotations();
window.VIEWER.scene = this.content;
@@ -541,6 +546,53 @@ export class Viewer {
}
}
+ updateAnnotations() {
+ // Clear existing
+ this.annotationEls.forEach((a) => a.el.remove());
+ this.annotationEls = [];
+
+ if (!this.state.showLabels || !this.content) return;
+
+ const annotations = buildAnnotations(this.content);
+
+ annotations.forEach((ann) => {
+ const canvas = renderAnnotationCanvas(ann);
+ const el = document.createElement('div');
+ el.classList.add('annotation-label');
+ el.appendChild(canvas);
+ this.el.appendChild(el);
+ this.annotationEls.push({ el, position: ann.position });
+ });
+ }
+
+ projectAnnotations() {
+ if (this.annotationEls.length === 0) return;
+
+ const width = this.el.clientWidth;
+ const height = this.el.clientHeight;
+ const halfW = width / 2;
+ const halfH = height / 2;
+ const tempVec = new Vector3();
+
+ this.annotationEls.forEach(({ el, position }) => {
+ tempVec.copy(position);
+ tempVec.project(this.activeCamera);
+
+ // Behind camera check
+ if (tempVec.z > 1) {
+ el.style.display = 'none';
+ return;
+ }
+
+ const x = (tempVec.x * halfW) + halfW;
+ const y = -(tempVec.y * halfH) + halfH;
+
+ el.style.display = '';
+ el.style.left = x + 'px';
+ el.style.top = y + 'px';
+ });
+ }
+
addAxesHelper() {
this.axesDiv = document.createElement('div');
this.el.appendChild(this.axesDiv);
@@ -591,6 +643,8 @@ export class Viewer {
dispFolder.add({ screenshot: () => this.takeScreenshot() }, 'screenshot').name('Screenshot [P]');
const infoCtrl = dispFolder.add(this.state, 'showInfo').name('model info');
infoCtrl.onChange(() => this.updateModelInfo(this.content, this.clips));
+ const labelsCtrl = dispFolder.add(this.state, 'showLabels').name('mesh labels');
+ labelsCtrl.onChange(() => this.updateAnnotations());
// Lighting controls.
const lightFolder = gui.addFolder('Lighting');
@@ -738,6 +792,9 @@ export class Viewer {
this.modelInfo = null;
}
+ this.annotationEls.forEach((a) => a.el.remove());
+ this.annotationEls = [];
+
this.scene.remove(this.content);
// dispose geometry
diff --git a/style.css b/style.css
index f071f24d8..efcfc6737 100644
--- a/style.css
+++ b/style.css
@@ -124,6 +124,15 @@ body {
animation: fadeInUp 0.4s ease 0.3s forwards;
}
+.annotation-label {
+ position: absolute;
+ z-index: 4;
+ pointer-events: none;
+ transform: translate(-50%, -100%) translateY(-8px);
+ opacity: 0;
+ animation: fadeInUp 0.3s ease 0.5s forwards;
+}
+
@keyframes fadeInUp {
to {
opacity: 1;
@@ -319,6 +328,11 @@ header h1 > a {
}
}
+ .annotation-label {
+ transform: scale(0.8) translate(-50%, -100%) translateY(-6px);
+ transform-origin: center bottom;
+ }
+
.report {
padding: var(--space-md);
max-width: 100vw;