2020-12-10 19:51:02 +01:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace TAO.VertexAnimation
|
|
|
|
|
{
|
2022-12-10 15:02:53 +01:00
|
|
|
|
public static class Texture2DArrayUtils
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
|
|
|
|
public static Texture2DArray CreateTextureArray(Texture2D[] a_textures, bool a_useMipChain, bool a_isLinear,
|
2021-01-18 13:42:45 +01:00
|
|
|
|
TextureWrapMode a_wrapMode = TextureWrapMode.Repeat, FilterMode a_filterMode = FilterMode.Bilinear, int a_anisoLevel = 1, string a_name = "", bool a_makeNoLongerReadable = true)
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
|
|
|
|
if(!IsValidForTextureArray(a_textures) || !IsValidCopyTexturePlatform())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
Texture2DArray textureArray = new Texture2DArray(a_textures[0].width, a_textures[0].height, a_textures.Length, a_textures[0].format, a_useMipChain, a_isLinear);
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
if (IsValidCopyTexturePlatform())
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < a_textures.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Graphics.CopyTexture(a_textures[i], 0, 0, textureArray, i, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
textureArray.wrapMode = a_wrapMode;
|
|
|
|
|
textureArray.filterMode = a_filterMode;
|
|
|
|
|
textureArray.anisoLevel = a_anisoLevel;
|
|
|
|
|
textureArray.name = a_name;
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2021-01-18 13:42:45 +01:00
|
|
|
|
textureArray.Apply(false, a_makeNoLongerReadable);
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
return textureArray;
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
public static bool IsValidForTextureArray(Texture2D[] a_textures)
|
|
|
|
|
{
|
|
|
|
|
if (a_textures == null || a_textures.Length <= 0)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("No textures assigned!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
for (int i = 0; i < a_textures.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (a_textures[i] == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Texture " + i.ToString() + " not assigned!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
if (a_textures[0].width != a_textures[i].width || a_textures[0].height != a_textures[i].height)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Texture " + a_textures[i].name + " has a different size!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
if (a_textures[0].format != a_textures[i].format || a_textures[0].graphicsFormat != a_textures[i].graphicsFormat)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Texture " + a_textures[i].name + " has a different format/graphics format!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-21 03:21:51 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
if (!a_textures[0].isReadable)
|
|
|
|
|
{
|
2020-12-21 03:21:51 +01:00
|
|
|
|
#if UNITY_EDITOR
|
2021-01-14 00:45:25 +01:00
|
|
|
|
//Debug.LogWarning("Texture " + a_textures[i].name + " is not readable!");
|
|
|
|
|
return true;
|
2020-12-21 03:21:51 +01:00
|
|
|
|
#else
|
2021-01-14 00:45:25 +01:00
|
|
|
|
Debug.LogError("Texture " + a_textures[i].name + " is not readable!");
|
|
|
|
|
return false;
|
2020-12-21 03:21:51 +01:00
|
|
|
|
#endif
|
2021-01-14 00:45:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-12-21 03:21:51 +01:00
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
|
public static bool IsValidCopyTexturePlatform()
|
|
|
|
|
{
|
|
|
|
|
switch (SystemInfo.copyTextureSupport)
|
|
|
|
|
{
|
|
|
|
|
case UnityEngine.Rendering.CopyTextureSupport.None:
|
|
|
|
|
Debug.LogError("No CopyTextureSupport on this platform!");
|
|
|
|
|
return false;
|
|
|
|
|
case UnityEngine.Rendering.CopyTextureSupport.Basic:
|
|
|
|
|
return true;
|
|
|
|
|
case UnityEngine.Rendering.CopyTextureSupport.Copy3D:
|
|
|
|
|
return true;
|
|
|
|
|
case UnityEngine.Rendering.CopyTextureSupport.DifferentTypes:
|
|
|
|
|
return true;
|
|
|
|
|
case UnityEngine.Rendering.CopyTextureSupport.TextureToRT:
|
|
|
|
|
return true;
|
|
|
|
|
case UnityEngine.Rendering.CopyTextureSupport.RTToTexture:
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
2020-12-21 03:21:51 +01:00
|
|
|
|
#if UNITY_EDITOR
|
2021-01-14 00:45:25 +01:00
|
|
|
|
return true;
|
2020-12-21 03:21:51 +01:00
|
|
|
|
#else
|
2021-01-14 00:45:25 +01:00
|
|
|
|
Debug.LogError("No CopyTextureSupport on this platform!");
|
|
|
|
|
return false;
|
2020-12-21 03:21:51 +01:00
|
|
|
|
#endif
|
2021-01-14 00:45:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
}
|