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.

1.2 KB
Raw Blame
// Apply server-persisted material overrides to a loaded Three.js model.
// Patch shape: { [materialName]: { color, metalness, roughness, emissive } }
// Only fields present in the patch override; everything else stays from the GLB.

import { traverseMaterials } from '../viewer/internal.js';

export function applyMaterialOverrides(model, overrides) {
	if (!model || !overrides || typeof overrides !== 'object') return;
	traverseMaterials(model, (mat) => {
		const p = overrides[mat.name];
		if (!p) return;
		if (p.color !== undefined) mat.color.set(p.color);
		if (p.metalness !== undefined) mat.metalness = Number(p.metalness);
		if (p.roughness !== undefined) mat.roughness = Number(p.roughness);
		if (p.emissive !== undefined) mat.emissive.set(p.emissive);
		mat.needsUpdate = true;
	});
}

export function listMaterials(model) {
	const result = [];
	if (!model) return result;
	traverseMaterials(model, (mat) => {
		if (mat.isMeshStandardMaterial || mat.isMeshPhysicalMaterial) {
			result.push({
				name: mat.name,
				color: '#' + mat.color.getHexString(),
				metalness: mat.metalness,
				roughness: mat.roughness,
				emissive: '#' + mat.emissive.getHexString(),
			});
		}
	});
	return result;
}