Texture resize done using raw data from texture2d. Got rid of unused plugins. Improved image change timing
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
using System.Threading;
|
||||
// Initial version at: http://wiki.unity3d.com/index.php/TextureScale#TextureScale.cs
|
||||
// Modified by ftc to work with raw texture data (it consumes less memory).
|
||||
// Currently works only on RGB24 and RGBA32 textures.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Unity.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
public class TextureScale
|
||||
@@ -14,8 +20,10 @@ public class TextureScale
|
||||
}
|
||||
}
|
||||
|
||||
private static Color32[] texColors;
|
||||
private static NativeArray<byte> texBytes;
|
||||
private static TextureFormat texFormat;
|
||||
private static Color32[] newColors;
|
||||
private static int h;
|
||||
private static int w;
|
||||
private static float ratioX;
|
||||
private static float ratioY;
|
||||
@@ -35,8 +43,16 @@ public class TextureScale
|
||||
|
||||
private static void ThreadedScale(Texture2D tex, int newWidth, int newHeight, bool useBilinear)
|
||||
{
|
||||
texColors = tex.GetPixels32();
|
||||
newColors = new Color32[newWidth * newHeight];
|
||||
texBytes = tex.GetRawTextureData<byte>();
|
||||
texFormat = tex.format;
|
||||
if (texFormat != TextureFormat.RGB24 && texFormat != TextureFormat.RGBA32)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
if (newColors == null || newColors.Length < newWidth * newHeight)
|
||||
{
|
||||
newColors = new Color32[newWidth * newHeight];
|
||||
}
|
||||
if (useBilinear)
|
||||
{
|
||||
ratioX = 1.0f / ((float)newWidth / (tex.width - 1));
|
||||
@@ -48,6 +64,7 @@ public class TextureScale
|
||||
ratioY = ((float)tex.height) / newHeight;
|
||||
}
|
||||
w = tex.width;
|
||||
h = tex.height;
|
||||
w2 = newWidth;
|
||||
var cores = Mathf.Min(SystemInfo.processorCount, newHeight);
|
||||
var slice = newHeight / cores;
|
||||
@@ -96,19 +113,63 @@ public class TextureScale
|
||||
}
|
||||
|
||||
tex.Resize(newWidth, newHeight);
|
||||
tex.SetPixels32(newColors);
|
||||
var newBytes = tex.GetRawTextureData<byte>();
|
||||
for (int i = 0; i < newWidth * newHeight; i++)
|
||||
{
|
||||
var color = newColors[i];
|
||||
if (tex.format == TextureFormat.RGB24)
|
||||
{
|
||||
int s = 3 * i;
|
||||
newBytes[s] = color.r;
|
||||
newBytes[s + 1] = color.g;
|
||||
newBytes[s + 2] = color.b;
|
||||
}
|
||||
else if (tex.format == TextureFormat.RGBA32)
|
||||
{
|
||||
int s = 4 * i;
|
||||
newBytes[s] = color.r;
|
||||
newBytes[s + 1] = color.g;
|
||||
newBytes[s + 2] = color.b;
|
||||
newBytes[s + 3] = color.a;
|
||||
}
|
||||
}
|
||||
tex.Apply();
|
||||
|
||||
texColors = null;
|
||||
newColors = null;
|
||||
//newColors = null;
|
||||
}
|
||||
|
||||
private static Color32 GetColor(int position)
|
||||
{
|
||||
if (texFormat == TextureFormat.RGB24)
|
||||
{
|
||||
int s = (int)position * 3;
|
||||
Color32 result = new Color32();
|
||||
result.r = texBytes[s];
|
||||
result.g = texBytes[s + 1];
|
||||
result.b = texBytes[s + 2];
|
||||
result.a = 0;
|
||||
return result;
|
||||
}
|
||||
else if (texFormat == TextureFormat.RGBA32)
|
||||
{
|
||||
int s = (int)position * 4;
|
||||
Color32 result = new Color32();
|
||||
result.r = texBytes[s];
|
||||
result.g = texBytes[s + 1];
|
||||
result.b = texBytes[s + 2];
|
||||
result.a = texBytes[s + 3];
|
||||
return result;
|
||||
}
|
||||
else throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static void BilinearScale(System.Object obj)
|
||||
{
|
||||
ThreadData threadData = (ThreadData)obj;
|
||||
for (var y = threadData.start; y < threadData.end; y++)
|
||||
{
|
||||
int yFloor = (int)Mathf.Floor(y * ratioY);
|
||||
while (yFloor + 1 >= h) yFloor--;
|
||||
var y1 = yFloor * w;
|
||||
var y2 = (yFloor + 1) * w;
|
||||
var yw = y * w2;
|
||||
@@ -117,12 +178,11 @@ public class TextureScale
|
||||
{
|
||||
int xFloor = (int)Mathf.Floor(x * ratioX);
|
||||
var xLerp = x * ratioX - xFloor;
|
||||
newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor + 1], xLerp),
|
||||
ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor + 1], xLerp),
|
||||
y * ratioY - yFloor);
|
||||
var c1 = ColorLerpUnclamped(GetColor(y1 + xFloor), GetColor(y1 + xFloor + 1), xLerp);
|
||||
var c2 = ColorLerpUnclamped(GetColor(y2 + xFloor), GetColor(y2 + xFloor + 1), xLerp);
|
||||
newColors[yw + x] = ColorLerpUnclamped(c1, c2, y * ratioY - yFloor);
|
||||
}
|
||||
}
|
||||
|
||||
mutex.WaitOne();
|
||||
finishCount++;
|
||||
mutex.ReleaseMutex();
|
||||
@@ -137,7 +197,7 @@ public class TextureScale
|
||||
var yw = y * w2;
|
||||
for (var x = 0; x < w2; x++)
|
||||
{
|
||||
newColors[yw + x] = texColors[(int)(thisY + ratioX * x)];
|
||||
newColors[yw + x] = GetColor((int)(thisY + ratioX * x));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user