Spaces:
Paused
Paused
File size: 14,247 Bytes
0008f70 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | // @ts-check
/**
* The avatar stage: a TalkingHead 3D avatar with real-time audio-driven
* lip-sync (HeadAudio), plus static model fallback for custom GLBs.
*
* WHY CUSTOM GLBS FAIL (ROOT CAUSE):
* TalkingHead.showAvatar() expects morph targets (ARKit viseme blend shapes)
* and a Mixamo-compatible skeleton. Most downloaded GLB models (Sketchfab,
* free 3D scans, etc.) are STATIC β no morph targets, no skeleton.
* showAvatar() throws and the model never renders.
*
* FIX: Two-phase loading:
* Phase 1 β Try showAvatar() (works for ReadyPlayerMe, morph-target models)
* Phase 2 β On failure, load GLB directly via THREE.GLTFLoader as a static
* model in the TalkingHead scene. No lip-sync, but the model
* renders correctly with proper lighting and camera.
*/
import { TalkingHead } from "@met4citizen/talkinghead";
import { HeadAudio } from "./vendor/headaudio.min.mjs";
import { Box3, Vector3, HemisphereLight, DirectionalLight } from "three";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
const HEADAUDIO_WORKLET_URL = "/vendor/headworklet.min.mjs";
const HEADAUDIO_MODEL_URL = "/vendor/model-en-mixed.bin";
export const AVATAR_MOODS = [
"neutral", "happy", "angry", "sad", "fear", "disgust", "love", "sleep",
];
export const AVATAR_GESTURES = [
"handup", "index", "ok", "thumbup", "thumbdown", "side", "shrug",
];
const HEAD_CONFIG = {
ttsEndpoint: "N/A",
lipsyncModules: [],
cameraView: "upper",
cameraDistance: -1.4,
cameraY: -0.15,
cameraX: -0.18,
cameraRotateEnable: false,
lightAmbientIntensity: 0,
lightDirectIntensity: 0,
lightSpotIntensity: 0,
avatarIdleEyeContact: 0.3,
avatarSpeakingEyeContact: 0.7,
};
/** Fully dispose all Three.js + AudioContext resources. */
function _fullyDispose(head) {
try {
if (head.scene) {
head.scene.traverse((obj) => {
if (obj.isMesh) {
obj.geometry?.dispose();
if (obj.material) {
(Array.isArray(obj.material) ? obj.material : [obj.material]).forEach((m) => m.dispose());
}
}
});
}
while (head.scene?.children?.length) head.scene.remove(head.scene.children[0]);
if (head.renderer) {
head.renderer.dispose();
const canvas = head.renderer.domElement;
if (canvas?.parentNode) canvas.parentNode.removeChild(canvas);
}
if (head.audioCtx && head.audioCtx.state !== "closed") {
head.audioCtx.close().catch(() => {});
}
head.dispose?.();
} catch (e) {
console.warn("[AvatarStage] dispose:", e);
}
}
export class AvatarStage {
/** @param {HTMLElement} container */
constructor(container) {
this._container = container;
/** @type {TalkingHead | null} */
this.head = null;
/** @type {any | null} */
this._headaudio = null;
this._lastSpeechEnded = 0;
this._loadingEl = document.getElementById("loading");
this._lastError = null;
/** @type {boolean} True when model is static (no morph targets) */
this._staticModel = false;
/** @type {THREE.Object3D | null} */
this._modelRoot = null;
/** @type {THREE.Light[]} */
this._staticLights = [];
}
/**
* Initialize the avatar. Returns true on success, false on failure.
* Two-phase: tries showAvatar() first, falls back to static GLTF loading.
*/
async init(opt = {}) {
// ββ FULL DISPOSE ββββββββββββββββββββββββββββββββββββββββββββββββββ
if (this.head) {
this._removeStaticLights();
_fullyDispose(this.head);
this.head = null;
this._headaudio = null;
this._modelRoot = null;
this._staticModel = false;
}
while (this._container.firstChild) this._container.removeChild(this._container.firstChild);
this._lastError = null;
const isDefault = !opt.avatarUrl;
this.head = new TalkingHead(this._container, {
...HEAD_CONFIG,
cameraDistance: isDefault ? HEAD_CONFIG.cameraDistance : -1.25,
cameraY: isDefault ? HEAD_CONFIG.cameraY : 0.0,
cameraX: isDefault ? HEAD_CONFIG.cameraX : 0.0,
});
console.log("[AvatarStage] Loading:", opt.avatarUrl ?? "default");
// ββ Phase 1: Try showAvatar() ββββββββββββββββββββββββββββββββββββ
let morphOk = false;
if (isDefault) {
try {
await this._loadShowAvatar(opt);
morphOk = true;
} catch (err) {
this._lastError = err?.message || String(err);
console.error("[AvatarStage] Default showAvatar FAILED:", this._lastError);
_fullyDispose(this.head);
this.head = null;
return false;
}
} else {
try {
await this._loadShowAvatar(opt);
morphOk = true;
console.log("[AvatarStage] Custom model loaded WITH morph targets");
} catch (err) {
console.warn("[AvatarStage] showAvatar failed, trying static:", err?.message);
}
}
// ββ Phase 2: Static fallback for custom models ββββββββββββββββββββ
if (!morphOk) {
try {
await this._loadStaticModel(/** @type {string} */ (opt.avatarUrl), opt);
this._staticModel = true;
this._addStaticLights();
console.log("[AvatarStage] Custom model loaded as STATIC");
} catch (err2) {
this._lastError = err2?.message || String(err2);
console.error("[AvatarStage] Static ALSO failed:", this._lastError);
_fullyDispose(this.head);
this.head = null;
return false;
}
}
// ββ Camera / BBox adjust βββββββββββββββββββββββββββββββββββββββββ
this._adjustCamera(opt.avatarUrl);
// ββ Lipsync (morph-target models only) βββββββββββββββββββββββββββ
if (!this._staticModel) {
try { await this._initLipsync(); } catch (err) { console.warn("[AvatarStage] Lipsync:", err); }
} else {
console.log("[AvatarStage] Static model β lipsync skipped");
}
return true;
}
/** Load via TalkingHead showAvatar (morph-target models) */
async _loadShowAvatar(opt) {
const head = /** @type {TalkingHead} */ (this.head);
await head.showAvatar(
{ url: opt.avatarUrl ?? "/avatars/vuong.glb", body: opt.body ?? "F", avatarMood: "neutral" },
(ev) => {
if (ev.lengthComputable) {
const pct = Math.min(100, Math.round((ev.loaded / ev.total) * 100));
if (this._loadingEl) this._loadingEl.textContent = `Loading avatar ${pct}%`;
}
opt.onprogress?.(ev);
},
);
}
/** Load GLB as a static model via THREE.GLTFLoader */
_loadStaticModel(url, opt) {
return new Promise((resolve, reject) => {
const loader = new GLTFLoader();
const head = /** @type {TalkingHead} */ (this.head);
loader.load(
url,
(gltf) => {
const model = gltf.scene;
this._modelRoot = model;
// Remove any default placeholder from scene
for (let i = head.scene.children.length - 1; i >= 0; i--) {
const c = head.scene.children[i];
if (c !== head.camera) head.scene.remove(c);
}
// Center the model visually
model.visible = true;
head.scene.add(model);
if (this._loadingEl) this._loadingEl.textContent = "Positioning model...";
opt?.onprogress?.({ lengthComputable: true, loaded: 1, total: 1 });
resolve();
},
(ev) => {
if (ev.lengthComputable) {
const pct = Math.min(100, Math.round((ev.loaded / ev.total) * 100));
if (this._loadingEl) this._loadingEl.textContent = `Loading model ${pct}%`;
}
opt?.onprogress?.(ev);
},
(err) => reject(new Error(`GLTF load: ${err?.message || err}`)),
);
});
}
/** Add lighting for static models (TalkingHead lights are disabled at intensity 0) */
_addStaticLights() {
this._removeStaticLights();
const head = /** @type {TalkingHead} */ (this.head);
const lights = [
new HemisphereLight(0xffffff, 0x444444, 0.7),
new DirectionalLight(0xffffff, 0.9),
];
lights[1].position.set(5, 8, 5);
const fill = new DirectionalLight(0x8888ff, 0.3);
fill.position.set(-3, 2, -4);
lights.push(fill);
for (const l of lights) { head.scene.add(l); this._staticLights.push(l); }
console.log("[AvatarStage] Added", lights.length, "lights for static model");
}
_removeStaticLights() {
for (const l of this._staticLights) {
l.parent?.remove(l);
l.dispose?.();
}
this._staticLights = [];
}
/** Auto-adjust camera + model position based on bounding box */
_adjustCamera(avatarUrl) {
const head = /** @type {TalkingHead} */ (this.head);
if (!head.scene || !head.camera) return;
if (!avatarUrl && !this._staticModel) {
// Default morph model: just update projection
requestAnimationFrame(() => {
try { head.renderer?.render(head.scene, head.camera); head.camera.updateProjectionMatrix?.(); } catch {}
});
return;
}
try {
const box = new Box3().setFromObject(head.scene);
const size = box.getSize(new Vector3());
const center = box.getCenter(new Vector3());
console.log("[AvatarStage] BBox:", { size, center });
if (size.x === 0 && size.y === 0 && size.z === 0) {
console.warn("[AvatarStage] Empty bbox, skipping");
return;
}
if (size.y > 2.0) {
// Full-body: shift up + zoom out
head.scene.position.y = -(center.y - size.y * 0.35);
head.camera.position.z = -Math.max(size.z, size.y, 1.0) * 1.2;
head.camera.position.y = size.y * 0.3;
console.log("[AvatarStage] Full-body adjusted");
} else if (size.y > 0.3) {
// Head/shoulder: center
head.camera.position.z = -Math.max(size.z, 0.5) * 2.5;
head.camera.position.y = center.y * 0.5;
console.log("[AvatarStage] Head model adjusted");
}
requestAnimationFrame(() => {
try { head.renderer?.render(head.scene, head.camera); head.camera.updateProjectionMatrix?.(); } catch {}
});
box.dispose();
} catch (e) {
console.warn("[AvatarStage] Camera adjust:", e);
}
}
async _initLipsync() {
const head = /** @type {TalkingHead} */ (this.head);
if (!head.audioCtx) { console.warn("[AvatarStage] No AudioContext"); return; }
try { await head.audioCtx.audioWorklet.addModule(HEADAUDIO_WORKLET_URL); } catch (e) { console.warn("[AvatarStage] Worklet:", e); return; }
const headaudio = new HeadAudio(head.audioCtx);
await headaudio.loadModel(HEADAUDIO_MODEL_URL);
if (!head.audioSpeechGainNode) { console.warn("[AvatarStage] No audioSpeechGainNode"); return; }
head.audioSpeechGainNode.connect(headaudio);
headaudio.onvalue = (key, value) => {
const mt = head.mtAvatar?.[key];
if (mt) Object.assign(mt, { newvalue: value, needsUpdate: true });
};
head.opt.update = headaudio.update.bind(headaudio);
headaudio.onended = () => { this._lastSpeechEnded = Date.now(); };
headaudio.onstarted = () => {
if (Date.now() - this._lastSpeechEnded > 150) { head.lookAtCamera(500); head.speakWithHands(); }
};
this._headaudio = headaudio;
}
get audioCtx() { return this.head?.audioCtx ?? null; }
get voiceSink() { return this.head?.audioAnalyzerNode ?? null; }
get lastError() { return this._lastError; }
get isStaticModel() { return this._staticModel; }
/**
* Capture the current WebGL frame as a PNG data URL.
* Re-renders one frame then reads the buffer immediately (no await in
* between) so it works even when preserveDrawingBuffer is false.
* @returns {string|null} PNG data URL or null on failure.
*/
capturePNG() {
const head = this.head;
if (!head || !head.renderer || !head.scene || !head.camera) return null;
try {
head.renderer.render(head.scene, head.camera);
const canvas = head.renderer.domElement;
return canvas.toDataURL("image/png");
} catch (e) {
console.warn("[AvatarStage] capturePNG:", e);
return null;
}
}
/**
* Start the avatar animation loop and try to resume AudioContext.
* Returns a Promise that resolves when AudioContext resumes (if called
* from a user-gesture handler) or never (if called from boot).
*/
resume() {
this.head?.start();
if (this.head && this.head.audioCtx.state === "suspended") {
return this.head.audioCtx.resume().catch(() => {});
}
return Promise.resolve();
}
setConversationState(status) {
const h = this.head;
if (!h) return;
switch (status) {
case "user-speaking": h.isSpeaking = false; h.lookAtCamera(800); break;
case "ai-speaking": h.isSpeaking = true; break;
case "processing": h.isSpeaking = false; break;
case "closed": case "error": case "idle": h.isSpeaking = false; h.stopGesture(300); break;
default: h.isSpeaking = false;
}
}
runTool(name, args) {
const h = this.head;
if (!h) return null;
if (this._staticModel) {
if (name === "set_mood") return "Mood unavailable (static model)";
if (name === "make_hand_gesture") return "Gestures unavailable (static model)";
if (name === "make_facial_expression") return "Expressions unavailable (static model)";
}
if (name === "set_mood") {
if (!AVATAR_MOODS.includes(args.mood)) return `Unknown mood: ${args.mood}`;
h.setMood(args.mood); return `Mood set to ${args.mood}.`;
}
if (name === "make_hand_gesture") {
if (!AVATAR_GESTURES.includes(args.gesture)) return `Unknown gesture: ${args.gesture}`;
h.playGesture(args.gesture, 3); return `Playing gesture ${args.gesture}.`;
}
if (name === "make_facial_expression") {
if (!args.emoji) return "No emoji given.";
h.speakEmoji(args.emoji); return `Expressing ${args.emoji}.`;
}
return null;
}
} |