Skip to content

Commit

Permalink
Merge pull request #934 from anatawa12/split-animator-optimizer-assembly
Browse files Browse the repository at this point in the history
Split animator optimizer assembly and make it requires 2021
  • Loading branch information
anatawa12 authored Mar 21, 2024
2 parents 80ae90f + 329d677 commit 752a6cd
Show file tree
Hide file tree
Showing 127 changed files with 775 additions and 277 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog].
## [Unreleased]
### Added
- Animator Optimizer `#854`
- Most features of Animator Optimizer is not available in Unity 2019.
- Animator Optimizer optimizes your Animator Controller without behaviour Changes
- Current Optimizer includes the following optimization
- Remove meaningless properties `#854`
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog].
### Added
- Support for VRCSDK 3.5.2-beta.1 `#926`
- Animator Optimizer `#854`
- Most features of Animator Optimizer is not available in Unity 2019.
- Animator Optimizer optimizes your Animator Controller without behaviour Changes
- Current Optimizer includes the following optimization
- Remove meaningless properties `#854`
Expand Down
3 changes: 0 additions & 3 deletions Editor/AnimatorController.meta

This file was deleted.

2 changes: 1 addition & 1 deletion Editor/AnimatorParserV2/AnimatorParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private void CollectAvatarDescriptorModifications([NotNull] RootPropModNodeConta
new AnimatorWeightChangesList(controllers[layer]?.ComputeLayerCount() ?? 0);
var playableWeightChanged = new AnimatorLayerMap<AnimatorWeightChange>();
foreach (var layer in descriptor.baseAnimationLayers)
ACUtils.CollectWeightChangesInController(controllers[layer.type],
VRCSDKUtils.CollectWeightChangesInController(controllers[layer.type],
playableWeightChanged, animatorLayerWeightChanged);

if (_mmdWorldCompatibility)
Expand Down
13 changes: 2 additions & 11 deletions Editor/ContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@ public static T[] GetComponents<T>([NotNull] this BuildContext context) where T
return context.AvatarRootObject.GetComponentsInChildren<T>(true);
}

private static MeshInfo2Holder GetHolder([NotNull] this BuildContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
return context.Extension<MeshInfo2Context>().Holder;
}

public static MeshInfo2 GetMeshInfoFor([NotNull] this BuildContext context, SkinnedMeshRenderer renderer) =>
context.GetHolder().GetMeshInfoFor(renderer);

public static ObjectMappingBuilder GetMappingBuilder([NotNull] this BuildContext context)
public static ObjectMappingBuilder<PropertyInfo> GetMappingBuilder([NotNull] this BuildContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
return context.Extension<ObjectMappingContext>().MappingBuilder;
Expand All @@ -46,7 +37,7 @@ public static void RecordMoveProperty([NotNull] this BuildContext context, Compo
public static void RecordRemoveProperty([NotNull] this BuildContext context, Component from, string oldProp) =>
GetMappingBuilder(context).RecordRemoveProperty(from, oldProp);

public static AnimationComponentInfo GetAnimationComponent([NotNull] this BuildContext context,
public static AnimationComponentInfo<PropertyInfo> GetAnimationComponent([NotNull] this BuildContext context,
ComponentOrGameObject component) =>
GetMappingBuilder(context).GetAnimationComponent(component);

Expand Down
5 changes: 5 additions & 0 deletions Editor/Inspector/TraceAndOptimizeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ protected override void OnInspectorGUIInner()
EditorGUILayout.PropertyField(_optimizePhysBone);
EditorGUILayout.PropertyField(_optimizeAnimator);

#if !UNITY_2021_3_OR_NEWER
if (_optimizeAnimator.boolValue)
EditorGUILayout.HelpBox(AAOL10N.Tr("TraceAndOptimize:OptimizeAnimator:Unity2019"), MessageType.Info);
#endif

_advancedSettingsLabel.text = AAOL10N.Tr("TraceAndOptimize:prop:advancedSettings");
if (EditorGUILayout.PropertyField(_advancedSettings, _advancedSettingsLabel, false))
{
Expand Down
3 changes: 0 additions & 3 deletions Editor/Math.meta

This file was deleted.

2 changes: 0 additions & 2 deletions Editor/ObjectMapping/AnimationObjectMapper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.Processors.TraceAndOptimizes;
using JetBrains.Annotations;
using UnityEditor;
using UnityEngine;
Expand Down
145 changes: 30 additions & 115 deletions Editor/ObjectMapping/ObjectMappingBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.AnimatorParsersV2;
using JetBrains.Annotations;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Anatawa12.AvatarOptimizer
{
/// <summary>
/// The class manages Object location mapping
/// </summary>
internal class ObjectMappingBuilder
internal class ObjectMappingBuilder<TPropInfo>
where TPropInfo : struct, IPropertyInfo<TPropInfo>
{
// Responsibility of this class can be classified to the following parts
// - Track moving GameObjects
Expand Down Expand Up @@ -78,10 +77,10 @@ public void RecordCopyProperty(ComponentOrGameObject fromComponent, string oldPr
public void RecordRemoveProperty(ComponentOrGameObject from, string oldProp) =>
GetComponentInfo(from).RemoveProperty(oldProp);

public AnimationComponentInfo GetAnimationComponent(ComponentOrGameObject component)
public AnimationComponentInfo<TPropInfo> GetAnimationComponent(ComponentOrGameObject component)
=> GetComponentInfo(component);

public IEnumerable<AnimationComponentInfo> GetAllAnimationComponents() =>
public IEnumerable<AnimationComponentInfo<TPropInfo>> GetAllAnimationComponents() =>
_componentInfos.Values.Where(x => !x.IsMerged);

private BuildingComponentInfo GetComponentInfo(ComponentOrGameObject component)
Expand All @@ -95,17 +94,6 @@ private BuildingComponentInfo GetComponentInfo(ComponentOrGameObject component)
return info;
}

public void ImportModifications(
RootPropModNodeContainer modifications
)
{
foreach (var ((target, prop), value) in modifications.FloatNodes)
GetComponentInfo(target).ImportProperty(prop, value);

foreach (var ((target, prop), value) in modifications.ObjectNodes)
GetComponentInfo(target).ImportProperty(prop, value);
}

public ObjectMapping BuildObjectMapping()
{
return new ObjectMapping(
Expand All @@ -119,12 +107,10 @@ class AnimationPropertyInfo
[CanBeNull] public readonly string Name;
[CanBeNull] public AnimationPropertyInfo MergedTo { get; private set; }
private MappedPropertyInfo? _mappedPropertyInfo;
[CanBeNull] private RootPropModNode<float> _floatNode;
[CanBeNull] private RootPropModNode<Object> _objectNode;

public TPropInfo PropertyInfo;

[CanBeNull] public List<AnimationPropertyInfo> CopiedTo { get; private set; }
[CanBeNull]
public RootPropModNode<float> FloatNode => _floatNode?.Normalize();
public RootPropModNode<Object> ObjectNode => _objectNode?.Normalize();

public AnimationPropertyInfo([NotNull] BuildingComponentInfo component, [NotNull] string name)
{
Expand All @@ -141,15 +127,15 @@ private AnimationPropertyInfo()
public void MergeTo(AnimationPropertyInfo property)
{
MergedTo = property;
MergeNode(ref property._floatNode, ref _floatNode);
PropertyInfo.MergeTo(ref property.PropertyInfo);
}

public void CopyTo(AnimationPropertyInfo property)
{
if (CopiedTo == null)
CopiedTo = new List<AnimationPropertyInfo>();
CopiedTo.Add(property);
MergeNode(ref property._floatNode, ref _floatNode);
PropertyInfo.CopyTo(ref property.PropertyInfo);
}

public MappedPropertyInfo GetMappedInfo()
Expand Down Expand Up @@ -195,48 +181,9 @@ private MappedPropertyInfo ComputeMappedInfo()
return new MappedPropertyInfo(descriptor, copied.ToArray());
}
}

private static void MergeNode<T>([CanBeNull] ref RootPropModNode<T> mergeTo,
[CanBeNull] ref RootPropModNode<T> merge)
{
if (merge == null || merge.IsEmpty) return;
if (mergeTo == null || merge.IsEmpty)
{
mergeTo = merge;
return;
}

mergeTo.Add(merge);
merge.Invalidate();
merge = null;
}

public void ImportProperty(RootPropModNode<float> node)
{
if (FloatNode != null) throw new InvalidOperationException();
_floatNode = node;
}

public void ImportProperty(RootPropModNode<Object> node)
{
if (ObjectNode != null) throw new InvalidOperationException();
_objectNode = node;
}

public void AddModification(ComponentPropModNodeBase<float> node, bool alwaysApplied)
{
if (_floatNode == null) _floatNode = new RootPropModNode<float>();
_floatNode.Add(node, alwaysApplied);
}

public void AddModification(ComponentPropModNodeBase<Object> node, bool alwaysApplied)
{
if (_objectNode == null) _objectNode = new RootPropModNode<Object>();
_objectNode.Add(node, alwaysApplied);
}
}

class BuildingComponentInfo : AnimationComponentInfo
class BuildingComponentInfo : AnimationComponentInfo<TPropInfo>
{
internal readonly int InstanceId;
internal readonly Type Type;
Expand Down Expand Up @@ -347,68 +294,36 @@ public override ComponentOrGameObject TargetComponent

public override string[] Properties => _afterPropertyIds.Keys.ToArray();

public override bool ContainsFloat(string property) =>
_afterPropertyIds.TryGetValue(property, out var info) && info.FloatNode != null;

public override bool TryGetFloat(string propertyName, out RootPropModNode<float> animation)
{
animation = default;
if (!_afterPropertyIds.TryGetValue(propertyName, out var info))
return false;
animation = info.FloatNode;
return animation != null;
}

public override void AddModification(string prop, ComponentPropModNodeBase<float> node, bool alwaysApplied) =>
GetProperty(prop).AddModification(node, alwaysApplied);

public void ImportProperty(string prop, RootPropModNode<float> node) =>
GetProperty(prop).ImportProperty(node);

public void ImportProperty(string prop, RootPropModNode<Object> node) =>
GetProperty(prop).ImportProperty(node);

public override IEnumerable<(string, RootPropModNode<float>)> AllFloatProperties =>
_afterPropertyIds
.Where(x => x.Value.FloatNode != null)
.Select(x => (x.Key, x.Value.FloatNode));

public override bool ContainsObject(string property) =>
_afterPropertyIds.TryGetValue(property, out var info) && info.ObjectNode != null;

public override bool TryGetObject(string propertyName, out RootPropModNode<Object> animation)
public override ref TPropInfo GetPropertyInfo(string property) => ref GetProperty(property).PropertyInfo;
public override TPropInfo TryGetPropertyInfo(string property)
{
animation = default;
if (!_afterPropertyIds.TryGetValue(propertyName, out var info))
return false;
animation = info.ObjectNode;
return animation != null;
if (_afterPropertyIds.TryGetValue(property, out var info))
return info.PropertyInfo;
return default;
}

public override void AddModification(string prop, ComponentPropModNodeBase<Object> node, bool alwaysApplied) =>
GetProperty(prop).AddModification(node, alwaysApplied);

public override IEnumerable<(string, RootPropModNode<Object>)> AllObjectProperties =>
_afterPropertyIds
.Where(x => x.Value.ObjectNode != null)
.Select(x => (x.Key, x.Value.ObjectNode));
public override IEnumerable<(string name, TPropInfo info)> GetAllPropertyInfo =>
_afterPropertyIds.Select((x) => (x.Key, x.Value.PropertyInfo));
}
}

internal abstract class AnimationComponentInfo
internal interface IPropertyInfo<T>
where T : struct, IPropertyInfo<T>
{
void MergeTo(ref T property);
void CopyTo(ref T property);
}

internal abstract class AnimationComponentInfo<TPropInfo>
where TPropInfo : struct, IPropertyInfo<TPropInfo>
{
public abstract string[] Properties { get; }
public abstract ComponentOrGameObject TargetComponent { get; }

public abstract void RemoveProperty(string property);
public abstract bool ContainsFloat(string property);
public abstract bool TryGetFloat(string propertyName, out RootPropModNode<float> animation);
public abstract void AddModification(string prop, ComponentPropModNodeBase<float> node, bool alwaysApplied);
public abstract IEnumerable<(string, RootPropModNode<float>)> AllFloatProperties { get; }

public abstract bool ContainsObject(string property);
public abstract bool TryGetObject(string propertyName, out RootPropModNode<Object> animation);
public abstract void AddModification(string prop, ComponentPropModNodeBase<Object> node, bool alwaysApplied);
public abstract IEnumerable<(string, RootPropModNode<Object>)> AllObjectProperties { get; }

public abstract ref TPropInfo GetPropertyInfo(string property);
public abstract TPropInfo TryGetPropertyInfo(string property);
public abstract IEnumerable<(string name, TPropInfo info)> GetAllPropertyInfo { get; }
}
}
4 changes: 2 additions & 2 deletions Editor/ObjectMapping/ObjectMappingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace Anatawa12.AvatarOptimizer
{
internal class ObjectMappingContext : IExtensionContext
{
public ObjectMappingBuilder MappingBuilder { get; private set; }
public ObjectMappingBuilder<PropertyInfo> MappingBuilder { get; private set; }

public void OnActivate(BuildContext context)
{
MappingBuilder = new ObjectMappingBuilder(context.AvatarRootObject);
MappingBuilder = new ObjectMappingBuilder<PropertyInfo>(context.AvatarRootObject);
}

public void OnDeactivate(BuildContext context)
Expand Down
Loading

0 comments on commit 752a6cd

Please sign in to comment.