Skip to content

Commit

Permalink
Merge pull request #332 from anatawa12/avatar-descriptor-default-anim…
Browse files Browse the repository at this point in the history
…ation

chore(trace-and-optimize): improve parsing animations
  • Loading branch information
anatawa12 authored Aug 19, 2023
2 parents fefcc61 + cafc243 commit 813c59d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ The format is based on [Keep a Changelog].
### Fixed
- Unity Editor may freezes when there are circular dependency `#329`
- Network ID is not assigned for newly created PBs `#331`
- Internally assigned animator controller is not skipped for default choosen playable layer in Trace and Optimize `#332`
- VRCSDK assigned default animators are not considered in Trace and Optimize `#332`
- This bug doesn't create bad behavior for now but will does in the feature.
- Humanoid Animation are not considered in Trace and Optimize `#332`
- This bug doesn't create bad behavior for now but will does in the feature.

### Security

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ The format is based on [Keep a Changelog].
### Fixed
- Unity Editor may freezes when there are circular dependency `#329`
- Network ID is not assigned for newly created PBs `#331`
- Internally assigned animator controller is not skipped for default choosen playable layer in Trace and Optimize `#332`
- VRCSDK assigned default animators are not considered in Trace and Optimize `#332`
- This bug doesn't create bad behavior for now but will does in the feature.
- Humanoid Animation are not considered in Trace and Optimize `#332`
- This bug doesn't create bad behavior for now but will does in the feature.

### Security

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
using VRC.SDKBase;
using Object = UnityEngine.Object;

namespace Anatawa12.AvatarOptimizer.Processors
{
partial class TraceAndOptimizeProcessor
{
private static CachedGuidLoader<AnimatorController>[] _defaultLayers = CreateDefaultLayers();

private static CachedGuidLoader<AnimatorController>[] CreateDefaultLayers()
{
var array = new CachedGuidLoader<AnimatorController>[(int)(VRCAvatarDescriptor.AnimLayerType.IKPose + 1)];
// vrc_AvatarV3LocomotionLayer
array[(int) VRCAvatarDescriptor.AnimLayerType.Base] = "4e4e1a372a526074884b7311d6fc686b";
// vrc_AvatarV3IdleLayer
array[(int) VRCAvatarDescriptor.AnimLayerType.Additive] = "573a1373059632b4d820876efe2d277f";
// vrc_AvatarV3HandsLayer
array[(int) VRCAvatarDescriptor.AnimLayerType.Gesture] = "404d228aeae421f4590305bc4cdaba16";
// vrc_AvatarV3ActionLayer
array[(int) VRCAvatarDescriptor.AnimLayerType.Action] = "3e479eeb9db24704a828bffb15406520";
// vrc_AvatarV3FaceLayer
array[(int) VRCAvatarDescriptor.AnimLayerType.FX] = "d40be620cf6c698439a2f0a5144919fe";
// vrc_AvatarV3SittingLayer
array[(int)VRCAvatarDescriptor.AnimLayerType.Sitting] = "1268460c14f873240981bf15aa88b21a";
// vrc_AvatarV3UtilityTPose
array[(int)VRCAvatarDescriptor.AnimLayerType.TPose] = "00121b5812372b74f9012473856d8acf";
// vrc_AvatarV3UtilityIKPose
array[(int)VRCAvatarDescriptor.AnimLayerType.IKPose] = "a9b90a833b3486e4b82834c9d1f7c4ee";
return array;
}

private void GatherAnimationModifications()
{
foreach (var animator in _session.GetComponents<Animator>())
{
GatherAnimationModificationsInController(animator.gameObject, animator.runtimeAnimatorController);
GatherHumanoidModifications(animator);
}

var descriptor = _session.GetRootComponent<VRCAvatarDescriptor>();

if (descriptor)
{
RuntimeAnimatorController GetPlayableLayerController(VRCAvatarDescriptor.CustomAnimLayer layer)
{
if (!layer.isDefault && layer.animatorController)
{
return layer.animatorController;
}

var typeIndex = (int)layer.type;
if (typeIndex < 0) return null;
if (typeIndex >= _defaultLayers.Length) return null;
ref var loader = ref _defaultLayers[typeIndex];
if (!loader.IsValid) return null;
var controller = loader.Value;
if (controller == null)
throw new InvalidOperationException($"default controller for {layer.type} not found");
return controller;
}

foreach (var layer in descriptor.specialAnimationLayers)
{
GatherAnimationModificationsInController(descriptor.gameObject, layer.animatorController);
GatherAnimationModificationsInController(descriptor.gameObject, GetPlayableLayerController(layer));
}

if (descriptor.customizeAnimationLayers)
{
foreach (var layer in descriptor.baseAnimationLayers)
{
GatherAnimationModificationsInController(descriptor.gameObject, layer.animatorController);
GatherAnimationModificationsInController(descriptor.gameObject, GetPlayableLayerController(layer));
}
}

Expand Down Expand Up @@ -219,6 +265,30 @@ private void GatherAnimationModifications()
}
}

/// Mark rotations of humanoid bones as changeable variables
private void GatherHumanoidModifications(Animator animator)
{
// if it's not humanoid, this pass doesn't matter
if (!animator.isHuman) return;
for (var bone = HumanBodyBones.Hips; bone < HumanBodyBones.LastBone; bone++)
{
var transform = animator.GetBoneTransform(bone);
if (!transform) continue;

if (!_modifiedProperties.TryGetValue(transform, out var properties))
_modifiedProperties.Add(transform, properties = new Dictionary<string, AnimationProperty>());

foreach (var key in new[]
{ "m_LocalRotation.x", "m_LocalRotation.y", "m_LocalRotation.z", "m_LocalRotation.w" })
{
if (properties.TryGetValue(key, out var property))
properties[key] = property.Merge(AnimationProperty.Variable().AlwaysApplied());
else
properties.Add(key, AnimationProperty.Variable().AlwaysApplied());
}
}
}

private readonly Dictionary<(GameObject, AnimationClip), ParsedAnimation> _parsedAnimationCache =
new Dictionary<(GameObject, AnimationClip), ParsedAnimation>();

Expand Down
2 changes: 2 additions & 0 deletions Editor/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ public CachedGuidLoader(string guid)
AssetDatabase.LoadAssetAtPath<T>(
AssetDatabase.GUIDToAssetPath(_guid));

public bool IsValid => _guid != null;

public static implicit operator CachedGuidLoader<T>(string guid) =>
new CachedGuidLoader<T>(guid);
}
Expand Down

0 comments on commit 813c59d

Please sign in to comment.