Advertisement
Guest User

Untitled

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