Mono example.

Added a simple MonoBehaviour example.
This commit is contained in:
max
2021-04-13 22:11:12 +02:00
parent ec6b1b0e9e
commit b469a30c84
28 changed files with 4532 additions and 1 deletions

View File

@ -0,0 +1,87 @@
using TAO.VertexAnimation;
using UnityEngine;
public class MonoAnimationSystem : MonoBehaviour
{
[SerializeField]
private VA_Animation[] animations = null;
private VA_Animation curAnimation = null;
private float animationTime;
private MeshRenderer[] meshRenderers = null;
private void Awake()
{
if (animations == null)
{
enabled = false;
Debug.LogWarning("No animations added!");
return;
}
curAnimation = animations[0];
meshRenderers = GetComponentsInChildren<MeshRenderer>(true);
}
private void Update()
{
PlayAnimation();
InterpolationData interpolationData = GetInterpolationData(curAnimation, animationTime);
UpdateMaterials(interpolationData);
}
// This is playing the animation.
private void PlayAnimation()
{
animationTime += Time.deltaTime * curAnimation.Data.frameTime;
if (animationTime > curAnimation.Data.duration)
{
animationTime = 0;
int newAnimation = Random.Range(0, animations.Length);
curAnimation = animations[newAnimation];
}
}
// This is something like what the hybrid renderer does with the VA_AnimationDataComponent.
private void UpdateMaterials(InterpolationData interpolationData)
{
MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock();
materialPropertyBlock.SetVector("_AnimationData", new Vector4(animationTime, curAnimation.Data.animationMapIndex, interpolationData.animationTime, interpolationData.animationMapIndex));
foreach (var mr in meshRenderers)
{
if (mr.enabled && mr.gameObject.activeSelf)
{
mr.SetPropertyBlock(materialPropertyBlock);
}
}
}
// This is resembles the VA_AnimatorSystem.
private static InterpolationData GetInterpolationData(VA_Animation animation, float animationTime)
{
InterpolationData data = new InterpolationData();
// Calculate next frame time for lerp.
float animationTimeNext = animationTime + (1.0f / animation.Data.maxFrames);
if (animationTimeNext > animation.Data.duration)
{
// Set time. Using the difference to smooth out animations when looping.
animationTimeNext -= animationTime;
}
data.animationTime = animationTimeNext;
data.animationMapIndex = animation.Data.animationMapIndex;
return data;
}
public struct InterpolationData
{
public float animationTime;
public int animationMapIndex;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6403e23dec57bf74ca12c13771cfa4c8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: