MCPcopy Create free account
hub / github.com/esengine/esengine / calculateBoneMatrices

Function calculateBoneMatrices

scripts/test-animation-times.mjs:140–196  ·  view source on GitHub ↗
(skeleton, nodes, animTransforms)

Source from the content-addressed store, hash-verified

138}
139
140function 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

Callers 1

mainFunction · 0.70

Calls 5

addJointFunction · 0.70
identityFunction · 0.70
createTransformMatrixFunction · 0.70
multiplyMatricesFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected