Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UniVRM components support #569

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Editor/Animation/AnimationDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ internal void OnActivate(ndmf.BuildContext context)
#if MA_VRCSDK3_AVATARS
var avatarDescriptor = context.AvatarDescriptor;

if (!avatarDescriptor) return;

foreach (var layer in avatarDescriptor.baseAnimationLayers)
{
BootstrapLayer(layer);
Expand Down
7 changes: 5 additions & 2 deletions Editor/Animation/AnimationUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ internal static void CloneAllControllers(BuildContext context)
// This helps reduce the risk that we'll accidentally modify the original assets.

#if MA_VRCSDK3_AVATARS
context.AvatarDescriptor.baseAnimationLayers =
var avatarDescriptor = context.AvatarDescriptor;
if (!avatarDescriptor) return;

avatarDescriptor.baseAnimationLayers =
CloneLayers(context, context.AvatarDescriptor.baseAnimationLayers);
context.AvatarDescriptor.specialAnimationLayers =
avatarDescriptor.specialAnimationLayers =
CloneLayers(context, context.AvatarDescriptor.specialAnimationLayers);
#endif
}
Expand Down
2 changes: 2 additions & 0 deletions Editor/FixupPasses/FixupExpressionsMenuPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal class FixupExpressionsMenuPass

internal static void FixupExpressionsMenu(BuildContext context)
{
if (!context.AvatarDescriptor) return;

context.AvatarDescriptor.customExpressions = true;

var expressionsMenu = context.AvatarDescriptor.expressionsMenu;
Expand Down
1 change: 1 addition & 0 deletions Editor/MergeAnimatorProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ internal void OnPreprocessAvatar(GameObject avatarGameObject, BuildContext conte
mergeSessions.Clear();

var descriptor = avatarGameObject.GetComponent<VRCAvatarDescriptor>();
if (!descriptor) return;

if (descriptor.baseAnimationLayers != null) InitSessions(descriptor.baseAnimationLayers);
if (descriptor.specialAnimationLayers != null) InitSessions(descriptor.specialAnimationLayers);
Expand Down
40 changes: 40 additions & 0 deletions Editor/MergeArmatureHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.Animations;

#if MA_VRM0
using VRM;
#endif

#if MA_VRM1
using UniVRM10;
#endif

using Object = UnityEngine.Object;

#endregion
Expand Down Expand Up @@ -109,6 +118,35 @@ internal void OnPreprocessAvatar(ndmf.BuildContext context, GameObject avatarGam
}
#endif

#if MA_VRM0
foreach (var c in avatarGameObject.transform.GetComponentsInChildren<VRMSpringBone>(true))
{
RetainBoneReferences(c);
}

foreach (var c in avatarGameObject.transform.GetComponentsInChildren<VRMSpringBoneColliderGroup>(true))
{
RetainBoneReferences(c);
}
#endif

#if MA_VRM1
foreach (var c in avatarGameObject.transform.GetComponentsInChildren<VRM10SpringBoneJoint>(true))
{
RetainBoneReferences(c);
}

foreach (var c in avatarGameObject.transform.GetComponentsInChildren<VRM10SpringBoneCollider>(true))
{
RetainBoneReferences(c);
}

foreach (var c in avatarGameObject.transform.GetComponentsInChildren<VRM10SpringBoneColliderGroup>(true))
{
RetainBoneReferences(c);
}
#endif

foreach (var c in avatarGameObject.transform.GetComponentsInChildren<IConstraint>(true))
{
RetainBoneReferences(c as Component);
Expand Down Expand Up @@ -477,5 +515,7 @@ private void PruneDuplicatePhysBones()
}
}
#endif

// TODO - deduplicate VRM0/1 SpringBone components... doesn't break avatars either
}
}
27 changes: 27 additions & 0 deletions Editor/OptimizationPasses/GCGameObjectsPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
using VRC.SDK3.Dynamics.PhysBone.Components;
#endif

#if MA_VRM0
using VRM;
#endif

namespace nadena.dev.modular_avatar.core.editor
{
/// <summary>
Expand Down Expand Up @@ -66,6 +70,13 @@ private void MarkAll()
break;
#endif

#if MA_VRM0
case VRMSpringBone sb:
MarkObject(obj);
MarkSpringBone(sb);
break;
#endif

case AvatarTagComponent _:
// Tag components will not be retained at runtime, so pretend they're not there.
break;
Expand Down Expand Up @@ -147,6 +158,22 @@ private void MarkPhysBone(VRCPhysBone pb)
}
#endif

#if MA_VRM0
private void MarkSpringBone(VRMSpringBone sb)
{
foreach (var rootBone in sb.RootBones)
{
foreach (var obj in GameObjects(rootBone.gameObject))
{
MarkObject(obj);
}
}

// Mark etc
MarkAllReferencedObjects(sb);
}
#endif

private void MarkAllReferencedObjects(Component component)
{
var so = new SerializedObject(component);
Expand Down
2 changes: 2 additions & 0 deletions Editor/OptimizationPasses/PruneParametersPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ internal class PruneParametersPass : Pass<PruneParametersPass>
{
protected override void Execute(ndmf.BuildContext context)
{
if (!context.AvatarDescriptor) return;

var expParams = context.AvatarDescriptor.expressionParameters;
if (expParams != null && context.IsTemporaryAsset(expParams))
{
Expand Down
2 changes: 2 additions & 0 deletions Editor/RenameParametersHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ private void SetExpressionParameters(GameObject avatarRoot, ImmutableDictionary<
.ToImmutableDictionary();

var avatar = avatarRoot.GetComponent<VRCAvatarDescriptor>();
if (!avatar) return;

var expParams = avatar.expressionParameters;

if (expParams == null)
Expand Down
14 changes: 13 additions & 1 deletion Editor/nadena.dev.modular-avatar.core.editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"VRC.SDK3A",
"VRC.SDKBase",
"nadena.dev.ndmf",
"nadena.dev.ndmf.vrchat"
"nadena.dev.ndmf.vrchat",
"VRM",
"VRM10"
],
"includePlatforms": [
"Editor"
Expand Down Expand Up @@ -41,6 +43,16 @@
"name": "com.vrchat.avatars",
"expression": "",
"define": "MA_VRCSDK3_AVATARS"
},
{
"name": "com.vrmc.univrm",
"expression": "",
"define": "MA_VRM0"
},
{
"name": "com.vrmc.vrm",
"expression": "",
"define": "MA_VRM1"
}
],
"noEngineReferences": false
Expand Down
10 changes: 10 additions & 0 deletions Runtime/nadena.dev.modular-avatar.core.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
"name": "com.vrchat.avatars",
"expression": "",
"define": "MA_VRCSDK3_AVATARS"
},
{
"name": "com.vrmc.univrm",
"expression": "",
"define": "MA_VRM0"
},
{
"name": "com.vrmc.vrm",
"expression": "",
"define": "MA_VRM1"
}
],
"noEngineReferences": false
Expand Down
10 changes: 10 additions & 0 deletions UnitTests~/Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
"name": "com.vrchat.avatars",
"expression": "",
"define": "MA_VRCSDK3_AVATARS"
},
{
"name": "com.vrmc.univrm",
"expression": "",
"define": "MA_VRM0"
},
{
"name": "com.vrmc.vrm",
"expression": "",
"define": "MA_VRM1"
}
],
"noEngineReferences": false
Expand Down