-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationComponent.cpp
More file actions
143 lines (116 loc) · 3.3 KB
/
AnimationComponent.cpp
File metadata and controls
143 lines (116 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include"AnimationComponent.h"
AnimationComponent::AnimationComponent(std::shared_ptr<GpuBufferFactory> buffer
, std::shared_ptr<GpuDescriptorSetLayoutFactory> layout
, std::shared_ptr<DescriptorSetFactory> desc)
{
bufferFactory = buffer;
layoutFactory = layout;
descriptorSetFactory = desc;
gltfModel = nullptr;
deltaTime = 0.0;
startTime = 0.0;
animationChange = false;
currentPlayAnimationName = "";
nextPlayAnimationName = "";
}
//3Dモデルを設定する
void AnimationComponent::setGltfModel(std::shared_ptr<GltfModel> model)
{
gltfModel = model;
nodeTransform.setNodeCount(gltfModel->nodeCount);
}
//アニメーションを切り替える
void AnimationComponent::switchPlayAnimation(std::string next)
{
nextPlayAnimationName = next;
}
////特定のスケルトンのアニメーション行列を取得
std::shared_ptr<DescriptorSet> AnimationComponent::getJointMatrices(int index)
{
return descriptorSet[index];
}
//アタッチ後の次のフレーム開始時点で実行
void AnimationComponent::OnStart()
{
jointMatrices.resize(gltfModel->jointNum);
animationBuffer.resize(gltfModel->primitiveCount);
descriptorSet.resize(gltfModel->primitiveCount);
animationNames.resize(gltfModel->animations.size());
//gpu上のバッファを作成
createBuffer();
//ディスクリプタセットを作成
createDescriptorSet();
}
//GPU上のバッファを作成する
void AnimationComponent::createBuffer()
{
const GltfNode* root = gltfModel->getRootNode();
for (auto node : root->children)
{
createBuffer(node);
}
}
void AnimationComponent::createBuffer(const GltfNode* node)
{
for (auto mesh : node->meshArray)
{
animationBuffer[mesh->meshIndex] =
bufferFactory->Create(sizeof(AnimationUBO), BufferUsage::UNIFORM, BufferTransferType::DST);
}
for (auto child : node->children)
{
createBuffer(child);
}
}
//ディスクリプタセットを作る
void AnimationComponent::createDescriptorSet()
{
const GltfNode* root = gltfModel->getRootNode();
for (auto node : root->children)
{
createDescriptorSet(node);
}
}
void AnimationComponent::createDescriptorSet(const GltfNode* node)
{
const std::shared_ptr<DescriptorSetLayout> layout
= layoutFactory->Create(LayoutPattern::SINGLE_UNIFORM_VERT);
for (auto mesh : node->meshArray)
{
DescriptorSetProperty property = descriptorSetFactory->getBuilder()
->initProperty()
.withBindingBuffer(0)
.withBuffer(animationBuffer[mesh->meshIndex])
.withDescriptorSetCount(1)
.withDescriptorSetLayout(layout)
.withRange(sizeof(AnimationUBO))
.withTypeBuffer(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)
.Build();
descriptorSet[mesh->meshIndex] = descriptorSetFactory->Create(property);
}
for (auto child : node->children)
{
createDescriptorSet(child);
}
}
//フレーム終了時に実行
void AnimationComponent::OnFrameEnd()
{
if (currentPlayAnimationName != "")
{
double currentTime = clock();
deltaTime = static_cast<double>(currentTime - startTime) / CLOCKS_PER_SEC;
//アニメーションを再生し終えた
//あるいは、アニメーションが切り替わった場合
if (deltaTime >= gltfModel->animationDuration(currentPlayAnimationName)
|| currentPlayAnimationName != nextPlayAnimationName)
{
currentPlayAnimationName = nextPlayAnimationName;
//再生時間を再び計測し始める
startTime = clock();
deltaTime = 0.0;
}
//アニメーション行列の計算
gltfModel->updateAnimation(deltaTime, currentPlayAnimationName, nodeTransform, jointMatrices);
}
}