modular-avatar/Runtime/ModularAvatarMergeArmature.cs

237 lines
7.9 KiB
C#
Raw Normal View History

2022-09-09 11:40:52 +08:00
/*
* MIT License
2023-08-05 14:47:03 +08:00
*
2022-09-09 11:40:52 +08:00
* Copyright (c) 2022 bd_
2023-08-05 14:47:03 +08:00
*
2022-09-09 11:40:52 +08:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2023-08-05 14:47:03 +08:00
*
2022-09-09 11:40:52 +08:00
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
2023-08-05 14:47:03 +08:00
*
2022-09-09 11:40:52 +08:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using nadena.dev.modular_avatar.core.armature_lock;
2022-08-28 04:38:52 +08:00
using UnityEngine;
using UnityEngine.Analytics;
using UnityEngine.Serialization;
2022-08-28 04:38:52 +08:00
2022-11-11 12:39:58 +08:00
namespace nadena.dev.modular_avatar.core
2022-08-28 04:38:52 +08:00
{
[Serializable]
public enum ArmatureLockMode
{
Legacy,
NotLocked,
BaseToMerge,
BidirectionalExact
}
[ExecuteInEditMode]
2022-10-20 10:42:33 +08:00
[DisallowMultipleComponent]
2022-11-10 09:49:00 +08:00
[AddComponentMenu("Modular Avatar/MA Merge Armature")]
[HelpURL("https://modular-avatar.nadena.dev/docs/reference/merge-armature?lang=auto")]
2022-08-28 04:38:52 +08:00
public class ModularAvatarMergeArmature : AvatarTagComponent
{
public AvatarObjectReference mergeTarget = new AvatarObjectReference();
public GameObject mergeTargetObject => mergeTarget.Get(this);
public string prefix = "";
public string suffix = "";
[FormerlySerializedAs("locked")] public bool legacyLocked;
public ArmatureLockMode LockMode = ArmatureLockMode.Legacy;
public bool mangleNames = true;
private ArmatureLockController _lockController;
internal Transform MapBone(Transform bone)
{
var relPath = RuntimeUtil.RelativePath(gameObject, bone.gameObject);
if (relPath == null) throw new ArgumentException("Bone is not a child of this component");
if (relPath == "") return mergeTarget.Get(this).transform;
var segments = relPath.Split('/');
var pointer = mergeTarget.Get(this).transform;
foreach (var segment in segments)
{
if (!segment.StartsWith(prefix) || !segment.EndsWith(suffix)) return null;
var targetObjectName = segment.Substring(prefix.Length,
segment.Length - prefix.Length - suffix.Length);
pointer = pointer.Find(targetObjectName);
}
return pointer;
}
internal Transform FindCorrespondingBone(Transform bone, Transform baseParent)
{
var childName = bone.gameObject.name;
if (!childName.StartsWith(prefix) || !childName.EndsWith(suffix)) return null;
var targetObjectName = childName.Substring(prefix.Length,
childName.Length - prefix.Length - suffix.Length);
return baseParent.Find(targetObjectName);
}
2023-01-19 20:32:44 +08:00
protected override void OnValidate()
2022-08-28 04:38:52 +08:00
{
2023-01-19 20:32:44 +08:00
base.OnValidate();
MigrateLockConfig();
RuntimeUtil.delayCall(SetLockMode);
}
2023-01-19 20:32:44 +08:00
internal void ResetArmatureLock()
{
if (_lockController != null)
{
_lockController.Dispose();
_lockController = null;
}
SetLockMode();
}
private void SetLockMode()
{
if (this == null) return;
if (_lockController == null)
2022-08-28 04:38:52 +08:00
{
_lockController = ArmatureLockController.ForMerge(this, GetBonesForLock);
}
if (_lockController.Mode != LockMode)
{
_lockController.Mode = LockMode;
if (!_lockController.IsStable())
{
_lockController.Mode = LockMode = ArmatureLockMode.NotLocked;
}
}
_lockController.Enabled = enabled;
}
private void MigrateLockConfig()
{
if (LockMode == ArmatureLockMode.Legacy)
{
LockMode = legacyLocked ? ArmatureLockMode.BidirectionalExact : ArmatureLockMode.BaseToMerge;
}
2022-08-28 04:38:52 +08:00
}
private void OnEnable()
{
MigrateLockConfig();
SetLockMode();
}
private void OnDisable()
{
// we use enabled instead of activeAndEnabled to ensure we track even when the GameObject is disabled
_lockController.Enabled = enabled;
}
2023-07-30 00:15:16 +08:00
protected override void OnDestroy()
{
2023-07-30 00:15:16 +08:00
base.OnDestroy();
_lockController?.Dispose();
_lockController = null;
}
public override void ResolveReferences()
2023-08-05 14:47:03 +08:00
{
mergeTarget?.Get(this);
}
private List<(Transform, Transform)> GetBonesForLock()
{
if (this == null) return null;
var mergeRoot = this.transform;
var baseRoot = mergeTarget.Get(this);
if (baseRoot == null) return null;
List<(Transform, Transform)> mergeBones = new List<(Transform, Transform)>();
ScanHierarchy(mergeRoot, baseRoot.transform);
return mergeBones;
void ScanHierarchy(Transform merge, Transform baseBone)
{
foreach (Transform t in merge)
{
var subMerge = t.GetComponent<ModularAvatarMergeArmature>();
if (subMerge != null && subMerge != this) continue;
var baseChild = FindCorrespondingBone(t, baseBone);
if (baseChild != null)
{
mergeBones.Add((t, baseChild));
ScanHierarchy(t, baseChild);
}
}
}
}
public void InferPrefixSuffix()
{
// We only infer if targeting the armature (below the Hips bone)
var rootAnimator = RuntimeUtil.FindAvatarTransformInParents(transform)?.GetComponent<Animator>();
if (rootAnimator == null || !rootAnimator.isHuman) return;
var hips = rootAnimator.GetBoneTransform(HumanBodyBones.Hips);
if (hips == null || hips.transform.parent != mergeTargetObject.transform) return;
// We also require that the attached object has exactly one child (presumably the hips)
if (transform.childCount != 1) return;
// Infer the prefix and suffix by comparing the names of the mergeTargetObject's hips with the child of the
// GameObject we're attached to.
var baseName = hips.name;
var mergeName = transform.GetChild(0).name;
var prefixLength = mergeName.IndexOf(baseName, StringComparison.InvariantCulture);
if (prefixLength < 0) return;
var suffixLength = mergeName.Length - prefixLength - baseName.Length;
prefix = mergeName.Substring(0, prefixLength);
suffix = mergeName.Substring(mergeName.Length - suffixLength);
if (prefix == "J_Bip_C_")
{
// VRM workaround
prefix = "J_Bip_";
}
if (!string.IsNullOrEmpty(prefix) || !string.IsNullOrEmpty(suffix))
{
RuntimeUtil.MarkDirty(this);
}
}
2022-08-28 04:38:52 +08:00
}
}