Files
a2f-service/examples/3d/babylonAdapter.js
2025-12-24 15:15:13 +08:00

77 lines
2.0 KiB
JavaScript

// Babylon.js 形态键适配器
class BabylonMorphTargetAdapter {
constructor() {
this.morphTargetCache = {};
}
buildCache(meshes) {
this.morphTargetCache = {};
let totalTargets = 0;
meshes.forEach(mesh => {
const mtm = mesh.morphTargetManager;
if (!mtm) return;
console.log(`网格 ${mesh.name}: ${mtm.numTargets} 个形态键`);
for (let i = 0; i < mtm.numTargets; i++) {
const mt = mtm.getTarget(i);
if (!mt?.name) continue;
const lowerName = mt.name.toLowerCase();
if (!this.morphTargetCache[lowerName]) {
this.morphTargetCache[lowerName] = [];
}
this.morphTargetCache[lowerName].push(mt);
totalTargets++;
if (i < 3) {
console.log(` ${mt.name} -> ${lowerName}`);
}
}
});
console.log(`总计: ${totalTargets} 个形态键映射`);
return totalTargets;
}
setInfluence(name, value) {
const lowerName = name.toLowerCase();
const targets = this.morphTargetCache[lowerName];
if (targets?.length) {
targets.forEach(mt => {
mt.influence = value;
});
}
}
getInfluence(name) {
const lowerName = name.toLowerCase();
const targets = this.morphTargetCache[lowerName];
if (targets?.length) {
return targets[0].influence;
}
return 0;
}
resetAll() {
for (const targets of Object.values(this.morphTargetCache)) {
targets.forEach(mt => {
mt.influence = 0;
});
}
}
getCacheSize() {
return Object.keys(this.morphTargetCache).length;
}
}
// 导出到全局
if (typeof window !== 'undefined') {
window.BabylonMorphTargetAdapter = BabylonMorphTargetAdapter;
}