2020-12-21 23:19:05 +01:00
|
|
|
|
using System.Collections.Generic;
|
2020-12-10 19:51:02 +01:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace TAO.VertexAnimation
|
|
|
|
|
{
|
2021-01-18 13:42:45 +01:00
|
|
|
|
[CreateAssetMenu(fileName = "new AnimationBook", menuName = "TAO/VertexAnimation/AnimationBook", order = 400)]
|
2021-01-14 00:45:25 +01:00
|
|
|
|
public class VA_AnimationBook : ScriptableObject
|
|
|
|
|
{
|
2021-01-19 01:18:25 +01:00
|
|
|
|
public VA_AnimationBook(Texture2DArray a_positionMap)
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
2021-01-19 01:18:25 +01:00
|
|
|
|
positionMap = a_positionMap;
|
2021-01-14 00:45:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
public VA_AnimationBook(Texture2DArray a_positionMap, List<VA_Animation> a_animations)
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
2021-01-19 01:18:25 +01:00
|
|
|
|
positionMap = a_positionMap;
|
2021-01-14 00:45:25 +01:00
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
foreach (var a in a_animations)
|
|
|
|
|
{
|
|
|
|
|
TryAddAnimation(a);
|
2021-01-14 00:45:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 13:59:31 +01:00
|
|
|
|
public int MaxFrames { get; private set; } = 0;
|
2021-01-14 00:45:25 +01:00
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
public Texture2DArray positionMap = null;
|
|
|
|
|
public List<VA_Animation> animations = new List<VA_Animation>();
|
|
|
|
|
public List<Material> materials = new List<Material>();
|
2021-01-14 00:45:25 +01:00
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
public bool TryAddAnimation(VA_Animation animation)
|
|
|
|
|
{
|
|
|
|
|
if (animations != null && animations.Count != 0)
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
2021-01-19 13:59:31 +01:00
|
|
|
|
if (!animations.Contains(animation))
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
2021-01-19 01:18:25 +01:00
|
|
|
|
animations.Add(animation);
|
2021-01-19 13:59:31 +01:00
|
|
|
|
OnValidate();
|
2021-01-19 01:18:25 +01:00
|
|
|
|
return true;
|
2021-01-14 00:45:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-19 01:18:25 +01:00
|
|
|
|
else
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
2021-01-19 01:18:25 +01:00
|
|
|
|
// Add first animation.
|
|
|
|
|
animations.Add(animation);
|
|
|
|
|
// Set maxFrames for this animation book.
|
2021-01-19 13:59:31 +01:00
|
|
|
|
OnValidate();
|
2021-01-14 00:45:25 +01:00
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
return true;
|
2021-01-14 00:45:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryAddMaterial(Material material)
|
|
|
|
|
{
|
|
|
|
|
if (!materials.Contains(material))
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
2021-01-19 01:18:25 +01:00
|
|
|
|
if (material.HasProperty("_PositionMap") && material.HasProperty("_MaxFrames"))
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
2021-01-19 01:18:25 +01:00
|
|
|
|
materials.Add(material);
|
|
|
|
|
return true;
|
2021-01-14 00:45:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
return false;
|
2021-01-14 00:45:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 13:59:31 +01:00
|
|
|
|
public void UpdateMaterials()
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
2021-01-19 13:59:31 +01:00
|
|
|
|
OnValidate();
|
2021-01-14 00:45:25 +01:00
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
if (materials != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var mat in materials)
|
|
|
|
|
{
|
|
|
|
|
if (mat != null)
|
|
|
|
|
{
|
|
|
|
|
if (mat.HasProperty("_MaxFrames"))
|
|
|
|
|
{
|
|
|
|
|
mat.SetFloat("_MaxFrames", MaxFrames);
|
|
|
|
|
}
|
2021-01-14 00:45:25 +01:00
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
if (mat.HasProperty("_PositionMap"))
|
|
|
|
|
{
|
|
|
|
|
mat.SetTexture("_PositionMap", positionMap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-14 00:45:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 13:59:31 +01:00
|
|
|
|
private void UpdateMaxFrames()
|
|
|
|
|
{
|
|
|
|
|
if (animations != null && animations.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
if (animations[0] != null)
|
|
|
|
|
{
|
|
|
|
|
MaxFrames = animations[0].Data.maxFrames;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
private void OnValidate()
|
2021-01-14 00:45:25 +01:00
|
|
|
|
{
|
2021-01-19 13:59:31 +01:00
|
|
|
|
UpdateMaxFrames();
|
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
if (animations != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var a in animations)
|
|
|
|
|
{
|
|
|
|
|
if (a != null)
|
|
|
|
|
{
|
|
|
|
|
if (a.Data.maxFrames != MaxFrames)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning(string.Format("{0} in {1} doesn't match maxFrames!", a.name, this.name));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-14 00:45:25 +01:00
|
|
|
|
|
2021-01-19 01:18:25 +01:00
|
|
|
|
if (positionMap != null)
|
|
|
|
|
{
|
2021-01-19 13:59:31 +01:00
|
|
|
|
if (positionMap.depth < animations.Count)
|
2021-01-19 01:18:25 +01:00
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning(string.Format("More animations ({0}) than positionMaps in {1}!", animations.Count, this.name));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-14 00:45:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-10 19:51:02 +01:00
|
|
|
|
}
|