-
Notifications
You must be signed in to change notification settings - Fork 13
/
Atlas.cs
243 lines (212 loc) · 7.63 KB
/
Atlas.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
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
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Nes3D.Engine3D
{
public static class Atlas
{
public class Slot : NesRect
{
public int slice;
public int x, y;
public bool flip;
public TextureData tex;
public Vector2 offset;
public Vector2 texSize;
public int NextX { get { return x + w; } }
public int NextY { get { return y + h; } }
public Slot(int _slice, int _x, int _y, int _w, int _h, bool _flip = false, TextureData _tex = null) : base(_w, _h)
{
slice = _slice;
x = _x;
y = _y;
flip = _flip;
tex = _tex;
offset = new Vector2(x, y) / SIZE;
if (tex != null)
{
if (!flip)
texSize = new Vector2(tex.w, tex.h) / SIZE;
else
texSize = new Vector2(tex.h, tex.w) / SIZE;
}
}
}
public const int SIZE = 512;
private const int SLICE_COUNT = 32;
private const int MAX_SLOT_PER_FRAME = 1023;
private static readonly TextureFormat format = TextureFormat.ARGB32;
static public Texture2DArray atlas;
private static Texture2D[] texList;
private static LinkedList<Slot> freeSlots;
private static Dictionary<TextureData, Slot> usedSlots;
private static Slot[] newRegSlots;
private static int head;
private static volatile int tail;
private static HashSet<int> unsyncedSlices;
public static void Init()
{
atlas = new Texture2DArray(SIZE, SIZE, SLICE_COUNT, format, false);
atlas.filterMode = FilterMode.Point;
atlas.wrapMode = TextureWrapMode.Clamp;
atlas.Apply();
texList = new Texture2D[SLICE_COUNT];
for (int i = 0; i < texList.Length; i++)
{
texList[i] = new Texture2D(SIZE, SIZE, format, false);
texList[i].filterMode = FilterMode.Point;
texList[i].wrapMode = TextureWrapMode.Clamp;
}
freeSlots = new LinkedList<Slot>();
usedSlots = new Dictionary<TextureData, Slot>();
newRegSlots = new Slot[MAX_SLOT_PER_FRAME + 1];
unsyncedSlices = new HashSet<int>();
Reset();
}
public static void Reset()
{
usedSlots.Clear();
freeSlots.Clear();
for (int i = 0; i < SLICE_COUNT; i++)
freeSlots.AddLast(new Slot(i, 0, 0, SIZE, SIZE));
unsyncedSlices.Clear();
head = tail = 0;
}
private static Slot FindFreeSlotFor(TextureData tex)
{
foreach(var node in freeSlots)
if (node.CanContain(tex))
return node;
throw new Exception("Out of texture pool");
}
private static void RegisterFreeSlot(Slot slot)
{
freeSlots.AddFirst(slot);
}
static public void Invalidate()
{
unsyncedSlices.Clear();
int tailValue = tail;
while (head != tailValue)
{
head = (head + 1) & MAX_SLOT_PER_FRAME;
Slot slot = newRegSlots[head];
newRegSlots[head] = null;
TextureData tex = slot.tex;
if (tex != null)
{
var texture = texList[slot.slice];
for (int i = 0; i < tex.w; i++)
for (int j = 0; j < tex.h; j++)
{
if (tex.format == ColorFormat.Index)
{
Color c = new Color(0, 0, 0, tex.GetPixel(i, j) / 3f);
if (!slot.flip)
texture.SetPixel(slot.x + i, slot.y + j, c);
else
texture.SetPixel(slot.x + j, slot.y + i, c);
}
else
{
if (!slot.flip)
texture.SetPixel(slot.x + i, slot.y + j, tex.GetColor(i, j));
else
texture.SetPixel(slot.x + j, slot.y + i, tex.GetColor(i, j));
}
}
unsyncedSlices.Add(slot.slice);
}
}
foreach (int i in unsyncedSlices)
{
texList[i].Apply();
Graphics.CopyTexture(texList[i], 0, atlas, i);
}
}
public static void Deregister(TextureData tex)
{
Slot slot = GetSlot(tex);
if (slot != null)
{
slot.tex = null;
usedSlots.Remove(tex);
RegisterFreeSlot(slot);
}
}
private static void Insert(this Slot slot, TextureData tex)
{
int w, h;
bool rotated;
freeSlots.Remove(slot);
if (slot.min < tex.max)
{
if (slot.minIndex == tex.minIndex)
rotated = false;
else
rotated = true;
}
else
{
if (slot.minIndex == tex.minIndex)
rotated = true;
else
rotated = false;
}
if (rotated)
{
w = tex.h;
h = tex.w;
}
else
{
w = tex.w;
h = tex.h;
}
Slot regSlot = new Slot(slot.slice, slot.x, slot.y, w, h, rotated, tex);
usedSlots[tex] = regSlot;
tail = (tail + 1) & MAX_SLOT_PER_FRAME;
newRegSlots[tail] = regSlot;
if (regSlot.w < slot.w)
{
Slot freeSlot = new Slot(slot.slice, regSlot.NextX, slot.y, slot.w - regSlot.w, regSlot.h);
RegisterFreeSlot(freeSlot);
}
if (regSlot.h < slot.h)
{
Slot freeSlot = new Slot(slot.slice, slot.x, regSlot.NextY, slot.w, slot.h - regSlot.h);
RegisterFreeSlot(freeSlot);
}
}
public static void Register(TextureData tex)
{
Slot slot = GetSlot(tex);
if (slot == null) // new registration
{
slot = FindFreeSlotFor(tex);
slot.Insert(tex);
}
else //update old slot
{
tail = (tail + 1) & MAX_SLOT_PER_FRAME;
newRegSlots[tail] = slot;
}
}
public static Slot GetSlot(TextureData tex)
{
Slot slot;
usedSlots.TryGetValue(tex, out slot);
return slot;
}
public static Vector2 Coord2Uv(TextureData tex, Vector2 coord)
{
Slot s = GetSlot(tex);
if (s != null)
{
Vector2 c = s.flip == false ? coord : new Vector2(coord.y, coord.x);
return c / SIZE;
}
return Vector2.zero;
}
}
}