Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ImagePacker.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //#define PACKING_DEBUG
  2. using System;
  3. using Unity.Burst;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. using Unity.Collections;
  7. using Unity.Collections.LowLevel.Unsafe;
  8. namespace UnityEditor.U2D.Common
  9. {
  10. [BurstCompile]
  11. internal static class ImagePacker
  12. {
  13. /// <summary>
  14. /// Given an array of rects, the method returns an array of rects arranged within outPackedWidth and outPackedHeight
  15. /// </summary>
  16. /// <param name="rects">Rects to pack</param>
  17. /// <param name="padding">Padding between each rect</param>
  18. /// <param name="outPackedRects">Rects arranged within outPackedWidth and outPackedHeight</param>
  19. /// <param name="outPackedWidth">Width of the packed rects</param>
  20. /// <param name="outPackedHeight">Height of the packed rects</param>
  21. public static void Pack(RectInt[] rects, int padding, out RectInt[] outPackedRects, out int outPackedWidth, out int outPackedHeight)
  22. {
  23. var packNode = InternalPack(rects, padding);
  24. outPackedWidth = packNode.rect.width;
  25. outPackedHeight = packNode.rect.height;
  26. var visitor = new CollectPackNodePositionVisitor();
  27. packNode.AcceptVisitor(visitor);
  28. outPackedRects = new RectInt[rects.Length];
  29. for (int i = 0; i < rects.Length; ++i)
  30. outPackedRects[i] = new RectInt(visitor.positions[i].x + padding, visitor.positions[i].y + padding, rects[i].width, rects[i].height);
  31. #if PACKING_DEBUG
  32. var emptyNodeCollector = new CollectEmptyNodePositionVisitor();
  33. packNode.AcceptVisitor(emptyNodeCollector);
  34. Array.Resize(ref outPackedRects, rects.Length + emptyNodeCollector.emptyAreas.Count);
  35. for (int i = rects.Length; i < outPackedRects.Length; ++i)
  36. outPackedRects[i] = emptyNodeCollector.emptyAreas[i - rects.Length];
  37. #endif
  38. }
  39. /// <summary>
  40. /// Packs image buffer into a single buffer. Image buffers are assumed to be 4 bytes per pixel in RGBA format
  41. /// </summary>
  42. /// <param name="buffers">Image buffers to pack</param>
  43. /// <param name="width">Image buffers width</param>
  44. /// <param name="height">Image buffers height</param>
  45. /// <param name="padding">Padding between each packed image</param>
  46. /// <param name="outPackedBuffer">Packed image buffer</param>
  47. /// <param name="outPackedBufferWidth">Packed image buffer's width</param>
  48. /// <param name="outPackedBufferHeight">Packed image buffer's height</param>
  49. /// <param name="outPackedRect">Location of each image buffers in the packed buffer</param>
  50. /// <param name="outUVTransform">Translation data from image original buffer to packed buffer</param>
  51. public static void Pack(NativeArray<Color32>[] buffers, int[] width, int[] height, int padding, out NativeArray<Color32> outPackedBuffer, out int outPackedBufferWidth, out int outPackedBufferHeight, out RectInt[] outPackedRect, out Vector2Int[] outUVTransform)
  52. {
  53. UnityEngine.Profiling.Profiler.BeginSample("Pack");
  54. // Determine the area that contains data in the buffer
  55. outPackedBuffer = default(NativeArray<Color32>);
  56. try
  57. {
  58. var tightRects = FindTightRectJob.Execute(buffers, width, height);
  59. var tightRectArea = new RectInt[tightRects.Length];
  60. for (var i = 0; i < tightRects.Length; ++i)
  61. {
  62. var t = tightRects[i];
  63. t.width = tightRects[i].width;
  64. t.height = tightRects[i].height;
  65. tightRectArea[i] = t;
  66. }
  67. Pack(tightRectArea, padding, out outPackedRect, out outPackedBufferWidth, out outPackedBufferHeight);
  68. var packBufferSize = (ulong)outPackedBufferWidth * (ulong)outPackedBufferHeight;
  69. if (packBufferSize < 0 || packBufferSize >= int.MaxValue)
  70. {
  71. throw new ArgumentException("Unable to create pack texture. Image size is too big to pack.");
  72. }
  73. outUVTransform = new Vector2Int[tightRectArea.Length];
  74. for (var i = 0; i < outUVTransform.Length; ++i)
  75. {
  76. outUVTransform[i] = new Vector2Int(outPackedRect[i].x - tightRects[i].x, outPackedRect[i].y - tightRects[i].y);
  77. }
  78. outPackedBuffer = new NativeArray<Color32>(outPackedBufferWidth * outPackedBufferHeight, Allocator.Persistent);
  79. Blit(outPackedBuffer, outPackedRect, outPackedBufferWidth, buffers, tightRects, width, padding);
  80. }
  81. catch (Exception ex)
  82. {
  83. if (outPackedBuffer.IsCreated)
  84. outPackedBuffer.Dispose();
  85. throw ex;
  86. }
  87. finally
  88. {
  89. UnityEngine.Profiling.Profiler.EndSample();
  90. }
  91. }
  92. static ImagePackNode InternalPack(RectInt[] rects, int padding)
  93. {
  94. if (rects == null || rects.Length == 0)
  95. return new ImagePackNode() { rect = new RectInt(0, 0, 0, 0)};
  96. var sortedRects = new ImagePackRect[rects.Length];
  97. for (int i = 0; i < rects.Length; ++i)
  98. {
  99. sortedRects[i] = new ImagePackRect();
  100. sortedRects[i].rect = rects[i];
  101. sortedRects[i].index = i;
  102. }
  103. var initialHeight = (int)NextPowerOfTwo((ulong)rects[0].height);
  104. Array.Sort<ImagePackRect>(sortedRects);
  105. var root = new ImagePackNode();
  106. root.rect = new RectInt(0, 0, (int)NextPowerOfTwo((ulong)rects[0].width), initialHeight);
  107. for (int i = 0; i < rects.Length; ++i)
  108. {
  109. if (!root.Insert(sortedRects[i], padding)) // we can't fit
  110. {
  111. int newWidth = root.rect.width , newHeight = root.rect.height;
  112. if (root.rect.width < root.rect.height)
  113. {
  114. newWidth = (int)NextPowerOfTwo((ulong)root.rect.width + 1);
  115. // Every time height changes, we reset height to grow again.
  116. newHeight = initialHeight;
  117. }
  118. else
  119. newHeight = (int)NextPowerOfTwo((ulong)root.rect.height + 1);
  120. // Reset all packing and try again
  121. root = new ImagePackNode();
  122. root.rect = new RectInt(0, 0, newWidth, newHeight);
  123. i = -1;
  124. }
  125. }
  126. return root;
  127. }
  128. public static void Blit(NativeArray<Color32> buffer, RectInt[] blitToArea, int bufferBytesPerRow, NativeArray<Color32>[] originalBuffer, RectInt[] blitFromArea, int[] bytesPerRow, int padding)
  129. {
  130. UnityEngine.Profiling.Profiler.BeginSample("Blit");
  131. for (var bufferIndex = 0; bufferIndex < blitToArea.Length && bufferIndex < originalBuffer.Length && bufferIndex < blitFromArea.Length; ++bufferIndex)
  132. {
  133. var fromArea = new int4(blitFromArea[bufferIndex].x, blitFromArea[bufferIndex].y, blitFromArea[bufferIndex].width, blitFromArea[bufferIndex].height);
  134. var toArea = new int4(blitToArea[bufferIndex].x, blitToArea[bufferIndex].y, blitToArea[bufferIndex].width, blitToArea[bufferIndex].height);
  135. unsafe
  136. {
  137. var originalBufferPtr = (Color32*) originalBuffer[bufferIndex].GetUnsafeReadOnlyPtr();
  138. var outputBufferPtr = (Color32*) buffer.GetUnsafePtr();
  139. BurstedBlit(originalBufferPtr, in fromArea, in toArea, bytesPerRow[bufferIndex], bufferBytesPerRow, outputBufferPtr);
  140. }
  141. }
  142. #if PACKING_DEBUG
  143. var emptyColors = new Color32[]
  144. {
  145. new Color32((byte)255, (byte)0, (byte)0, (byte)255),
  146. new Color32((byte)255, (byte)255, (byte)0, (byte)255),
  147. new Color32((byte)255, (byte)0, (byte)255, (byte)255),
  148. new Color32((byte)255, (byte)255, (byte)255, (byte)255),
  149. new Color32((byte)0, (byte)255, (byte)0, (byte)255),
  150. new Color32((byte)0, (byte)0, (byte)255, (byte)255)
  151. };
  152. for (int k = originalBuffer.Length; k < blitToArea.Length; ++k)
  153. {
  154. var rectFrom = blitToArea[k];
  155. for (int i = 0; i < rectFrom.height; ++i)
  156. {
  157. for (int j = 0; j < rectFrom.width; ++j)
  158. {
  159. c[((rectFrom.y + i) * bufferbytesPerRow) + rectFrom.x + j] =
  160. emptyColors[k % emptyColors.Length];
  161. }
  162. }
  163. }
  164. #endif
  165. UnityEngine.Profiling.Profiler.EndSample();
  166. }
  167. [BurstCompile]
  168. static unsafe void BurstedBlit(Color32* originalBuffer, in int4 rectFrom, in int4 rectTo, int bytesPerRow, int bufferBytesPerRow, Color32* outputBuffer)
  169. {
  170. var c = outputBuffer;
  171. var b = originalBuffer;
  172. var toXStart = (int)(rectTo.z * 0.5f - rectFrom.z * 0.5f);
  173. var toYStart = (int)(rectTo.w * 0.5f - rectFrom.w * 0.5f);
  174. toXStart = toXStart <= 0 ? rectTo.x : toXStart + rectTo.x;
  175. toYStart = toYStart <= 0 ? rectTo.y : toYStart + rectTo.y;
  176. for (var i = 0; i < rectFrom.w && i < rectTo.w; ++i)
  177. {
  178. for (var j = 0; j < rectFrom.z && j < rectTo.z; ++j)
  179. {
  180. var cc = b[(rectFrom.y + i) * bytesPerRow + rectFrom.x + j];
  181. c[((toYStart + i) * bufferBytesPerRow) + toXStart + j] = cc;
  182. }
  183. }
  184. }
  185. internal static ulong NextPowerOfTwo(ulong v)
  186. {
  187. v -= 1;
  188. v |= v >> 16;
  189. v |= v >> 8;
  190. v |= v >> 4;
  191. v |= v >> 2;
  192. v |= v >> 1;
  193. return v + 1;
  194. }
  195. internal class ImagePackRect : IComparable<ImagePackRect>
  196. {
  197. public RectInt rect;
  198. public int index;
  199. public int CompareTo(ImagePackRect obj)
  200. {
  201. var lhsArea = rect.width * rect.height;
  202. var rhsArea = obj.rect.width * obj.rect.height;
  203. if (lhsArea > rhsArea)
  204. return -1;
  205. if (lhsArea < rhsArea)
  206. return 1;
  207. if (index < obj.index)
  208. return -1;
  209. return 1;
  210. }
  211. }
  212. }
  213. }