|
| 1 | +import * as THREE from 'three'; |
| 2 | +import { VRMCore, VRMExpressionMorphTargetBind } from '@pixiv/three-vrm-core'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Traverse an entire tree and collect meshes. |
| 6 | + */ |
| 7 | +function collectMeshes(scene: THREE.Group): Set<THREE.Mesh> { |
| 8 | + const meshes = new Set<THREE.Mesh>(); |
| 9 | + |
| 10 | + scene.traverse((obj) => { |
| 11 | + if (!(obj as any).isMesh) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + const mesh = obj as THREE.Mesh; |
| 16 | + meshes.add(mesh); |
| 17 | + }); |
| 18 | + |
| 19 | + return meshes; |
| 20 | +} |
| 21 | + |
| 22 | +function combineMorph( |
| 23 | + positionAttributes: (THREE.BufferAttribute | THREE.InterleavedBufferAttribute)[], |
| 24 | + binds: Iterable<VRMExpressionMorphTargetBind>, |
| 25 | + morphTargetsRelative: boolean, |
| 26 | +): THREE.BufferAttribute { |
| 27 | + const newArray = new Float32Array(positionAttributes[0].count * 3); |
| 28 | + let weightSum = 0.0; |
| 29 | + |
| 30 | + if (morphTargetsRelative) { |
| 31 | + weightSum = 1.0; |
| 32 | + } else { |
| 33 | + for (const bind of binds) { |
| 34 | + weightSum += bind.weight; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + for (const bind of binds) { |
| 39 | + const src = positionAttributes[bind.index]; |
| 40 | + const weight = bind.weight / weightSum; |
| 41 | + |
| 42 | + for (let i = 0; i < src.count; i++) { |
| 43 | + newArray[i * 3 + 0] += src.getX(i) * weight; |
| 44 | + newArray[i * 3 + 1] += src.getY(i) * weight; |
| 45 | + newArray[i * 3 + 2] += src.getZ(i) * weight; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const newAttribute = new THREE.BufferAttribute(newArray, 3); |
| 50 | + return newAttribute; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * A map from expression names to a set of morph target binds. |
| 55 | + */ |
| 56 | +type NameBindSetMap = Map<string, Set<VRMExpressionMorphTargetBind>>; |
| 57 | + |
| 58 | +/** |
| 59 | + * Combine morph targets by VRM expressions. |
| 60 | + * |
| 61 | + * This function prevents crashes caused by the limitation of the number of morph targets, especially on mobile devices. |
| 62 | + * |
| 63 | + * @param vrm The VRM instance |
| 64 | + */ |
| 65 | +export function combineMorphs(vrm: VRMCore): void { |
| 66 | + const meshes = collectMeshes(vrm.scene); |
| 67 | + |
| 68 | + // Iterate over all expressions and check which morph targets are used |
| 69 | + const meshNameBindSetMapMap = new Map<THREE.Mesh, NameBindSetMap>(); |
| 70 | + |
| 71 | + const expressionMap = vrm.expressionManager?.expressionMap; |
| 72 | + if (expressionMap != null) { |
| 73 | + for (const [expressionName, expression] of Object.entries(expressionMap)) { |
| 74 | + const bindsToDeleteSet = new Set<VRMExpressionMorphTargetBind>(); |
| 75 | + for (const bind of expression.binds) { |
| 76 | + if (bind instanceof VRMExpressionMorphTargetBind) { |
| 77 | + if (bind.weight !== 0.0) { |
| 78 | + for (const mesh of bind.primitives) { |
| 79 | + let nameBindSetMap = meshNameBindSetMapMap.get(mesh); |
| 80 | + if (nameBindSetMap == null) { |
| 81 | + nameBindSetMap = new Map(); |
| 82 | + meshNameBindSetMapMap.set(mesh, nameBindSetMap); |
| 83 | + } |
| 84 | + |
| 85 | + let bindSet = nameBindSetMap.get(expressionName); |
| 86 | + if (bindSet == null) { |
| 87 | + bindSet = new Set(); |
| 88 | + nameBindSetMap.set(expressionName, bindSet); |
| 89 | + } |
| 90 | + |
| 91 | + bindSet.add(bind); |
| 92 | + } |
| 93 | + } |
| 94 | + bindsToDeleteSet.add(bind); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + for (const bind of bindsToDeleteSet) { |
| 99 | + expression.deleteBind(bind); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Combine morphs |
| 105 | + for (const mesh of meshes) { |
| 106 | + const nameBindSetMap = meshNameBindSetMapMap.get(mesh); |
| 107 | + if (nameBindSetMap == null) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + const geometry = mesh.geometry.clone(); |
| 112 | + mesh.geometry = geometry; |
| 113 | + const morphTargetsRelative = geometry.morphTargetsRelative; |
| 114 | + |
| 115 | + const hasPMorph = geometry.morphAttributes.position != null; |
| 116 | + const hasNMorph = geometry.morphAttributes.normal != null; |
| 117 | + |
| 118 | + const morphAttributes: typeof geometry.morphAttributes = {}; |
| 119 | + const morphTargetDictionary: typeof mesh.morphTargetDictionary = {}; |
| 120 | + const morphTargetInfluences: typeof mesh.morphTargetInfluences = []; |
| 121 | + |
| 122 | + if (hasPMorph || hasNMorph) { |
| 123 | + if (hasPMorph) { |
| 124 | + morphAttributes.position = []; |
| 125 | + } |
| 126 | + if (hasNMorph) { |
| 127 | + morphAttributes.normal = []; |
| 128 | + } |
| 129 | + |
| 130 | + let i = 0; |
| 131 | + for (const [name, bindSet] of nameBindSetMap) { |
| 132 | + if (hasPMorph) { |
| 133 | + morphAttributes.position[i] = combineMorph(geometry.morphAttributes.position, bindSet, morphTargetsRelative); |
| 134 | + } |
| 135 | + if (hasNMorph) { |
| 136 | + morphAttributes.normal[i] = combineMorph(geometry.morphAttributes.normal, bindSet, morphTargetsRelative); |
| 137 | + } |
| 138 | + |
| 139 | + expressionMap?.[name].addBind( |
| 140 | + new VRMExpressionMorphTargetBind({ |
| 141 | + index: i, |
| 142 | + weight: 1.0, |
| 143 | + primitives: [mesh], |
| 144 | + }), |
| 145 | + ); |
| 146 | + |
| 147 | + morphTargetDictionary[name] = i; |
| 148 | + morphTargetInfluences.push(0.0); |
| 149 | + |
| 150 | + i++; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + geometry.morphAttributes = morphAttributes; |
| 155 | + mesh.morphTargetDictionary = morphTargetDictionary; |
| 156 | + mesh.morphTargetInfluences = morphTargetInfluences; |
| 157 | + } |
| 158 | +} |
0 commit comments