TextureScale.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Only works on ARGB32, RGB24 and Alpha8 textures that are marked readable
  2. using System.Threading;
  3. using UnityEngine;
  4. public class TextureScale
  5. {
  6. public class ThreadData
  7. {
  8. public int start;
  9. public int end;
  10. public ThreadData (int s, int e) {
  11. start = s;
  12. end = e;
  13. }
  14. }
  15. private static Color[] texColors;
  16. private static Color[] newColors;
  17. private static int w;
  18. private static float ratioX;
  19. private static float ratioY;
  20. private static int w2;
  21. private static int finishCount;
  22. private static Mutex mutex;
  23. public static void Point (Texture2D tex, int newWidth, int newHeight)
  24. {
  25. ThreadedScale (tex, newWidth, newHeight, false);
  26. }
  27. public static void Bilinear (Texture2D tex, int newWidth, int newHeight)
  28. {
  29. ThreadedScale (tex, newWidth, newHeight, true);
  30. }
  31. private static void ThreadedScale (Texture2D tex, int newWidth, int newHeight, bool useBilinear)
  32. {
  33. texColors = tex.GetPixels();
  34. newColors = new Color[newWidth * newHeight];
  35. if (useBilinear)
  36. {
  37. ratioX = 1.0f / ((float)newWidth / (tex.width-1));
  38. ratioY = 1.0f / ((float)newHeight / (tex.height-1));
  39. }
  40. else {
  41. ratioX = ((float)tex.width) / newWidth;
  42. ratioY = ((float)tex.height) / newHeight;
  43. }
  44. w = tex.width;
  45. w2 = newWidth;
  46. var cores = Mathf.Min(SystemInfo.processorCount, newHeight);
  47. var slice = newHeight/cores;
  48. finishCount = 0;
  49. if (mutex == null) {
  50. mutex = new Mutex(false);
  51. }
  52. if (cores > 1)
  53. {
  54. int i = 0;
  55. ThreadData threadData;
  56. for (i = 0; i < cores-1; i++) {
  57. threadData = new ThreadData(slice * i, slice * (i + 1));
  58. ParameterizedThreadStart ts = useBilinear ? new ParameterizedThreadStart(BilinearScale) : new ParameterizedThreadStart(PointScale);
  59. Thread thread = new Thread(ts);
  60. thread.Start(threadData);
  61. }
  62. threadData = new ThreadData(slice*i, newHeight);
  63. if (useBilinear)
  64. {
  65. BilinearScale(threadData);
  66. }
  67. else
  68. {
  69. PointScale(threadData);
  70. }
  71. while (finishCount < cores)
  72. {
  73. Thread.Sleep(1);
  74. }
  75. }
  76. else
  77. {
  78. ThreadData threadData = new ThreadData(0, newHeight);
  79. if (useBilinear)
  80. {
  81. BilinearScale(threadData);
  82. }
  83. else
  84. {
  85. PointScale(threadData);
  86. }
  87. }
  88. tex.Resize(newWidth, newHeight);
  89. tex.SetPixels(newColors);
  90. tex.Apply();
  91. }
  92. public static void BilinearScale (System.Object obj)
  93. {
  94. ThreadData threadData = (ThreadData) obj;
  95. for (var y = threadData.start; y < threadData.end; y++)
  96. {
  97. int yFloor = (int)Mathf.Floor(y * ratioY);
  98. var y1 = yFloor * w;
  99. var y2 = (yFloor+1) * w;
  100. var yw = y * w2;
  101. for (var x = 0; x < w2; x++) {
  102. int xFloor = (int)Mathf.Floor(x * ratioX);
  103. var xLerp = x * ratioX-xFloor;
  104. newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor+1], xLerp),
  105. ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor+1], xLerp),
  106. y*ratioY-yFloor);
  107. }
  108. }
  109. mutex.WaitOne();
  110. finishCount++;
  111. mutex.ReleaseMutex();
  112. }
  113. public static void PointScale (System.Object obj)
  114. {
  115. ThreadData threadData = (ThreadData) obj;
  116. for (var y = threadData.start; y < threadData.end; y++)
  117. {
  118. var thisY = (int)(ratioY * y) * w;
  119. var yw = y * w2;
  120. for (var x = 0; x < w2; x++) {
  121. newColors[yw + x] = texColors[(int)(thisY + ratioX*x)];
  122. }
  123. }
  124. mutex.WaitOne();
  125. finishCount++;
  126. mutex.ReleaseMutex();
  127. }
  128. private static Color ColorLerpUnclamped (Color c1, Color c2, float value)
  129. {
  130. return new Color (c1.r + (c2.r - c1.r)*value,
  131. c1.g + (c2.g - c1.g)*value,
  132. c1.b + (c2.b - c1.b)*value,
  133. c1.a + (c2.a - c1.a)*value);
  134. }
  135. }