-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathCubismUserModel.cpp
More file actions
321 lines (266 loc) · 7.94 KB
/
Copy pathCubismUserModel.cpp
File metadata and controls
321 lines (266 loc) · 7.94 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Copyright(c) Live2D Inc. All rights reserved.
*
* Use of this source code is governed by the Live2D Open Software license
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
*/
#include "CubismUserModel.hpp"
#include "Motion/CubismMotion.hpp"
#include "Physics/CubismPhysics.hpp"
namespace Live2D { namespace Cubism { namespace Framework {
CubismUserModel::CubismUserModel()
: _moc(NULL)
, _model(NULL)
, _motionManager(NULL)
, _expressionManager(NULL)
, _eyeBlink(NULL)
, _breath(NULL)
, _modelMatrix(NULL)
, _pose(NULL)
, _dragManager(NULL)
, _physics(NULL)
, _modelUserData(NULL)
, _look(NULL)
, _initialized(false)
, _updating(false)
, _opacity(1.0f)
, _lastLipSyncValue(0.0f)
, _accelerationX(0.0f)
, _accelerationY(0.0f)
, _accelerationZ(0.0f)
, _mocConsistency(false)
, _motionConsistency(false)
, _debugMode(false)
, _renderer(NULL)
{
// モーションマネージャーを作成
// MotionQueueManagerクラスからの継承なので使い方は同じ
_motionManager = CSM_NEW CubismMotionManager();
_motionManager->SetEventCallback(CubismDefaultMotionEventCallback, this);
// 表情モーションマネージャを作成
_expressionManager = CSM_NEW CubismExpressionMotionManager();
// ドラッグによるアニメーション
_dragManager = CSM_NEW CubismTargetPoint();
}
CubismUserModel::~CubismUserModel()
{
CSM_DELETE(_motionManager);
CSM_DELETE(_expressionManager);
if (_moc)
{
_moc->DeleteModel(_model);
}
CubismMoc::Delete(_moc);
CSM_DELETE(_modelMatrix);
CubismPose::Delete(_pose);
CubismEyeBlink::Delete(_eyeBlink);
CubismBreath::Delete(_breath);
CSM_DELETE(_dragManager);
CubismPhysics::Delete(_physics);
CubismModelUserData::Delete(_modelUserData);
CubismLook::Delete(_look);
DeleteRenderer();
}
void CubismUserModel::SetAcceleration(csmFloat32 x, csmFloat32 y, csmFloat32 z)
{
_accelerationX = x;
_accelerationY = y;
_accelerationZ = z;
}
void CubismUserModel::LoadModel(const csmByte* buffer, csmSizeInt size, csmBool shouldCheckMocConsistency)
{
_moc = CubismMoc::Create(buffer, size, shouldCheckMocConsistency);
if (_moc == NULL)
{
CubismLogError("Failed to CubismMoc::Create().");
return;
}
_model = _moc->CreateModel();
if (_model == NULL)
{
CubismLogError("Failed to CreateModel().");
return;
}
_model->SaveParameters();
_modelMatrix = CSM_NEW CubismModelMatrix(_model->GetCanvasWidth(), _model->GetCanvasHeight());
}
ACubismMotion* CubismUserModel::LoadExpression(const csmByte* buffer, csmSizeInt size, const csmChar* name)
{
if (!buffer)
{
CubismLogError("Failed to LoadExpression().");
return NULL;
}
return CubismExpressionMotion::Create(buffer, size);
}
void CubismUserModel::LoadPose(const csmByte* buffer, csmSizeInt size)
{
_pose = CubismPose::Create(buffer, size);
if (!_pose)
{
CubismLogError("Failed to LoadPose().");
}
}
void CubismUserModel::LoadPhysics(const csmByte* buffer, csmSizeInt size)
{
_physics = CubismPhysics::Create(buffer, size);
if (!_physics)
{
CubismLogError("Failed to LoadPhysics().");
}
}
void CubismUserModel::LoadUserData(const csmByte* buffer, csmSizeInt size)
{
if (!buffer)
{
CubismLogError("Failed to LoadUserData().");
return;
}
_modelUserData = CubismModelUserData::Create(buffer, size);
}
csmBool CubismUserModel::IsHit(CubismIdHandle drawableId, csmFloat32 pointX, csmFloat32 pointY)
{
const csmInt32 drawIndex = _model->GetDrawableIndex(drawableId);
if (drawIndex < 0)
{
return false; // 存在しない場合はfalse
}
const csmInt32 count = _model->GetDrawableVertexCount(drawIndex);
const csmFloat32* vertices = _model->GetDrawableVertices(drawIndex);
csmFloat32 left = vertices[0];
csmFloat32 right = vertices[0];
csmFloat32 top = vertices[1];
csmFloat32 bottom = vertices[1];
for (csmInt32 j = 1; j < count; ++j)
{
csmFloat32 x = vertices[Constant::VertexOffset + j * Constant::VertexStep];
csmFloat32 y = vertices[Constant::VertexOffset + j * Constant::VertexStep + 1];
if (x < left)
{
left = x; // Min x
}
if (x > right)
{
right = x; // Max x
}
if (y < top)
{
top = y; // Min y
}
if (y > bottom)
{
bottom = y; // Max y
}
}
const csmFloat32 tx = _modelMatrix->InvertTransformX(pointX);
const csmFloat32 ty = _modelMatrix->InvertTransformY(pointY);
return ((left <= tx) && (tx <= right) && (top <= ty) && (ty <= bottom));
}
ACubismMotion* CubismUserModel::LoadMotion(const csmByte* buffer, csmSizeInt size, const csmChar* name,
ACubismMotion::FinishedMotionCallback onFinishedMotionHandler, ACubismMotion::BeganMotionCallback onBeganMotionHandler,
ICubismModelSetting* modelSetting, const csmChar* group, const csmInt32 index, csmBool shouldCheckMotionConsistency)
{
if (!buffer)
{
CubismLogError("Failed to LoadMotion(). Buffer is NULL.");
return NULL;
}
ACubismMotion* motion = CubismMotion::Create(buffer, size, onFinishedMotionHandler, onBeganMotionHandler, shouldCheckMotionConsistency);
if (!motion)
{
CubismLogError("Failed to create motion from buffer in LoadMotion().");
return NULL;
}
// 必要であればモーションフェード値を上書き
if (modelSetting)
{
const csmFloat32 fadeInTime = modelSetting->GetMotionFadeInTimeValue(group, index);
if (fadeInTime >= 0.0f)
{
motion->SetFadeInTime(fadeInTime);
}
const csmFloat32 fadeOutTime = modelSetting->GetMotionFadeOutTimeValue(group, index);
if (fadeOutTime >= 0.0f)
{
motion->SetFadeOutTime(fadeOutTime);
}
}
return motion;
}
void CubismUserModel::SetDragging(csmFloat32 x, csmFloat32 y)
{
_dragManager->Set(x, y);
}
CubismModelMatrix* CubismUserModel::GetModelMatrix() const
{
return _modelMatrix;
}
csmBool CubismUserModel::IsInitialized()
{
return _initialized;
}
void CubismUserModel::IsInitialized(csmBool v)
{
_initialized = v;
}
csmBool CubismUserModel::IsUpdating()
{
return _updating;
}
void CubismUserModel::IsUpdating(csmBool v)
{
_updating = v;
}
void CubismUserModel::SetRenderTargetSize(csmUint32 width, csmUint32 height)
{
if (_renderer)
{
_renderer->SetRenderTargetSize(width, height);
}
}
void CubismUserModel::SetOpacity(csmFloat32 a)
{
_opacity = a;
}
csmFloat32 CubismUserModel::GetOpacity()
{
return _opacity;
}
CubismModel* CubismUserModel::GetModel() const
{
return _model;
}
Core::csmMocVersion CubismUserModel::GetMocVersionFromBuffer(const csmByte* buffer, csmSizeInt size)
{
return CubismMoc::GetMocVersionFromBuffer(buffer, size);
}
void CubismUserModel::CreateRenderer(csmUint32 width, csmUint32 height, csmInt32 maskBufferCount)
{
if (_renderer)
{
DeleteRenderer();
}
_renderer = Rendering::CubismRenderer::Create(width, height);
_renderer->Initialize(_model, maskBufferCount);
}
void CubismUserModel::DeleteRenderer()
{
if (_renderer)
{
Rendering::CubismRenderer::Delete(_renderer);
_renderer = NULL;
}
}
void CubismUserModel::CubismDefaultMotionEventCallback(const CubismMotionQueueManager* caller, const csmString& eventValue, void* customData)
{
CubismUserModel* model = reinterpret_cast<CubismUserModel*>(customData);
if (model != NULL)
{
model->MotionEventFired(eventValue);
}
}
void CubismUserModel::MotionEventFired(const csmString& eventValue)
{
CubismLogInfo("%s",eventValue.GetRawString());
}
}}}