(skeleton, nodes, animTransforms)
| 138 | } |
| 139 | |
| 140 | function calculateBoneMatrices(skeleton, nodes, animTransforms) { |
| 141 | const { joints } = skeleton; |
| 142 | const boneCount = joints.length; |
| 143 | const localMatrices = new Array(boneCount); |
| 144 | const worldMatrices = new Array(boneCount); |
| 145 | const skinMatrices = new Array(boneCount); |
| 146 | |
| 147 | const processed = new Set(); |
| 148 | const processingOrder = []; |
| 149 | |
| 150 | function addJoint(jointIndex) { |
| 151 | if (processed.has(jointIndex)) return; |
| 152 | const joint = joints[jointIndex]; |
| 153 | if (joint.parentIndex >= 0 && !processed.has(joint.parentIndex)) { |
| 154 | addJoint(joint.parentIndex); |
| 155 | } |
| 156 | processingOrder.push(jointIndex); |
| 157 | processed.add(jointIndex); |
| 158 | } |
| 159 | |
| 160 | for (let i = 0; i < boneCount; i++) addJoint(i); |
| 161 | |
| 162 | for (const jointIndex of processingOrder) { |
| 163 | const joint = joints[jointIndex]; |
| 164 | const node = nodes[joint.nodeIndex]; |
| 165 | |
| 166 | if (!node) { |
| 167 | localMatrices[jointIndex] = identity(); |
| 168 | worldMatrices[jointIndex] = identity(); |
| 169 | skinMatrices[jointIndex] = identity(); |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | const animTransform = animTransforms.get(joint.nodeIndex); |
| 174 | const pos = animTransform?.position || node.transform.position; |
| 175 | const rot = animTransform?.rotation || node.transform.rotation; |
| 176 | const scl = animTransform?.scale || node.transform.scale; |
| 177 | |
| 178 | localMatrices[jointIndex] = createTransformMatrix(pos, rot, scl); |
| 179 | |
| 180 | if (joint.parentIndex >= 0) { |
| 181 | worldMatrices[jointIndex] = multiplyMatrices( |
| 182 | worldMatrices[joint.parentIndex], |
| 183 | localMatrices[jointIndex] |
| 184 | ); |
| 185 | } else { |
| 186 | worldMatrices[jointIndex] = localMatrices[jointIndex]; |
| 187 | } |
| 188 | |
| 189 | skinMatrices[jointIndex] = multiplyMatrices( |
| 190 | worldMatrices[jointIndex], |
| 191 | joint.inverseBindMatrix |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | return skinMatrices; |
| 196 | } |
| 197 |
no test coverage detected