2021-01-09 15:56:38 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace TAO.VertexAnimation
|
|
|
|
{
|
2021-01-14 00:45:25 +01:00
|
|
|
public static class AnimationBaker
|
|
|
|
{
|
|
|
|
[System.Serializable]
|
|
|
|
public struct BakedData
|
|
|
|
{
|
|
|
|
public Mesh mesh;
|
|
|
|
public List<Texture2D> positionMaps;
|
2021-01-18 13:42:45 +01:00
|
|
|
public int maxFrames;
|
2021-02-18 03:19:07 +01:00
|
|
|
public Vector3 minBounds;
|
|
|
|
public Vector3 maxBounds;
|
2021-01-14 00:45:25 +01:00
|
|
|
|
|
|
|
// Returns main position map.
|
|
|
|
public Texture2D GetPositionMap
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return positionMaps[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
public struct AnimationInfo
|
|
|
|
{
|
2021-01-21 10:13:29 +01:00
|
|
|
public bool applyRootMotion;
|
2021-01-14 00:45:25 +01:00
|
|
|
public int rawFrameHeight;
|
|
|
|
public int frameHeight;
|
|
|
|
public int frameSpacing;
|
|
|
|
public int frames;
|
|
|
|
public int maxFrames;
|
|
|
|
public int textureWidth;
|
|
|
|
public int textureHeight;
|
|
|
|
public int fps;
|
|
|
|
|
|
|
|
// Create animation info and calculate values.
|
2021-01-21 10:13:29 +01:00
|
|
|
public AnimationInfo(Mesh mesh, bool applyRootMotion, int frames, int textureWidth, int fps)
|
2021-01-14 00:45:25 +01:00
|
|
|
{
|
2021-01-21 10:13:29 +01:00
|
|
|
this.applyRootMotion = applyRootMotion;
|
2021-01-14 00:45:25 +01:00
|
|
|
this.frames = frames;
|
|
|
|
this.textureWidth = textureWidth;
|
|
|
|
this.fps = fps;
|
|
|
|
|
|
|
|
rawFrameHeight = Mathf.CeilToInt((float)mesh.vertices.Length / this.textureWidth);
|
|
|
|
frameHeight = Mathf.NextPowerOfTwo(rawFrameHeight);
|
|
|
|
frameSpacing = (frameHeight - rawFrameHeight) + 1;
|
|
|
|
|
|
|
|
textureHeight = Mathf.NextPowerOfTwo(frameHeight * this.frames);
|
|
|
|
|
|
|
|
maxFrames = textureHeight / frameHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 03:19:07 +01:00
|
|
|
[System.Serializable]
|
|
|
|
public struct BakedAnimation
|
|
|
|
{
|
|
|
|
public Texture2D positionMap;
|
|
|
|
public Vector3 minBounds;
|
|
|
|
public Vector3 maxBounds;
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:13:29 +01:00
|
|
|
public static BakedData Bake(this GameObject model, AnimationClip[] animationClips, bool applyRootMotion, int fps, int textureWidth)
|
2021-01-14 00:45:25 +01:00
|
|
|
{
|
|
|
|
BakedData bakedData = new BakedData()
|
|
|
|
{
|
|
|
|
mesh = null,
|
2021-02-18 03:19:07 +01:00
|
|
|
positionMaps = new List<Texture2D>(),
|
|
|
|
minBounds = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue),
|
|
|
|
maxBounds = new Vector3(float.MinValue, float.MinValue, float.MinValue)
|
2021-01-14 00:45:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Calculate what our max frames/time is going to be.
|
|
|
|
int maxFrames = 0;
|
|
|
|
foreach (AnimationClip ac in animationClips)
|
|
|
|
{
|
|
|
|
int frames = Mathf.FloorToInt(fps * ac.length);
|
|
|
|
|
|
|
|
if (maxFrames < frames)
|
|
|
|
{
|
|
|
|
maxFrames = frames;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the target mesh to calculate the animation info.
|
|
|
|
Mesh mesh = model.GetComponent<SkinnedMeshRenderer>().sharedMesh;
|
|
|
|
|
|
|
|
// Get the info for the biggest animation.
|
2021-01-21 10:13:29 +01:00
|
|
|
AnimationInfo animationInfo = new AnimationInfo(mesh, applyRootMotion, maxFrames, textureWidth, fps);
|
2021-01-14 00:45:25 +01:00
|
|
|
|
|
|
|
foreach (AnimationClip ac in animationClips)
|
|
|
|
{
|
|
|
|
// Set the frames for this animation.
|
|
|
|
animationInfo.frames = Mathf.FloorToInt(fps * ac.length);
|
|
|
|
|
|
|
|
BakedData bd = Bake(model, ac, animationInfo);
|
|
|
|
bakedData.mesh = bd.mesh;
|
|
|
|
bakedData.positionMaps.AddRange(bd.positionMaps);
|
2021-01-18 13:42:45 +01:00
|
|
|
bakedData.maxFrames = maxFrames;
|
2021-02-18 03:19:07 +01:00
|
|
|
bakedData.minBounds = Vector3.Min(bakedData.minBounds, bd.minBounds);
|
|
|
|
bakedData.maxBounds = Vector3.Max(bakedData.maxBounds, bd.maxBounds);
|
2021-01-14 00:45:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 03:19:07 +01:00
|
|
|
bakedData.mesh.bounds = new Bounds()
|
|
|
|
{
|
|
|
|
max = bakedData.maxBounds,
|
|
|
|
min = bakedData.minBounds
|
|
|
|
};
|
2021-02-17 21:20:53 +01:00
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
return bakedData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BakedData Bake(this GameObject model, AnimationClip animationClip, AnimationInfo animationInfo)
|
|
|
|
{
|
|
|
|
Mesh mesh = new Mesh
|
|
|
|
{
|
|
|
|
name = string.Format("{0}", model.name)
|
|
|
|
};
|
|
|
|
|
2021-01-21 10:13:29 +01:00
|
|
|
// Set root motion options.
|
|
|
|
if (model.TryGetComponent(out Animator animator))
|
|
|
|
{
|
|
|
|
animator.applyRootMotion = animationInfo.applyRootMotion;
|
|
|
|
}
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
// Bake mesh for a copy and to apply the new UV's to.
|
|
|
|
SkinnedMeshRenderer skinnedMeshRenderer = model.GetComponent<SkinnedMeshRenderer>();
|
2022-12-10 15:02:53 +01:00
|
|
|
mesh = model.GetComponent<SkinnedMeshRenderer>().sharedMesh.Copy();
|
2021-01-14 00:45:25 +01:00
|
|
|
mesh.RecalculateBounds();
|
|
|
|
|
|
|
|
mesh.uv3 = mesh.BakePositionUVs(animationInfo);
|
|
|
|
|
2021-02-18 03:19:07 +01:00
|
|
|
BakedAnimation bakedAnimation = BakeAnimation(model, animationClip, animationInfo);
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
BakedData bakedData = new BakedData()
|
|
|
|
{
|
|
|
|
mesh = mesh,
|
2021-02-18 03:19:07 +01:00
|
|
|
positionMaps = new List<Texture2D>() { bakedAnimation.positionMap },
|
2021-02-17 21:20:53 +01:00
|
|
|
maxFrames = animationInfo.maxFrames,
|
2021-02-18 03:19:07 +01:00
|
|
|
minBounds = bakedAnimation.minBounds,
|
|
|
|
maxBounds = bakedAnimation.maxBounds
|
|
|
|
};
|
|
|
|
|
|
|
|
mesh.bounds = new Bounds()
|
|
|
|
{
|
|
|
|
max = bakedAnimation.maxBounds,
|
|
|
|
min = bakedAnimation.minBounds
|
2021-01-14 00:45:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return bakedData;
|
|
|
|
}
|
|
|
|
|
2021-02-18 03:19:07 +01:00
|
|
|
public static BakedAnimation BakeAnimation(this GameObject model, AnimationClip animationClip, AnimationInfo animationInfo)
|
2021-01-14 00:45:25 +01:00
|
|
|
{
|
|
|
|
// Create positionMap Texture without MipMaps which is Linear and HDR to store values in a bigger range.
|
|
|
|
Texture2D positionMap = new Texture2D(animationInfo.textureWidth, animationInfo.textureHeight, TextureFormat.RGBAHalf, false, true);
|
|
|
|
|
2021-02-17 21:20:53 +01:00
|
|
|
// Keep track of min/max bounds.
|
2021-02-18 03:19:07 +01:00
|
|
|
Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
|
|
|
|
Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
|
2021-02-17 21:20:53 +01:00
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
// Create instance to sample from.
|
|
|
|
GameObject inst = GameObject.Instantiate(model);
|
|
|
|
SkinnedMeshRenderer skinnedMeshRenderer = inst.GetComponent<SkinnedMeshRenderer>();
|
|
|
|
|
|
|
|
int y = 0;
|
|
|
|
for (int f = 0; f < animationInfo.frames; f++)
|
|
|
|
{
|
|
|
|
animationClip.SampleAnimation(inst, (animationClip.length / animationInfo.frames) * f);
|
|
|
|
|
|
|
|
Mesh sampledMesh = new Mesh();
|
|
|
|
skinnedMeshRenderer.BakeMesh(sampledMesh);
|
|
|
|
|
|
|
|
List<Vector3> verts = new List<Vector3>();
|
|
|
|
sampledMesh.GetVertices(verts);
|
|
|
|
List<Vector3> normals = new List<Vector3>();
|
|
|
|
sampledMesh.GetNormals(normals);
|
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
for (int v = 0; v < verts.Count; v++)
|
|
|
|
{
|
2021-02-18 03:19:07 +01:00
|
|
|
min = Vector3.Min(min, verts[v]);
|
|
|
|
max = Vector3.Max(max, verts[v]);
|
|
|
|
|
2021-01-14 00:45:25 +01:00
|
|
|
positionMap.SetPixel(x, y,
|
|
|
|
new Color(verts[v].x, verts[v].y, verts[v].z,
|
2021-02-03 22:13:59 +01:00
|
|
|
VectorUtils.EncodeFloat3ToFloat1(normals[v]))
|
2021-01-14 00:45:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
x++;
|
|
|
|
if (x >= animationInfo.textureWidth)
|
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
y += animationInfo.frameSpacing;
|
|
|
|
}
|
|
|
|
|
|
|
|
GameObject.DestroyImmediate(inst);
|
|
|
|
|
2021-01-19 16:35:37 +01:00
|
|
|
positionMap.name = string.Format("VA_N-{0}_F-{1}_MF-{2}_FPS-{3}", NamingConventionUtils.ConvertToValidString(animationClip.name), animationInfo.frames, animationInfo.maxFrames, animationInfo.fps);
|
2021-01-14 00:45:25 +01:00
|
|
|
positionMap.filterMode = FilterMode.Point;
|
|
|
|
positionMap.Apply(false, true);
|
|
|
|
|
2021-02-18 03:19:07 +01:00
|
|
|
return new BakedAnimation()
|
|
|
|
{
|
|
|
|
positionMap = positionMap,
|
|
|
|
minBounds = min,
|
|
|
|
maxBounds = max
|
|
|
|
};
|
2021-01-14 00:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Vector2[] BakePositionUVs(this Mesh mesh, AnimationInfo animationInfo)
|
|
|
|
{
|
|
|
|
Vector2[] uv3 = new Vector2[mesh.vertexCount];
|
|
|
|
|
|
|
|
float xOffset = 1.0f / animationInfo.textureWidth;
|
|
|
|
float yOffset = 1.0f / animationInfo.textureHeight;
|
|
|
|
|
|
|
|
float x = xOffset / 2.0f;
|
|
|
|
float y = yOffset / 2.0f;
|
|
|
|
|
|
|
|
for (int v = 0; v < uv3.Length; v++)
|
|
|
|
{
|
|
|
|
uv3[v] = new Vector2(x, y);
|
|
|
|
|
|
|
|
x += xOffset;
|
|
|
|
if (x >= 1.0f)
|
|
|
|
{
|
|
|
|
x = xOffset / 2.0f;
|
|
|
|
y += yOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh.uv3 = uv3;
|
|
|
|
|
|
|
|
return uv3;
|
|
|
|
}
|
|
|
|
}
|
2021-01-09 15:56:38 +01:00
|
|
|
}
|