From 2ea76b947ed6049ccff159942cfb4d2c8e3854c8 Mon Sep 17 00:00:00 2001 From: max Date: Wed, 17 Feb 2021 21:20:53 +0100 Subject: [PATCH] Added bounds. --- Editor/Scripts/Editor/LitGUI.cs | 1 - Editor/Scripts/ModelBaker/VA_ModelBaker.cs | 1 + Runtime/Scripts/ModelBaker/AnimationBaker.cs | 26 +++++++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Editor/Scripts/Editor/LitGUI.cs b/Editor/Scripts/Editor/LitGUI.cs index fc879a5..6879ba7 100644 --- a/Editor/Scripts/Editor/LitGUI.cs +++ b/Editor/Scripts/Editor/LitGUI.cs @@ -1,4 +1,3 @@ -using System.Linq; using UnityEditor; using UnityEngine; diff --git a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs index d302e9e..0214e40 100644 --- a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs +++ b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs @@ -113,6 +113,7 @@ namespace TAO.VertexAnimation.Editor foreach (var m in meshes) { + m.bounds = bakedData.mesh.bounds; m.Finalize(); AssetDatabase.AddObjectToAsset(m, this); } diff --git a/Runtime/Scripts/ModelBaker/AnimationBaker.cs b/Runtime/Scripts/ModelBaker/AnimationBaker.cs index 0d84ba0..28427d1 100644 --- a/Runtime/Scripts/ModelBaker/AnimationBaker.cs +++ b/Runtime/Scripts/ModelBaker/AnimationBaker.cs @@ -79,6 +79,9 @@ 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. @@ -88,8 +91,13 @@ 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.mesh.bounds = bounds; + return bakedData; } @@ -116,18 +124,27 @@ namespace TAO.VertexAnimation BakedData bakedData = new BakedData() { mesh = mesh, - positionMaps = new List() { BakePositionMap(model, animationClip, animationInfo) }, - maxFrames = animationInfo.maxFrames + positionMaps = new List() { BakePositionMap(model, animationClip, animationInfo, out Bounds bounds) }, + maxFrames = animationInfo.maxFrames, }; + mesh.bounds = bounds; return bakedData; } - public static Texture2D BakePositionMap(this GameObject model, AnimationClip animationClip, AnimationInfo animationInfo) + // TODO: Add nicer way to return data/bounds. + public static Texture2D BakePositionMap(this GameObject model, AnimationClip animationClip, AnimationInfo animationInfo, out Bounds bounds) { // 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) + }; + // Create instance to sample from. GameObject inst = GameObject.Instantiate(model); SkinnedMeshRenderer skinnedMeshRenderer = inst.GetComponent(); @@ -139,7 +156,10 @@ 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);