com.wallstop-studios.unity-helpers 2.0.0-rc13 → 2.0.0-rc15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,364 @@
1
+ namespace UnityHelpers.UI
2
+ {
3
+ using System;
4
+ using System.Collections.Generic;
5
+ using System.Diagnostics;
6
+ using System.Linq;
7
+ using System.Threading;
8
+ using System.Threading.Tasks;
9
+ using Core.Extension;
10
+ using Core.Helper;
11
+ using UnityEditor;
12
+ using UnityEngine;
13
+ using UnityEngine.UIElements;
14
+ using Utils;
15
+ using Debug = UnityEngine.Debug;
16
+
17
+ public readonly struct AnimatedSpriteLayer
18
+ {
19
+ public const float FrameRate = 12f;
20
+
21
+ public readonly Vector2[] offsets;
22
+ public readonly Sprite[] frames;
23
+ public readonly float alpha;
24
+
25
+ public AnimatedSpriteLayer(
26
+ IEnumerable<Sprite> sprites,
27
+ IEnumerable<Vector2> offsets,
28
+ float alpha = 1
29
+ )
30
+ {
31
+ frames = sprites?.ToArray() ?? Array.Empty<Sprite>();
32
+ foreach (Sprite frame in frames)
33
+ {
34
+ if (frame == null)
35
+ {
36
+ continue;
37
+ }
38
+
39
+ frame.texture.MakeReadable();
40
+ }
41
+
42
+ this.offsets =
43
+ offsets?.Zip(frames, (offset, frame) => frame.pixelsPerUnit * offset).ToArray()
44
+ ?? Array.Empty<Vector2>();
45
+ Debug.Assert(
46
+ this.offsets.Length == frames.Length,
47
+ $"Expected {frames.Length} to match {this.offsets.Length}"
48
+ );
49
+ this.alpha = Mathf.Clamp01(alpha);
50
+ }
51
+
52
+ public AnimatedSpriteLayer(
53
+ AnimationClip clip,
54
+ IEnumerable<Vector2> offsets,
55
+ float alpha = 1
56
+ )
57
+ : this(
58
+ #if UNITY_EDITOR
59
+ clip.GetSpritesFromClip(),
60
+ #else
61
+ Enumerable.Empty<Sprite>(),
62
+ #endif
63
+ offsets, alpha) { }
64
+ }
65
+
66
+ public sealed class LayeredImage : VisualElement
67
+ {
68
+ private readonly AnimatedSpriteLayer[] _layers;
69
+ private readonly Texture2D[] _computed;
70
+ private readonly Color _backgroundColor;
71
+
72
+ private readonly Rect? _largestArea;
73
+
74
+ public LayeredImage(
75
+ IEnumerable<AnimatedSpriteLayer> inputSpriteLayers,
76
+ Color? backgroundColor = null,
77
+ float fps = AnimatedSpriteLayer.FrameRate
78
+ )
79
+ {
80
+ _layers = inputSpriteLayers.ToArray();
81
+ _backgroundColor = backgroundColor ?? Color.white;
82
+ _computed = ComputeTextures().ToArray();
83
+ _largestArea = null;
84
+ foreach (Texture2D computed in _computed)
85
+ {
86
+ if (_largestArea == null)
87
+ {
88
+ _largestArea = new Rect(0, 0, computed.width, computed.height);
89
+ }
90
+ else
91
+ {
92
+ Rect largestArea = _largestArea.Value;
93
+ largestArea.width = Mathf.Max(largestArea.width, computed.width);
94
+ largestArea.height = Mathf.Max(largestArea.height, computed.height);
95
+ _largestArea = largestArea;
96
+ }
97
+ }
98
+
99
+ Render(0);
100
+ float fpsMs = 1000f / fps;
101
+ if (1 < _computed.Length)
102
+ {
103
+ #if UNITY_EDITOR
104
+ if (!Application.isPlaying)
105
+ {
106
+ TimeSpan lastTick = TimeSpan.Zero;
107
+ TimeSpan fpsSpan = TimeSpan.FromMilliseconds(fpsMs);
108
+ int index = 0;
109
+ Stopwatch timer = Stopwatch.StartNew();
110
+ EditorApplication.update += () =>
111
+ {
112
+ TimeSpan elapsed = timer.Elapsed;
113
+ if (lastTick + fpsSpan < elapsed)
114
+ {
115
+ index = index.WrappedIncrement(_computed.Length);
116
+ lastTick = elapsed;
117
+ Render(index);
118
+ }
119
+ };
120
+ return;
121
+ }
122
+
123
+ #endif
124
+ {
125
+ int index = 0;
126
+ CoroutineHandler.Instance.StartFunctionAsCoroutine(
127
+ () =>
128
+ {
129
+ index = index.WrappedIncrement(_computed.Length);
130
+ Render(index);
131
+ },
132
+ 1f / fps
133
+ );
134
+ }
135
+ }
136
+ }
137
+
138
+ private void Render(int index)
139
+ {
140
+ Texture2D computed = _computed[index];
141
+ if (computed != null)
142
+ {
143
+ style.backgroundImage = computed;
144
+ style.width = computed.width;
145
+ style.height = computed.height;
146
+ }
147
+
148
+ style.marginRight = 0;
149
+ style.marginBottom = 0;
150
+ if (_largestArea != null)
151
+ {
152
+ Rect largestArea = _largestArea.Value;
153
+ if (style.width.value.value < largestArea.width)
154
+ {
155
+ style.marginRight = largestArea.width - style.width.value.value;
156
+ }
157
+
158
+ if (style.height.value.value < largestArea.height)
159
+ {
160
+ style.marginBottom = largestArea.height - style.height.value.value;
161
+ }
162
+ }
163
+ }
164
+
165
+ private IEnumerable<Texture2D> ComputeTextures()
166
+ {
167
+ const float pixelCutoff = 0.01f;
168
+ int frameCount = _layers.Select(layer => layer.frames.Length).Distinct().Single();
169
+
170
+ Color transparent = Color.clear;
171
+ for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex)
172
+ {
173
+ int minX = int.MaxValue;
174
+ int maxX = int.MinValue;
175
+ int minY = int.MaxValue;
176
+ int maxY = int.MinValue;
177
+ foreach (AnimatedSpriteLayer layer in _layers)
178
+ {
179
+ if (!layer.frames.Any())
180
+ {
181
+ continue;
182
+ }
183
+
184
+ Sprite sprite = layer.frames[frameIndex];
185
+ Vector2 offset = layer.offsets[frameIndex];
186
+ Rect spriteRect = sprite.rect;
187
+
188
+ int left = Mathf.RoundToInt(offset.x + spriteRect.xMin);
189
+ int right = Mathf.RoundToInt(offset.x + spriteRect.xMax);
190
+ int bottom = Mathf.RoundToInt(offset.y + spriteRect.yMin);
191
+ int top = Mathf.RoundToInt(offset.y + spriteRect.yMax);
192
+
193
+ minX = Mathf.Min(minX, left);
194
+ maxX = Mathf.Max(maxX, right);
195
+ minY = Mathf.Min(minY, bottom);
196
+ maxY = Mathf.Max(maxY, top);
197
+ }
198
+
199
+ if (minX == int.MaxValue)
200
+ {
201
+ continue;
202
+ }
203
+
204
+ // Calculate the width and height of the non-transparent region
205
+ int width = maxX - minX + 1;
206
+ int height = maxY - minY + 1;
207
+
208
+ Color[] pixels = new Color[width * height];
209
+ Array.Fill(pixels, Color.clear);
210
+
211
+ foreach (AnimatedSpriteLayer layer in _layers)
212
+ {
213
+ if (!layer.frames.Any())
214
+ {
215
+ continue;
216
+ }
217
+
218
+ Sprite sprite = layer.frames[frameIndex];
219
+ Vector2 offset = layer.offsets[frameIndex];
220
+ float alpha = layer.alpha;
221
+ int offsetX = Mathf.RoundToInt(offset.x);
222
+ int offsetY = Mathf.RoundToInt(offset.y);
223
+ Texture2D texture = sprite.texture;
224
+ Rect spriteRect = sprite.rect;
225
+
226
+ int spriteX = Mathf.RoundToInt(spriteRect.xMin);
227
+ int spriteWidth = Mathf.RoundToInt(spriteRect.width);
228
+ int spriteY = Mathf.RoundToInt(spriteRect.yMin);
229
+ int spriteHeight = Mathf.RoundToInt(spriteRect.height);
230
+ Color[] spritePixels = texture.GetPixels(
231
+ spriteX,
232
+ spriteY,
233
+ spriteWidth,
234
+ spriteHeight
235
+ );
236
+
237
+ Parallel.For(
238
+ 0,
239
+ spritePixels.Length,
240
+ inIndex =>
241
+ {
242
+ int x = inIndex % spriteWidth;
243
+ int y = inIndex / spriteWidth;
244
+
245
+ Color pixelColor = spritePixels[inIndex];
246
+ if (pixelColor.a < pixelCutoff)
247
+ {
248
+ return;
249
+ }
250
+
251
+ int textureX = offsetX + x + spriteX;
252
+ int textureY = offsetY + y + spriteY;
253
+ int index = textureY * width + textureX;
254
+
255
+ if (index < 0 || pixels.Length <= index)
256
+ {
257
+ return;
258
+ }
259
+
260
+ Color existingColor = pixels[index];
261
+ if (existingColor == transparent)
262
+ {
263
+ existingColor = _backgroundColor;
264
+ }
265
+
266
+ Color blendedColor = Color.Lerp(existingColor, pixelColor, alpha);
267
+ pixels[index] = blendedColor;
268
+ }
269
+ );
270
+ }
271
+
272
+ // Find the bounds of the non-transparent pixels in the temporary texture
273
+ int finalMinX = int.MaxValue;
274
+ int finalMaxX = int.MinValue;
275
+ int finalMinY = int.MaxValue;
276
+ int finalMaxY = int.MinValue;
277
+
278
+ Parallel.For(
279
+ 0,
280
+ height * width,
281
+ inIndex =>
282
+ {
283
+ Color pixelColor = pixels[inIndex];
284
+ if (pixelColor.a < pixelCutoff)
285
+ {
286
+ return;
287
+ }
288
+
289
+ int x = inIndex % width;
290
+ int y = inIndex / width;
291
+
292
+ int expectedX = finalMinX;
293
+ while (x < expectedX)
294
+ {
295
+ expectedX = Interlocked.CompareExchange(ref finalMinX, x, expectedX);
296
+ }
297
+
298
+ expectedX = finalMaxX;
299
+ while (expectedX < x)
300
+ {
301
+ expectedX = Interlocked.CompareExchange(ref finalMaxX, x, expectedX);
302
+ }
303
+
304
+ int expectedY = finalMinY;
305
+ while (y < expectedY)
306
+ {
307
+ expectedY = Interlocked.CompareExchange(ref finalMinY, y, expectedY);
308
+ }
309
+
310
+ expectedY = finalMaxY;
311
+ while (expectedY < y)
312
+ {
313
+ expectedY = Interlocked.CompareExchange(ref finalMaxY, y, expectedY);
314
+ }
315
+ }
316
+ );
317
+
318
+ if (finalMinX == int.MaxValue)
319
+ {
320
+ continue;
321
+ }
322
+
323
+ // Calculate the final width and height of the culled texture
324
+ int finalWidth = finalMaxX - finalMinX + 1;
325
+ int finalHeight = finalMaxY - finalMinY + 1;
326
+
327
+ Color[] finalPixels = new Color[finalWidth * finalHeight];
328
+ Array.Fill(finalPixels, _backgroundColor);
329
+
330
+ // Copy the non-transparent pixels from the temporary texture to the final texture
331
+ Parallel.For(
332
+ 0,
333
+ finalWidth * finalHeight,
334
+ inIndex =>
335
+ {
336
+ int x = inIndex % finalWidth;
337
+ int y = inIndex / finalWidth;
338
+ int outerX = x + finalMinX;
339
+ int outerY = y + finalMinY;
340
+ Color pixelColor = pixels[outerY * width + outerX];
341
+ if (pixelColor.a < pixelCutoff)
342
+ {
343
+ return;
344
+ }
345
+ finalPixels[y * finalWidth + x] = pixelColor;
346
+ }
347
+ );
348
+
349
+ Texture2D finalTexture = new(
350
+ finalWidth,
351
+ finalHeight,
352
+ TextureFormat.RGBA32,
353
+ mipChain: false,
354
+ linear: false,
355
+ createUninitialized: true
356
+ );
357
+ finalTexture.SetPixels(finalPixels);
358
+ finalTexture.Apply(false, false);
359
+
360
+ yield return finalTexture;
361
+ }
362
+ }
363
+ }
364
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: d4af7f20a66c40e686d59e4253d72156
3
+ timeCreated: 1735605169
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: dd17cad9fb2348fa8249a668bc458174
3
+ timeCreated: 1735605163
@@ -25,6 +25,9 @@
25
25
  RunTest(new XorShiftRandom(), timeout);
26
26
  RunTest(new DotNetRandom(), timeout);
27
27
  RunTest(new WyRandom(), timeout);
28
+ RunTest(new SplitMix64(), timeout);
29
+ RunTest(new RomuDuo(), timeout);
30
+ RunTest(new XorShiroRandom(), timeout);
28
31
  }
29
32
 
30
33
  private static void RunTest<T>(T random, TimeSpan timeout)
@@ -0,0 +1,9 @@
1
+ namespace UnityHelpers.Tests.Random
2
+ {
3
+ using Core.Random;
4
+
5
+ public sealed class RomuDuoRandomTests : RandomTestBase
6
+ {
7
+ protected override IRandom NewRandom() => new RomuDuo();
8
+ }
9
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 6998ef56b2d04d58977cd95f226c6f50
3
+ timeCreated: 1735610612
@@ -0,0 +1,9 @@
1
+ namespace UnityHelpers.Tests.Random
2
+ {
3
+ using Core.Random;
4
+
5
+ public sealed class SplitMix64RandomTests : RandomTestBase
6
+ {
7
+ protected override IRandom NewRandom() => new SplitMix64();
8
+ }
9
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 9c07a25c515041908a3920ff7d2c2607
3
+ timeCreated: 1735606130
@@ -0,0 +1,9 @@
1
+ namespace UnityHelpers.Tests.Random
2
+ {
3
+ using Core.Random;
4
+
5
+ public sealed class XorShiroRandomTests : RandomTestBase
6
+ {
7
+ protected override IRandom NewRandom() => new XorShiroRandom();
8
+ }
9
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 533b6ecf4c3d4115ad733f59afa8618f
3
+ timeCreated: 1735610645
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc13",
3
+ "version": "2.0.0-rc15",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},