-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPreviewController.cs
More file actions
440 lines (364 loc) · 17.5 KB
/
Copy pathPreviewController.cs
File metadata and controls
440 lines (364 loc) · 17.5 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using Data;
using Loading;
using Services;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.VFX;
using Utils;
namespace Preview
{
public class PreviewController : MonoBehaviour
{
[SerializeField] private Camera mainCamera;
[SerializeField] private PreviewCameraController previewCameraController;
[SerializeField] private DragRotator avatarRotator;
[SerializeField] private DragRotator wearableRotator;
[SerializeField] private PreviewUIPresenter previewUIPresenter;
[SerializeField] private AvatarLoader avatarLoader;
[SerializeField] private WearableLoader wearableLoader;
[SerializeField] private Vector3 wearableOffset = new(-5, 0, 0);
[SerializeField] private EmoteAnimationController emoteAnimationController;
[SerializeField] private VisualEffect confirmationVFX;
[SerializeField] private GameObject animationReference;
[SerializeField] private GameObject platform;
[SerializeField] private float wearablePadding = 0.15f;
private bool _loading;
private bool _shouldReload;
private bool _shouldCleanup;
private AvatarColors _marketplaceColors;
private string _marketplaceColorsProfile;
private void Start()
{
previewUIPresenter.ShowAvatarClicked += OnShowAvatarClicked;
previewUIPresenter.ShowWearableClicked += OnShowWearableClicked;
previewUIPresenter.EmoteToggleClicked += OnEmoteToggleClicked;
previewUIPresenter.AvatarColorChanged += OnAvatarColorChanged;
previewUIPresenter.ContainerDrag += avatarRotator.OnDrag;
previewUIPresenter.ContainerDrag += wearableRotator.OnDrag;
emoteAnimationController.EmoteAnimationEnded += OnEmoteAnimationEnded;
avatarRotator.AllowVertical = false;
wearableRotator.AllowVertical = false;
StartCoroutine(Reload());
}
private void OnEmoteAnimationEnded()
{
previewUIPresenter.SetAnimationPlaying(false);
}
private void OnEmoteToggleClicked(bool playing)
{
if (playing)
{
emoteAnimationController.ReplayEmote();
}
else
{
emoteAnimationController.StopEmote();
}
}
private void OnAvatarColorChanged(PreviewUIPresenter.ColorTarget target, Color color)
{
if (_marketplaceColors == null) return;
_marketplaceColors = new AvatarColors(
target == PreviewUIPresenter.ColorTarget.Eyes ? color : _marketplaceColors.Eyes,
target == PreviewUIPresenter.ColorTarget.Hair ? color : _marketplaceColors.Hair,
target == PreviewUIPresenter.ColorTarget.Skin ? color : _marketplaceColors.Skin);
avatarLoader.ApplyColors(_marketplaceColors);
wearableLoader.ApplyColors(_marketplaceColors);
}
private void OnShowWearableClicked()
{
PlayerPrefs.SetInt("PreviewAvatarShown", 0);
previewCameraController.ShowMarketplaceWearable(true);
wearableRotator.ResetRotation();
}
private void OnShowAvatarClicked()
{
PlayerPrefs.SetInt("PreviewAvatarShown", 1);
previewCameraController.ShowMarketplaceWearable(false);
avatarRotator.ResetRotation();
}
public void SetSpringBonesParams(SpringBones.SpringBonesParamsPayload payload) =>
avatarLoader.SetSpringBonesParams(payload);
public float GetEmoteLength() => emoteAnimationController.GetEmoteLength();
public bool IsEmotePlaying() => emoteAnimationController.IsEmotePlaying();
public void PlayEmote()
{
if (emoteAnimationController.IsPaused)
emoteAnimationController.ResumeEmote();
else
emoteAnimationController.ReplayEmote();
}
public void PauseEmote() => emoteAnimationController.PauseEmote();
public void GoToEmote(float seconds) => emoteAnimationController.GoToEmote(seconds);
public void StopEmote() => emoteAnimationController.StopEmote();
public void EnableSound() => emoteAnimationController.EnableSound();
public void DisableSound() => emoteAnimationController.DisableSound();
public bool HasSound() => emoteAnimationController.HasAudio;
public void InvokeReload()
{
_shouldCleanup = false;
StartCoroutine(Reload());
}
private async Awaitable Reload()
{
if (_loading)
{
_shouldReload = true;
return;
}
Cleanup();
previewUIPresenter.ShowLoader(true);
_loading = true;
mainCamera.cullingMask = 0; // Render nothing
avatarLoader.enabled = false; // Disables Update for Outline
wearableLoader.enabled = false; // Disables Update for Outline
do
{
_shouldReload = false;
// We store the instance in case it gets recreated by a call to AangConfiguration.RecreateFrom
var config = AangConfiguration.Instance;
avatarRotator.enabled = false;
wearableRotator.enabled = false;
avatarRotator.ResetRotation();
wearableRotator.ResetRotation();
animationReference.SetActive(config.ShowAnimationReference);
platform.SetActive(config.Mode is PreviewMode.Authentication);
mainCamera.backgroundColor = config.Background;
mainCamera.orthographic = config.Projection == "orthographic";
previewUIPresenter.EnableLoader(!config.DisableLoader);
previewCameraController.SetMode(config.Mode);
confirmationVFX.gameObject.SetActive(config.Mode is PreviewMode.Jesus);
var hasEmoteOverride = false;
var hasWearableOverride = false;
var hasEmoteAudio = false;
var showingAvatar = false;
try
{
await EntityService.PreloadBodyEntities();
switch (config.Mode)
{
case PreviewMode.Marketplace:
var urns = await LoadUrns(config);
Assert.IsTrue(urns.Count == 1,
$"Marketplace mode only allows one urn, found: {urns.Count}");
var result = await LoadForMarketplace(config.Profile, urns[0], config.Emote);
previewUIPresenter.EnableEmoteControls(result.emoteOverride);
if (result.validRepresentation)
{
showingAvatar = PlayerPrefs.GetInt("PreviewAvatarShown", 0) == 1 ||
result.emoteOverride;
previewUIPresenter.SetSwitcherState(
showingAvatar
? PreviewUIPresenter.SwitcherState.Avatar
: PreviewUIPresenter.SwitcherState.Wearable, result.avatarBodyShape);
}
else
{
previewUIPresenter.SetSwitcherState(PreviewUIPresenter.SwitcherState.WearableLocked,
result.avatarBodyShape);
}
hasEmoteOverride = result.emoteOverride;
hasWearableOverride = !hasEmoteOverride;
hasEmoteAudio = result.emoteOverrideAudio;
break;
case PreviewMode.Authentication:
case PreviewMode.Profile:
showingAvatar = true;
await LoadForProfile(config.Profile, config.Emote);
break;
case PreviewMode.Builder:
await LoadForBuilder(config.BodyShape,
config.EyeColor,
config.HairColor,
config.SkinColor,
config.Urns.ToArray(),
config.Emote,
config.Base64);
break;
case PreviewMode.Jesus:
showingAvatar = true;
confirmationVFX.Play();
await LoadForProfile(config.Profile, "character/Particles_Anim", true);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
catch (Exception e)
{
JSBridge.NativeCalls.OnError(e.Message);
throw;
}
// Wait for 1 frame for animation to kick in before re-centering the object on screen
await Awaitable.NextFrameAsync();
if (hasWearableOverride)
{
GameObjectUtils.CenterAndFit(wearableLoader.transform, mainCamera, wearablePadding);
wearableLoader.transform.position += wearableOffset;
}
else if (hasEmoteOverride)
{
GameObjectUtils.CenterAndFit(avatarLoader.transform, mainCamera, wearablePadding);
}
if (config.Mode is PreviewMode.Marketplace)
{
previewCameraController.ShowMarketplaceWearable(!showingAvatar);
}
else if (config.Mode is PreviewMode.Builder)
{
avatarRotator.DragSpeed = 2f;
}
avatarRotator.enabled = true;
wearableRotator.enabled = true;
avatarRotator.EnableAutoRotate = config.Mode is PreviewMode.Marketplace && !hasEmoteOverride;
previewUIPresenter.EnableEmoteControls(hasEmoteOverride);
previewUIPresenter.EnableZoom(config.Mode is PreviewMode.Marketplace or PreviewMode.Builder);
previewUIPresenter.EnableSwitcher(hasWearableOverride);
previewUIPresenter.EnableAudioControls(hasEmoteAudio);
previewUIPresenter.EnableColorPicker(config.Mode is PreviewMode.Marketplace);
if (config.Mode is PreviewMode.Marketplace)
{
previewUIPresenter.SetColorPickerColors(_marketplaceColors);
}
} while (_shouldReload);
previewUIPresenter.ShowLoader(false);
_loading = false;
mainCamera.cullingMask = -1; // Render everything
avatarLoader.enabled = true; // Enables Update for Outline
wearableLoader.enabled = true;
if (_shouldCleanup)
{
Cleanup();
}
JSBridge.NativeCalls.OnLoadComplete();
}
private async Awaitable LoadForBuilder(string bodyShapeName,
Color? eyeColor,
Color? hairColor,
Color? skinColor,
string[] urns,
string emoteName,
List<byte[]> base64)
{
var bodyShape = bodyShapeName.Equals(WearablesConstants.BODY_SHAPE_FEMALE, StringComparison.OrdinalIgnoreCase)
? BodyShape.Female
: BodyShape.Male;
var base64Entities = base64.Select(EntityDefinition.FromBase64).ToArray();
var base64Emote = base64Entities.FirstOrDefault(e => e.Type == EntityType.Emote);
var base64WearableEntities = base64Entities.Where(e => e.Type != EntityType.Emote);
var urnEntities = await EntityService.GetEntities(urns);
// Slot-based deduplication: one wearable per category, base64 items take priority
var slots = new Dictionary<string, EntityDefinition>();
foreach (var entity in urnEntities.Where(e => e.Type != EntityType.Emote))
{
slots[entity.Category] = entity;
}
foreach (var entity in base64WearableEntities)
{
slots[entity.Category] = entity;
}
var wearableEntities = slots.Values.ToArray();
var colors = new AvatarColors(eyeColor ?? Color.black, hairColor ?? Color.black, skinColor ?? Color.black);
var emoteEntity = base64Emote ?? (emoteName == "idle" ? null : EntityDefinition.FromEmbeddedEmote(emoteName, true));
await avatarLoader.LoadAvatar(bodyShape,
wearableEntities,
emoteEntity,
Array.Empty<string>(),
colors);
if (AangConfiguration.Instance.DisableFace)
{
avatarLoader.HideFacialFeatures();
}
}
private async Awaitable<(bool emoteOverride, bool emoteOverrideAudio, bool validRepresentation, BodyShape avatarBodyShape)> LoadForMarketplace(string profileID, string urn,
string defaultEmote)
{
Assert.IsNotNull(profileID);
Assert.IsNotNull(urn);
Assert.IsNotNull(defaultEmote);
var avatar = await APIService.GetAvatar(profileID);
var avatarBodyShape = avatar.GetBodyShape();
var avatarColors = _marketplaceColors != null && _marketplaceColorsProfile == profileID
? _marketplaceColors
: avatar.GetAvatarColors();
_marketplaceColors = avatarColors;
_marketplaceColorsProfile = profileID;
var allEntities = await EntityService.GetEntities(avatar.wearables.Append(urn).ToArray());
var overrideDefinition = allEntities.First(ed => ed.URN == urn);
bool hasValidRepresentation;
IEnumerable<EntityDefinition> wearables;
EntityDefinition emoteDefinition;
switch (overrideDefinition.Type)
{
case EntityType.Emote:
emoteDefinition = overrideDefinition;
wearables = allEntities.Where(wd => wd.URN != emoteDefinition.URN);
hasValidRepresentation = true;
break;
case EntityType.Wearable or EntityType.FacialFeature:
emoteDefinition = defaultEmote == "idle" ? null : EntityDefinition.FromEmbeddedEmote(defaultEmote, false);
wearables = allEntities.Where(ed =>
ed.Category != overrideDefinition.Category || ed.URN == overrideDefinition.URN);
hasValidRepresentation = overrideDefinition.HasRepresentation(avatarBodyShape);
break;
default:
throw new NotSupportedException($"Trying to override type: {overrideDefinition.Type}");
}
// Load the avatar
if (hasValidRepresentation)
{
await avatarLoader.LoadAvatar(avatarBodyShape, wearables, emoteDefinition,
avatar.forceRender.Append(overrideDefinition.Category).ToArray(),
avatarColors);
}
if (overrideDefinition.Type is EntityType.Wearable or EntityType.FacialFeature)
{
await wearableLoader.LoadWearable(overrideDefinition, avatarBodyShape, avatarColors);
}
else
{
wearableLoader.Cleanup();
}
// TODO: This check for audio clip is ugly
return (overrideDefinition.Type == EntityType.Emote, emoteAnimationController.HasAudio, hasValidRepresentation, avatarBodyShape);
}
private async Awaitable LoadForProfile(string profileID, string defaultEmote, bool loop = false)
{
Assert.IsNotNull(profileID);
var avatar = await APIService.GetAvatar(profileID);
var entities = await EntityService.GetEntities(avatar.wearables);
await avatarLoader.LoadAvatar(avatar.GetBodyShape(), entities,
EntityDefinition.FromEmbeddedEmote(defaultEmote, loop), avatar.forceRender, avatar.GetAvatarColors());
}
private async Awaitable<List<string>> LoadUrns(AangConfiguration config)
{
if (config.Urns.Count > 0) return config.Urns;
// If we have a contract and item id or token id we need to fetch the urn first
if (config.Contract != null && (config.ItemID != null || config.TokenID != null))
{
return new List<string>
{
config.ItemID != null
? (await APIService.GetMarketplaceItemFromID(config.Contract, config.ItemID)).data[0].urn
: (await APIService.GetMarketplaceItemFromToken(config.Contract, config.TokenID)).data[0].nft
.urn
};
}
return null;
}
public void Cleanup()
{
if (_loading)
{
_shouldCleanup = true;
return;
}
_shouldCleanup = false;
emoteAnimationController.ClearEmote();
avatarLoader.ClearEmote();
}
}
}