-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCubismDrawableComponent.cpp
More file actions
411 lines (334 loc) · 10.8 KB
/
CubismDrawableComponent.cpp
File metadata and controls
411 lines (334 loc) · 10.8 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
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
/**
* 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 "Model/CubismDrawableComponent.h"
#include "Model/CubismModelActor.h"
#include "Model/CubismModelComponent.h"
#include "Rendering/CubismDrawableSceneProxy.h"
#include "UserData/CubismUserData3Json.h"
#include "CubismLog.h"
#include "Live2DCubismCore.h"
#include "Materials/MaterialInstanceDynamic.h"
#include "Math/Vector.h"
UCubismDrawableComponent::UCubismDrawableComponent()
{
PrimaryComponentTick.bCanEverTick = true;
PrimaryComponentTick.TickGroup = TG_DuringPhysics;
bTickInEditor = true;
bBoundsDirty = true;
}
void UCubismDrawableComponent::Setup(UCubismModelComponent* InModel)
{
check(InModel);
check(Index >= 0 && Index < InModel->GetDrawableCount());
if (Model == InModel)
{
return;
}
Model = InModel;
Id = Model->GetDrawableId(Index);
RenderOrder = Model->GetDrawableRenderOrder(Index);
TextureIndex = Model->GetDrawableTextureIndex(Index);
{
const int32 VertexIndexCount(Model->GetDrawableVertexIndexCount(Index));
const int32 VertexCount(Model->GetDrawableVertexCount(Index));
const int32 MaskCount(Model->GetDrawableMaskCount(Index));
const csmVector2* DrawableVertexUvs(Model->GetDrawableVertexUv(Index));
VertexUvs.Empty();
VertexUvs.Reserve(VertexCount);
for (int32 i = 0; i < VertexCount; i++)
{
VertexUvs.Add(FVector2D(DrawableVertexUvs[i].X, DrawableVertexUvs[i].Y));
}
const csmVector2* DrawableVertexPositions(Model->GetDrawableVertexPosition(Index));
VertexPositions.Empty();
VertexPositions.Reserve(VertexCount);
for (int32 i = 0; i < VertexCount; i++)
{
VertexPositions.Add(FVector2D(-DrawableVertexPositions[i].X, DrawableVertexPositions[i].Y));
}
const uint16* DrawableVertexIndices(Model->GetDrawableVertexIndex(Index));
VertexIndices.Empty();
VertexIndices.Reserve(VertexIndexCount);
for (int32 i = 0; i < VertexIndexCount; ++i)
{
VertexIndices.Add(static_cast<int32>(DrawableVertexIndices[i]));
}
const int32* DrawableMasks(Model->GetDrawableMask(Index));
Masks.Empty();
Masks.Reserve(MaskCount);
for (int32 MaskIndex = 0; MaskIndex < MaskCount; ++MaskIndex)
{
Masks.Add(DrawableMasks[MaskIndex]);
}
}
Opacity = Model->GetDrawableOpacity(Index);
BaseColor = FLinearColor::White;
MultiplyColor = Model->GetDrawableMultiplyColor(Index);
ScreenColor = Model->GetDrawableScreenColor(Index);
bTwoSided = Model->GetDrawableIsTwoSided(Index);
UserMultiplyColor = MultiplyColor;
UserScreenColor = ScreenColor;
bUserTwoSided = bTwoSided;
bOverwriteFlagForDrawableMultiplyColors = false;
bOverwriteFlagForDrawableScreenColors = false;
bOverwriteFlagForDrawableIsTwoSided = false;
ParentPartIndex = Model->GetDrawableParentPartIndex(Index);
BlendMode = Model->GetDrawableBlendMode(Index);
InvertedMask = Model->GetDrawableInvertedMask(Index);
FString MaterialName;
switch(BlendMode)
{
case ECubismDrawableBlendMode::Normal:
{
MaterialName = !IsMasked()? TEXT("CustomUnlitNormal") : InvertedMask? TEXT("CustomUnlitNormalMaskedInverted") : TEXT("CustomUnlitNormalMasked");
break;
}
case ECubismDrawableBlendMode::Additive:
{
MaterialName = !IsMasked()? TEXT("CustomUnlitAdditive") : InvertedMask? TEXT("CustomUnlitAdditiveMaskedInverted") : TEXT("CustomUnlitAdditiveMasked");
break;
}
case ECubismDrawableBlendMode::Multiplicative:
{
MaterialName = !IsMasked()? TEXT("CustomUnlitMultiplicative") : InvertedMask? TEXT("CustomUnlitMultiplicativeMaskedInverted") : TEXT("CustomUnlitMultiplicativeMasked");
break;
}
default:
{
ensure(false);
break;
}
}
UMaterial* Material = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullptr, *(TEXT("/Live2DCubismSDK/Materials") / MaterialName)));
UMaterialInstanceDynamic* MaterialInstance = UMaterialInstanceDynamic::Create(Material, this, *MaterialName);
SetMaterial(0, static_cast<UMaterialInterface*>(MaterialInstance));
if (!Model->UserDataJson)
{
UserDataTag = TEXT("");
}
else
{
const FCubismUserDataEntry& UserDataEntry = Model->UserDataJson->Data[ECubismUserDataTargetType::ArtMesh];
if (UserDataEntry.Tags.Contains(Id))
{
UserDataTag = UserDataEntry.Tags[Id];
}
else
{
UserDataTag = TEXT("");
}
}
AddTickPrerequisiteComponent(Model); // must be updated after model updated
}
TArray<int32> UCubismDrawableComponent::GetVertexIndices() const
{
return VertexIndices;
}
TArray<FVector2D> UCubismDrawableComponent::GetVertexPositions() const
{
return VertexPositions;
}
FVector UCubismDrawableComponent::ToGlobalPosition(const FVector2D VertexPosition) const
{
const float Scale = 0.01f * Model->GetPixelsPerUnit();
// align the model on the y-z plane
return FVector(0.0f, Scale * VertexPosition.X, Scale * VertexPosition.Y);
}
TArray<FVector2D> UCubismDrawableComponent::GetVertexUvs() const
{
return VertexUvs;
}
const TArray<int32> UCubismDrawableComponent::GetDrawableMask() const
{
return TArray<int32>(Model->GetDrawableMask(Index), Model->GetDrawableMaskCount(Index));
}
int32 UCubismDrawableComponent::GetDrawableMaskCount() const
{
return Model->GetDrawableMaskCount(Index);
}
TObjectPtr<UCubismModelComponent> UCubismDrawableComponent::GetModel()
{
if (TObjectPtr<UCubismModelComponent> ModelComp = Cast<UCubismModelComponent>(GetOwner()->FindComponentByClass<UCubismModelComponent>()))
{
return ModelComp;
}
return nullptr;
}
// UObject interface
void UCubismDrawableComponent::PostLoad()
{
Super::PostLoad();
const TObjectPtr<UCubismModelComponent> ModelComp = GetModel();
if (ModelComp)
{
Setup(ModelComp);
}
}
#if WITH_EDITOR
void UCubismDrawableComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
const FName PropertyName = PropertyChangedEvent.GetPropertyName();
if (PropertyName == GET_MEMBER_NAME_CHECKED(UCubismDrawableComponent, bOverwriteFlagForDrawableMultiplyColors))
{
// If the flag is changed from false to true, the stored color is applied.
if (bOverwriteFlagForDrawableMultiplyColors)
{
MultiplyColor = UserMultiplyColor;
}
// If the flag is changed from true to false, the color from the model is applied.
else
{
MultiplyColor = Model->GetDrawableMultiplyColor(Index);
}
}
if (PropertyName == GET_MEMBER_NAME_CHECKED(UCubismDrawableComponent, MultiplyColor))
{
UserMultiplyColor = MultiplyColor;
}
if (PropertyName == GET_MEMBER_NAME_CHECKED(UCubismDrawableComponent, bOverwriteFlagForDrawableScreenColors))
{
// If the flag is changed from false to true, the stored color is applied.
if (bOverwriteFlagForDrawableScreenColors)
{
ScreenColor = UserScreenColor;
}
// If the flag is changed from true to false, the color from the model is applied.
else
{
ScreenColor = Model->GetDrawableScreenColor(Index);
}
}
if (PropertyName == GET_MEMBER_NAME_CHECKED(UCubismDrawableComponent, ScreenColor))
{
UserScreenColor = ScreenColor;
}
if (PropertyName == GET_MEMBER_NAME_CHECKED(UCubismDrawableComponent, bOverwriteFlagForDrawableIsTwoSided))
{
// If the flag is changed from false to true, the stored value is applied.
if (bOverwriteFlagForDrawableIsTwoSided)
{
bTwoSided = bUserTwoSided;
}
// If the flag is changed from true to false, the value from the model is applied.
else
{
bTwoSided = Model->GetDrawableIsTwoSided(Index);
}
}
if (PropertyName == GET_MEMBER_NAME_CHECKED(UCubismDrawableComponent, bTwoSided))
{
bUserTwoSided = bTwoSided;
}
}
#endif
// End of UObject interface
// UActorComponent interface
void UCubismDrawableComponent::OnComponentCreated()
{
Super::OnComponentCreated();
const TObjectPtr<UCubismModelComponent> ModelComp = GetModel();
if (ModelComp)
{
Setup(ModelComp);
}
}
void UCubismDrawableComponent::SendRenderDynamicData_Concurrent()
{
Super::SendRenderDynamicData_Concurrent();
if (FCubismDrawableSceneProxy* DrawableProxy = static_cast<FCubismDrawableSceneProxy*>(SceneProxy))
{
FCubismDrawableDynamicMeshData NewDynamicData;
NewDynamicData.Index = Index;
for (const FVector2D& LocalPosition : GetVertexPositions())
{
NewDynamicData.Positions.Add(FVector3f(ToGlobalPosition(LocalPosition)));
}
for (const FVector2D& UV : GetVertexUvs())
{
NewDynamicData.UVs.Add(FVector2f(UV));
}
NewDynamicData.Indices.Append(VertexIndices);
NewDynamicData.bTwoSided = bTwoSided;
ENQUEUE_RENDER_COMMAND(DrawableUpdateDynamicData)(
[DrawableProxy, NewDynamicData](FRHICommandListImmediate& RHICmdList)
{
DrawableProxy->UpdateDynamicData(RHICmdList, NewDynamicData);
}
);
}
}
void UCubismDrawableComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (Model->GetDrawableDynamicFlagOpacityDidChange(Index))
{
Opacity = Model->GetDrawableOpacity(Index);
}
if (Model->GetDrawableDynamicFlagVertexPositionsDidChange(Index))
{
VertexPositions.Empty();
const csmVector2* DrawableVertexPositions(Model->GetDrawableVertexPosition(Index));
const int32 VertexCount(Model->GetDrawableVertexCount(Index));
VertexPositions.Reserve(VertexCount);
for (int32 i = 0; i < VertexCount; i++)
{
VertexPositions.Add(FVector2D(-DrawableVertexPositions[i].X, DrawableVertexPositions[i].Y));
}
bBoundsDirty = true;
MarkRenderDynamicDataDirty();
}
if (Model->GetDrawableDynamicFlagBlendColorDidChange(Index))
{
if (!bOverwriteFlagForDrawableMultiplyColors)
{
MultiplyColor = Model->GetDrawableMultiplyColor(Index);
}
if (!bOverwriteFlagForDrawableScreenColors)
{
ScreenColor = Model->GetDrawableScreenColor(Index);
}
}
}
// End of UActorComponent interface
//~ Begin USceneComponent Interface
FBoxSphereBounds UCubismDrawableComponent::CalcBounds(const FTransform& LocalToWorld) const
{
if (Model)
{
if (bBoundsDirty)
{
FBox Box(ForceInit);
for (const FVector2D& LocalPosition : GetVertexPositions())
{
Box += ToGlobalPosition(LocalPosition);
}
LocalBounds = FBoxSphereBounds(Box);
bBoundsDirty = false;
}
}
return LocalBounds.TransformBy(LocalToWorld);
}
//~ End USceneComponent Interface
//~ Begin UPrimitiveComponent Interface
FPrimitiveSceneProxy* UCubismDrawableComponent::CreateSceneProxy()
{
FCubismDrawableDynamicMeshData DynamicData;
DynamicData.Index = Index;
for (const FVector2D& LocalPosition : GetVertexPositions())
{
DynamicData.Positions.Add(FVector3f(ToGlobalPosition(LocalPosition)));
}
for (const FVector2D& UV : GetVertexUvs())
{
DynamicData.UVs.Add(FVector2f(UV));
}
DynamicData.Indices.Append(VertexIndices);
DynamicData.bTwoSided = bTwoSided;
return new FCubismDrawableSceneProxy(this, DynamicData);
}
//~ End UPrimitiveComponent Interface