-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArcadianRenderer.cs
205 lines (161 loc) · 4.88 KB
/
ArcadianRenderer.cs
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
namespace ORARenderer
{
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArcadianRenderer : MonoBehaviour
{
[SerializeField] private SkinnedMeshRenderer arcadianRenderer;
/// <summary>
/// Ordered the same as the materials of the SkinnedMeshRenderer
/// </summary>
[SerializeField] private List<string> materialLocationNames = new List<string>()
{
"Skin",
"Eyes",
"Mouth",
"Top",
"Bottom",
"Right Hand",
"Left Hand",
"Head"
};
public List<string> MaterialLocationNames { get { return materialLocationNames; } }
public void RequestParts(ArcadianLoadRequest loadRequest) => StartCoroutine(RequestPartsCR(loadRequest));
public void RequestPartsRandom() => StartCoroutine(RequestRandomCR());
public bool IsLoading { get; private set; } = false;
#region Private
private ORAReader oraReader = null;
private IEnumerator RequestPartsCR(ArcadianLoadRequest loadRequest)
{
if (loadRequest == null)
yield break;
if (oraReader == null)
oraReader = ORAReader.GetInstance();
if (oraReader == null)
{
Debug.LogWarning("ORA renderer could not instantiate!");
yield break;
}
IsLoading = true;
yield return new WaitUntil(() => oraReader.IsLoaded);
ArcadianParts newParts = oraReader.GetPartData(loadRequest);
ReplaceParts(newParts);
IsLoading = false;
}
private IEnumerator RequestRandomCR()
{
if (oraReader == null)
oraReader = ORAReader.GetInstance();
if (oraReader == null)
{
Debug.LogWarning("ORA renderer could not instantiate!");
yield break;
}
yield return new WaitUntil(() => oraReader.IsLoaded);
ArcadianParts randomParts = oraReader.GetRandom();
ReplaceParts(randomParts);
}
private void ReplaceParts(ArcadianParts newParts)
{
bool isErrorLogged = false;
for (int i = 0; i < arcadianRenderer.materials.Length; i++)
{
if (i >= materialLocationNames.Count)
{
if (!isErrorLogged)
{
Debug.LogError(
"Not all material locations are specified in ArcadianRenderer.materialLocationNames!" +
" Add location names for all mats used by the SkinnedMeshRenderer to this List, and make sure they are the same order."
);
isErrorLogged = true;
}
LoadBlankPart(arcadianRenderer.materials[i]);
continue;
}
else if (!oraReader.ArcadianReference.Locations.Exists(x => x.Name == materialLocationNames[i]))
{
LoadBlankPart(arcadianRenderer.materials[i]);
continue;
}
Predicate<LocationData> match = x => x != null && x.Name == materialLocationNames[i];
if (newParts == null || newParts.Locations == null || !newParts.Locations.Exists(match))
{
bool loadDefault =
materialLocationNames[i] == "Skin" ||
materialLocationNames[i] == "Eyes" ||
materialLocationNames[i] == "Mouth";
if (loadDefault)
{
var defaultPart = oraReader.ArcadianReference.Locations.Find(match).Parts[0];
ReplacePart(arcadianRenderer.materials[i], defaultPart);
}
else
{
LoadBlankPart(arcadianRenderer.materials[i]);
}
continue;
}
var location = newParts.Locations.Find(match);
if (location.Parts.Count == 0)
LoadBlankPart(arcadianRenderer.materials[i]);
else
ReplacePart(arcadianRenderer.materials[i], location.Parts[0]);
}
}
private void ReplacePart(Material material, PartData partData)
{
if (partData == null || partData.Src == null || partData.Src.Length == 0)
{
LoadBlankPart(material);
return;
}
Texture2D sourceTex = new Texture2D(2, 2);
sourceTex.LoadImage(partData.Src);
material.mainTexture = ResizePart(sourceTex, partData, oraReader.ArcadianReference.canvasSizeW, oraReader.ArcadianReference.canvasSizeH);
}
private Texture2D ResizePart(Texture2D source, PartData partData, int targetWidth, int targetHeight)
{
Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, true);
Color[] rPixels = result.GetPixels(0);
Color[] sPixels = source.GetPixels(0);
int sPx = 0;
Rect dstRect = new Rect(
partData.PosX,
(result.height - partData.PosY) - source.height,
source.width,
source.height
);
for (int px = 0; px < rPixels.Length; px++)
{
int x = px % targetWidth;
int y = px / targetHeight;
if (dstRect.Contains(new Vector2(x, y)))
{
rPixels[px] = sPixels[sPx];
sPx++;
}
else
{
rPixels[px] = Color.clear;
}
}
result.SetPixels(rPixels, 0);
result.Apply();
return result;
}
private void LoadBlankPart(Material material)
{
Texture2D blankTexture = new Texture2D(1, 1);
Color[] pixels = blankTexture.GetPixels(0);
for (int px = 0; px < pixels.Length; px++)
pixels[px] = Color.clear;
blankTexture.SetPixels(pixels, 0);
blankTexture.Apply();
material.mainTexture = blankTexture;
}
#endregion
}
}