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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-10-05 10:43:46 +08:00
|
|
|
|
using System;
|
2022-09-12 05:53:25 +08:00
|
|
|
|
using System.Collections.Generic;
|
2022-08-28 04:38:52 +08:00
|
|
|
|
using UnityEngine;
|
2022-10-03 09:29:52 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
#endif
|
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
|
|
|
|
{
|
2022-08-30 06:13:26 +08:00
|
|
|
|
[ExecuteInEditMode]
|
2022-10-20 10:42:33 +08:00
|
|
|
|
[DisallowMultipleComponent]
|
2022-11-10 09:49:00 +08:00
|
|
|
|
[AddComponentMenu("Modular Avatar/MA Merge Armature")]
|
2022-08-28 04:38:52 +08:00
|
|
|
|
public class ModularAvatarMergeArmature : AvatarTagComponent
|
|
|
|
|
{
|
2022-11-08 10:49:44 +08:00
|
|
|
|
private const float POS_EPSILON = 0.001f * 0.001f;
|
|
|
|
|
private const float ROT_EPSILON = 0.001f * 0.001f;
|
2022-09-12 05:53:25 +08:00
|
|
|
|
|
2023-07-29 17:04:59 +08:00
|
|
|
|
public AvatarObjectReference mergeTarget = new AvatarObjectReference();
|
2022-10-03 09:16:58 +08:00
|
|
|
|
public GameObject mergeTargetObject => mergeTarget.Get(this);
|
|
|
|
|
|
2023-07-29 17:04:59 +08:00
|
|
|
|
public string prefix = "";
|
|
|
|
|
public string suffix = "";
|
|
|
|
|
public bool locked = false;
|
|
|
|
|
public bool mangleNames = true;
|
2022-08-30 06:13:26 +08:00
|
|
|
|
|
2022-09-12 05:53:25 +08:00
|
|
|
|
private class BoneBinding
|
|
|
|
|
{
|
|
|
|
|
public Transform baseBone;
|
|
|
|
|
public Transform mergeBone;
|
2022-10-03 09:16:58 +08:00
|
|
|
|
|
2022-09-12 05:53:25 +08:00
|
|
|
|
public Vector3 lastLocalPos;
|
|
|
|
|
public Vector3 lastLocalScale;
|
|
|
|
|
public Quaternion lastLocalRot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<BoneBinding> lockedBones;
|
2022-10-03 09:16:58 +08:00
|
|
|
|
|
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();
|
|
|
|
|
|
2022-09-11 01:47:41 +08:00
|
|
|
|
RuntimeUtil.delayCall(() =>
|
2022-08-28 04:38:52 +08:00
|
|
|
|
{
|
2022-08-30 05:00:01 +08:00
|
|
|
|
if (this == null) return;
|
2022-08-30 06:13:26 +08:00
|
|
|
|
|
|
|
|
|
CheckLock();
|
2022-09-11 01:47:41 +08:00
|
|
|
|
});
|
2022-08-28 04:38:52 +08:00
|
|
|
|
}
|
2022-08-30 06:13:26 +08:00
|
|
|
|
|
2022-09-12 05:53:25 +08:00
|
|
|
|
private void OnEnable()
|
2022-08-30 06:13:26 +08:00
|
|
|
|
{
|
2022-09-12 05:53:25 +08:00
|
|
|
|
RuntimeUtil.delayCall(CheckLock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
RuntimeUtil.delayCall(CheckLock);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-30 00:15:16 +08:00
|
|
|
|
protected override void OnDestroy()
|
2022-09-12 05:53:25 +08:00
|
|
|
|
{
|
2023-07-30 00:15:16 +08:00
|
|
|
|
base.OnDestroy();
|
2022-09-12 05:53:25 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
EditorApplication.update -= EditorUpdate;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 13:44:07 +08:00
|
|
|
|
public override void ResolveReferences()
|
2023-08-05 14:47:03 +08:00
|
|
|
|
{
|
|
|
|
|
mergeTarget?.Get(this);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 05:53:25 +08:00
|
|
|
|
void EditorUpdate()
|
|
|
|
|
{
|
2022-11-08 10:49:44 +08:00
|
|
|
|
if (this == null)
|
2022-09-12 05:53:25 +08:00
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
EditorApplication.update -= EditorUpdate;
|
|
|
|
|
#endif
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-08-30 06:13:26 +08:00
|
|
|
|
|
2022-11-08 10:49:44 +08:00
|
|
|
|
if (!locked || lockedBones == null)
|
|
|
|
|
{
|
|
|
|
|
CheckLock();
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 05:53:25 +08:00
|
|
|
|
if (lockedBones != null)
|
2022-08-30 06:13:26 +08:00
|
|
|
|
{
|
2022-09-12 05:53:25 +08:00
|
|
|
|
foreach (var bone in lockedBones)
|
2022-08-30 06:13:26 +08:00
|
|
|
|
{
|
2022-09-12 05:53:25 +08:00
|
|
|
|
if (bone.baseBone == null || bone.mergeBone == null)
|
|
|
|
|
{
|
|
|
|
|
lockedBones = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-10-03 09:16:58 +08:00
|
|
|
|
|
2022-09-12 05:53:25 +08:00
|
|
|
|
var mergeBone = bone.mergeBone;
|
|
|
|
|
var correspondingObject = bone.baseBone;
|
2022-10-03 09:16:58 +08:00
|
|
|
|
bool lockBasePosition = bone.baseBone == mergeTargetObject.transform;
|
|
|
|
|
|
2022-09-12 05:53:25 +08:00
|
|
|
|
if ((mergeBone.localPosition - bone.lastLocalPos).sqrMagnitude > POS_EPSILON
|
|
|
|
|
|| (mergeBone.localScale - bone.lastLocalScale).sqrMagnitude > POS_EPSILON
|
|
|
|
|
|| Quaternion.Angle(bone.lastLocalRot, mergeBone.localRotation) > ROT_EPSILON)
|
2022-08-30 06:13:26 +08:00
|
|
|
|
{
|
2022-09-12 05:53:25 +08:00
|
|
|
|
if (lockBasePosition) mergeBone.position = correspondingObject.position;
|
|
|
|
|
else correspondingObject.localPosition = mergeBone.localPosition;
|
|
|
|
|
|
|
|
|
|
correspondingObject.localScale = mergeBone.localScale;
|
|
|
|
|
correspondingObject.localRotation = mergeBone.localRotation;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (lockBasePosition) mergeBone.position = correspondingObject.position;
|
|
|
|
|
else mergeBone.localPosition = correspondingObject.localPosition;
|
|
|
|
|
mergeBone.localScale = correspondingObject.localScale;
|
|
|
|
|
mergeBone.localRotation = correspondingObject.localRotation;
|
2022-08-30 06:13:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 05:53:25 +08:00
|
|
|
|
bone.lastLocalPos = mergeBone.localPosition;
|
|
|
|
|
bone.lastLocalScale = mergeBone.localScale;
|
|
|
|
|
bone.lastLocalRot = mergeBone.localRotation;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckLock()
|
|
|
|
|
{
|
|
|
|
|
if (RuntimeUtil.isPlaying) return;
|
2022-10-03 09:16:58 +08:00
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
2022-09-12 05:53:25 +08:00
|
|
|
|
EditorApplication.update -= EditorUpdate;
|
2022-10-03 09:16:58 +08:00
|
|
|
|
#endif
|
2022-09-12 05:53:25 +08:00
|
|
|
|
|
|
|
|
|
bool shouldLock = locked && isActiveAndEnabled;
|
|
|
|
|
bool wasLocked = lockedBones != null;
|
|
|
|
|
if (shouldLock != wasLocked)
|
|
|
|
|
{
|
|
|
|
|
if (!shouldLock)
|
|
|
|
|
{
|
|
|
|
|
lockedBones = null;
|
2022-08-30 06:13:26 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-10-03 09:16:58 +08:00
|
|
|
|
if (mergeTargetObject == null) return;
|
2022-09-12 05:53:25 +08:00
|
|
|
|
lockedBones = new List<BoneBinding>();
|
2022-10-03 09:16:58 +08:00
|
|
|
|
|
2022-08-30 06:13:26 +08:00
|
|
|
|
foreach (var xform in GetComponentsInChildren<Transform>(true))
|
|
|
|
|
{
|
|
|
|
|
Transform baseObject = FindCorresponding(xform);
|
2022-10-03 09:16:58 +08:00
|
|
|
|
|
2022-11-08 10:49:44 +08:00
|
|
|
|
if (baseObject == null) continue;
|
|
|
|
|
|
2022-09-12 05:53:25 +08:00
|
|
|
|
lockedBones.Add(new BoneBinding()
|
2022-09-11 01:47:41 +08:00
|
|
|
|
{
|
2022-09-12 05:53:25 +08:00
|
|
|
|
baseBone = baseObject,
|
|
|
|
|
mergeBone = xform,
|
|
|
|
|
lastLocalPos = xform.localPosition,
|
|
|
|
|
lastLocalScale = xform.localScale,
|
|
|
|
|
lastLocalRot = xform.localRotation
|
|
|
|
|
});
|
2022-08-30 06:13:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-03 09:16:58 +08:00
|
|
|
|
|
2022-09-12 05:53:25 +08:00
|
|
|
|
#if UNITY_EDITOR
|
2022-11-08 10:49:44 +08:00
|
|
|
|
if (shouldLock)
|
2022-09-12 05:53:25 +08:00
|
|
|
|
{
|
|
|
|
|
EditorApplication.update += EditorUpdate;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2022-08-30 06:13:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Transform FindCorresponding(Transform xform)
|
|
|
|
|
{
|
|
|
|
|
if (xform == null) return null;
|
2022-10-03 09:16:58 +08:00
|
|
|
|
if (xform == transform) return mergeTargetObject.transform;
|
2022-08-30 06:13:26 +08:00
|
|
|
|
|
|
|
|
|
var correspondingParent = FindCorresponding(xform.parent);
|
|
|
|
|
if (correspondingParent == null) return null;
|
2022-10-03 09:16:58 +08:00
|
|
|
|
|
2022-08-30 06:13:26 +08:00
|
|
|
|
return correspondingParent.Find(prefix + xform.name + suffix);
|
|
|
|
|
}
|
2022-10-05 10:43:46 +08:00
|
|
|
|
|
|
|
|
|
public void InferPrefixSuffix()
|
|
|
|
|
{
|
|
|
|
|
// We only infer if targeting the armature (below the Hips bone)
|
|
|
|
|
var rootAnimator = RuntimeUtil.FindAvatarInParents(transform)?.GetComponent<Animator>();
|
|
|
|
|
if (rootAnimator == null) 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 (!string.IsNullOrEmpty(prefix) || !string.IsNullOrEmpty(suffix))
|
|
|
|
|
{
|
|
|
|
|
RuntimeUtil.MarkDirty(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-28 04:38:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|