Tanoda
FaceBuilder.cs
Go to the documentation of this file.
1#pragma warning disable 649
2using System.Collections;
3using UnityEngine;
4using UnityEngine.UI;
5
6namespace TriLibCore.Samples
7{
13 {
17 private const float TurnLength = 1f;
18
22 private const float Distance = 100f;
23
27 private const string BaseURI = "https://ricardoreis.net/trilib/demos/assetdownloader/";
28
32 [SerializeField] private GameObject _downloadTemplate;
33
38 public void PreviousPart(string partName)
39 {
40 StartCoroutine(TurnWrapper(partName, -90f));
41 }
42
47 public void NextPart(string partName)
48 {
49 StartCoroutine(TurnWrapper(partName, 90f));
50 }
51
58 private IEnumerator TurnWrapper(string partName, float angle)
59 {
60 var wrapper = GameObject.Find($"{partName}Wrapper");
61 if (wrapper == null) yield break;
62 var line = GameObject.Find($"{partName}Line");
63 if (line == null) yield break;
64 var buttons = line.GetComponentsInChildren<Button>();
65 for (var i = 0; i < buttons.Length; i++)
66 {
67 var button = buttons[i];
68 button.enabled = false;
69 }
70
71 var initialYaw = wrapper.transform.rotation.eulerAngles.y;
72 var finalYaw = initialYaw + angle;
73 for (var i = 0f; i < TurnLength; i += Time.deltaTime)
74 {
75 var eulerAngles = wrapper.transform.rotation.eulerAngles;
76 eulerAngles.y = Mathf.Lerp(initialYaw, finalYaw, Easing(i / TurnLength));
77 wrapper.transform.rotation = Quaternion.Euler(eulerAngles);
78 yield return null;
79 }
80
81 var finalEulerAngles = wrapper.transform.rotation.eulerAngles;
82 finalEulerAngles.y = finalYaw;
83 wrapper.transform.rotation = Quaternion.Euler(finalEulerAngles);
84 for (var i = 0; i < buttons.Length; i++)
85 {
86 var button = buttons[i];
87 button.enabled = true;
88 }
89 }
90
94 private static float Easing(float value)
95 {
96 if ((value *= 2f) < 1f) return 0.5f * value * value * value;
97 return 0.5f * ((value -= 2f) * value * value + 2f);
98 }
99
104 private void LoadParts(string partName)
105 {
106 for (var i = 0; i < 4; i++) LoadPart(partName, i);
107 }
108
114 private void LoadPart(string partName, int partIndex)
115 {
116 var wrapper = GameObject.Find($"{partName}Wrapper");
117 if (wrapper == null) return;
118 var request = AssetDownloader.CreateWebRequest($"{BaseURI}{partName}{partIndex}.zip");
119 AssetDownloader.LoadModelFromUri(request, OnLoad, OnMaterialsLoad, OnProgress, OnError, wrapper,
120 AssetLoaderOptions, partIndex, null, true);
121 }
122
124 protected override void Start()
125 {
126 base.Start();
127 AssetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
128 LoadParts("Hair");
129 LoadParts("Eyes");
130 LoadParts("Nose");
131 LoadParts("Mouth");
132 }
133
134
137 protected override void OnError(IContextualizedError contextualizedError)
138 {
139 var context = contextualizedError.GetContext();
140 if (context is AssetLoaderContext assetLoaderContext)
141 {
142 var zipLoadCustomContextData = (ZipLoadCustomContextData) assetLoaderContext.CustomData;
143 var uriLoadCustomContextData = (UriLoadCustomContextData) zipLoadCustomContextData.CustomData;
144 var downloaded = Instantiate(_downloadTemplate, _downloadTemplate.transform.parent);
145 var text = downloaded.GetComponentInChildren<Text>();
146 text.text =
147 $"Error: {uriLoadCustomContextData.UnityWebRequest.uri.Segments[uriLoadCustomContextData.UnityWebRequest.uri.Segments.Length - 1]}";
148 downloaded.SetActive(true);
149 }
150
151 base.OnError(contextualizedError);
152 }
153
159 protected override void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
160 {
161 if (assetLoaderContext.RootGameObject != null)
162 {
163 var zipLoadCustomContextData = (ZipLoadCustomContextData) assetLoaderContext.CustomData;
164 var uriLoadCustomContextData = (UriLoadCustomContextData) zipLoadCustomContextData.CustomData;
165 var downloaded = Instantiate(_downloadTemplate, _downloadTemplate.transform.parent);
166 var text = downloaded.GetComponentInChildren<Text>();
167 text.text =
168 $"Done: {uriLoadCustomContextData.UnityWebRequest.uri.Segments[uriLoadCustomContextData.UnityWebRequest.uri.Segments.Length - 1]}";
169 downloaded.SetActive(true);
170 }
171
172 base.OnMaterialsLoad(assetLoaderContext);
173 }
174
180 protected override void OnLoad(AssetLoaderContext assetLoaderContext)
181 {
182 if (assetLoaderContext.RootGameObject != null)
183 {
184 var zipLoadCustomContextData = (ZipLoadCustomContextData) assetLoaderContext.CustomData;
185 var uriLoadCustomContextData = (UriLoadCustomContextData) zipLoadCustomContextData.CustomData;
186 var partIndex = (int) uriLoadCustomContextData.CustomData;
187 var rotation = Quaternion.Euler(0f, partIndex * 90f, 0f);
188 assetLoaderContext.RootGameObject.transform.SetPositionAndRotation(
189 rotation * new Vector3(0f, 0f, Distance), rotation);
190 }
191
192 base.OnLoad(assetLoaderContext);
193 }
194 }
195}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
TriLibCore.AssetLoaderOptions AssetLoaderOptions
Definition: TriLibLoader.cs:9
Represents a base class used in TriLib samples.
virtual void OnProgress(AssetLoaderContext assetLoaderContext, float value)
Event is triggered when the Model loading progress changes.
Represents a TriLib sample which allows the user to load models from a website and switch between the...
Definition: FaceBuilder.cs:13
void PreviousPart(string partName)
Switches to the previous part.
Definition: FaceBuilder.cs:38
override void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
Event triggered when the Model and all its resources loaded.
Definition: FaceBuilder.cs:159
void NextPart(string partName)
Switches to the next part.
Definition: FaceBuilder.cs:47
override void OnLoad(AssetLoaderContext assetLoaderContext)
Event triggered when the Model Meshes and hierarchy are loaded.
Definition: FaceBuilder.cs:180
override void Start()
Checks if the Dispatcher instance exists, stores this class instance as the Singleton and load all ar...
Definition: FaceBuilder.cs:124
override void OnError(IContextualizedError contextualizedError)
Event triggered when there is any Model loading error.
Definition: FaceBuilder.cs:137
Represents a class passed as the custom data to the Asset Loader Context when loading Models from URI...
Represents a class passed as the custom data to the Asset Loader Context when loading Models from Zip...