2023-09-24 15:59:15 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-09-27 19:24:42 +08:00
|
|
|
|
using nadena.dev.modular_avatar.core.armature_lock;
|
2023-09-24 15:59:15 +08:00
|
|
|
|
using UnityEngine;
|
2023-11-10 14:37:56 +08:00
|
|
|
|
#if MA_VRCSDK3_AVATARS
|
2023-09-24 15:59:15 +08:00
|
|
|
|
using VRC.SDKBase;
|
2023-11-10 14:37:56 +08:00
|
|
|
|
#endif
|
2023-09-24 15:59:15 +08:00
|
|
|
|
|
|
|
|
|
namespace nadena.dev.modular_avatar.core.ArmatureAwase
|
|
|
|
|
{
|
|
|
|
|
[ExecuteInEditMode]
|
|
|
|
|
//[AddComponentMenu("")]
|
|
|
|
|
[DisallowMultipleComponent]
|
2023-10-11 19:40:26 +08:00
|
|
|
|
[HelpURL("https://modular-avatar.nadena.dev/docs/reference/move-independently?lang=auto")]
|
2023-09-24 15:59:15 +08:00
|
|
|
|
class MAMoveIndependently : MonoBehaviour, IEditorOnly
|
|
|
|
|
{
|
2023-12-28 16:58:37 +08:00
|
|
|
|
private float EPSILON = 0.0000001f;
|
2023-09-24 15:59:15 +08:00
|
|
|
|
|
|
|
|
|
private GameObject[] m_groupedBones;
|
|
|
|
|
|
|
|
|
|
public GameObject[] GroupedBones
|
|
|
|
|
{
|
|
|
|
|
get => m_groupedBones.Clone() as GameObject[];
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
m_groupedBones = value.Clone() as GameObject[];
|
|
|
|
|
OnValidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ChildState
|
|
|
|
|
{
|
|
|
|
|
internal Vector3 childLocalPos;
|
|
|
|
|
internal Quaternion childLocalRot;
|
|
|
|
|
internal Vector3 childLocalScale;
|
|
|
|
|
|
|
|
|
|
// The child world position, recorded when we first initialized (or after unexpected child movement)
|
2023-12-28 16:58:37 +08:00
|
|
|
|
internal Matrix4x4 childToRoot;
|
2023-09-24 15:59:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Dictionary<Transform, ChildState> _children = new Dictionary<Transform, ChildState>();
|
|
|
|
|
private HashSet<Transform> _excluded = new HashSet<Transform>();
|
|
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
hideFlags = HideFlags.DontSave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We need to reparent the TRS values of the children from our prior frame state to the current frame state.
|
|
|
|
|
// This is done by computing the world affine matrix for the child in the prior frame, then converting to
|
|
|
|
|
// a local affine matrix in the current frame.
|
|
|
|
|
|
|
|
|
|
private void OnValidate()
|
|
|
|
|
{
|
|
|
|
|
hideFlags = HideFlags.DontSave;
|
|
|
|
|
_excluded = new HashSet<Transform>();
|
|
|
|
|
if (m_groupedBones == null)
|
|
|
|
|
{
|
|
|
|
|
m_groupedBones = Array.Empty<GameObject>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var grouped in m_groupedBones)
|
|
|
|
|
{
|
|
|
|
|
if (grouped != null)
|
|
|
|
|
{
|
|
|
|
|
_excluded.Add(grouped.transform);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
_priorFramePos = transform.localPosition;
|
|
|
|
|
_priorFrameRot = transform.localRotation;
|
|
|
|
|
_priorFrameScale = transform.localScale;
|
|
|
|
|
|
2023-09-24 15:59:15 +08:00
|
|
|
|
_children.Clear();
|
|
|
|
|
CheckChildren();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HashSet<Transform> _observed = new HashSet<Transform>();
|
|
|
|
|
|
|
|
|
|
private void CheckChildren()
|
|
|
|
|
{
|
|
|
|
|
_observed.Clear();
|
|
|
|
|
|
|
|
|
|
CheckChildren(transform);
|
|
|
|
|
foreach (var obj in m_groupedBones)
|
|
|
|
|
{
|
|
|
|
|
CheckChildren(obj.transform);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove any children that are no longer children
|
|
|
|
|
var toRemove = new List<Transform>();
|
|
|
|
|
foreach (var child in _children)
|
|
|
|
|
{
|
|
|
|
|
if (child.Key == null || !_observed.Contains(child.Key))
|
|
|
|
|
{
|
|
|
|
|
toRemove.Add(child.Key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var child in toRemove)
|
|
|
|
|
{
|
|
|
|
|
_children.Remove(child);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
private Matrix4x4 ParentTransformMatrix(Transform parent)
|
|
|
|
|
{
|
|
|
|
|
Matrix4x4 transform = Matrix4x4.TRS(
|
|
|
|
|
parent.localPosition,
|
|
|
|
|
parent.localRotation,
|
|
|
|
|
parent.localScale
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (_excluded.Contains(parent))
|
|
|
|
|
{
|
|
|
|
|
transform = ParentTransformMatrix(parent.parent) * transform;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return transform;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 15:59:15 +08:00
|
|
|
|
private void CheckChildren(Transform parent)
|
|
|
|
|
{
|
2023-12-28 16:58:37 +08:00
|
|
|
|
Matrix4x4 parentToRoot = ParentTransformMatrix(parent);
|
|
|
|
|
Matrix4x4 rootToParent = parentToRoot.inverse;
|
|
|
|
|
|
2023-09-24 15:59:15 +08:00
|
|
|
|
foreach (Transform child in parent)
|
|
|
|
|
{
|
|
|
|
|
if (_excluded.Contains(child)) continue;
|
|
|
|
|
|
|
|
|
|
_observed.Add(child);
|
|
|
|
|
|
|
|
|
|
var localPosition = child.localPosition;
|
|
|
|
|
var localRotation = child.localRotation;
|
|
|
|
|
var localScale = child.localScale;
|
|
|
|
|
|
2023-09-27 19:24:42 +08:00
|
|
|
|
if (!ArmatureLockController.MovedThisFrame && _children.TryGetValue(child, out var state))
|
2023-09-24 15:59:15 +08:00
|
|
|
|
{
|
|
|
|
|
var deltaPos = localPosition - state.childLocalPos;
|
|
|
|
|
var deltaRot = Quaternion.Angle(localRotation, state.childLocalRot);
|
|
|
|
|
var deltaScale = (localScale - state.childLocalScale).sqrMagnitude;
|
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
if (deltaPos.magnitude > EPSILON || deltaRot > EPSILON || deltaScale > EPSILON)
|
2023-09-24 15:59:15 +08:00
|
|
|
|
{
|
2023-12-28 16:58:37 +08:00
|
|
|
|
// The child object was moved in between parent updates; reconstruct its childToRoot to correct
|
|
|
|
|
// for this.
|
|
|
|
|
var oldChildTRS = Matrix4x4.TRS(
|
|
|
|
|
state.childLocalPos,
|
|
|
|
|
state.childLocalRot,
|
|
|
|
|
state.childLocalScale
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var newChildTRS = Matrix4x4.TRS(
|
|
|
|
|
localPosition,
|
|
|
|
|
localRotation,
|
|
|
|
|
localScale
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
state.childToRoot = state.childToRoot * oldChildTRS.inverse * newChildTRS;
|
|
|
|
|
}
|
2023-09-27 19:24:42 +08:00
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
Matrix4x4 childNewLocal = rootToParent * state.childToRoot;
|
2023-09-27 19:24:42 +08:00
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
var newPosition = childNewLocal.MultiplyPoint(Vector3.zero);
|
|
|
|
|
var newRotation = childNewLocal.rotation;
|
|
|
|
|
var newScale = childNewLocal.lossyScale;
|
2023-09-25 21:57:45 +08:00
|
|
|
|
#if UNITY_EDITOR
|
2023-12-28 16:58:37 +08:00
|
|
|
|
UnityEditor.Undo.RecordObject(child, UnityEditor.Undo.GetCurrentGroupName());
|
2023-09-25 21:57:45 +08:00
|
|
|
|
#endif
|
2023-09-24 15:59:15 +08:00
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
child.localPosition = newPosition;
|
|
|
|
|
child.localRotation = newRotation;
|
|
|
|
|
child.localScale = newScale;
|
2023-09-27 19:24:42 +08:00
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
state.childLocalPos = child.localPosition;
|
|
|
|
|
state.childLocalRot = child.localRotation;
|
|
|
|
|
state.childLocalScale = child.localScale;
|
2023-09-24 15:59:15 +08:00
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
_children[child] = state;
|
2023-09-24 15:59:15 +08:00
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
continue;
|
2023-09-24 15:59:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Matrix4x4 childTRS = Matrix4x4.TRS(localPosition, localRotation, localScale);
|
|
|
|
|
|
|
|
|
|
state = new ChildState()
|
|
|
|
|
{
|
|
|
|
|
childLocalPos = localPosition,
|
|
|
|
|
childLocalRot = localRotation,
|
|
|
|
|
childLocalScale = localScale,
|
2023-12-28 16:58:37 +08:00
|
|
|
|
childToRoot = parentToRoot * childTRS,
|
2023-09-24 15:59:15 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_children[child] = state;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 19:24:42 +08:00
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
UpdateLoopController.OnMoveIndependentlyUpdate += OnUpdate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
2023-09-24 15:59:15 +08:00
|
|
|
|
{
|
2023-09-27 19:24:42 +08:00
|
|
|
|
UpdateLoopController.OnMoveIndependentlyUpdate -= OnUpdate;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
private Vector3 _priorFramePos, _priorFrameScale;
|
|
|
|
|
private Quaternion _priorFrameRot;
|
|
|
|
|
|
2023-09-27 19:24:42 +08:00
|
|
|
|
void OnUpdate()
|
|
|
|
|
{
|
|
|
|
|
if (this == null)
|
|
|
|
|
{
|
|
|
|
|
UpdateLoopController.OnMoveIndependentlyUpdate -= OnUpdate;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
var pos = transform.localPosition;
|
|
|
|
|
var rot = transform.localRotation;
|
|
|
|
|
var scale = transform.localScale;
|
2023-09-24 15:59:15 +08:00
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
var deltaPos = transform.parent.localToWorldMatrix.MultiplyVector(pos - _priorFramePos);
|
|
|
|
|
var deltaRot = Quaternion.Angle(rot, _priorFrameRot);
|
2023-09-24 15:59:15 +08:00
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
var deltaScaleX = Mathf.Abs((scale - _priorFrameScale).x) / _priorFrameScale.x;
|
|
|
|
|
var deltaScaleY = Mathf.Abs((scale - _priorFrameScale).y) / _priorFrameScale.y;
|
|
|
|
|
var deltaScaleZ = Mathf.Abs((scale - _priorFrameScale).z) / _priorFrameScale.z;
|
|
|
|
|
|
|
|
|
|
if (float.IsNaN(deltaScaleX) || float.IsInfinity(deltaScaleX)) deltaScaleX = 1;
|
|
|
|
|
if (float.IsNaN(deltaScaleY) || float.IsInfinity(deltaScaleY)) deltaScaleY = 1;
|
|
|
|
|
if (float.IsNaN(deltaScaleZ) || float.IsInfinity(deltaScaleZ)) deltaScaleZ = 1;
|
2023-12-27 19:47:25 +08:00
|
|
|
|
|
2023-12-28 16:58:37 +08:00
|
|
|
|
float maxDeltaScale = Mathf.Max(deltaScaleX, Mathf.Max(deltaScaleY, deltaScaleZ));
|
|
|
|
|
|
|
|
|
|
if (deltaPos.magnitude > EPSILON || deltaRot > EPSILON || maxDeltaScale > 0.001)
|
|
|
|
|
{
|
|
|
|
|
CheckChildren();
|
|
|
|
|
|
|
|
|
|
_priorFramePos = pos;
|
|
|
|
|
_priorFrameRot = rot;
|
|
|
|
|
_priorFrameScale = scale;
|
|
|
|
|
}
|
2023-09-24 15:59:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|