Skip to content

Commit

Permalink
Bake animations into subset of sim scenegraph more suitable for expor…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
ammaraskar committed May 13, 2024
1 parent be73b02 commit d1cdb48
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
11 changes: 9 additions & 2 deletions Assets/Scripts/OpenTS2/Components/SimCharacterComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace OpenTS2.Components
[RequireComponent(typeof(AssetReferenceComponent))]
public class SimCharacterComponent : MonoBehaviour
{
public static SimCharacterComponent CreateNakedBaseSim()
public static SimCharacterComponent CreateNakedBaseSim(bool makeAnimationRig = true)
{
// Load the skeleton, body, hair and face resources.
const string skeletonResourceName = "auskel_cres";
Expand Down Expand Up @@ -49,7 +49,14 @@ public static SimCharacterComponent CreateNakedBaseSim()
simCharacterObject.GetComponent<AssetReferenceComponent>().AddReference(skeletonAsset, bodyAsset, baldHairAsset, baseFaceAsset);

var simsComponent = simCharacterObject.GetComponent<SimCharacterComponent>();
simsComponent.SetupAnimationRig(scenegraph);
if (makeAnimationRig)
{
simsComponent.SetupAnimationRig(scenegraph);
}
else
{
simsComponent.Scenegraph = scenegraph;
}

return simsComponent;
}
Expand Down
51 changes: 48 additions & 3 deletions Assets/Scripts/OpenTS2/Engine/Tests/SimAnimationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class SimAnimationTest : MonoBehaviour
private Animation _animationObj;
private SimCharacterComponent _sim;

private SimCharacterComponent _bakedSim;
private Animation _bakedAnim;

private void Start()
{
var contentProvider = ContentProvider.Get();
Expand All @@ -46,6 +49,35 @@ private void Start()
}
UpdateAnimationsListAndGetSelection();

// Create another sim that we can bake animations into and make clean for export.
_bakedSim = SimCharacterComponent.CreateNakedBaseSim(makeAnimationRig:false);
_bakedSim.gameObject.name = "bakedSim";
// Remove ikpole, ikctr and handcontrol objects from the baked sim.
foreach (var bone in _bakedSim.Scenegraph.transform.Find("auskel").GetComponentsInChildren<Transform>())
{
Debug.Log($"bone name: {bone.name}");
if (bone.name.Contains("ikctr") || bone.name.Contains("ikpole"))
{
UnityEngine.Object.DestroyImmediate(bone.gameObject);
Debug.Log("Destroyed");
}
}
UnityEngine.Object.DestroyImmediate(_bakedSim.Scenegraph.transform.Find("auskel/root_trans/r_handcontrol0")
.gameObject);
UnityEngine.Object.DestroyImmediate(_bakedSim.Scenegraph.transform.Find("auskel/root_trans/l_handcontrol0")
.gameObject);
UnityEngine.Object.DestroyImmediate(_bakedSim.Scenegraph.transform.Find("auskel/root_trans/r_handcontrol1")
.gameObject);
UnityEngine.Object.DestroyImmediate(_bakedSim.Scenegraph.transform.Find("auskel/root_trans/l_handcontrol1")
.gameObject);
UnityEngine.Object.DestroyImmediate(_bakedSim.Scenegraph.transform.Find("auskel/transform")
.gameObject);
UnityEngine.Object.DestroyImmediate(_bakedSim.Scenegraph.transform.Find("auskel/transform")
.gameObject);
UnityEngine.Object.DestroyImmediate(_bakedSim.Scenegraph.transform.Find("auskel/transform")
.gameObject);
_bakedAnim = _bakedSim.Scenegraph.transform.Find("auskel").gameObject.AddComponent<Animation>();

_sim = SimCharacterComponent.CreateNakedBaseSim();

const string animationName = "a-pose-neutral-stand_anim";
Expand Down Expand Up @@ -115,7 +147,7 @@ private ScenegraphAnimationAsset UpdateAnimationsListAndGetSelection()
IEnumerator BakeAnimationIntoNewAnimation(string animName, ScenegraphAnimationAsset anim)
{
var bakedName = $"{animName}_baked";
if (_animationObj.GetClip(bakedName) != null)
if (_bakedAnim.GetClip(bakedName) != null)
{
yield break;
}
Expand Down Expand Up @@ -153,10 +185,22 @@ IEnumerator BakeAnimationIntoNewAnimation(string animName, ScenegraphAnimationAs
animState.time = t;
}

var boneSet = new HashSet<string>();
foreach (var bone in _bakedSim.Scenegraph.transform.Find("auskel").GetComponentsInChildren<Transform>())
{
boneSet.Add(bone.name);
}

foreach (var entry in boneToKeyframes)
{
if (!boneSet.Contains(entry.Key))
{
continue;
}
var relativeBonePath = _sim.Scenegraph.BoneNamesToRelativePaths[entry.Key];
relativeBonePath = relativeBonePath.Replace("auskel/", "");
var posAndRot = entry.Value;
Debug.Log(relativeBonePath);

bakedClip.SetCurve(relativeBonePath, typeof(Transform), "localPosition.x", new AnimationCurve(posAndRot.PosX.ToArray()));
bakedClip.SetCurve(relativeBonePath, typeof(Transform), "localPosition.y", new AnimationCurve(posAndRot.PosY.ToArray()));
Expand All @@ -178,12 +222,13 @@ IEnumerator BakeAnimationIntoNewAnimation(string animName, ScenegraphAnimationAs

foreach (var blendRelativePath in relativePathsToBlend)
{
ScenegraphAnimationAsset.CreateBlendCurveForChannel(bakedClip, channel, blendRelativePath);
var blendPath = blendRelativePath.Replace("auskel/", "");
ScenegraphAnimationAsset.CreateBlendCurveForChannel(bakedClip, channel, blendPath);
}
}
}

_animationObj.AddClip(bakedClip, bakedName);
_bakedAnim.AddClip(bakedClip, bakedName);
baking = false;
}

Expand Down
1 change: 1 addition & 0 deletions Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"dependencies": {
"com.unity.animation.rigging": "1.0.3",
"com.unity.collab-proxy": "1.15.13",
"com.unity.formats.fbx": "5.1.1",
"com.unity.ide.rider": "2.0.7",
"com.unity.ide.visualstudio": "2.0.14",
"com.unity.ide.vscode": "1.2.5",
Expand Down
21 changes: 19 additions & 2 deletions Packages/packages-lock.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"dependencies": {
"com.autodesk.fbx": {
"version": "5.1.1",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.animation.rigging": {
"version": "1.0.3",
"depth": 0,
Expand Down Expand Up @@ -35,6 +42,16 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.formats.fbx": {
"version": "5.1.1",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.timeline": "1.7.1",
"com.autodesk.fbx": "5.1.1"
},
"url": "https://packages.unity.com"
},
"com.unity.ide.rider": {
"version": "2.0.7",
"depth": 0,
Expand Down Expand Up @@ -97,8 +114,8 @@
"url": "https://packages.unity.com"
},
"com.unity.timeline": {
"version": "1.4.8",
"depth": 0,
"version": "1.7.1",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.modules.director": "1.0.0",
Expand Down

0 comments on commit d1cdb48

Please sign in to comment.