2020-12-10 19:51:02 +01:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace TAO.VertexAnimation
|
|
|
|
|
{
|
|
|
|
|
[CreateAssetMenu(fileName = "new AnimationBook", menuName = "AnimationBook", order = 0)]
|
|
|
|
|
public class VA_AnimationBook : ScriptableObject
|
|
|
|
|
{
|
|
|
|
|
public int maxFrames;
|
2020-12-14 20:27:44 +01:00
|
|
|
|
public List<TextureGroup> textureGroups = new List<TextureGroup>() { new TextureGroup { shaderParamName = "_PositionMap", textureType = TextureType.AnimationMap, wrapMode = TextureWrapMode.Repeat, filterMode = FilterMode.Point, isLinear = false } };
|
2020-12-10 19:51:02 +01:00
|
|
|
|
public List<VA_AnimationPage> animationPages = new List<VA_AnimationPage>();
|
|
|
|
|
|
|
|
|
|
public Material[] materials;
|
|
|
|
|
public List<Texture2DArray> texture2DArray = null;
|
|
|
|
|
|
|
|
|
|
public void Create()
|
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
GenerateTextures();
|
|
|
|
|
SetMaterials();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnValidate()
|
|
|
|
|
{
|
|
|
|
|
// TODO: Check for naming conflicts and textures.
|
|
|
|
|
// TODO: Debug message box instead of debug logs.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReferenceDuplicates()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < textureGroups.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
List<Texture2D> t = new List<Texture2D>();
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < animationPages.Count; j++)
|
|
|
|
|
{
|
|
|
|
|
// Check if exist.
|
|
|
|
|
if (!t.Contains(animationPages[j].textures[i].texture2D))
|
|
|
|
|
{
|
|
|
|
|
t.Add(animationPages[j].textures[i].texture2D);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add index reference.
|
|
|
|
|
animationPages[j].textures[i].textureArrayIndex = t.IndexOf(animationPages[j].textures[i].texture2D);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GenerateTextures()
|
|
|
|
|
{
|
|
|
|
|
ReferenceDuplicates();
|
|
|
|
|
|
2020-12-10 19:51:02 +01:00
|
|
|
|
texture2DArray.Clear();
|
2020-12-14 20:27:44 +01:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < textureGroups.Count; i++)
|
2020-12-10 19:51:02 +01:00
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
var t = GetTextures(i).ToArray();
|
|
|
|
|
if (VA_Texture2DArrayUtils.IsValidForTextureArray(t))
|
2020-12-10 19:51:02 +01:00
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
texture2DArray.Add(VA_Texture2DArrayUtils.CreateTextureArray(t, false, textureGroups[i].isLinear, textureGroups[i].wrapMode, textureGroups[i].filterMode, 1, name + textureGroups[i].shaderParamName));
|
2020-12-10 19:51:02 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-14 20:27:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Texture2D> GetTextures(int textureIndex)
|
|
|
|
|
{
|
|
|
|
|
List<Texture2D> textures = new List<Texture2D>();
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2020-12-14 20:27:44 +01:00
|
|
|
|
foreach (var ap in animationPages)
|
|
|
|
|
{
|
|
|
|
|
// Check if exist.
|
|
|
|
|
if (!textures.Contains(ap.textures[textureIndex].texture2D))
|
|
|
|
|
{
|
|
|
|
|
textures.Add(ap.textures[textureIndex].texture2D);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return textures;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetMaterials()
|
|
|
|
|
{
|
|
|
|
|
if (materials != null)
|
2020-12-10 19:51:02 +01:00
|
|
|
|
{
|
|
|
|
|
foreach (Material mat in materials)
|
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
if (mat != null)
|
2020-12-10 19:51:02 +01:00
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
if (mat.HasProperty("_MaxFrames"))
|
|
|
|
|
{
|
|
|
|
|
mat.SetFloat("_MaxFrames", maxFrames);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 19:51:02 +01:00
|
|
|
|
for (int i = 0; i < texture2DArray.Count; i++)
|
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
if (mat.HasProperty(textureGroups[i].shaderParamName))
|
|
|
|
|
{
|
|
|
|
|
mat.SetTexture(textureGroups[i].shaderParamName, texture2DArray[i]);
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 20:27:44 +01:00
|
|
|
|
public List<VA_AnimationData> GetAnimationData()
|
2020-12-10 19:51:02 +01:00
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
List<VA_AnimationData> data = new List<VA_AnimationData>();
|
|
|
|
|
|
|
|
|
|
foreach (var ap in animationPages)
|
|
|
|
|
{
|
|
|
|
|
data.Add(new VA_AnimationData
|
|
|
|
|
{
|
|
|
|
|
name = ap.name,
|
|
|
|
|
frames = ap.frames,
|
|
|
|
|
maxFrames = maxFrames,
|
|
|
|
|
frameTime = 1.0f / maxFrames,
|
|
|
|
|
duration = 1.0f / maxFrames * ap.frames,
|
|
|
|
|
animationMapIndex = GetFirstAnimationMapIndex(in ap.textures),
|
|
|
|
|
colorMapIndex = GetFirstColorMapIndex(in ap.textures)
|
|
|
|
|
});
|
2020-12-10 19:51:02 +01:00
|
|
|
|
}
|
2020-12-14 20:27:44 +01:00
|
|
|
|
|
|
|
|
|
return data;
|
2020-12-10 19:51:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 20:27:44 +01:00
|
|
|
|
private int GetFirstAnimationMapIndex(in List<TextureEntry> textures)
|
2020-12-10 19:51:02 +01:00
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < textureGroups.Count; i++)
|
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
if(textureGroups[i].textureType == TextureType.AnimationMap)
|
|
|
|
|
{
|
|
|
|
|
return textures[i].textureArrayIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
|
2020-12-14 20:27:44 +01:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetFirstColorMapIndex(in List<TextureEntry> textures)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < textureGroups.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (textureGroups[i].textureType == TextureType.ColorMap)
|
2020-12-10 19:51:02 +01:00
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
return textures[i].textureArrayIndex;
|
2020-12-10 19:51:02 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 20:27:44 +01:00
|
|
|
|
return -1;
|
2020-12-10 19:51:02 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public struct VA_AnimationPage
|
|
|
|
|
{
|
|
|
|
|
public string name;
|
|
|
|
|
public int frames;
|
2020-12-14 20:27:44 +01:00
|
|
|
|
public List<TextureEntry> textures;
|
2020-12-10 19:51:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public struct TextureGroup
|
|
|
|
|
{
|
|
|
|
|
public string shaderParamName;
|
2020-12-14 20:27:44 +01:00
|
|
|
|
public TextureType textureType;
|
|
|
|
|
public TextureWrapMode wrapMode;
|
|
|
|
|
public FilterMode filterMode;
|
2020-12-10 19:51:02 +01:00
|
|
|
|
public bool isLinear;
|
|
|
|
|
}
|
2020-12-14 20:27:44 +01:00
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public class TextureEntry
|
|
|
|
|
{
|
|
|
|
|
public Texture2D texture2D = null;
|
|
|
|
|
public int textureArrayIndex = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum TextureType
|
|
|
|
|
{
|
|
|
|
|
AnimationMap,
|
|
|
|
|
ColorMap
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
}
|