Store pixel data in byte array and use SetPixelData when possible.
This commit is contained in:
Родитель
070d46f2ae
Коммит
75ba599d15
|
@ -112,39 +112,36 @@ namespace UnityVolumeRendering
|
|||
TextureFormat texformat = SystemInfo.SupportsTextureFormat(TextureFormat.RHalf) ? TextureFormat.RHalf : TextureFormat.RFloat;
|
||||
Texture3D texture = new Texture3D(dimX, dimY, dimZ, texformat, false);
|
||||
texture.wrapMode = TextureWrapMode.Clamp;
|
||||
|
||||
|
||||
int minValue = GetMinDataValue();
|
||||
int maxValue = GetMaxDataValue();
|
||||
int maxRange = maxValue - minValue;
|
||||
|
||||
Color[] cols;
|
||||
bool isHalfFloat = texformat == TextureFormat.RHalf;
|
||||
try
|
||||
{
|
||||
cols = new Color[data.Length];
|
||||
// Create a bute array for filling the texture. Store has half (16 bit) or single (32 bit) float values.
|
||||
int sampleSize = isHalfFloat ? 2 : 4;
|
||||
byte[] bytes = new byte[data.Length * sampleSize]; // This can cause OutOfMemoryException
|
||||
for (int iData = 0; iData < data.Length; iData++)
|
||||
{
|
||||
float pixelValue = (float)(data[iData] - minValue) / maxRange;
|
||||
byte[] pixelBytes = isHalfFloat ? BitConverter.GetBytes(Mathf.FloatToHalf(pixelValue)) : BitConverter.GetBytes(pixelValue);
|
||||
|
||||
Array.Copy(pixelBytes, 0, bytes, iData * sampleSize, sampleSize);
|
||||
}
|
||||
|
||||
texture.SetPixelData(bytes, 0);
|
||||
}
|
||||
catch (OutOfMemoryException ex)
|
||||
{
|
||||
cols = null;
|
||||
Debug.LogWarning("Out of memory when creating texture. Using fallback method.");
|
||||
for (int x = 0; x < dimX; x++)
|
||||
for (int y = 0; y < dimY; y++)
|
||||
for (int z = 0; z < dimZ; z++)
|
||||
texture.SetPixel(x, y, z, new Color((float)(data[x + y * dimX + z * (dimX * dimY)] - minValue) / maxRange, 0.0f, 0.0f, 0.0f));
|
||||
}
|
||||
for (int x = 0; x < dimX; x++)
|
||||
{
|
||||
for (int y = 0; y < dimY; y++)
|
||||
{
|
||||
for (int z = 0; z < dimZ; z++)
|
||||
{
|
||||
int iData = x + y * dimX + z * (dimX * dimY);
|
||||
if (cols == null)
|
||||
{
|
||||
texture.SetPixel(x, y, z, new Color((float)(data[iData] - minValue) / maxRange, 0.0f, 0.0f, 0.0f));
|
||||
}
|
||||
else
|
||||
{
|
||||
cols[iData] = new Color((float)(data[iData] - minValue) / maxRange, 0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(cols != null) texture.SetPixels(cols);
|
||||
|
||||
texture.Apply();
|
||||
return texture;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче