CSEngine
Loading...
Searching...
No Matches
DAEConvertSGameObject.cpp
1#include "DAEConvertSGameObject.h"
2#include "../../../../Component/Animation/JointComponent.h"
3#include "../../../../Component/Animation/AnimatorComponent.h"
4#include "../../../../Manager/EngineCore.h"
5#include "../../../../Object/SGameObjectFromSPrefab.h"
6
7using namespace CSE;
8
9DAEConvertSGameObject::DAEConvertSGameObject() = default;
10
11DAEConvertSGameObject::~DAEConvertSGameObject() = default;
12
13SGameObject* DAEConvertSGameObject::CreateJoints(SGameObject* parent, Joint* data) {
14
15 if (parent == nullptr) return nullptr;
16
17 auto jointObject = new SGameObjectFromSPrefab(data->GetName());
18 auto joint = jointObject->CreateComponent<JointComponent>();
19 joint->SetID(data->GetIndex());
20 int jointIndex = CORE->GetCore(ResMgr)->GetStringHash(data->GetName());
21 joint->SetAnimationJointId(jointIndex);
22 joint->SetBindLocalMatrix(data->GetBindLocalTransform());
23
24 auto children = data->GetChildren();
25 for (const auto& child : children) {
26 CreateJoints(jointObject, child);
27 }
28
29 parent->AddChild(jointObject);
30
31 return jointObject;
32}
33
34SGameObject* DAEConvertSGameObject::CreateAnimation(SGameObject* parent, JointComponent* rootJoint,
35 AnimationData* animationData,
36 const std::string& name, Animation* animation) {
37 if (parent == nullptr) return nullptr;
38
39 auto animationObject = new SGameObjectFromSPrefab(parent->GetName() + "_animation");
40 auto animator = animationObject->CreateComponent<AnimatorComponent>();
41
42 std::list<KeyFrame*> frames;
43 unsigned int length = animationData->keyFrames.size();
44
45 for (int i = 0; i < length; i++) {
46 frames.push_back(CreateKeyFrame(animationData->keyFrames[i]));
47 }
48
49 if (animation == nullptr)
50 animation = new Animation();
51 animation->LinkResource(name + ".prefab?animation");
52 animation->SetKeyframe(animationData->lengthSeconds, frames);
53
54 animator->PlayAnimation(animation);
55 animator->SetRootJoint(rootJoint);
56
57 parent->AddChild(animationObject);
58
59 return animationObject;
60}
61
62KeyFrame* DAEConvertSGameObject::CreateKeyFrame(KeyFrameData* data) {
63 std::vector<JointTransform*> resultData;
64 const auto& jointTransforms = data->jointTransforms;
65 resultData.reserve(jointTransforms.size());
66
67 for (const auto& jointData : jointTransforms) {
68 JointTransform* jointTransform = CreateTransform(jointData);
69 resultData.push_back(jointTransform);
70 }
71 return new KeyFrame(data->time, std::move(resultData));
72}
73
74JointTransform* DAEConvertSGameObject::CreateTransform(JointTransformData* data) {
75 mat4 mat = data->jointLocalTransform;
76 vec3 translation = vec3(mat.w.x, mat.w.y, mat.w.z);
77 Quaternion rotation = Quaternion::ToQuaternion(mat);
78 return new JointTransform(translation, rotation);
79}