mirror of
https://github.com/maxartz15/VertexAnimation.git
synced 2025-06-13 14:56:18 +02:00
Simplify data.
Remoing asset builder. Simple data setup.
This commit is contained in:
54
Runtime/Scripts/ModelBaker/NamingConventionUtils.cs
Normal file
54
Runtime/Scripts/ModelBaker/NamingConventionUtils.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TAO.VertexAnimation
|
||||
{
|
||||
public static class NamingConventionUtils
|
||||
{
|
||||
public struct TextureInfo
|
||||
{
|
||||
public string name;
|
||||
public int frames;
|
||||
public int maxFrames;
|
||||
public int fps;
|
||||
}
|
||||
|
||||
public static TextureInfo GetTextureInfo(this string name)
|
||||
{
|
||||
TextureInfo textureInfo = new TextureInfo();
|
||||
|
||||
string[] parts = name.Split('_');
|
||||
foreach (var p in parts)
|
||||
{
|
||||
if (p.StartsWith("N-"))
|
||||
{
|
||||
textureInfo.name = p.Remove(0, 2);
|
||||
}
|
||||
else if (p.StartsWith("F-"))
|
||||
{
|
||||
if (int.TryParse(p.Remove(0, 2), out int frames))
|
||||
{
|
||||
textureInfo.frames = frames;
|
||||
}
|
||||
}
|
||||
else if (p.StartsWith("MF-"))
|
||||
{
|
||||
if (int.TryParse(p.Remove(0, 3), out int maxFrames))
|
||||
{
|
||||
textureInfo.maxFrames = maxFrames;
|
||||
}
|
||||
}
|
||||
else if (p.StartsWith("FPS-"))
|
||||
{
|
||||
if (int.TryParse(p.Remove(0, 4), out int fps))
|
||||
{
|
||||
textureInfo.fps = fps;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return textureInfo;
|
||||
}
|
||||
}
|
||||
}
|
11
Runtime/Scripts/ModelBaker/NamingConventionUtils.cs.meta
Normal file
11
Runtime/Scripts/ModelBaker/NamingConventionUtils.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e54b4ebcbd661d44592a0fa8e9f7d1a2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
30
Runtime/Scripts/VA_Animation.cs
Normal file
30
Runtime/Scripts/VA_Animation.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace TAO.VertexAnimation
|
||||
{
|
||||
public class VA_Animation : ScriptableObject
|
||||
{
|
||||
public VA_Animation(int a_maxFrames, int a_frames, int a_fps, int a_positionMapIndex, int a_colorMapIndex = -1)
|
||||
{
|
||||
Data = new VA_AnimationData(this.name, a_frames, a_maxFrames, a_fps, a_positionMapIndex, a_colorMapIndex);
|
||||
}
|
||||
|
||||
public VA_Animation(VA_AnimationData a_data)
|
||||
{
|
||||
this.name = a_data.name.ToString();
|
||||
Data = a_data;
|
||||
}
|
||||
|
||||
public VA_AnimationData Data
|
||||
{
|
||||
get; private set;
|
||||
}
|
||||
|
||||
// data.name will be overwritten by this.name.
|
||||
public void SetData(VA_AnimationData a_data)
|
||||
{
|
||||
a_data.name = this.name;
|
||||
Data = a_data;
|
||||
}
|
||||
}
|
||||
}
|
11
Runtime/Scripts/VA_Animation.cs.meta
Normal file
11
Runtime/Scripts/VA_Animation.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3a9c3bcb07d87f4087ae78ddec018b4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -6,173 +6,120 @@ namespace TAO.VertexAnimation
|
||||
[CreateAssetMenu(fileName = "new AnimationBook", menuName = "TAO/VertexAnimation/AnimationBook", order = 400)]
|
||||
public class VA_AnimationBook : ScriptableObject
|
||||
{
|
||||
public PlayData playData = null;
|
||||
#if UNITY_EDITOR
|
||||
public EditorData editorData = new EditorData();
|
||||
#endif
|
||||
|
||||
private void OnValidate()
|
||||
public VA_AnimationBook(Texture2DArray a_positionMap)
|
||||
{
|
||||
// TODO: Check for naming conflicts and textures.
|
||||
// TODO: Debug message box instead of debug logs.
|
||||
positionMap = a_positionMap;
|
||||
}
|
||||
|
||||
public VA_AnimationBook(Texture2DArray a_positionMap, List<VA_Animation> a_animations)
|
||||
{
|
||||
positionMap = a_positionMap;
|
||||
|
||||
foreach (var a in a_animations)
|
||||
{
|
||||
TryAddAnimation(a);
|
||||
}
|
||||
}
|
||||
|
||||
public int MaxFrames
|
||||
{
|
||||
get; private set;
|
||||
}
|
||||
|
||||
public Texture2DArray positionMap = null;
|
||||
public List<VA_Animation> animations = new List<VA_Animation>();
|
||||
public List<Material> materials = new List<Material>();
|
||||
|
||||
public bool TryAddAnimation(VA_Animation animation)
|
||||
{
|
||||
if (animations != null && animations.Count != 0)
|
||||
{
|
||||
if (!animations.Contains(animation) && animation.Data.maxFrames == MaxFrames)
|
||||
{
|
||||
animations.Add(animation);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add first animation.
|
||||
animations.Add(animation);
|
||||
// Set maxFrames for this animation book.
|
||||
MaxFrames = animations[0].Data.maxFrames;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryAddMaterial(Material material)
|
||||
{
|
||||
if (!materials.Contains(material))
|
||||
{
|
||||
if (material.HasProperty("_PositionMap") && material.HasProperty("_MaxFrames"))
|
||||
{
|
||||
materials.Add(material);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void RemoveAnimation(VA_Animation animation)
|
||||
{
|
||||
if (animations != null)
|
||||
{
|
||||
animations.Remove(animation);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetMaterials()
|
||||
{
|
||||
if (playData.materials != null)
|
||||
if (materials != null)
|
||||
{
|
||||
foreach (Material mat in playData.materials)
|
||||
foreach (var mat in materials)
|
||||
{
|
||||
if (mat != null)
|
||||
{
|
||||
if (mat.HasProperty("_MaxFrames"))
|
||||
{
|
||||
mat.SetFloat("_MaxFrames", playData.maxFrames);
|
||||
mat.SetFloat("_MaxFrames", MaxFrames);
|
||||
}
|
||||
|
||||
for (int i = 0; i < playData.texture2DArray.Count; i++)
|
||||
if (mat.HasProperty("_PositionMap"))
|
||||
{
|
||||
if (mat.HasProperty(playData.textureGroups[i].shaderParamName))
|
||||
{
|
||||
mat.SetTexture(playData.textureGroups[i].shaderParamName, playData.texture2DArray[i]);
|
||||
}
|
||||
mat.SetTexture("_PositionMap", positionMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region PlayData
|
||||
[System.Serializable]
|
||||
public class PlayData
|
||||
private void OnValidate()
|
||||
{
|
||||
public List<PlayTextureGroup> textureGroups = new List<PlayTextureGroup>();
|
||||
public List<PlayAnimationPage> animationPages = new List<PlayAnimationPage>();
|
||||
|
||||
public int fps;
|
||||
public int maxFrames;
|
||||
public Material[] materials;
|
||||
public List<Texture2DArray> texture2DArray = new List<Texture2DArray>();
|
||||
|
||||
// NOTE: for some reason FixedString32 data gets lost when entering play mode.
|
||||
// That is why this is here... and also the animationPages...
|
||||
public List<VA_AnimationData> GetAnimations
|
||||
if (animations != null)
|
||||
{
|
||||
get
|
||||
foreach (var a in animations)
|
||||
{
|
||||
List<VA_AnimationData> animations = new List<VA_AnimationData>();
|
||||
foreach (var ap in animationPages)
|
||||
if (a != null)
|
||||
{
|
||||
animations.Add(new VA_AnimationData
|
||||
if (a.Data.maxFrames != MaxFrames)
|
||||
{
|
||||
name = ap.name,
|
||||
frames = ap.frames,
|
||||
maxFrames = maxFrames,
|
||||
frameTime = 1.0f / maxFrames * fps,
|
||||
// TODO: Frames -1 ?????
|
||||
duration = 1.0f / maxFrames * (ap.frames - 1),
|
||||
animationMapIndex = GetFirstAnimationMapIndex(in ap.textures, in textureGroups),
|
||||
colorMapIndex = GetFirstColorMapIndex(in ap.textures, in textureGroups)
|
||||
});
|
||||
Debug.LogWarning(string.Format("{0} in {1} doesn't match maxFrames!", a.name, this.name));
|
||||
}
|
||||
}
|
||||
return animations;
|
||||
}
|
||||
}
|
||||
|
||||
public static int GetFirstAnimationMapIndex(in List<PlayTextureEntry> textures, in List<PlayTextureGroup> textureGroups)
|
||||
if (positionMap != null)
|
||||
{
|
||||
for (int i = 0; i < textureGroups.Count; i++)
|
||||
if (positionMap.depth > animations.Count)
|
||||
{
|
||||
if (textureGroups[i].textureType == TextureType.AnimationMap)
|
||||
{
|
||||
return textures[i].textureArrayIndex;
|
||||
}
|
||||
Debug.LogWarning(string.Format("More animations ({0}) than positionMaps in {1}!", animations.Count, this.name));
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int GetFirstColorMapIndex(in List<PlayTextureEntry> textures, in List<PlayTextureGroup> textureGroups)
|
||||
{
|
||||
for (int i = 0; i < textureGroups.Count; i++)
|
||||
{
|
||||
if (textureGroups[i].textureType == TextureType.ColorMap)
|
||||
{
|
||||
return textures[i].textureArrayIndex;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public struct PlayAnimationPage
|
||||
{
|
||||
public string name;
|
||||
public int frames;
|
||||
public List<PlayTextureEntry> textures;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public struct PlayTextureGroup
|
||||
{
|
||||
public string shaderParamName;
|
||||
public TextureType textureType;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public struct PlayTextureEntry
|
||||
{
|
||||
public int textureArrayIndex;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region EditorData
|
||||
#if UNITY_EDITOR
|
||||
[System.Serializable]
|
||||
public class EditorData
|
||||
{
|
||||
public List<EditorTextureGroup> textureGroups = new List<EditorTextureGroup>() { new EditorTextureGroup { shaderParamName = "_PositionMap", textureType = TextureType.AnimationMap, wrapMode = TextureWrapMode.Repeat, filterMode = FilterMode.Point, isLinear = false } };
|
||||
public List<EditorAnimationPage> animationPages = new List<EditorAnimationPage>();
|
||||
|
||||
public int fps;
|
||||
public int maxFrames;
|
||||
public Material[] materials;
|
||||
public List<Texture2DArray> texture2DArray = null;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public struct EditorAnimationPage
|
||||
{
|
||||
public string name;
|
||||
public int frames;
|
||||
public List<EditorTextureEntry> textures;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public struct EditorTextureGroup
|
||||
{
|
||||
public string shaderParamName;
|
||||
public TextureType textureType;
|
||||
public TextureWrapMode wrapMode;
|
||||
public FilterMode filterMode;
|
||||
public bool isLinear;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class EditorTextureEntry
|
||||
{
|
||||
public Texture2D texture2D = null;
|
||||
public int textureArrayIndex = -1;
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
|
||||
public enum TextureType
|
||||
{
|
||||
AnimationMap,
|
||||
ColorMap
|
||||
}
|
||||
}
|
||||
}
|
@ -10,16 +10,20 @@ namespace TAO.VertexAnimation
|
||||
private List<VA_AnimationBook> animationBooks = new List<VA_AnimationBook>();
|
||||
|
||||
[HideInInspector]
|
||||
public List<VA_AnimationData> animations = null;
|
||||
public List<VA_AnimationData> animationData = null;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
animations = new List<VA_AnimationData>();
|
||||
animationData = new List<VA_AnimationData>();
|
||||
|
||||
foreach (VA_AnimationBook ab in animationBooks)
|
||||
foreach (VA_AnimationBook book in animationBooks)
|
||||
{
|
||||
ab.SetMaterials();
|
||||
animations.AddRange(ab.playData.GetAnimations);
|
||||
book.SetMaterials();
|
||||
|
||||
foreach (VA_Animation animation in book.animations)
|
||||
{
|
||||
animationData.Add(animation.Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,12 +28,12 @@ namespace TAO.VertexAnimation
|
||||
ref VA_AnimationLibraryData animationDataBlobAsset = ref blobBuilder.ConstructRoot<VA_AnimationLibraryData>();
|
||||
|
||||
// Set all the data.
|
||||
BlobBuilderArray<VA_AnimationData> animationDataArray = blobBuilder.Allocate(ref animationDataBlobAsset.animations, animationLib.animationLibrary.animations.Count);
|
||||
BlobBuilderArray<VA_AnimationData> animationDataArray = blobBuilder.Allocate(ref animationDataBlobAsset.animations, animationLib.animationLibrary.animationData.Count);
|
||||
|
||||
for (int i = 0; i < animationDataArray.Length; i++)
|
||||
{
|
||||
// Copy data.
|
||||
animationDataArray[i] = animationLib.animationLibrary.animations[i];
|
||||
animationDataArray[i] = animationLib.animationLibrary.animationData[i];
|
||||
}
|
||||
|
||||
// Construct blob asset reference.
|
||||
|
@ -6,20 +6,31 @@ namespace TAO.VertexAnimation
|
||||
[System.Serializable]
|
||||
public struct VA_AnimationData
|
||||
{
|
||||
public VA_AnimationData(FixedString32 a_name, int a_frames, int a_maxFrames, int a_fps, int a_positionMapIndex, int a_colorMapIndex = -1)
|
||||
{
|
||||
name = a_name;
|
||||
frames = a_frames;
|
||||
maxFrames = a_maxFrames;
|
||||
animationMapIndex = a_positionMapIndex;
|
||||
colorMapIndex = a_colorMapIndex;
|
||||
frameTime = 1.0f / a_maxFrames * a_fps;
|
||||
duration = 1.0f / a_maxFrames * (a_frames - 1);
|
||||
}
|
||||
|
||||
// The name of the animation.
|
||||
public FixedString32 name;
|
||||
// The frames in this animation.
|
||||
public int frames;
|
||||
// The maximum of frames the texture holds.
|
||||
public int maxFrames;
|
||||
// 1.0f / fps.
|
||||
public float frameTime;
|
||||
// FrameTime * frames.
|
||||
public float duration;
|
||||
// The index of the related animation texture.
|
||||
public int animationMapIndex;
|
||||
// The index of the related color textures if/when added.
|
||||
public int colorMapIndex;
|
||||
// Time of a single frame.
|
||||
public float frameTime;
|
||||
// Total time of the animation.
|
||||
public float duration;
|
||||
}
|
||||
|
||||
public struct VA_AnimationLibraryData
|
||||
|
Reference in New Issue
Block a user