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(Avatar): enhance AvatarPreview and CompanyLogo components with customizable class and background color

nirholas33466653f104
diff --git a/chat/src/App.svelte b/chat/src/App.svelte
index fb34afe59..1f2699efa 100644
--- a/chat/src/App.svelte
+++ b/chat/src/App.svelte
@@ -2346,6 +2346,7 @@
<KnobsSidebar {knobsOpen} {db} />
</div>
</main>
+{/if}
<SettingsModal
open={settingsModalOpen}
diff --git a/chat/src/AvatarIcon.svelte b/chat/src/AvatarIcon.svelte
new file mode 100644
index 000000000..aae841079
--- /dev/null
+++ b/chat/src/AvatarIcon.svelte
@@ -0,0 +1,128 @@
+<script context="module">
+ // Shared across all instances — render each avatar URL once per session
+ const cache = new Map();
+ const inflight = new Map();
+</script>
+
+<script>
+ import { onMount } from 'svelte';
+ import {
+ Scene,
+ PerspectiveCamera,
+ WebGLRenderer,
+ HemisphereLight,
+ DirectionalLight,
+ Box3,
+ Vector3,
+ SRGBColorSpace,
+ ACESFilmicToneMapping,
+ } from 'three';
+ import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
+
+ export let avatarUrl = '';
+
+ let dataUrl = cache.get(avatarUrl) ?? null;
+ let state = dataUrl ? 'done' : avatarUrl ? 'loading' : 'error';
+
+ onMount(() => {
+ if (state !== 'loading') return;
+ renderAvatar();
+ });
+
+ async function renderAvatar() {
+ if (cache.has(avatarUrl)) {
+ dataUrl = cache.get(avatarUrl);
+ state = 'done';
+ return;
+ }
+
+ // Deduplicate concurrent renders of the same URL
+ if (inflight.has(avatarUrl)) {
+ try {
+ dataUrl = await inflight.get(avatarUrl);
+ state = 'done';
+ } catch {
+ state = 'error';
+ }
+ return;
+ }
+
+ const promise = doRender(avatarUrl);
+ inflight.set(avatarUrl, promise);
+
+ try {
+ dataUrl = await promise;
+ state = 'done';
+ } catch {
+ state = 'error';
+ } finally {
+ inflight.delete(avatarUrl);
+ }
+ }
+
+ async function doRender(url) {
+ const resp = await fetch(url, { mode: 'cors' });
+ if (!resp.ok) throw new Error(resp.status);
+ const buffer = await resp.arrayBuffer();
+
+ const loader = new GLTFLoader();
+ const gltf = await new Promise((resolve, reject) =>
+ loader.parse(buffer, '', resolve, reject),
+ );
+
+ const scene = new Scene();
+ scene.add(gltf.scene);
+
+ const box = new Box3().setFromObject(gltf.scene);
+ const center = box.getCenter(new Vector3());
+ const boxSize = box.getSize(new Vector3());
+
+ // Frame the face — upper ~35% of the avatar's bounding box
+ const faceY = center.y + boxSize.y * 0.32;
+ const faceHeight = boxSize.y * 0.28;
+ const fov = 22;
+ const dist = faceHeight / (2 * Math.tan(((Math.PI / 180) * fov) / 2));
+
+ const camera = new PerspectiveCamera(fov, 1, 0.01, 1000);
+ camera.position.set(center.x, faceY, center.z + dist * 1.2);
+ camera.lookAt(center.x, faceY, center.z);
+
+ scene.add(new HemisphereLight(0xffffff, 0x444444, 2.0));
+ const dir = new DirectionalLight(0xffffff, 2.5);
+ dir.position.set(3, 10, 7);
+ scene.add(dir);
+
+ const offscreen = document.createElement('canvas');
+ offscreen.width = 128;
+ offscreen.height = 128;
+ const renderer = new WebGLRenderer({ canvas: offscreen, antialias: true, alpha: true });
+ renderer.setPixelRatio(1);
+ renderer.setSize(128, 128, false);
+ renderer.outputColorSpace = SRGBColorSpace;
+ renderer.toneMapping = ACESFilmicToneMapping;
+ renderer.toneMappingExposure = 1.0;
+ renderer.render(scene, camera);
+
+ const result = offscreen.toDataURL('image/png');
+ cache.set(url, result);
+
+ renderer.dispose();
+ scene.traverse((obj) => {
+ obj.geometry?.dispose();
+ const mats = Array.isArray(obj.material)
+ ? obj.material
+ : obj.material
+ ? [obj.material]
+ : [];
+ for (const m of mats) m.dispose();
+ });
+
+ return result;
+ }
+</script>
+
+{#if state === 'done' && dataUrl}
+ <img src={dataUrl} alt="" class="h-full w-full object-cover" />
+{:else if state === 'loading'}
+ <div class="h-full w-full animate-pulse bg-teal-100" />
+{/if}
diff --git a/chat/src/AvatarPreview.svelte b/chat/src/AvatarPreview.svelte
index 44017a1cf..322188782 100644
--- a/chat/src/AvatarPreview.svelte
+++ b/chat/src/AvatarPreview.svelte
@@ -18,6 +18,8 @@
export let model_url = null;
export let alt = '';
export let size = 128;
+ export let class_name = 'h-16 w-full rounded object-cover';
+ export let bg_color = '#e8e8e8';
let canvas;
let state = thumbnail_url ? 'done' : model_url ? 'idle' : 'none';
@@ -54,7 +56,7 @@
);
const scene = new Scene();
- scene.background = new Color('#e8e8e8');
+ scene.background = new Color(bg_color);
scene.add(gltf.scene);
const box = new Box3().setFromObject(gltf.scene);
@@ -100,9 +102,9 @@
</script>
{#if thumbnail_url}
- <img src={thumbnail_url} {alt} class="h-16 w-full rounded object-cover" loading="lazy" />
+ <img src={thumbnail_url} {alt} class={class_name} loading="lazy" />
{:else if state === 'none'}
- <div class="flex h-16 w-full items-center justify-center rounded bg-slate-100 text-[10px] text-slate-400">
+ <div class="{class_name} flex items-center justify-center bg-slate-100 text-[10px] text-slate-400">
No preview
</div>
{:else}
@@ -110,10 +112,10 @@
bind:this={canvas}
width={size}
height={size}
- class="h-16 w-full rounded object-cover {state === 'loading' ? 'animate-pulse bg-slate-100' : ''} {state === 'error' ? 'hidden' : ''}"
+ class="{class_name} {state === 'loading' ? 'animate-pulse bg-slate-100' : ''} {state === 'error' ? 'hidden' : ''}"
/>
{#if state === 'error'}
- <div class="flex h-16 w-full items-center justify-center rounded bg-slate-100 text-[10px] text-slate-400">
+ <div class="{class_name} flex items-center justify-center bg-slate-100 text-[10px] text-slate-400">
No preview
</div>
{/if}
diff --git a/chat/src/CompanyLogo.svelte b/chat/src/CompanyLogo.svelte
index 86a1f7390..aa2e7688d 100644
--- a/chat/src/CompanyLogo.svelte
+++ b/chat/src/CompanyLogo.svelte
@@ -1,4 +1,7 @@
<script>
+ import { talkingHeadAvatarUrl } from './stores.js';
+ import AvatarIcon from './AvatarIcon.svelte';
+
export let model;
export let size = 'h-3.5 w-3.5';
export let rounded = null;
@@ -7,7 +10,11 @@
const base = import.meta.env.BASE_URL;
</script>
-{#if model && model.provider}
+{#if $talkingHeadAvatarUrl}
+ <span class="{size} {rounded || 'rounded-sm'} overflow-hidden inline-block shrink-0">
+ <AvatarIcon avatarUrl={$talkingHeadAvatarUrl} />
+ </span>
+{:else if model && model.provider}
{#if model.provider === 'OpenAI' || model.id.startsWith('openai')}
<img src="{base}logos/openai.ico" loading="lazy" class="{size} {rounded || 'rounded-sm'}" alt="" />
{:else if model.provider === 'Anthropic' || model.id.startsWith('anthropic')}
diff --git a/chat/vite.config.js b/chat/vite.config.js
index 86a4b9d35..cfe303ff5 100644
--- a/chat/vite.config.js
+++ b/chat/vite.config.js
@@ -15,6 +15,7 @@ export default defineConfig(function () {
// Forward /api requests to the 3D-Agent dev server (vercel dev on port 3000)
proxy: {
'/api': { target: 'http://localhost:3000', changeOrigin: true },
+ '/avatars': { target: 'http://localhost:3000', changeOrigin: true },
},
},
plugins: [
diff --git a/public/chat/index.html b/public/chat/index.html
index eb0697ddc..6ac5d72c2 100644
--- a/public/chat/index.html
+++ b/public/chat/index.html
@@ -43,8 +43,8 @@
}
}
</script>
- <script type="module" crossorigin src="/chat/assets/index-BTEzDLIq.js"></script>
- <link rel="stylesheet" crossorigin href="/chat/assets/index-CgrzNPtI.css">
+ <script type="module" crossorigin src="/chat/assets/index-ldwhlKTn.js"></script>
+ <link rel="stylesheet" crossorigin href="/chat/assets/index-DyRom_PJ.css">
</head>
<body>
<div id="app"></div>