-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathL2DModel.cpp
627 lines (544 loc) · 14.8 KB
/
L2DModel.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#include "L2DModel.h"
#include "L2DFramework.h"
#include "Id/CubismIdManager.hpp"
#include "Live2DCubismCore.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
using namespace l2d;
using namespace cocos2d;
using namespace Live2D::Cubism::Framework;
static CubismMatrix44 CubismMatrixIdentity = {};
std::unordered_set<Model*> Model::instances;
CubismIdHandle csm_id(const std::string& str)
{
return CubismFramework::GetIdManager()->GetId(str.c_str());
}
Model::Model()
{
instances.insert(this);
}
Model::~Model()
{
if (_running)
{
Model::onExit();
}
if(recreatListener)
Director::getInstance()->getEventDispatcher()->removeEventListener(recreatListener);
CC_SAFE_DELETE(model);
instances.erase(this);
}
Model* Model::create(const std::string& dir, const std::string& fileName)
{
auto ret = new (std::nothrow) Model();
if (Framework::lazyInit() && ret && ret->_init(dir, fileName))
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
uint32_t Model::getMocVersion() const
{
return model->GetMocVersion();
}
bool Model::_init(const std::string& dir, const std::string& fileName)
{
if (!Widget::init()) return false;
debugRenderer = DrawNode::create();
if (!debugRenderer) return false;
addChild(debugRenderer, 1);
model = new (std::nothrow) LAppModel();
if (!model) return false;
auto d = dir;
const auto end = d[d.size() - 1];
if (end != '/' && end != '\\')
d += "/";
if (!model->LoadAssets(d.c_str(), fileName.c_str()))
{
cocos2d::log("can't load model '%s' at [%s]", fileName.c_str(), d.c_str());
return false;
}
recreatListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [&](EventCustom*)
{
model->ReloadRenderer();
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(recreatListener, 2);
model->GetRenderer<Rendering::CubismRenderer>()->IsPremultipliedAlpha(false);
Live2D::Cubism::Core::csmVector2 tmpSizeInPixels;
Live2D::Cubism::Core::csmVector2 tmpOriginInPixels;
csmFloat32 tmpPixelsPerUnit;
csmReadCanvasInfo(model->GetModel()->GetModel(),
&tmpSizeInPixels, &tmpOriginInPixels, &tmpPixelsPerUnit);
canvasSizePixel.setSize(tmpSizeInPixels.X, tmpSizeInPixels.Y);
originPixel.set(tmpOriginInPixels.X, tmpOriginInPixels.Y);
pixelsPerUnit = model->GetModel()->GetPixelsPerUnit();
// the full canvas size corresponds to vertex position range (-1,-1) to (1,1)
Mat4 trans;
Mat4::createTranslation(
canvasSizePixel.width / pixelsPerUnit / 2,
canvasSizePixel.height / pixelsPerUnit / 2, 0.f, &trans);
Mat4 scale;
Mat4::createScale(pixelsPerUnit, pixelsPerUnit, 1.f, &scale);
constTransform = scale * trans;
debugRenderer->setPosition(canvasSizePixel.width / 2, canvasSizePixel.height / 2);
debugRenderer->setScale(pixelsPerUnit);
updateHitBoxes();
setContentSize(canvasSizePixel);
setAnchorPoint(originPixel / pixelsPerUnit / 2);
loadModelInfo();
return true;
}
void Model::loadModelInfo()
{
homeDir = model->GetHomeDir();
auto& motions = model->GetMotions();
auto setting = model->GetModelSetting();
for (auto i = 0; i < setting->GetMotionGroupCount(); i++)
{
MotionGroupInfo ginfo;
const auto groupNmae = setting->GetMotionGroupName(i);
const auto count = setting->GetMotionCount(groupNmae);
std::vector<MotionInfo> motionInfos;
for (auto j = 0; j < count; j++)
{
MotionInfo info;
const auto name = StringUtils::format("%s_%d", groupNmae, i);
info.name = name;
info.fileName = setting->GetMotionFileName(groupNmae, j);
auto motion = motions[name.c_str()];
if (motion)
{
info.duration = motion->GetDuration();
info.loopDuration = motion->GetLoopDuration();
info.fadeInTime = motion->GetFadeInTime();
info.fadeOutTime = motion->GetFadeOutTime();
info.weight = motion->GetWeight();
}
info.soundFileName = setting->GetMotionSoundFileName(groupNmae, i);
ginfo.motionInfos.push_back(info);
}
ginfo.name = groupNmae;
motionGroupInfo[ginfo.name] = ginfo;
}
auto& expressions = model->GetExpressions();
for (auto it = expressions.Begin(); it != expressions.End(); ++it)
{
expressionNames.emplace_back(it->First.GetRawString());
}
const auto m = model->GetModel()->GetModel();
const auto partIds = csmGetPartIds(m);
const auto partCount = csmGetPartCount(m);
partNames.reserve(partCount);
for (auto i = 0; i < partCount; ++i)
partNames.emplace_back(partIds[i]);
const auto partParentIds = csmGetPartParentPartIndices(m);
if (partParentIds)
{
for (auto i = 0; i < partCount; ++i)
partParents.push_back(partParentIds[i]);
}
const auto drawableIds = csmGetDrawableIds(m);
const auto drawableCount = csmGetDrawableCount(m);
drawableNames.reserve(drawableCount);
for (auto i = 0; i < drawableCount; ++i)
drawableNames.emplace_back(drawableIds[i]);
}
Size Model::getCanvasSize() const
{
return canvasSizePixel;
}
Vec2 Model::getCanvasOrigin() const
{
return originPixel;
}
float Model::getPixelsPerUnit() const
{
return pixelsPerUnit;
}
std::string Model::getDirectory() const
{
return homeDir;
}
std::vector<std::string> Model::getMotionGroupNames() const
{
std::vector<std::string> ret;
for (auto& it : motionGroupInfo)
ret.push_back(it.first);
return ret;
}
size_t Model::getMotionCount(const std::string& groupName)
{
const auto it = motionGroupInfo.find(groupName);
if (it != motionGroupInfo.end())
return it->second.motionInfos.size();
return 0;
}
std::vector<std::string> Model::getExpressionNames() const
{
return expressionNames;
}
std::string Model::getSoundFileName(const std::string& groupName, int32_t index)
{
const auto it = motionGroupInfo.find(groupName);
if (it != motionGroupInfo.end())
{
if (0 <= index && index < it->second.motionInfos.size())
return it->second.motionInfos[index].soundFileName;
}
return {};
}
bool Model::startMotion(const char* groupName, int32_t index, int32_t priority)
{
return model->StartMotion(groupName, index, priority) != InvalidMotionQueueEntryHandleValue;
}
bool Model::startRandomMotion(const char* groupName, int32_t priority)
{
return model->StartRandomMotion(groupName, priority) != InvalidMotionQueueEntryHandleValue;
}
bool Model::areaHitTest(const char* hitAreaName, float x, float y)
{
const auto it = hitBoxes.find(hitAreaName);
if (it != hitBoxes.end())
{
const auto p = convertToNodeSpace(Vec2(x, y));
return it->second.containsPoint((p - canvasSizePixel / 2) / pixelsPerUnit);
}
return false;
}
bool Model::setExpression(const char* expressionName)
{
return model->SetExpression(expressionName);
}
bool Model::setRandomExpression()
{
return model->SetRandomExpression();
}
void Model::setAutoDragging(bool b)
{
if (b)
{
if(!autoDraggingCallback)
{
autoDraggingCallback = [=](Ref*, TouchEventType e)
{
switch (e)
{
case TouchEventType::BEGAN: break;
case TouchEventType::MOVED:
{
const auto p = getTouchMovePosition();
setTouchDragging(p.x, p.y);
}
break;
case TouchEventType::ENDED:
case TouchEventType::CANCELED:
resetDragging();
break;
default: ;
}
};
}
if (!autoDragging)
addTouchEventListener(autoDraggingCallback);
}
else
{
// only remove when enabled
if (autoDragging)
addTouchEventListener(nullptr);
}
autoDragging = b;
}
void Model::setDragging(float x, float y)
{
model->SetDragging(x, y);
}
void Model::setTouchDragging(float x, float y)
{
auto p = convertToNodeSpace(Vec2(x, y));
p.x = p.x / canvasSizePixel.width * 2 - 1;
p.y = p.y / canvasSizePixel.height * 2 - 1;
p.clamp(-Vec2::ONE, Vec2::ONE);
model->SetDragging(p.x, p.y);
}
void Model::resetDragging()
{
model->SetDragging(0, 0);
}
void Model::setAcceleration(float x, float y, float z)
{
model->SetAcceleration(x, y, z);
}
void Model::setModelOpacity(uint8_t opacity)
{
model->SetOpacity(opacity / 255.f);
}
uint8_t Model::getModelOpacity() const
{
return model->GetOpacity() * 255.f;
}
void Model::setModelColor(const Color4B& color)
{
model->GetRenderer<Rendering::CubismRenderer>()->SetModelColor(
color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
}
Color4B Model::getModelColor() const
{
const auto color = model->GetRenderer<Rendering::CubismRenderer>()->GetModelColor();
return { uint8_t(color.R*255.f), uint8_t(color.G*255.f), uint8_t(color.B*255.f), uint8_t(color.A*255.f) };
}
void Model::update(float delta)
{
Widget::update(delta);
model->Update();
}
void Model::onEnter()
{
Widget::onEnter();
model->Update();
}
void Model::onExit()
{
Widget::onExit();
}
void Model::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
{
if (!_visible)
return;
getNodeToParentTransform();
const auto& proj = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
auto tr = proj * transform * constTransform;
memcpy(viewForDraw.GetArray(), tr.m, 16 * sizeof(float));
updateHitBoxes();
auto viewCopy = viewForDraw;
model->Draw(viewCopy);
if (enableDebugRect)
drawDebugRects();
Widget::draw(renderer, transform, flags);
}
void Model::updateHitBoxes()
{
auto setting = model->GetModelSetting();
for (auto i = 0; i < setting->GetHitAreasCount(); i++)
{
const auto id = setting->GetHitAreaId(i);
const auto name = setting->GetHitAreaName(i);
const auto area = model->GetDrawableArea(
model->GetModel()->GetDrawableIndex(id), CubismMatrixIdentity, CubismVector2(2, 2));
hitBoxes[name] = { area.X - 1, area.Y - 1, area.Width, area.Height };
}
}
void Model::setDebugRectEnable(bool b)
{
enableDebugRect = b;
}
DrawNode* Model::getDebugRectRenderer() const
{
return debugRenderer;
}
void Model::drawDebugRects()
{
debugRenderer->clear();
// draw user data areas
const auto& userDataAreas = model->GetUserDataAreas(CubismMatrixIdentity, CubismVector2(2, 2));
for (auto j = 0U; j < userDataAreas.GetSize(); ++j)
{
const auto area = userDataAreas[j];
auto rect = Rect(area.X - 1, area.Y - 1, area.Width, area.Height);
debugRenderer->drawSolidRect(rect.origin, rect.origin + rect.size, userDataAreaColor);
}
// draw hit boxes
for (const auto& it : hitBoxes)
{
const auto rect = it.second;
debugRenderer->drawSolidRect(rect.origin, rect.origin + rect.size, hitAreaColor);
}
// draw model canvas
const auto halfSize = Vec2(canvasSizePixel / (pixelsPerUnit * 2));
debugRenderer->drawSolidRect(-halfSize, halfSize, Color4F(1.0f, 1.0f, 1.0f, 0.2f));
// draw full canvas
//debugRenderer->drawSolidRect(-Vec2::ONE, Vec2::ONE, Color4F(1.0f, 0.0f, 0.0f, 0.2f));
}
bool Model::hitTest(const Vec2& pt, const Camera* camera, Vec3* p) const
{
#if 0
// only test hit boxes
auto local = convertToNodeSpace(pt);
local = (local - canvasSizePixel / 2) / pixelsPerUnit;
for (auto& it : hitBoxes)
{
if (it.second.containsPoint(local))
return true;
}
#else
return Widget::hitTest(pt, camera, p);
#endif
}
int32_t Model::getParameterCount() const
{
return model->GetModel()->GetParameterCount();
}
std::vector<std::string> Model::getParameterNames() const
{
const auto m = model->GetModel()->GetModel();
const auto parameterIds = csmGetParameterIds(m);
const auto parameterCount = csmGetParameterCount(m);
std::vector<std::string> ret;
ret.reserve(parameterCount);
for (auto i = 0; i < parameterCount; ++i)
ret.emplace_back(parameterIds[i]);
return ret;
}
float Model::getParameterMax(const std::string& name) const
{
auto m = model->GetModel();
return m->GetParameterMaximumValue(m->GetParameterIndex(csm_id(name)));
}
float Model::getParameterMin(const std::string& name) const
{
auto m = model->GetModel();
return m->GetParameterMinimumValue(m->GetParameterIndex(csm_id(name)));
}
float Model::getParameterDefault(const std::string& name) const
{
auto m = model->GetModel();
return m->GetParameterDefaultValue(m->GetParameterIndex(csm_id(name)));
}
float Model::getParameter(const std::string& name) const
{
auto m = model->GetModel();
return m->GetParameterValue(m->GetParameterIndex(csm_id(name)));
}
void Model::setParameter(const std::string& name, float value, float weight)
{
model->GetModel()->SetParameterValue(csm_id(name), value, weight);
}
void Model::addParameter(const std::string& name, float value, float weight)
{
model->GetModel()->AddParameterValue(csm_id(name), value, weight);
}
void Model::multiplyParameter(const std::string& name, float value, float weight)
{
model->GetModel()->MultiplyParameterValue(csm_id(name), value, weight);
}
int32_t Model::getPartCount() const
{
return model->GetModel()->GetPartCount();
}
std::vector<std::string> Model::getPartNames() const
{
return partNames;
}
std::vector<int> Model::getPartParents() const
{
return partParents;
}
float Model::getPartOpacity(const std::string& name) const
{
return model->GetModel()->GetPartOpacity(csm_id(name));
}
void Model::setPartOpacity(const std::string& name, float opacity)
{
model->GetModel()->SetPartOpacity(csm_id(name), opacity);
}
int32_t Model::getDrawableCount() const
{
return model->GetModel()->GetDrawableCount();
}
std::vector<std::string> Model::getDrawableNames() const
{
return drawableNames;
}
float Model::getDrawableOpacity(const std::string& name) const
{
return model->GetModel()->GetDrawableOpacity(getDrawableIndex(name));
}
int32_t Model::getDrawableCulling(const std::string& name) const
{
return model->GetModel()->GetDrawableCulling(getDrawableIndex(name));
}
int32_t Model::getDrawableBlendMode(const std::string& name) const
{
return (int32_t)model->GetModel()->GetDrawableBlendMode(getDrawableIndex(name));
}
Vec2 Model::getGravity() const
{
const auto phy = model->GetPhysics();
if (phy)
{
const auto g = phy->GetOptions().Gravity;
return {g.X, g.Y};
}
return {};
}
void Model::setGravity(const Vec2& gravity)
{
auto phy = model->GetPhysics();
if (phy)
{
auto op = phy->GetOptions();
op.Gravity = CubismVector2(gravity.x, gravity.y);
phy->SetOptions(op);
}
}
Vec2 Model::getWind() const
{
const auto phy = model->GetPhysics();
if (phy)
{
const auto g = phy->GetOptions().Wind;
return {g.X, g.Y};
}
return {};
}
void Model::setWind(const Vec2& wind)
{
auto phy = model->GetPhysics();
if (phy)
{
auto op = phy->GetOptions();
op.Wind = CubismVector2(wind.x, wind.y);
phy->SetOptions(op);
}
}
void Model::setBlinkingInterval(float seconds)
{
auto blk = model->GetEyeBlink();
if (blk)
{
blk->SetBlinkingInterval(seconds);
}
}
void Model::setBlinkingSettings(float closing, float closed, float opening)
{
auto blk = model->GetEyeBlink();
if (blk)
{
blk->SetBlinkingSettings(closing, closed, opening);
}
}
bool Model::getLipSync() const
{
return model->GetLipSync();
}
void Model::setLipSync(bool value)
{
model->SetLipSync(value);
}
float Model::getLipValue() const
{
return model->GetLipValue();
}
void Model::setLipValue(float value)
{
model->SetLipValue(value);
}
void Model::setSoundEventHandler(const LAppModel::SoundEventHandler& f)
{
model->SetSoundEventHandler(f);
}
int32_t Model::getDrawableIndex(const std::string& name) const
{
return model->GetModel()->GetDrawableIndex(csm_id(name));
}