15#include <glm/gtc/matrix_transform.hpp>
16#include <glm/gtc/quaternion.hpp>
18#define GLM_ENABLE_EXPERIMENTAL
19#include <glm/gtx/quaternion.hpp>
56 animator.activeAnimationIndex = parentAnimator->activeAnimationIndex;
57 animator.currentTime = parentAnimator->currentTime;
58 animator.playbackSpeed = parentAnimator->playbackSpeed;
59 animator.loop = parentAnimator->loop;
60 animator.isPreviewing = parentAnimator->isPreviewing;
69 if (!animator->animations.empty()) {
74 controller.currentState = parentController->currentState;
75 controller.currentStateTime = parentController->currentStateTime;
76 controller.fromState = parentController->fromState;
77 controller.fromStateTime = parentController->fromStateTime;
78 controller.crossfadeProgress = parentController->crossfadeProgress;
79 controller.crossfadeDuration = parentController->crossfadeDuration;
80 controller.isCrossfading = parentController->isCrossfading;
81 controller.parameters = parentController->parameters;
93 Entity entity = controllerEntities[idx];
94 auto* controller = registry.get<AnimationControllerComponent>(entity);
95 auto* animator = registry.get<AnimatorComponent>(entity);
96 if (controller && animator) {
97 float entityDt = (editorMode.isPlaying || animator->isPreviewing || animator->playbackSpeed > 0.0f) ? dt : 0.0f;
98 updateController(*controller, *animator, entityDt);
105 if (!skeleton.joints.empty()) {
111 Entity entity = animatedEntities[idx];
112 auto* skeleton = registry.get<SkeletonComponent>(entity);
113 auto* animator = registry.get<AnimatorComponent>(entity);
114 if (skeleton && animator) {
115 float entityDt = (editorMode.isPlaying || animator->isPreviewing || animator->playbackSpeed > 0.0f) ? dt : 0.0f;
116 updateEntityAnimation(entity, *skeleton, *animator, entityDt);
123 if (!skeleton || skeleton->joints.empty()) {
124 float entityDt = (editorMode.isPlaying || animator.isPreviewing || animator.playbackSpeed > 0.0f) ? dt : 0.0f;
125 updateGenericAnimation(entity, animator, entityDt);
144 for (
const auto& state : controller.
states) {
158 for (
size_t i = 0; i < animator.
animations.size(); ++i) {
160 clipIdx =
static_cast<int>(i);
165 if (clipIdx == -1 && !animator.
animations.empty()) {
172 float effectiveDur = clip.duration;
173 for (
const auto& chan : clip.propertyChannels) {
174 if (!chan.keys.empty()) {
175 effectiveDur = std::max(effectiveDur, chan.keys.back().time);
178 if (effectiveDur <= 0.0f) effectiveDur = 1.0f;
179 clip.duration = effectiveDur;
194 for (
const auto& state : controller.
states) {
195 if (state.name == controller.
fromState) {
215 bool allConditionsMet =
true;
216 for (
const auto& cond : trans.conditions) {
217 float paramVal = 0.0f;
218 auto it = controller.
parameters.find(cond.parameterName);
220 paramVal = it->second;
223 if (cond.op ==
">") {
224 if (!(paramVal > cond.value)) allConditionsMet =
false;
225 }
else if (cond.op ==
"<") {
226 if (!(paramVal < cond.value)) allConditionsMet =
false;
227 }
else if (cond.op ==
"==") {
228 if (!(std::abs(paramVal - cond.value) < 1e-4f)) allConditionsMet =
false;
230 allConditionsMet =
false;
234 bool shouldTransition =
false;
235 if (!trans.conditions.empty()) {
236 shouldTransition = allConditionsMet;
238 float clipDur = 0.5f;
241 if (c.name == currState->
clipName) {
242 clipDur = c.duration;
247 if (clipDur <= 0.0f || controller.currentStateTime >= clipDur) {
248 shouldTransition =
true;
252 if (shouldTransition) {
271 for (
const auto& clip : animator.
animations) {
272 if (clip.name ==
name)
return &clip;
278 for (
const auto& channel : clip.
channels) {
279 if (channel.jointIndex < 0 || channel.jointIndex >=
static_cast<int>(skeleton.
joints.size())) {
282 auto& joint = skeleton.
joints[channel.jointIndex];
289 void blendPoses(
const std::vector<JointPose>& poseA,
const std::vector<JointPose>& poseB,
float weight, std::vector<JointPose>& outPose) {
290 for (
size_t i = 0; i < outPose.size(); ++i) {
291 outPose[i].translation = glm::mix(poseA[i].translation, poseB[i].translation,
weight);
292 outPose[i].rotation = glm::slerp(poseA[i].rotation, poseB[i].rotation,
weight);
300 if (tree.nodes.empty())
return;
302 float paramVal = 0.0f;
303 auto it = controller.
parameters.find(tree.parameterName);
305 paramVal = it->second;
310 auto itX = controller.
parameters.find(tree.parameterName);
312 paramX = itX->second;
316 auto itY = controller.
parameters.find(tree.parameterYName);
318 paramY = itY->second;
321 glm::vec2 p(paramX, paramY);
322 size_t numNodes = tree.nodes.size();
323 std::vector<float> weights(numNodes, 0.0f);
324 float totalWeight = 0.0f;
327 for (
size_t i = 0; i < numNodes; ++i) {
328 glm::vec2 pi = tree.nodes[i].threshold2D;
331 for (
size_t j = 0; j < numNodes; ++j) {
332 if (i == j)
continue;
333 glm::vec2 pj = tree.nodes[j].threshold2D;
334 glm::vec2 v = pj - pi;
335 float lenSq = glm::dot(v, v);
336 if (lenSq < 1e-5f)
continue;
338 glm::vec2 u = p - pi;
339 float t = glm::dot(u, v) / lenSq;
340 float val = 1.0f - t;
346 weights[i] = std::max(0.0f, minVal);
347 totalWeight += weights[i];
351 if (totalWeight > 1e-5f) {
352 for (
size_t i = 0; i < numNodes; ++i) {
353 weights[i] /= totalWeight;
356 std::vector<JointPose> blendedPose(skeleton.
joints.size());
357 for (
size_t j = 0; j < skeleton.
joints.size(); ++j) {
358 blendedPose[j] = { skeleton.
joints[j].bindTranslation, skeleton.
joints[j].bindRotation, skeleton.
joints[j].bindScale };
361 float cumulativeWeight = 0.0f;
362 bool firstPoseSet =
false;
364 for (
size_t i = 0; i < numNodes; ++i) {
365 float w = weights[i];
366 if (w < 1e-4f)
continue;
369 std::vector<JointPose> tempPose(skeleton.
joints.size());
370 for (
size_t j = 0; j < skeleton.
joints.size(); ++j) {
371 tempPose[j] = { skeleton.
joints[j].bindTranslation, skeleton.
joints[j].bindRotation, skeleton.
joints[j].bindScale };
373 sampleClip(*clip, stateTime, skeleton, tempPose);
376 blendedPose = tempPose;
377 cumulativeWeight = w;
380 float blendFactor = w / (cumulativeWeight + w);
381 blendPoses(blendedPose, tempPose, blendFactor, blendedPose);
382 cumulativeWeight += w;
386 outPose = blendedPose;
390 std::vector<BlendNode> sortedNodes = tree.nodes;
391 std::sort(sortedNodes.begin(), sortedNodes.end(), [](
const BlendNode& a,
const BlendNode& b) {
392 return a.threshold < b.threshold;
395 if (paramVal <= sortedNodes.front().threshold) {
397 sampleClip(*clip, stateTime, skeleton, outPose);
399 }
else if (paramVal >= sortedNodes.back().threshold) {
401 sampleClip(*clip, stateTime, skeleton, outPose);
404 for (
size_t i = 0; i < sortedNodes.size() - 1; ++i) {
405 if (paramVal >= sortedNodes[i].threshold && paramVal <= sortedNodes[i+1].threshold) {
406 float t = (paramVal - sortedNodes[i].threshold) / (sortedNodes[i+1].threshold - sortedNodes[i].threshold);
408 std::vector<JointPose> poseA(skeleton.
joints.size());
409 std::vector<JointPose> poseB(skeleton.
joints.size());
410 for (
size_t j = 0; j < skeleton.
joints.size(); ++j) {
411 poseA[j] = { skeleton.
joints[j].bindTranslation, skeleton.
joints[j].bindRotation, skeleton.
joints[j].bindScale };
416 sampleClip(*clipA, stateTime, skeleton, poseA);
419 sampleClip(*clipB, stateTime, skeleton, poseB);
430 sampleClip(*clip, stateTime, skeleton, outPose);
436 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
438 return static_cast<int>(i);
445 u = glm::normalize(u);
446 v = glm::normalize(v);
447 float cosTheta = glm::dot(u, v);
448 glm::vec3 rotationAxis;
450 if (cosTheta < -1.0f + 1e-6f) {
451 rotationAxis = glm::cross(glm::vec3(0.0f, 0.0f, 1.0f), u);
452 if (glm::dot(rotationAxis, rotationAxis) < 0.01f) {
453 rotationAxis = glm::cross(glm::vec3(1.0f, 0.0f, 0.0f), u);
455 rotationAxis = glm::normalize(rotationAxis);
456 return glm::angleAxis(glm::radians(180.0f), rotationAxis);
459 rotationAxis = glm::cross(u, v);
460 float s = std::sqrt((1.0f + cosTheta) * 2.0f);
461 float invs = 1.0f / s;
465 rotationAxis.x * invs,
466 rotationAxis.y * invs,
467 rotationAxis.z * invs
476 if (idxA == -1 || idxB == -1 || idxC == -1)
return;
478 glm::mat4 origGlobalA = globalTransforms[idxA];
479 glm::mat4 origGlobalB = globalTransforms[idxB];
480 glm::mat4 origGlobalC = globalTransforms[idxC];
482 glm::vec3 P_A = glm::vec3(origGlobalA[3]);
483 glm::vec3 P_B = glm::vec3(origGlobalB[3]);
484 glm::vec3 P_C = glm::vec3(origGlobalC[3]);
488 float L1 = glm::distance(P_B, P_A);
489 float L2 = glm::distance(P_C, P_B);
490 float D = glm::distance(P_T, P_A);
492 if (L1 < 1e-4f || L2 < 1e-4f)
return;
494 if (D > L1 + L2 - 0.001f) {
495 P_T = P_A + glm::normalize(P_T - P_A) * (L1 + L2 - 0.001f);
496 D = L1 + L2 - 0.001f;
499 P_T = P_A + glm::vec3(0.0f, 0.0f, 0.001f);
503 float cosAlpha = (L1 * L1 + D * D - L2 * L2) / (2.0f * L1 * D);
504 cosAlpha = glm::clamp(cosAlpha, -1.0f, 1.0f);
505 float alpha = std::acos(cosAlpha);
507 glm::vec3 d_AT = glm::normalize(P_T - P_A);
508 glm::vec3 v_AP = P_P - P_A;
509 glm::vec3 N = glm::cross(d_AT, v_AP);
510 if (glm::dot(N, N) < 1e-6f) {
511 N = glm::cross(d_AT, glm::vec3(0.0f, 1.0f, 0.0f));
512 if (glm::dot(N, N) < 1e-6f) {
513 N = glm::cross(d_AT, glm::vec3(1.0f, 0.0f, 0.0f));
516 N = glm::normalize(N);
517 glm::vec3 B_dir = glm::normalize(glm::cross(N, d_AT));
519 glm::vec3 P_B_new = P_A + (d_AT * std::cos(alpha) + B_dir * std::sin(alpha)) * L1;
520 glm::vec3 P_C_new = P_T;
522 glm::vec3 origDirA = P_B - P_A;
523 glm::vec3 targetDirA = P_B_new - P_A;
525 glm::mat3 rotPartA = glm::mat3(glm::toMat4(rotA)) * glm::mat3(origGlobalA);
526 glm::mat4 newGlobalA = glm::mat4(rotPartA);
527 newGlobalA[3] = glm::vec4(P_A, 1.0f);
529 glm::vec3 origDirB = P_C - P_B;
530 glm::vec3 targetDirB = P_C_new - P_B_new;
532 glm::mat3 rotPartB = glm::mat3(glm::toMat4(rotB)) * glm::mat3(origGlobalB);
533 glm::mat4 newGlobalB = glm::mat4(rotPartB);
534 newGlobalB[3] = glm::vec4(P_B_new, 1.0f);
536 glm::mat4 newGlobalC = origGlobalC;
537 newGlobalC[3] = glm::vec4(P_C_new, 1.0f);
540 globalTransforms[idxA] = origGlobalA * (1.0f - w) + newGlobalA * w;
541 globalTransforms[idxB] = origGlobalB * (1.0f - w) + newGlobalB * w;
542 globalTransforms[idxC] = origGlobalC * (1.0f - w) + newGlobalC * w;
544 auto updateLocal = [&](
int idx) {
545 int parentIdx = skeleton.
joints[idx].parentIndex;
546 if (parentIdx != -1) {
547 skeleton.
joints[idx].localTransform = glm::inverse(globalTransforms[parentIdx]) * globalTransforms[idx];
549 skeleton.
joints[idx].localTransform = globalTransforms[idx];
557 std::vector<bool> resolved(skeleton.
joints.size(),
false);
558 resolved[idxA] =
true;
559 resolved[idxB] =
true;
560 resolved[idxC] =
true;
562 std::function<void(
int)> propagate = [&](
int jointIdx) {
563 int parentIdx = skeleton.
joints[jointIdx].parentIndex;
564 if (parentIdx != -1 && resolved[parentIdx] && !resolved[jointIdx]) {
565 globalTransforms[jointIdx] = globalTransforms[parentIdx] * skeleton.
joints[jointIdx].localTransform;
566 resolved[jointIdx] =
true;
568 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
569 if (skeleton.
joints[i].parentIndex == jointIdx) {
570 propagate(
static_cast<int>(i));
583 std::vector<int> chainIndices;
587 chainIndices.push_back(idx);
591 if (chainIndices.size() < 2)
return;
593 int n =
static_cast<int>(chainIndices.size()) - 1;
594 std::vector<glm::vec3> positions(chainIndices.size());
595 std::vector<float> boneLengths(n);
596 float totalLength = 0.0f;
598 for (
size_t i = 0; i < chainIndices.size(); ++i) {
599 positions[i] = glm::vec3(globalTransforms[chainIndices[i]][3]);
601 boneLengths[i - 1] = glm::distance(positions[i], positions[i - 1]);
602 totalLength += boneLengths[i - 1];
607 glm::vec3 origin = positions[0];
609 float distToTarget = glm::distance(origin, target);
610 if (distToTarget > totalLength) {
611 for (
int i = 0; i < n; ++i) {
612 glm::vec3 dir = glm::normalize(target - positions[i]);
613 positions[i + 1] = positions[i] + dir * boneLengths[i];
617 float err = glm::distance(positions[n], target);
620 positions[n] = target;
621 for (
int i = n - 1; i >= 0; --i) {
622 glm::vec3 dir = glm::normalize(positions[i] - positions[i + 1]);
623 positions[i] = positions[i + 1] + dir * boneLengths[i];
626 positions[0] = origin;
627 for (
int i = 0; i < n; ++i) {
628 glm::vec3 dir = glm::normalize(positions[i + 1] - positions[i]);
629 positions[i + 1] = positions[i] + dir * boneLengths[i];
634 for (
int i = 0; i < n; ++i) {
635 int idxCurrent = chainIndices[i];
636 int idxNext = chainIndices[i + 1];
638 glm::mat4 origGlobalCurrent = globalTransforms[idxCurrent];
640 glm::mat4 currentGlobalNext = origGlobalCurrent * skeleton.
joints[idxNext].localTransform;
642 glm::vec3 P_current = glm::vec3(origGlobalCurrent[3]);
643 glm::vec3 P_next = glm::vec3(currentGlobalNext[3]);
645 glm::vec3 origDir = P_next - P_current;
646 glm::vec3 solvedDir = positions[i + 1] - positions[i];
648 if (glm::dot(origDir, origDir) < 1e-6f || glm::dot(solvedDir, solvedDir) < 1e-6f)
continue;
652 glm::quat blendedRot = glm::slerp(glm::quat(1.0f, 0.0f, 0.0f, 0.0f), rot, w);
655 glm::mat3 rotPart = glm::mat3(glm::toMat4(blendedRot)) * glm::mat3(origGlobalCurrent);
656 glm::mat4 newGlobal = glm::mat4(rotPart);
657 newGlobal[3] = glm::vec4(glm::mix(P_current, positions[i], w), 1.0f);
659 globalTransforms[idxCurrent] = newGlobal;
662 int parentIdx = skeleton.
joints[idxCurrent].parentIndex;
663 if (parentIdx != -1) {
664 skeleton.
joints[idxCurrent].localTransform = glm::inverse(globalTransforms[parentIdx]) * globalTransforms[idxCurrent];
666 skeleton.
joints[idxCurrent].localTransform = globalTransforms[idxCurrent];
670 globalTransforms[idxNext] = globalTransforms[idxCurrent] * skeleton.
joints[idxNext].localTransform;
673 std::vector<bool> resolved(skeleton.
joints.size(),
false);
674 for (
int idx : chainIndices) {
675 resolved[idx] =
true;
678 std::function<void(
int)> propagate = [&](
int jointIdx) {
679 int parentIdx = skeleton.
joints[jointIdx].parentIndex;
680 if (parentIdx != -1 && resolved[parentIdx] && !resolved[jointIdx]) {
681 globalTransforms[jointIdx] = globalTransforms[parentIdx] * skeleton.
joints[jointIdx].localTransform;
682 resolved[jointIdx] =
true;
684 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
685 if (skeleton.
joints[i].parentIndex == jointIdx) {
686 propagate(
static_cast<int>(i));
691 for (
int idx : chainIndices) {
700 if (skeleton.
joints.empty())
return;
703 std::vector<JointPose> finalPose(skeleton.
joints.size());
704 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
705 finalPose[i] = { skeleton.
joints[i].bindTranslation, skeleton.
joints[i].bindRotation, skeleton.
joints[i].bindScale };
710 if (!controller->currentState.empty()) {
713 for (
const auto& state : controller->states) {
714 if (state.name == controller->currentState) {
721 evaluateStatePose(*controller, *currState, controller->currentStateTime, animator, skeleton, finalPose);
725 if (controller->isCrossfading && !controller->fromState.empty()) {
727 for (
const auto& state : controller->states) {
728 if (state.name == controller->fromState) {
735 std::vector<JointPose> fromPose(skeleton.
joints.size());
736 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
737 fromPose[i] = { skeleton.
joints[i].bindTranslation, skeleton.
joints[i].bindRotation, skeleton.
joints[i].bindScale };
739 evaluateStatePose(*controller, *fState, controller->fromStateTime, animator, skeleton, fromPose);
741 float weight = glm::clamp(controller->crossfadeProgress / controller->crossfadeDuration, 0.0f, 1.0f);
757 static int lastPrintedIndex = -999;
759 std::cout <<
"[AnimationSystem] Entity plays animation index: " << animator.
activeAnimationIndex
760 <<
" (Name: " << clip->
name <<
")" << std::endl;
781 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
782 glm::mat4 tMat = glm::translate(glm::mat4(1.0f), finalPose[i].translation);
783 glm::mat4 rMat = glm::toMat4(finalPose[i].rotation);
784 glm::mat4 sMat = glm::scale(glm::mat4(1.0f), finalPose[i].
scale);
785 skeleton.
joints[i].localTransform = tMat * rMat * sMat;
789 std::vector<glm::mat4> globalTransforms(skeleton.
joints.size(), glm::mat4(1.0f));
790 std::vector<bool> resolved(skeleton.
joints.size(),
false);
792 auto resolveJointGlobal = [&](
auto& self,
int jointIdx) -> glm::mat4 {
793 if (jointIdx == -1)
return glm::mat4(1.0f);
794 if (resolved[jointIdx])
return globalTransforms[jointIdx];
796 glm::mat4 parentGlobal = self(self, skeleton.
joints[jointIdx].parentIndex);
797 globalTransforms[jointIdx] = parentGlobal * skeleton.
joints[jointIdx].localTransform;
798 resolved[jointIdx] =
true;
799 return globalTransforms[jointIdx];
802 for (
size_t i = 0; i < skeleton.
joints.size(); ++i) {
803 resolveJointGlobal(resolveJointGlobal,
static_cast<int>(i));
819 for (
size_t i = 0; i < std::min(skeleton.
joints.size(),
size_t(256)); ++i) {
824 VkDeviceSize bufferSize = 256 *
sizeof(glm::mat4);
827 skeleton.
gpuBuffer = std::make_shared<VulkanBuffer>();
830 renderer.device.getPhysicalDevice(),
832 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
833 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
836 renderer.descriptors.allocateJointsDescriptorSet(
850 if (keys.empty())
return defaultValue;
851 if (keys.size() == 1 ||
time <= keys.front().time)
return keys.front().value;
852 if (
time >= keys.back().time)
return keys.back().value;
855 for (
size_t i = 0; i < keys.size() - 1; ++i) {
862 const auto& k1 = keys[
index];
863 const auto& k2 = keys[
index+1];
864 float factor = (
time - k1.time) / (k2.time - k1.time);
865 return glm::mix(k1.value, k2.value, factor);
869 if (keys.empty())
return defaultValue;
870 if (keys.size() == 1 ||
time <= keys.front().time)
return keys.front().value;
871 if (
time >= keys.back().time)
return keys.back().value;
874 for (
size_t i = 0; i < keys.size() - 1; ++i) {
881 const auto& k1 = keys[
index];
882 const auto& k2 = keys[
index+1];
883 float factor = (
time - k1.time) / (k2.time - k1.time);
884 return glm::slerp(k1.value, k2.value, factor);
888 if (keys.empty())
return defaultValue;
889 if (keys.size() == 1 ||
time <= keys.front().time)
return keys.front().value;
890 if (
time >= keys.back().time)
return keys.back().value;
893 for (
size_t i = 0; i < keys.size() - 1; ++i) {
900 const auto& k1 = keys[
index];
901 const auto& k2 = keys[
index+1];
902 float factor = (
time - k1.time) / (k2.time - k1.time);
903 return glm::mix(k1.value, k2.value, factor);
907 if (keys.empty())
return defaultValue;
908 if (keys.size() == 1 ||
time <= keys.front().time)
return keys.front().value;
909 if (
time >= keys.back().time)
return keys.back().value;
912 for (
size_t i = 0; i < keys.size() - 1; ++i) {
919 const auto& k1 = keys[
index];
920 const auto& k2 = keys[
index+1];
921 float factor = (
time - k1.time) / (k2.time - k1.time);
922 return glm::mix(k1.value, k2.value, factor);
926 if (keys.empty())
return "";
927 if (
time <= keys.front().time)
return keys.front().stringValue;
928 if (
time >= keys.back().time)
return keys.back().stringValue;
929 for (
size_t i = 0; i < keys.size() - 1; ++i) {
931 return keys[i].stringValue;
934 return keys.back().stringValue;
951 auto normalizeName = [](
const std::string& s) {
954 if (c !=
' ' && c !=
'_' && c !=
'&' && c !=
'/') {
955 res +=
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
958 if (res.size() > 9 && res.substr(res.size() - 9) ==
"component") {
959 res = res.substr(0, res.size() - 9);
965 std::vector<Entity> targetEntities;
966 targetEntities.push_back(entity);
968 for (
size_t readIdx = 0; readIdx < targetEntities.size(); ++readIdx) {
969 Entity curr = targetEntities[readIdx];
971 if (hierarchy.parent.getId() == curr.
getId()) {
972 targetEntities.push_back(childEntity);
980 if (channel.keys.empty())
continue;
982 std::string normChanComp = normalizeName(channel.componentName);
983 std::string normChanField = normalizeName(channel.fieldName);
986 bool handledDirectly =
false;
987 for (
Entity targetEntity : targetEntities) {
989 bool isTex = (normChanField.find(
"texture") != std::string::npos || normChanField.find(
"image") != std::string::npos || normChanField.find(
"path") != std::string::npos);
990 bool isCol = (normChanField.find(
"color") != std::string::npos);
991 bool isFlipX = (normChanField.find(
"flipx") != std::string::npos);
992 bool isFlipY = (normChanField.find(
"flipy") != std::string::npos);
993 bool isSort = (normChanField.find(
"sort") != std::string::npos || normChanField.find(
"order") != std::string::npos);
997 spr->texturePath = strVal;
1000 mat->texturePath = strVal;
1002 spr->_loadedTexturePath = strVal;
1004 handledDirectly =
true;
1009 mat->color = spr->color;
1011 handledDirectly =
true;
1012 }
else if (isFlipX) {
1014 spr->flipX = (val.x > 0.5f);
1016 mat->roughness = spr->flipX ? -1.0f : 1.0f;
1018 handledDirectly =
true;
1019 }
else if (isFlipY) {
1021 spr->flipY = (val.x > 0.5f);
1023 mat->metallic = spr->flipY ? -1.0f : 1.0f;
1025 handledDirectly =
true;
1026 }
else if (isSort) {
1028 spr->sortOrder =
static_cast<int>(val.x);
1030 t->position.z = spr->sortOrder * 0.0001f;
1032 handledDirectly =
true;
1036 if (handledDirectly)
continue;
1039 for (
const auto& refl : reflReg.getReflections()) {
1040 if (refl.name == channel.componentName ||
1041 normalizeName(refl.name) == normChanComp ||
1042 normalizeName(refl.displayName) == normChanComp) {
1048 if (!targetRefl)
continue;
1050 for (
Entity targetEntity : targetEntities) {
1051 if (!targetRefl->
has(
registry, targetEntity))
continue;
1053 void* compPtr = targetRefl->
get(
registry, targetEntity);
1054 if (!compPtr)
continue;
1056 auto stripPrefix = [](
const std::string& s) {
1057 if (s.rfind(
"spr", 0) == 0 && s.size() > 3)
return s.substr(3);
1058 if (s.rfind(
"m_", 0) == 0 && s.size() > 2)
return s.substr(2);
1059 if (s.rfind(
"s_", 0) == 0 && s.size() > 2)
return s.substr(2);
1062 std::string sfn = stripPrefix(normChanField);
1065 for (
const auto& f : targetRefl->
fields) {
1066 std::string fn = normalizeName(f.name);
1067 std::string sfn2 = stripPrefix(fn);
1068 if (f.name == channel.fieldName || fn == normChanField || sfn == sfn2 ||
1069 (sfn.find(
"texture") != std::string::npos && sfn2.find(
"texture") != std::string::npos) ||
1070 (sfn.find(
"image") != std::string::npos && sfn2.find(
"texture") != std::string::npos) ||
1071 (sfn.find(
"texture") != std::string::npos && sfn2.find(
"image") != std::string::npos)) {
1077 if (!targetField)
continue;
1079 char* fieldPtr =
static_cast<char*
>(compPtr) + targetField->
offset;
1084 *
reinterpret_cast<float*
>(fieldPtr) = val.x;
1086 *
reinterpret_cast<int*
>(fieldPtr) =
static_cast<int>(val.x);
1088 *
reinterpret_cast<bool*
>(fieldPtr) = (val.x > 0.5f);
1090 *
reinterpret_cast<glm::vec2*
>(fieldPtr) = glm::vec2(val.x, val.y);
1092 *
reinterpret_cast<glm::vec3*
>(fieldPtr) = glm::vec3(val.x, val.y, val.z);
1094 *
reinterpret_cast<glm::vec4*
>(fieldPtr) = val;
1097 *
reinterpret_cast<std::string*
>(fieldPtr) = strVal;
1103 mat->texturePath = spr->texturePath;
1105 spr->_loadedTexturePath = spr->texturePath;
1124 float effectiveDur = clip->
duration;
1126 if (!chan.keys.empty()) {
1127 effectiveDur = std::max(effectiveDur, chan.keys.back().time);
1130 if (effectiveDur <= 0.0f) effectiveDur = 1.0f;
1137 if (animator.
loop) {
@ TwoBone
Definition IKSolver.hpp:11
@ FABRIK
Definition IKSolver.hpp:12
void updateGenericAnimation(Entity entity, AnimatorComponent &animator, float dt)
Definition AnimationSystem.hpp:1112
int findJointIndex(const SkeletonComponent &skeleton, const std::string &name)
Definition AnimationSystem.hpp:435
void sampleAndApplyProperties(Entity entity, AnimatorComponent &animator)
Definition AnimationSystem.hpp:937
void updateController(AnimationControllerComponent &controller, AnimatorComponent &animator, float dt)
Updates high-level state machine progression and checks transitions.
Definition AnimationSystem.hpp:134
glm::vec3 interpolateTranslation(const std::vector< Keyframe > &keys, float time, const glm::vec3 &defaultValue)
Definition AnimationSystem.hpp:849
void updateEntityAnimation(Entity entity, SkeletonComponent &skeleton, AnimatorComponent &animator, float dt)
Updates the animation timing and calculates local/global joint matrices for an entity.
Definition AnimationSystem.hpp:699
void solve2BoneIK(SkeletonComponent &skeleton, const IKSolverComponent &ik, std::vector< glm::mat4 > &globalTransforms)
Definition AnimationSystem.hpp:471
void solveFABRIK(SkeletonComponent &skeleton, const IKSolverComponent &ik, std::vector< glm::mat4 > &globalTransforms)
Definition AnimationSystem.hpp:580
glm::quat rotationBetweenVectors(glm::vec3 u, glm::vec3 v)
Definition AnimationSystem.hpp:444
std::string sampleStringProperty(const std::vector< PropertyKeyframe > &keys, float time)
Definition AnimationSystem.hpp:925
glm::quat interpolateRotation(const std::vector< KeyframeRot > &keys, float time, const glm::quat &defaultValue)
Definition AnimationSystem.hpp:868
void update(float dt) override
Updates skeletal poses for all animated entities.
Definition AnimationSystem.hpp:47
const AnimationClip * findClip(const AnimatorComponent &animator, const std::string &name)
Definition AnimationSystem.hpp:270
glm::vec3 interpolateScale(const std::vector< Keyframe > &keys, float time, const glm::vec3 &defaultValue)
Definition AnimationSystem.hpp:887
std::vector< Entity > animatedEntities
Definition AnimationSystem.hpp:1152
void sampleClip(const AnimationClip &clip, float time, const SkeletonComponent &skeleton, std::vector< JointPose > &outPose)
Definition AnimationSystem.hpp:277
EditorModeState & editorMode
Definition AnimationSystem.hpp:1150
std::vector< Entity > controllerEntities
Definition AnimationSystem.hpp:1151
void evaluateStatePose(const AnimationControllerComponent &controller, const AnimationState &state, float stateTime, const AnimatorComponent &animator, const SkeletonComponent &skeleton, std::vector< JointPose > &outPose)
Definition AnimationSystem.hpp:297
AnimationSystem(Registry ®, VulkanRenderer &renderer, EditorModeState &editorMode)
Definition AnimationSystem.hpp:30
std::vector< Entity > genericAnimatedEntities
Definition AnimationSystem.hpp:1153
void blendPoses(const std::vector< JointPose > &poseA, const std::vector< JointPose > &poseB, float weight, std::vector< JointPose > &outPose)
Definition AnimationSystem.hpp:289
Registry & registry
Definition AnimationSystem.hpp:1148
glm::vec4 interpolateProperty(const std::vector< PropertyKeyframe > &keys, float time, const glm::vec4 &defaultValue)
Definition AnimationSystem.hpp:906
VulkanRenderer & renderer
Definition AnimationSystem.hpp:1149
static ComponentReflectionRegistry & getInstance()
Definition ComponentReflection.cpp:6
static JobSystem & getInstance()
Get the singleton instance.
Definition JobSystem.hpp:23
void parallelFor(int count, std::function< void(int)> func)
Splits a total loop count into chunks and evaluates them in parallel.
Definition JobSystem.cpp:143
Lightweight identifier representing a game object in the Entity Component System.
Definition Entity.hpp:11
static constexpr IdType INVALID_ENTITY
Constant representing a null or invalid entity.
Definition Entity.hpp:16
IdType getId() const
Retrieve the raw identifier of the entity.
Definition Entity.hpp:32
Coordinates entities and components, providing creation, removal, lookup, and views.
Definition Registry.hpp:30
auto view()
Generates a filtered view of entities containing the requested components.
Definition Registry.hpp:279
Base class for ECS systems that process sets of entities.
Definition System.hpp:12
Manages core Vulkan pipeline states, device interaction, frames, buffers, and command submissions.
Definition VulkanRenderer.hpp:127
@ Vec3
Definition ComponentReflection.hpp:20
@ Int
Definition ComponentReflection.hpp:18
@ Float
Definition ComponentReflection.hpp:17
@ String
Definition ComponentReflection.hpp:23
@ Bool
Definition ComponentReflection.hpp:19
@ Vec4
Definition ComponentReflection.hpp:25
@ Vec2
Definition ComponentReflection.hpp:24
Collection of channels representing a single skeletal animation state.
Definition Animator.hpp:68
std::vector< AnimationChannel > channels
Bone channels mapping keyframe states.
Definition Animator.hpp:74
std::string name
Name of the clip (e.g. "Walk", "Run").
Definition Animator.hpp:70
float duration
Total duration of the animation in seconds.
Definition Animator.hpp:72
std::vector< PropertyChannel > propertyChannels
Reflected component field channels.
Definition Animator.hpp:76
Component that manages parameters, state machines, transitions, and blending.
Definition AnimationController.hpp:67
std::string fromState
Definition AnimationController.hpp:78
std::string currentState
Definition AnimationController.hpp:71
std::unordered_map< std::string, float > parameters
Definition AnimationController.hpp:75
float currentStateTime
Definition AnimationController.hpp:80
float crossfadeDuration
Definition AnimationController.hpp:83
float fromStateTime
Definition AnimationController.hpp:81
std::string targetState
Definition AnimationController.hpp:79
std::vector< AnimationState > states
Definition AnimationController.hpp:68
std::vector< AnimationTransition > transitions
Definition AnimationController.hpp:69
float crossfadeProgress
Definition AnimationController.hpp:82
bool isCrossfading
Definition AnimationController.hpp:84
Represents a state in the animation state machine, referencing a clip or a blend tree.
Definition AnimationController.hpp:32
BlendTree blendTree
Definition AnimationController.hpp:35
bool isBlendTree
Definition AnimationController.hpp:36
bool isLooping
Definition AnimationController.hpp:37
float speed
Definition AnimationController.hpp:38
std::string clipName
Definition AnimationController.hpp:34
Updates skeletal poses for all animated entities.
Definition AnimationSystem.hpp:37
glm::vec3 scale
Definition AnimationSystem.hpp:40
glm::quat rotation
Definition AnimationSystem.hpp:39
glm::vec3 translation
Definition AnimationSystem.hpp:38
Animator controller managing active clips and tracking playback timing.
Definition Animator.hpp:84
float currentTime
Current playback time offset in seconds.
Definition Animator.hpp:90
float playbackSpeed
Definition Animator.hpp:92
bool loop
Definition Animator.hpp:94
int activeAnimationIndex
Index of the active clip in playback.
Definition Animator.hpp:88
std::vector< AnimationClip > animations
List of all clips loaded from the model asset.
Definition Animator.hpp:86
Represents a single animation clip in a blend tree with its target threshold value.
Definition AnimationController.hpp:11
Struct keeping track of active editor states (e.g. fly mode vs editor UI interaction).
Definition EditorModeState.hpp:7
Reflection metadata for a single field inside a component.
Definition ComponentReflection.hpp:33
size_t offset
Definition ComponentReflection.hpp:36
Reflection metadata and ECS callbacks for a component.
Definition ComponentReflection.hpp:48
std::vector< ComponentField > fields
Definition ComponentReflection.hpp:56
std::function< void *(Registry &, Entity)> get
Definition ComponentReflection.hpp:62
std::function< bool(Registry &, Entity)> has
Definition ComponentReflection.hpp:60
Component that renders a textured 2D quad (sprite) on an entity.
Definition SpriteRenderer.hpp:24
Represents a parent-child relationship between ECS entities.
Definition Hierarchy.hpp:8
Specifies target joint chains and goal targets for analytical 2-Bone or iterative FABRIK IK.
Definition IKSolver.hpp:20
int maxIterations
Definition IKSolver.hpp:34
float tolerance
Definition IKSolver.hpp:36
std::string endJointName
Definition IKSolver.hpp:29
float targetWeight
Definition IKSolver.hpp:44
glm::vec3 polePosition
Definition IKSolver.hpp:41
glm::vec3 targetPosition
Definition IKSolver.hpp:39
std::vector< std::string > jointChainNames
Definition IKSolver.hpp:32
std::string middleJointName
Definition IKSolver.hpp:27
std::string startJointName
Definition IKSolver.hpp:25
Represents a material component defining rendering properties.
Definition Material.hpp:17
Component managing the list of joints and generating the offset matrices palette.
Definition Skeleton.hpp:37
std::shared_ptr< VulkanBuffer > gpuBuffer
Definition Skeleton.hpp:44
std::vector< Joint > joints
List of joints forming the skeleton hierarchy.
Definition Skeleton.hpp:40
VkDescriptorSet descriptorSet
Definition Skeleton.hpp:45
std::vector< glm::mat4 > jointMatrices
Calculated final joint offsets list uploaded to VRAM descriptors.
Definition Skeleton.hpp:42
const char * name
Definition ufbx.cpp:9020
double scale
Definition ufbx.cpp:26002
double time
Definition ufbx.cpp:26005
static ufbxi_noinline void weight
Definition ufbx.cpp:25700
static ufbxi_noinline ufbxi_aa_node index
Definition ufbx.cpp:4444