You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

53 lines
1.5 KiB

using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
public class AliasModel
{
public static readonly Regex AnimationRegex = new Regex(@"^[a-zA-Z]+");
private readonly List<(int, Mesh)> animationMeshes = new List<(int, Mesh)>(); // TODO: make this a fixed array after initialization
public void AddAnimation(int startFrame, Mesh mesh)
{
animationMeshes.Add((startFrame, mesh));
}
public int GetAnimationFrameCount(float frameNum)
{
if (!FindAnimation((int)frameNum, out Mesh mesh, out int startFrame))
return 0;
return mesh.GetBlendShapeFrameCount(0);
}
public void Animate(float frameNum, out Mesh mesh, out float blendWeight)
{
blendWeight = 0;
int frameIndex = (int)frameNum;
if (!FindAnimation(frameIndex, out mesh, out int startFrame))
return;
int numFrames = mesh.GetBlendShapeFrameCount(0);
blendWeight = ((frameNum - startFrame) / numFrames) % 1;
}
private bool FindAnimation(int frameIndex, out Mesh mesh, out int startFrame)
{
mesh = null;
startFrame = 0;
for (int i = 0; i < animationMeshes.Count; ++i)
{
startFrame = animationMeshes[i].Item1;
if (frameIndex >= startFrame)
{
mesh = animationMeshes[i].Item2;
return true;
}
}
return false; // Shouldn't happen
}
}