From bc8aa08193dcfd24d75238e39f741ef3613d773c Mon Sep 17 00:00:00 2001 From: max Date: Thu, 18 Feb 2021 03:19:07 +0100 Subject: [PATCH] Recalculated Bounds. --- Editor/Scripts/ModelBaker/VA_ModelBaker.cs | 15 +++-- Runtime/Scripts/ModelBaker/AnimationBaker.cs | 65 +++++++++++++------- Runtime/Scripts/ModelBaker/MeshUtils.cs | 3 +- 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs index 0214e40..e1a2749 100644 --- a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs +++ b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs @@ -111,15 +111,20 @@ namespace TAO.VertexAnimation.Editor { AssetDatabaseUtils.RemoveChildAssets(this, new Object[2] { book, material }); - foreach (var m in meshes) + Bounds bounds = new Bounds { - m.bounds = bakedData.mesh.bounds; - m.Finalize(); - AssetDatabase.AddObjectToAsset(m, this); + max = bakedData.maxBounds, + min = bakedData.minBounds + }; + + for (int i = 0; i < meshes.Length; i++) + { + meshes[i].bounds = bounds; + meshes[i].Finalize(); + AssetDatabase.AddObjectToAsset(meshes[i], this); } AssetDatabase.AddObjectToAsset(positionMap, this); - AssetDatabase.SaveAssets(); if (generatePrefab) diff --git a/Runtime/Scripts/ModelBaker/AnimationBaker.cs b/Runtime/Scripts/ModelBaker/AnimationBaker.cs index 28427d1..9877606 100644 --- a/Runtime/Scripts/ModelBaker/AnimationBaker.cs +++ b/Runtime/Scripts/ModelBaker/AnimationBaker.cs @@ -11,6 +11,8 @@ namespace TAO.VertexAnimation public Mesh mesh; public List positionMaps; public int maxFrames; + public Vector3 minBounds; + public Vector3 maxBounds; // Returns main position map. public Texture2D GetPositionMap @@ -53,12 +55,22 @@ namespace TAO.VertexAnimation } } + [System.Serializable] + public struct BakedAnimation + { + public Texture2D positionMap; + public Vector3 minBounds; + public Vector3 maxBounds; + } + public static BakedData Bake(this GameObject model, AnimationClip[] animationClips, bool applyRootMotion, int fps, int textureWidth) { BakedData bakedData = new BakedData() { mesh = null, - positionMaps = new List() + positionMaps = new List(), + minBounds = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue), + maxBounds = new Vector3(float.MinValue, float.MinValue, float.MinValue) }; // Calculate what our max frames/time is going to be. @@ -79,9 +91,6 @@ namespace TAO.VertexAnimation // Get the info for the biggest animation. AnimationInfo animationInfo = new AnimationInfo(mesh, applyRootMotion, maxFrames, textureWidth, fps); - // Bounds - Bounds bounds = new Bounds(); - foreach (AnimationClip ac in animationClips) { // Set the frames for this animation. @@ -91,12 +100,15 @@ namespace TAO.VertexAnimation bakedData.mesh = bd.mesh; bakedData.positionMaps.AddRange(bd.positionMaps); bakedData.maxFrames = maxFrames; - - bounds.min = Vector3.Min(bounds.min, mesh.bounds.min); - bounds.max = Vector3.Max(bounds.max, mesh.bounds.max); + bakedData.minBounds = Vector3.Min(bakedData.minBounds, bd.minBounds); + bakedData.maxBounds = Vector3.Max(bakedData.maxBounds, bd.maxBounds); } - bakedData.mesh.bounds = bounds; + bakedData.mesh.bounds = new Bounds() + { + max = bakedData.maxBounds, + min = bakedData.minBounds + }; return bakedData; } @@ -121,29 +133,34 @@ namespace TAO.VertexAnimation mesh.uv3 = mesh.BakePositionUVs(animationInfo); + BakedAnimation bakedAnimation = BakeAnimation(model, animationClip, animationInfo); + BakedData bakedData = new BakedData() { mesh = mesh, - positionMaps = new List() { BakePositionMap(model, animationClip, animationInfo, out Bounds bounds) }, + positionMaps = new List() { bakedAnimation.positionMap }, maxFrames = animationInfo.maxFrames, + minBounds = bakedAnimation.minBounds, + maxBounds = bakedAnimation.maxBounds + }; + + mesh.bounds = new Bounds() + { + max = bakedAnimation.maxBounds, + min = bakedAnimation.minBounds }; - mesh.bounds = bounds; return bakedData; } - // TODO: Add nicer way to return data/bounds. - public static Texture2D BakePositionMap(this GameObject model, AnimationClip animationClip, AnimationInfo animationInfo, out Bounds bounds) + public static BakedAnimation BakeAnimation(this GameObject model, AnimationClip animationClip, AnimationInfo animationInfo) { // 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); // Keep track of min/max bounds. - bounds = new Bounds - { - min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue), - max = new Vector3(float.MinValue, float.MinValue, float.MinValue) - }; + Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); + Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue); // Create instance to sample from. GameObject inst = GameObject.Instantiate(model); @@ -156,10 +173,6 @@ namespace TAO.VertexAnimation Mesh sampledMesh = new Mesh(); skinnedMeshRenderer.BakeMesh(sampledMesh); - - sampledMesh.RecalculateBounds(); - bounds.min = Vector3.Min(bounds.min, sampledMesh.bounds.min + bounds.center); - bounds.max = Vector3.Min(bounds.max, sampledMesh.bounds.max + bounds.center); List verts = new List(); sampledMesh.GetVertices(verts); @@ -169,6 +182,9 @@ namespace TAO.VertexAnimation int x = 0; for (int v = 0; v < verts.Count; v++) { + min = Vector3.Min(min, verts[v]); + max = Vector3.Max(max, verts[v]); + positionMap.SetPixel(x, y, new Color(verts[v].x, verts[v].y, verts[v].z, VectorUtils.EncodeFloat3ToFloat1(normals[v])) @@ -190,7 +206,12 @@ namespace TAO.VertexAnimation positionMap.filterMode = FilterMode.Point; positionMap.Apply(false, true); - return positionMap; + return new BakedAnimation() + { + positionMap = positionMap, + minBounds = min, + maxBounds = max + }; } public static Vector2[] BakePositionUVs(this Mesh mesh, AnimationInfo animationInfo) diff --git a/Runtime/Scripts/ModelBaker/MeshUtils.cs b/Runtime/Scripts/ModelBaker/MeshUtils.cs index 6fe29dd..ac8eed2 100644 --- a/Runtime/Scripts/ModelBaker/MeshUtils.cs +++ b/Runtime/Scripts/ModelBaker/MeshUtils.cs @@ -16,6 +16,7 @@ namespace TAO.VertexAnimation normals = mesh.normals, tangents = mesh.tangents, colors = mesh.colors, + bounds = mesh.bounds, uv = mesh.uv, uv2 = mesh.uv2, uv3 = mesh.uv3, @@ -26,8 +27,6 @@ namespace TAO.VertexAnimation uv8 = mesh.uv8 }; - copy.RecalculateBounds(); - return copy; }