-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMeshController.cpp
172 lines (145 loc) · 5.13 KB
/
MeshController.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "stdafx.h"
#include "MeshController.h"
#include "RenderQueue.h"
#include "RenderSkin.h"
#include <algorithm>
namespace happy
{
static float resolveBlend(system_clock::time_point start, system_clock::time_point current, float source, float target, float duration)
{
if (duration && source != target)
{
std::chrono::duration<float, std::ratio<1, 1>> offset(current - start);
return bb::lerp(source, target, fminf(1.0f, offset.count() / duration));
}
return target;
}
void MeshController::setMesh(shared_ptr<RenderMesh> mesh)
{
m_Mesh = mesh;
if (auto skin = dynamic_cast<RenderSkin*>(mesh.get()))
{
m_RenderItem.m_Skin = *skin;
m_Static = false;
}
else
{
m_Static = true;
}
m_RenderItem.m_Color = bb::vec4(1.0f, 1.0f, 1.0f, 1.0f);
m_RenderItem.m_AnimationCount = 1;
m_RenderItem.m_Groups = 0x00;
m_RenderItem.m_CurrentBlendAnimation = bb::vec2(1, 0);
m_RenderItem.m_CurrentBlendFrame = bb::vec2(0, 0);
memset(m_RenderItem.m_CurrentFrames, 0, sizeof(m_RenderItem.m_CurrentFrames));
m_RenderItem.m_CurrentWorld.identity();
m_RenderItem.m_CurrentWorld.scale(bb::vec3(1, 1, 1) * 0.5f);
m_RenderItem.m_PreviousBlendAnimation = m_RenderItem.m_CurrentBlendAnimation;
m_RenderItem.m_PreviousBlendFrame = m_RenderItem.m_CurrentBlendFrame;
memcpy(m_RenderItem.m_PreviousFrames, m_RenderItem.m_CurrentFrames, sizeof(m_RenderItem.m_CurrentFrames));
m_RenderItem.m_PreviousWorld = m_RenderItem.m_CurrentWorld;
}
void MeshController::setAlpha(float alpha)
{
m_RenderItem.m_Color.w = alpha;
}
void MeshController::setColor(bb::vec4 color)
{
m_RenderItem.m_Color = color;
}
void MeshController::setRenderGroups(StencilMask &groups)
{
m_RenderItem.m_Groups = groups;
}
int MeshController::addAnimation(string name, Animation animation, system_clock::time_point start)
{
anim_state state;
state.m_Name = name;
state.m_Anim = animation;
state.m_Blender = start;
state.m_Timer = start;
state.m_SpeedMultiplier = 1;
state.m_BlendDuration = 0;
state.m_BlendSource = 0;
state.m_BlendTarget = 0;
state.m_Looped = false;
m_States.push_back(state);
return (int)(m_States.size() - 1);
}
int MeshController::getAnimationIndex(string name)
{
for (unsigned index = 0; index < m_States.size(); ++index)
{
if (name == m_States[index].m_Name) return index;
}
return -1;
}
void MeshController::setAnimationTimer(int id, system_clock::time_point start, system_clock::duration offset)
{
m_States[id].m_Timer = start - offset;
}
void MeshController::setAnimationBlend(int id, float blend, system_clock::time_point start, float duration)
{
auto &s = m_States[id];
s.m_BlendSource = resolveBlend(s.m_Blender, start, s.m_BlendSource, s.m_BlendTarget, s.m_BlendDuration);
s.m_BlendTarget = blend;
s.m_Blender = start;
s.m_BlendDuration = duration;
}
void MeshController::setAnimationSpeed(int id, float multiplier)
{
m_States[id].m_SpeedMultiplier = multiplier;
}
void MeshController::resetAllAnimationBlends(system_clock::time_point start, float duration)
{
for (auto &s : m_States)
{
s.m_BlendSource = resolveBlend(s.m_Blender, start, s.m_BlendSource, s.m_BlendTarget, s.m_BlendDuration);
s.m_BlendTarget = 0;
s.m_Blender = start;
s.m_BlendDuration = duration;
}
}
bb::mat4 &MeshController::worldMatrix()
{
return m_RenderItem.m_CurrentWorld;
}
void MeshController::update(system_clock::time_point time)
{
m_RenderItem.m_PreviousBlendAnimation = m_RenderItem.m_CurrentBlendAnimation;
m_RenderItem.m_PreviousBlendFrame = m_RenderItem.m_CurrentBlendFrame;
memcpy(m_RenderItem.m_PreviousFrames, m_RenderItem.m_CurrentFrames, sizeof(m_RenderItem.m_CurrentFrames));
m_RenderItem.m_PreviousWorld = m_RenderItem.m_CurrentWorld;
vector<pair<unsigned,float>> influences;
for (unsigned i = 0; i < m_States.size(); ++i)
{
float blend = resolveBlend(m_States[i].m_Blender, time, m_States[i].m_BlendSource, m_States[i].m_BlendTarget, m_States[i].m_BlendDuration);
if (blend) influences.emplace_back(i, blend);
}
std::sort(influences.begin(), influences.end(), [](const pair<unsigned, float>& a, const pair<unsigned, float>& b)
{
return a.second > b.second;
});
m_RenderItem.m_AnimationCount = min(2, (unsigned)influences.size());
float total = 0;
for (unsigned a = 0; a < m_RenderItem.m_AnimationCount; ++a)
{
auto &state = m_States[influences[a].first];
std::chrono::duration<float, std::ratio<1, 1>> timer(time - state.m_Timer);
float x = timer.count() * state.m_SpeedMultiplier;
m_RenderItem.m_CurrentFrames[a * 2 + 0] = state.m_Anim.getFrame0(x);
m_RenderItem.m_CurrentFrames[a * 2 + 1] = state.m_Anim.getFrame1(x);
m_RenderItem.m_CurrentBlendFrame[a] = state.m_Anim.getFrameBlend(x);
m_RenderItem.m_CurrentBlendAnimation[a] = influences[a].second;
total += influences[a].second;
}
m_RenderItem.m_CurrentBlendAnimation = m_RenderItem.m_CurrentBlendAnimation * (1.0f / total);
}
void MeshController::render(RenderQueue_Root &queue) const
{
if (m_Static)
queue.pushRenderMesh(*m_Mesh, m_RenderItem.m_Color, m_RenderItem.m_CurrentWorld, m_RenderItem.m_Groups);
else
queue.pushSkinRenderItem(m_RenderItem);
}
}