71 lines
1.7 KiB
JavaScript
71 lines
1.7 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;
|
|
|
|
|
|
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++;
|
|
}
|
|
});
|
|
|
|
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;
|
|
}
|