From 20431dad6d2a3cefb0e28509a946955acc2edf6b Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 7 May 2021 18:43:59 +0900 Subject: [PATCH 01/37] RotationOffset --- .../VRM10/Editor/VRM10RotationOffsetDrawer.cs | 28 +++++++++++++++++++ .../Editor/VRM10RotationOffsetDrawer.cs.meta | 11 ++++++++ .../Components/Constraint/VRM10Constraint.cs | 1 + .../Constraint/VRM10RotationConstraint.cs | 3 ++ .../Constraint/VRM10RotationOffset.cs | 17 +++++++++++ .../Constraint/VRM10RotationOffset.cs.meta | 11 ++++++++ 6 files changed, 71 insertions(+) create mode 100644 Assets/VRM10/Editor/VRM10RotationOffsetDrawer.cs create mode 100644 Assets/VRM10/Editor/VRM10RotationOffsetDrawer.cs.meta create mode 100644 Assets/VRM10/Runtime/Components/Constraint/VRM10RotationOffset.cs create mode 100644 Assets/VRM10/Runtime/Components/Constraint/VRM10RotationOffset.cs.meta diff --git a/Assets/VRM10/Editor/VRM10RotationOffsetDrawer.cs b/Assets/VRM10/Editor/VRM10RotationOffsetDrawer.cs new file mode 100644 index 000000000..a893ecbe7 --- /dev/null +++ b/Assets/VRM10/Editor/VRM10RotationOffsetDrawer.cs @@ -0,0 +1,28 @@ + +using UnityEditor; +using UnityEngine; + +namespace UniVRM10 +{ + [CustomPropertyDrawer(typeof(VRM10RotationOffset))] + public class IngredientDrawer : PropertyDrawer + { + public override float GetPropertyHeight(SerializedProperty property, GUIContent label) + { + return 16f + 18f; + } + + // Draw the property inside the given rect + void DrawEuler(Rect position, SerializedProperty property) + { + Vector3 euler = property.quaternionValue.eulerAngles; + euler = EditorGUI.Vector3Field(position, "Offset", euler); + property.quaternionValue = Quaternion.Euler(euler); + } + + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + DrawEuler(position, property.FindPropertyRelative(nameof(VRM10RotationOffset.Rotation))); + } + } +} diff --git a/Assets/VRM10/Editor/VRM10RotationOffsetDrawer.cs.meta b/Assets/VRM10/Editor/VRM10RotationOffsetDrawer.cs.meta new file mode 100644 index 000000000..2f04a1587 --- /dev/null +++ b/Assets/VRM10/Editor/VRM10RotationOffsetDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 615a7cbd2c3cd5d42ab2719d6e74c1b8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs index 6ff82479a..7e6f11f0f 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs @@ -1,3 +1,4 @@ +using System; using UnityEngine; namespace UniVRM10 diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index 0263fbbec..b94ba5c18 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -13,6 +13,9 @@ namespace UniVRM10 [SerializeField] public Transform Source = default; + [SerializeField] + public VRM10RotationOffset SourceOffset = VRM10RotationOffset.Identity; + [SerializeField] public ObjectSpace SourceCoordinate = default; diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationOffset.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationOffset.cs new file mode 100644 index 000000000..a6382b5f1 --- /dev/null +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationOffset.cs @@ -0,0 +1,17 @@ +using System; +using UnityEngine; + +namespace UniVRM10 +{ + [Serializable] + public struct VRM10RotationOffset + { + [SerializeField] + public Quaternion Rotation; + + public static VRM10RotationOffset Identity => new VRM10RotationOffset + { + Rotation = Quaternion.identity, + }; + } +} diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationOffset.cs.meta b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationOffset.cs.meta new file mode 100644 index 000000000..4fc95a462 --- /dev/null +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationOffset.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5b8a5228a8756d64684e8dca601ccafc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From d8836717cbcc9bd9bd48d1c2e09b1ec01399d1a2 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 27 Apr 2021 18:13:38 +0900 Subject: [PATCH 02/37] VRM10RotationConstraintEditor --- .../VRM10/Editor/Components/Constraint.meta | 8 ++ .../VRM10RotationConstraintEditor.cs | 90 ++++++++++++++++++ .../VRM10RotationConstraintEditor.cs.meta | 11 +++ .../VRM10/Editor/EnumFlagAttributeDrawer.cs | 64 +++++++++++++ .../Editor/EnumFlagAttributeDrawer.cs.meta | 11 +++ .../Constraint/ConstraintDestination.cs | 59 ++++++------ .../Components/Constraint/ConstraintSource.cs | 94 +++++++------------ .../Constraint/VRM10PositionConstraint.cs | 18 ++-- .../Constraint/VRM10RotationConstraint.cs | 26 +++-- Assets/VRM10/Runtime/EnumFlagsAttribute.cs | 8 ++ .../VRM10/Runtime/EnumFlagsAttribute.cs.meta | 11 +++ 11 files changed, 297 insertions(+), 103 deletions(-) create mode 100644 Assets/VRM10/Editor/Components/Constraint.meta create mode 100644 Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs create mode 100644 Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs.meta create mode 100644 Assets/VRM10/Editor/EnumFlagAttributeDrawer.cs create mode 100644 Assets/VRM10/Editor/EnumFlagAttributeDrawer.cs.meta create mode 100644 Assets/VRM10/Runtime/EnumFlagsAttribute.cs create mode 100644 Assets/VRM10/Runtime/EnumFlagsAttribute.cs.meta diff --git a/Assets/VRM10/Editor/Components/Constraint.meta b/Assets/VRM10/Editor/Components/Constraint.meta new file mode 100644 index 000000000..2a1fd51d0 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7b369ee7eb21d1047bb03e139faa7eda +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs new file mode 100644 index 000000000..a1c0b5179 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -0,0 +1,90 @@ +using System.Text; +using UniGLTF.Extensions.VRMC_node_constraint; +using UnityEditor; +using UnityEngine; + +namespace UniVRM10 +{ + [CustomEditor(typeof(VRM10RotationConstraint))] + public class VRM10RotationConstraintEditor : Editor + { + VRM10RotationConstraint m_target; + + void OnEnable() + { + m_target = (VRM10RotationConstraint)target; + } + + void DrawSrcModel() + { + var model = m_target.ModelRoot; + var src = m_target.Source; + if (model == null) + { + Handles.Label(src.position, "ModelRoot required"); + return; + } + + const float size = 0.05f; + Handles.color = Color.red; + Handles.DrawLine(src.position, src.position + model.right * size); + Handles.color = Color.green; + Handles.DrawLine(src.position, src.position + model.up * size); + Handles.color = Color.black; + Handles.DrawLine(src.position, src.position + model.forward * size); + } + + void DrawSrcLocal() + { + // init + + // current + + // delta + } + + private GUIStyle _style; + + public void OnSceneGUI() + { + if (m_target.Source == null) + { + return; + } + + // this to target line + Handles.color = Color.yellow; + var rot = Quaternion.LookRotation(m_target.Source.position - m_target.transform.position, Vector3.up); + var len = (m_target.Source.position - m_target.transform.position).magnitude; + Handles.ArrowHandleCap(0, m_target.transform.position, rot, len, EventType.Repaint); + + // show delta + if (_style == null) + { + _style = new GUIStyle("box"); + } + var euler = m_target.Delta.eulerAngles; + var sb = new StringBuilder(); + sb.AppendLine(m_target.SourceCoordinate.ToString()); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"{euler.x:0.} => 0" : $"{euler.x:0.}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"{euler.y:0.} => 0" : $"{euler.y:0.}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"{euler.z:0.} => 0" : $"{euler.z:0.}"); + Handles.Label(m_target.Source.position, sb.ToString(), _style); + + switch (m_target.SourceCoordinate) + { + case ObjectSpace.model: + DrawSrcModel(); + break; + + case ObjectSpace.local: + DrawSrcLocal(); + break; + + default: + throw new System.NotImplementedException(); + } + } + + } +} diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs.meta b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs.meta new file mode 100644 index 000000000..ada1c4062 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cd4ca141215a42c45a0036791f2f8a4d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Editor/EnumFlagAttributeDrawer.cs b/Assets/VRM10/Editor/EnumFlagAttributeDrawer.cs new file mode 100644 index 000000000..8f7ba6e5c --- /dev/null +++ b/Assets/VRM10/Editor/EnumFlagAttributeDrawer.cs @@ -0,0 +1,64 @@ +using System; +using UnityEngine; +using UnityEditor; + +namespace UniVRM10 +{ + /// + /// Flag設定したEnumのインスペクター表示を変えるクラス + /// + [CustomPropertyDrawer(typeof(EnumFlagsAttribute))] + public sealed class EnumFlagsAttributeDrawer : PropertyDrawer + { + public override void OnGUI( + Rect position, + SerializedProperty prop, + GUIContent label + ) + { + var buttonsIntValue = 0; + var enumLength = prop.enumNames.Length; + var labelWidth = EditorGUIUtility.labelWidth; + var buttonPressed = new bool[enumLength]; + var buttonWidth = (position.width - labelWidth) / enumLength; + + var labelPos = new Rect( + position.x, + position.y, + labelWidth, + position.height + ); + EditorGUI.LabelField(labelPos, label); + EditorGUI.BeginChangeCheck(); + + for (int i = 0; i < enumLength; i++) + { + buttonPressed[i] = (prop.intValue & (1 << i)) == 1 << i; + + var buttonPos = new Rect( + position.x + labelWidth + buttonWidth * i, + position.y, + buttonWidth, + position.height + ); + + buttonPressed[i] = GUI.Toggle( + buttonPos, + buttonPressed[i], + prop.enumNames[i], + "Button" + ); + + if (buttonPressed[i]) + { + buttonsIntValue += 1 << i; + } + } + + if (EditorGUI.EndChangeCheck()) + { + prop.intValue = buttonsIntValue; + } + } + } +} diff --git a/Assets/VRM10/Editor/EnumFlagAttributeDrawer.cs.meta b/Assets/VRM10/Editor/EnumFlagAttributeDrawer.cs.meta new file mode 100644 index 000000000..3ea85c8eb --- /dev/null +++ b/Assets/VRM10/Editor/EnumFlagAttributeDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 62a17679a0c7f7843beaa97ae4121d63 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs index 21cf3b0f2..e6ef3c8cf 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs @@ -7,49 +7,39 @@ namespace UniVRM10 class ConstraintDestination { readonly Transform m_transform; - readonly ObjectSpace m_coords; + readonly TRS m_modelInitial; + readonly TRS m_localInitial; + public readonly Transform ModelRoot; - readonly TRS m_initial; - - public ConstraintDestination(Transform t, ObjectSpace coords, Transform modelRoot = null) + public ConstraintDestination(Transform t, Transform modelRoot = null) { + ModelRoot = modelRoot; m_transform = t; - m_coords = coords; - switch (m_coords) - { - // case ObjectSpace.World: - // m_initial = TRS.GetWorld(t); - // break; - - case ObjectSpace.local: - m_initial = TRS.GetLocal(t); - break; - - case ObjectSpace.model: - m_initial = TRS.GetRelative(t, modelRoot.worldToLocalMatrix); - break; - - default: - throw new NotImplementedException(); - } + m_localInitial = TRS.GetLocal(t); + m_modelInitial = TRS.GetRelative(t, modelRoot.worldToLocalMatrix); } - public void ApplyTranslation(Vector3 delta, float weight, Transform modelRoot = null) + public void ApplyTranslation(Vector3 delta, float weight, ObjectSpace coords, Transform modelRoot = null) { - var value = m_initial.Translation + delta * weight; - switch (m_coords) + switch (coords) { // case DestinationCoordinates.World: // m_transform.position = value; // break; case ObjectSpace.local: - m_transform.localPosition = value; + { + var value = m_localInitial.Translation + delta * weight; + m_transform.localPosition = value; + } break; case ObjectSpace.model: - m_transform.position = modelRoot.localToWorldMatrix.MultiplyPoint(value); + { + var value = m_modelInitial.Translation + delta * weight; + m_transform.position = modelRoot.localToWorldMatrix.MultiplyPoint(value); + } break; default: @@ -57,22 +47,27 @@ namespace UniVRM10 } } - public void ApplyRotation(Quaternion delta, float weight, Transform modelRoot = null) + public void ApplyRotation(Quaternion delta, float weight, ObjectSpace coords, Transform modelRoot = null) { // 0~1 で clamp しない slerp - var value = Quaternion.LerpUnclamped(Quaternion.identity, delta, weight) * m_initial.Rotation; - switch (m_coords) + switch (coords) { // case DestinationCoordinates.World: // m_transform.rotation = value; // break; case ObjectSpace.local: - m_transform.localRotation = value; + { + var value = Quaternion.LerpUnclamped(Quaternion.identity, delta, weight) * m_localInitial.Rotation; + m_transform.localRotation = value; + } break; case ObjectSpace.model: - m_transform.rotation = modelRoot.rotation * value; + { + var value = Quaternion.LerpUnclamped(Quaternion.identity, delta, weight) * m_modelInitial.Rotation; + m_transform.rotation = modelRoot.rotation * value; + } break; default: diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs index a199b2de4..c148d2169 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs @@ -6,72 +6,50 @@ namespace UniVRM10 { class ConstraintSource { - readonly Transform m_modelRoot; - + public readonly Transform ModelRoot; readonly Transform m_transform; + readonly TRS m_modelInitial; + readonly TRS m_localInitial; - readonly ObjectSpace m_coords; - - readonly TRS m_initial; - - public Vector3 TranslationDelta + public Vector3 TranslationDelta(ObjectSpace coords) { - get - { - switch (m_coords) - { - // case ObjectSpace.World: return m_transform.position - m_initial.Translation; - case ObjectSpace.local: return m_transform.localPosition - m_initial.Translation; - case ObjectSpace.model: return m_modelRoot.worldToLocalMatrix.MultiplyPoint(m_transform.position) - m_initial.Translation; - default: throw new NotImplementedException(); - } - } - } - - public Quaternion RotationDelta - { - get - { - switch (m_coords) - { - // 右からかけるか、左からかけるか、それが問題なのだ - // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); - case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(m_initial.Rotation); - case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(m_modelRoot.rotation) * Quaternion.Inverse(m_initial.Rotation); - default: throw new NotImplementedException(); - } - } - } - - public ConstraintSource(Transform t, ObjectSpace coords, Transform modelRoot = null) - { - m_transform = t; - m_coords = coords; - switch (coords) { - // case SourceCoordinates.World: - // m_initial = TRS.GetWorld(t); - // break; + // case ObjectSpace.World: return m_transform.position - m_initial.Translation; + case ObjectSpace.local: return m_transform.localPosition - m_localInitial.Translation; + case ObjectSpace.model: return ModelRoot.worldToLocalMatrix.MultiplyPoint(m_transform.position) - m_modelInitial.Translation; + default: throw new NotImplementedException(); + } + } - case ObjectSpace.local: - m_initial = TRS.GetLocal(t); - break; + public Quaternion RotationDelta(ObjectSpace coords) + { + switch (coords) + { + // 右からかけるか、左からかけるか、それが問題なのだ + // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); + case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(m_localInitial.Rotation); + case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelRoot.rotation) * Quaternion.Inverse(m_modelInitial.Rotation); + default: throw new NotImplementedException(); + } + } - case ObjectSpace.model: - { - var world = TRS.GetWorld(t); - m_modelRoot = modelRoot; - m_initial = new TRS - { - Translation = modelRoot.worldToLocalMatrix.MultiplyPoint(world.Translation), - Rotation = world.Rotation * Quaternion.Inverse(m_modelRoot.rotation), - }; - } - break; + public ConstraintSource(Transform t, Transform modelRoot = null) + { + m_transform = t; - default: - throw new NotImplementedException(); + { + m_localInitial = TRS.GetLocal(t); + } + + { + var world = TRS.GetWorld(t); + ModelRoot = modelRoot; + m_modelInitial = new TRS + { + Translation = modelRoot.worldToLocalMatrix.MultiplyPoint(world.Translation), + Rotation = world.Rotation * Quaternion.Inverse(ModelRoot.rotation), + }; } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs index 103cbb535..e69466a26 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs @@ -38,8 +38,14 @@ namespace UniVRM10 void OnValidate() { // Debug.Log("Validate"); - m_src = null; - m_dst = null; + if (m_src != null && m_src.ModelRoot != ModelRoot) + { + m_src = null; + } + if (m_dst != null && m_dst.ModelRoot != ModelRoot) + { + m_dst = null; + } } void Reset() @@ -62,15 +68,15 @@ namespace UniVRM10 if (m_src == null) { - m_src = new ConstraintSource(Source, SourceCoordinate, ModelRoot); + m_src = new ConstraintSource(Source, ModelRoot); } if (m_dst == null) { - m_dst = new ConstraintDestination(transform, DestinationCoordinate, ModelRoot); + m_dst = new ConstraintDestination(transform, ModelRoot); } - var delta = FreezeAxes.Freeze(m_src.TranslationDelta); - m_dst.ApplyTranslation(delta, Weight, ModelRoot); + var delta = FreezeAxes.Freeze(m_src.TranslationDelta(SourceCoordinate)); + m_dst.ApplyTranslation(delta, Weight, DestinationCoordinate, ModelRoot); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index b94ba5c18..ef8c2d581 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -23,6 +23,7 @@ namespace UniVRM10 public ObjectSpace DestinationCoordinate = default; [SerializeField] + [EnumFlags] public AxisMask FreezeAxes = default; [SerializeField] @@ -33,6 +34,11 @@ namespace UniVRM10 public Transform ModelRoot = default; ConstraintSource m_src; + public Quaternion Delta + { + get; + private set; + } ConstraintDestination m_dst; @@ -42,8 +48,14 @@ namespace UniVRM10 void OnValidate() { // Debug.Log("Validate"); - m_src = null; - m_dst = null; + if (m_src != null && m_src.ModelRoot != ModelRoot) + { + m_src = null; + } + if (m_dst != null && m_dst.ModelRoot != ModelRoot) + { + m_dst = null; + } } void Reset() @@ -70,20 +82,20 @@ namespace UniVRM10 if (m_src == null) { - m_src = new ConstraintSource(Source, SourceCoordinate, ModelRoot); + m_src = new ConstraintSource(Source, ModelRoot); } if (m_dst == null) { - m_dst = new ConstraintDestination(transform, DestinationCoordinate, ModelRoot); + m_dst = new ConstraintDestination(transform, ModelRoot); } // 軸制限をしたオイラー角 - var delta = m_src.RotationDelta; - var fleezed = FreezeAxes.Freeze(delta.eulerAngles); + Delta = m_src.RotationDelta(SourceCoordinate); + var fleezed = FreezeAxes.Freeze(Delta.eulerAngles); var rotation = Quaternion.Euler(fleezed); // Debug.Log($"{delta} => {rotation}"); // オイラー角を再度Quaternionへ。weight を加味してSlerpする - m_dst.ApplyRotation(rotation, Weight, ModelRoot); + m_dst.ApplyRotation(rotation, Weight, DestinationCoordinate, ModelRoot); } } } diff --git a/Assets/VRM10/Runtime/EnumFlagsAttribute.cs b/Assets/VRM10/Runtime/EnumFlagsAttribute.cs new file mode 100644 index 000000000..698ab6e2c --- /dev/null +++ b/Assets/VRM10/Runtime/EnumFlagsAttribute.cs @@ -0,0 +1,8 @@ +using System; +using UnityEngine; + +namespace UniVRM10 +{ + [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)] + public sealed class EnumFlagsAttribute : PropertyAttribute { } +} diff --git a/Assets/VRM10/Runtime/EnumFlagsAttribute.cs.meta b/Assets/VRM10/Runtime/EnumFlagsAttribute.cs.meta new file mode 100644 index 000000000..3f89217c9 --- /dev/null +++ b/Assets/VRM10/Runtime/EnumFlagsAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5f3c3f9559a399143816d5eab6f0b4ee +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 60737fb030bbf9a7a76ec7430272086d54861b67 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 27 Apr 2021 18:32:19 +0900 Subject: [PATCH 03/37] Clamp180 --- .../VRM10RotationConstraintEditor.cs | 59 ++++++++++++++----- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index a1c0b5179..4e5ca1aca 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -43,7 +43,26 @@ namespace UniVRM10 // delta } - private GUIStyle _style; + static GUIStyle s_style; + + /// + /// Euler各を +- 180 にクランプする + /// + /// + /// + static Vector3 Clamp180(Vector3 v) + { + var x = v.x; + while (x < -180) x += 360; + while (x > 180) x -= 360; + var y = v.y; + while (y < -180) y += 360; + while (y > 180) y -= 360; + var z = v.z; + while (z < -180) z += 360; + while (z > 180) z -= 360; + return new Vector3(x, y, z); + } public void OnSceneGUI() { @@ -51,25 +70,36 @@ namespace UniVRM10 { return; } + if (s_style == null) + { + s_style = new GUIStyle("box"); + } // this to target line Handles.color = Color.yellow; - var rot = Quaternion.LookRotation(m_target.Source.position - m_target.transform.position, Vector3.up); - var len = (m_target.Source.position - m_target.transform.position).magnitude; - Handles.ArrowHandleCap(0, m_target.transform.position, rot, len, EventType.Repaint); + Handles.DrawLine(m_target.Source.position, m_target.transform.position); - // show delta - if (_style == null) + var euler = Clamp180(m_target.Delta.eulerAngles); + + // show source { - _style = new GUIStyle("box"); + var sb = new StringBuilder(); + sb.AppendLine($"source: {m_target.SourceCoordinate}"); + sb.AppendLine($"{euler.x:0.}"); + sb.AppendLine($"{euler.y:0.}"); + sb.Append($"{euler.z:0.}"); + Handles.Label(m_target.Source.position, sb.ToString(), s_style); + } + + // show dst + { + var sb = new StringBuilder(); + sb.AppendLine($"constraint: {m_target.DestinationCoordinate}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{euler.x:0.}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{euler.y:0.}"); + sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{euler.z:0.}"); + Handles.Label(m_target.transform.position, sb.ToString(), s_style); } - var euler = m_target.Delta.eulerAngles; - var sb = new StringBuilder(); - sb.AppendLine(m_target.SourceCoordinate.ToString()); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"{euler.x:0.} => 0" : $"{euler.x:0.}"); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"{euler.y:0.} => 0" : $"{euler.y:0.}"); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"{euler.z:0.} => 0" : $"{euler.z:0.}"); - Handles.Label(m_target.Source.position, sb.ToString(), _style); switch (m_target.SourceCoordinate) { @@ -85,6 +115,5 @@ namespace UniVRM10 throw new System.NotImplementedException(); } } - } } From 9dd707134b084e734388009acb392c6046e7c72b Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 27 Apr 2021 19:39:07 +0900 Subject: [PATCH 04/37] DrawDstLocal --- .../VRM10RotationConstraintEditor.cs | 41 ++++++++++++++++++- .../Constraint/ConstraintDestination.cs | 8 ++-- .../Components/Constraint/ConstraintSource.cs | 8 ++-- .../Runtime/Components/Constraint/TRS.cs | 2 + .../Constraint/VRM10RotationConstraint.cs | 37 +++++++++++++++++ 5 files changed, 86 insertions(+), 10 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index 4e5ca1aca..e89ff2501 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -1,3 +1,4 @@ +using System; using System.Text; using UniGLTF.Extensions.VRMC_node_constraint; using UnityEditor; @@ -36,11 +37,32 @@ namespace UniVRM10 void DrawSrcLocal() { + Handles.color = Color.green; + // init + Handles.matrix = m_target.GetSourceLocalInit(); + Handles.CubeHandleCap(0, Vector3.zero, Quaternion.identity, 0.02f, EventType.Repaint); // current + Handles.matrix = m_target.Source.localToWorldMatrix; + var size = 0.04f; + Handles.DrawWireCube(Vector3.zero, new Vector3(size, size, size)); + } - // delta + void DrawDstLocal() + { + Handles.color = Color.red; + + var s = m_target.transform.lossyScale; + + // init + Handles.matrix = m_target.GetDstLocalInit(); + Handles.CubeHandleCap(0, Vector3.zero, Quaternion.identity, 0.02f / s.x, EventType.Repaint); + + // current + Handles.matrix = m_target.transform.localToWorldMatrix; + var size = 0.04f / s.x; + Handles.DrawWireCube(Vector3.zero, new Vector3(size, size, size)); } static GUIStyle s_style; @@ -84,6 +106,8 @@ namespace UniVRM10 // show source { var sb = new StringBuilder(); + sb.AppendLine(); + sb.AppendLine(); sb.AppendLine($"source: {m_target.SourceCoordinate}"); sb.AppendLine($"{euler.x:0.}"); sb.AppendLine($"{euler.y:0.}"); @@ -112,7 +136,20 @@ namespace UniVRM10 break; default: - throw new System.NotImplementedException(); + throw new NotImplementedException(); + } + + switch (m_target.DestinationCoordinate) + { + case ObjectSpace.model: + break; + + case ObjectSpace.local: + DrawDstLocal(); + break; + + default: + throw new NotImplementedException(); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs index e6ef3c8cf..56261bc5a 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs @@ -8,7 +8,7 @@ namespace UniVRM10 { readonly Transform m_transform; readonly TRS m_modelInitial; - readonly TRS m_localInitial; + public readonly TRS LocalInitial; public readonly Transform ModelRoot; public ConstraintDestination(Transform t, Transform modelRoot = null) @@ -16,7 +16,7 @@ namespace UniVRM10 ModelRoot = modelRoot; m_transform = t; - m_localInitial = TRS.GetLocal(t); + LocalInitial = TRS.GetLocal(t); m_modelInitial = TRS.GetRelative(t, modelRoot.worldToLocalMatrix); } @@ -30,7 +30,7 @@ namespace UniVRM10 case ObjectSpace.local: { - var value = m_localInitial.Translation + delta * weight; + var value = LocalInitial.Translation + delta * weight; m_transform.localPosition = value; } break; @@ -58,7 +58,7 @@ namespace UniVRM10 case ObjectSpace.local: { - var value = Quaternion.LerpUnclamped(Quaternion.identity, delta, weight) * m_localInitial.Rotation; + var value = Quaternion.LerpUnclamped(Quaternion.identity, delta, weight) * LocalInitial.Rotation; m_transform.localRotation = value; } break; diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs index c148d2169..43832d0a4 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs @@ -9,14 +9,14 @@ namespace UniVRM10 public readonly Transform ModelRoot; readonly Transform m_transform; readonly TRS m_modelInitial; - readonly TRS m_localInitial; + public readonly TRS LocalInitial; public Vector3 TranslationDelta(ObjectSpace coords) { switch (coords) { // case ObjectSpace.World: return m_transform.position - m_initial.Translation; - case ObjectSpace.local: return m_transform.localPosition - m_localInitial.Translation; + case ObjectSpace.local: return m_transform.localPosition - LocalInitial.Translation; case ObjectSpace.model: return ModelRoot.worldToLocalMatrix.MultiplyPoint(m_transform.position) - m_modelInitial.Translation; default: throw new NotImplementedException(); } @@ -28,7 +28,7 @@ namespace UniVRM10 { // 右からかけるか、左からかけるか、それが問題なのだ // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); - case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(m_localInitial.Rotation); + case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(LocalInitial.Rotation); case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelRoot.rotation) * Quaternion.Inverse(m_modelInitial.Rotation); default: throw new NotImplementedException(); } @@ -39,7 +39,7 @@ namespace UniVRM10 m_transform = t; { - m_localInitial = TRS.GetLocal(t); + LocalInitial = TRS.GetLocal(t); } { diff --git a/Assets/VRM10/Runtime/Components/Constraint/TRS.cs b/Assets/VRM10/Runtime/Components/Constraint/TRS.cs index fffdf7c55..eb5600d53 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/TRS.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/TRS.cs @@ -8,6 +8,8 @@ namespace UniVRM10 public Quaternion Rotation; public Vector3 Scale; + public Matrix4x4 Matrix => Matrix4x4.TRS(Translation, Rotation, Scale); + public static TRS GetWorld(Transform t) { return new TRS diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index ef8c2d581..badcdb2d7 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -34,6 +34,27 @@ namespace UniVRM10 public Transform ModelRoot = default; ConstraintSource m_src; + public Matrix4x4 GetSourceLocalInit() + { + if (m_src != null) + { + var parent = Matrix4x4.identity; + if (Source != null && Source.parent != null) + { + parent = Source.parent.localToWorldMatrix; + } + return parent * m_src.LocalInitial.Matrix; + } + else if (Source != null) + { + return Source.localToWorldMatrix; + } + else + { + return Matrix4x4.identity; + } + } + public Quaternion Delta { get; @@ -41,6 +62,22 @@ namespace UniVRM10 } ConstraintDestination m_dst; + public Matrix4x4 GetDstLocalInit() + { + if (m_src != null) + { + var parent = Matrix4x4.identity; + if (transform.parent != null) + { + parent = transform.parent.localToWorldMatrix; + } + return parent * m_dst.LocalInitial.Matrix; + } + else + { + return transform.localToWorldMatrix; + } + } /// /// Editorで設定値の変更を反映するために、クリアする From bb9d9136c3cda223670d53d6bbb23fb5e026e034 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 28 Apr 2021 17:42:58 +0900 Subject: [PATCH 05/37] Coords --- .../Editor/Components/Constraint/Coords.cs | 32 +++++++++++++++++++ .../Components/Constraint/Coords.cs.meta | 11 +++++++ .../VRM10RotationConstraintEditor.cs | 20 ++++++------ 3 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 Assets/VRM10/Editor/Components/Constraint/Coords.cs create mode 100644 Assets/VRM10/Editor/Components/Constraint/Coords.cs.meta diff --git a/Assets/VRM10/Editor/Components/Constraint/Coords.cs b/Assets/VRM10/Editor/Components/Constraint/Coords.cs new file mode 100644 index 000000000..d0f307c92 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/Coords.cs @@ -0,0 +1,32 @@ +using UnityEditor; +using UnityEngine; + +namespace UniVRM10 +{ + public static class Coords + { + public static void Write(Matrix4x4 m, float size) + { + Handles.matrix = m * Matrix4x4.Scale(new Vector3(size, size, size)); + + // Handles.CubeHandleCap(0, Vector3.zero, Quaternion.identity, size, EventType.Repaint); + Handles.color = Color.red; + Handles.DrawLine(Vector3.zero, Vector3.right); + Handles.color = Color.green; + Handles.DrawLine(Vector3.zero, Vector3.up); + Handles.color = Color.blue; + Handles.DrawLine(Vector3.zero, Vector3.forward); + + Handles.color = Color.white; + // xy + Handles.DrawLine(Vector3.right + Vector3.up, Vector3.right); + Handles.DrawLine(Vector3.right + Vector3.up, Vector3.up); + // yz + Handles.DrawLine(Vector3.up + Vector3.forward, Vector3.forward); + Handles.DrawLine(Vector3.up + Vector3.forward, Vector3.up); + // zx + Handles.DrawLine(Vector3.forward + Vector3.right, Vector3.forward); + Handles.DrawLine(Vector3.forward + Vector3.right, Vector3.right); + } + } +} diff --git a/Assets/VRM10/Editor/Components/Constraint/Coords.cs.meta b/Assets/VRM10/Editor/Components/Constraint/Coords.cs.meta new file mode 100644 index 000000000..c41246f85 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/Coords.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 069e3004cdd162e488caa0fab3983f05 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index e89ff2501..a09155b13 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -37,31 +37,29 @@ namespace UniVRM10 void DrawSrcLocal() { - Handles.color = Color.green; + var s = m_target.transform.lossyScale.x; // init - Handles.matrix = m_target.GetSourceLocalInit(); - Handles.CubeHandleCap(0, Vector3.zero, Quaternion.identity, 0.02f, EventType.Repaint); + Coords.Write(m_target.GetSourceLocalInit(), 0.2f / s); // current Handles.matrix = m_target.Source.localToWorldMatrix; - var size = 0.04f; + Handles.color = Color.yellow; + var size = 0.05f / s; Handles.DrawWireCube(Vector3.zero, new Vector3(size, size, size)); } void DrawDstLocal() { - Handles.color = Color.red; + var s = m_target.transform.lossyScale.x; - var s = m_target.transform.lossyScale; - - // init - Handles.matrix = m_target.GetDstLocalInit(); - Handles.CubeHandleCap(0, Vector3.zero, Quaternion.identity, 0.02f / s.x, EventType.Repaint); + // init + Coords.Write(m_target.GetDstLocalInit(), 0.2f / s); // current Handles.matrix = m_target.transform.localToWorldMatrix; - var size = 0.04f / s.x; + Handles.color = Color.yellow; + var size = 0.05f / s; Handles.DrawWireCube(Vector3.zero, new Vector3(size, size, size)); } From e7fe9af5b0d8945fc0dc82e5e1b4171b951a7060 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 28 Apr 2021 18:12:56 +0900 Subject: [PATCH 06/37] SrcDrawCurrent, DstDrawCurrent --- .../VRM10RotationConstraintEditor.cs | 69 +++++++++++-------- .../Components/Constraint/ConstraintSource.cs | 16 +++-- .../Constraint/VRM10RotationConstraint.cs | 49 +++++++++---- 3 files changed, 89 insertions(+), 45 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index a09155b13..605e000a9 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -16,32 +16,11 @@ namespace UniVRM10 m_target = (VRM10RotationConstraint)target; } - void DrawSrcModel() - { - var model = m_target.ModelRoot; - var src = m_target.Source; - if (model == null) - { - Handles.Label(src.position, "ModelRoot required"); - return; - } - - const float size = 0.05f; - Handles.color = Color.red; - Handles.DrawLine(src.position, src.position + model.right * size); - Handles.color = Color.green; - Handles.DrawLine(src.position, src.position + model.up * size); - Handles.color = Color.black; - Handles.DrawLine(src.position, src.position + model.forward * size); - } - - void DrawSrcLocal() + #region SRC + void SrcDrawCurrent() { var s = m_target.transform.lossyScale.x; - // init - Coords.Write(m_target.GetSourceLocalInit(), 0.2f / s); - // current Handles.matrix = m_target.Source.localToWorldMatrix; Handles.color = Color.yellow; @@ -49,12 +28,27 @@ namespace UniVRM10 Handles.DrawWireCube(Vector3.zero, new Vector3(size, size, size)); } - void DrawDstLocal() + void SrcDrawModelCoords() { var s = m_target.transform.lossyScale.x; - // init - Coords.Write(m_target.GetDstLocalInit(), 0.2f / s); + // init + Coords.Write(m_target.GetSourceModelCoords(), 0.2f / s); + } + + void SrcDrawLocalCoords() + { + var s = m_target.transform.lossyScale.x; + + // init + Coords.Write(m_target.GetSourceLocalCoords(), 0.2f / s); + } + #endregion + + #region Dst + void DstDrawCurrent() + { + var s = m_target.transform.lossyScale.x; // current Handles.matrix = m_target.transform.localToWorldMatrix; @@ -63,6 +57,20 @@ namespace UniVRM10 Handles.DrawWireCube(Vector3.zero, new Vector3(size, size, size)); } + void DstDrawModelCoords() + { + + } + + void DstDrawLocalCoords() + { + var s = m_target.transform.lossyScale.x; + + // init + Coords.Write(m_target.GetDstLocalInit(), 0.2f / s); + } + #endregion + static GUIStyle s_style; /// @@ -126,29 +134,32 @@ namespace UniVRM10 switch (m_target.SourceCoordinate) { case ObjectSpace.model: - DrawSrcModel(); + SrcDrawModelCoords(); break; case ObjectSpace.local: - DrawSrcLocal(); + SrcDrawLocalCoords(); break; default: throw new NotImplementedException(); } + SrcDrawCurrent(); switch (m_target.DestinationCoordinate) { case ObjectSpace.model: + DstDrawModelCoords(); break; case ObjectSpace.local: - DrawDstLocal(); + DstDrawLocalCoords(); break; default: throw new NotImplementedException(); } + DstDrawCurrent(); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs index 43832d0a4..a3809fc75 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs @@ -8,7 +8,15 @@ namespace UniVRM10 { public readonly Transform ModelRoot; readonly Transform m_transform; - readonly TRS m_modelInitial; + + /// + /// initial: ModelRoot.localToWorldMatrix^-1 * t.localToWorldMatrix + /// + public readonly TRS ModelInitial; + + /// + /// initial: t.localPosition, t.localRotation, t.localScale + /// public readonly TRS LocalInitial; public Vector3 TranslationDelta(ObjectSpace coords) @@ -17,7 +25,7 @@ namespace UniVRM10 { // case ObjectSpace.World: return m_transform.position - m_initial.Translation; case ObjectSpace.local: return m_transform.localPosition - LocalInitial.Translation; - case ObjectSpace.model: return ModelRoot.worldToLocalMatrix.MultiplyPoint(m_transform.position) - m_modelInitial.Translation; + case ObjectSpace.model: return ModelRoot.worldToLocalMatrix.MultiplyPoint(m_transform.position) - ModelInitial.Translation; default: throw new NotImplementedException(); } } @@ -29,7 +37,7 @@ namespace UniVRM10 // 右からかけるか、左からかけるか、それが問題なのだ // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(LocalInitial.Rotation); - case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelRoot.rotation) * Quaternion.Inverse(m_modelInitial.Rotation); + case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelRoot.rotation) * Quaternion.Inverse(ModelInitial.Rotation); default: throw new NotImplementedException(); } } @@ -45,7 +53,7 @@ namespace UniVRM10 { var world = TRS.GetWorld(t); ModelRoot = modelRoot; - m_modelInitial = new TRS + ModelInitial = new TRS { Translation = modelRoot.worldToLocalMatrix.MultiplyPoint(world.Translation), Rotation = world.Rotation * Quaternion.Inverse(ModelRoot.rotation), diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index badcdb2d7..f2e507821 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -34,25 +34,49 @@ namespace UniVRM10 public Transform ModelRoot = default; ConstraintSource m_src; - public Matrix4x4 GetSourceLocalInit() + + /// + /// Model の座標系 + /// + /// + public Matrix4x4 GetSourceModelCoords() { - if (m_src != null) + if (Source != null) { - var parent = Matrix4x4.identity; - if (Source != null && Source.parent != null) + if (ModelRoot != null) { - parent = Source.parent.localToWorldMatrix; + return ModelRoot.localToWorldMatrix * Matrix4x4.Translate(Source.position); } - return parent * m_src.LocalInitial.Matrix; } - else if (Source != null) + + return Matrix4x4.identity; + } + + /// + /// Local の座標系。つまり親座標系 + /// + /// + public Matrix4x4 GetSourceLocalCoords() + { + if (Source != null) { - return Source.localToWorldMatrix; - } - else - { - return Matrix4x4.identity; + if (m_src != null) + { + // runtime + var parent = Matrix4x4.identity; + if (Source.parent != null) + { + parent = Source.parent.localToWorldMatrix; + } + return parent * m_src.LocalInitial.Matrix; + } + else + { + return Source.localToWorldMatrix; + } } + + return Matrix4x4.identity; } public Quaternion Delta @@ -66,6 +90,7 @@ namespace UniVRM10 { if (m_src != null) { + // runtime var parent = Matrix4x4.identity; if (transform.parent != null) { From 864415f614fc31e44fff8d6f42a23abfa9ae0b6a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 28 Apr 2021 19:15:21 +0900 Subject: [PATCH 07/37] impl DstDrawModelCoords --- .../VRM10RotationConstraintEditor.cs | 5 ++++- .../Constraint/VRM10RotationConstraint.cs | 21 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index 605e000a9..f12f39e1d 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -59,7 +59,10 @@ namespace UniVRM10 void DstDrawModelCoords() { + var s = m_target.transform.lossyScale.x; + // init + Coords.Write(m_target.GetDstModelCoords(), 0.2f / s); } void DstDrawLocalCoords() @@ -67,7 +70,7 @@ namespace UniVRM10 var s = m_target.transform.lossyScale.x; // init - Coords.Write(m_target.GetDstLocalInit(), 0.2f / s); + Coords.Write(m_target.GetDstLocalCoords(), 0.2f / s); } #endregion diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index f2e507821..6aea64770 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -41,15 +41,19 @@ namespace UniVRM10 /// public Matrix4x4 GetSourceModelCoords() { + Matrix4x4 m = Matrix4x4.identity; if (Source != null) { if (ModelRoot != null) { - return ModelRoot.localToWorldMatrix * Matrix4x4.Translate(Source.position); + m = Matrix4x4.Rotate(ModelRoot.rotation); } } - - return Matrix4x4.identity; + if (Source != null) + { + m *= Matrix4x4.Translate(Source.position); + } + return m; } /// @@ -86,7 +90,16 @@ namespace UniVRM10 } ConstraintDestination m_dst; - public Matrix4x4 GetDstLocalInit() + public Matrix4x4 GetDstModelCoords() + { + Matrix4x4 m = Matrix4x4.identity; + if (ModelRoot != null) + { + m = Matrix4x4.Rotate(ModelRoot.rotation); + } + return m * Matrix4x4.Translate(transform.position); + } + public Matrix4x4 GetDstLocalCoords() { if (m_src != null) { From 8dd9ef6a0a494b21120a45f315da7b71df71ca0d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 10 May 2021 16:54:39 +0900 Subject: [PATCH 08/37] rename Write to Draw --- Assets/VRM10/Editor/Components/Constraint/Coords.cs | 2 +- .../Constraint/VRM10RotationConstraintEditor.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/Coords.cs b/Assets/VRM10/Editor/Components/Constraint/Coords.cs index d0f307c92..77a56fc74 100644 --- a/Assets/VRM10/Editor/Components/Constraint/Coords.cs +++ b/Assets/VRM10/Editor/Components/Constraint/Coords.cs @@ -5,7 +5,7 @@ namespace UniVRM10 { public static class Coords { - public static void Write(Matrix4x4 m, float size) + public static void Draw(Matrix4x4 m, float size) { Handles.matrix = m * Matrix4x4.Scale(new Vector3(size, size, size)); diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index f12f39e1d..f5adc601e 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -33,7 +33,7 @@ namespace UniVRM10 var s = m_target.transform.lossyScale.x; // init - Coords.Write(m_target.GetSourceModelCoords(), 0.2f / s); + Coords.Draw(m_target.GetSourceModelCoords(), 0.2f / s); } void SrcDrawLocalCoords() @@ -41,7 +41,7 @@ namespace UniVRM10 var s = m_target.transform.lossyScale.x; // init - Coords.Write(m_target.GetSourceLocalCoords(), 0.2f / s); + Coords.Draw(m_target.GetSourceLocalCoords(), 0.2f / s); } #endregion @@ -62,7 +62,7 @@ namespace UniVRM10 var s = m_target.transform.lossyScale.x; // init - Coords.Write(m_target.GetDstModelCoords(), 0.2f / s); + Coords.Draw(m_target.GetDstModelCoords(), 0.2f / s); } void DstDrawLocalCoords() @@ -70,7 +70,7 @@ namespace UniVRM10 var s = m_target.transform.lossyScale.x; // init - Coords.Write(m_target.GetDstLocalCoords(), 0.2f / s); + Coords.Draw(m_target.GetDstLocalCoords(), 0.2f / s); } #endregion From 43fc06c71514fcae575921304db7965b41fcc7c5 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 10 May 2021 19:47:11 +0900 Subject: [PATCH 09/37] TR --- .../Constraint/{Coords.cs => TRExtensions.cs} | 6 +-- .../{Coords.cs.meta => TRExtensions.cs.meta} | 2 +- .../VRM10RotationConstraintEditor.cs | 8 ++-- .../Constraint/ConstraintDestination.cs | 8 ++-- .../Components/Constraint/ConstraintSource.cs | 14 +++--- .../VRM10/Runtime/Components/Constraint/TR.cs | 36 ++++++++++++++++ .../Constraint/{TRS.cs.meta => TR.cs.meta} | 2 +- .../Runtime/Components/Constraint/TRS.cs | 43 ------------------- .../Constraint/VRM10RotationConstraint.cs | 43 ++++++++++--------- 9 files changed, 77 insertions(+), 85 deletions(-) rename Assets/VRM10/Editor/Components/Constraint/{Coords.cs => TRExtensions.cs} (85%) rename Assets/VRM10/Editor/Components/Constraint/{Coords.cs.meta => TRExtensions.cs.meta} (83%) create mode 100644 Assets/VRM10/Runtime/Components/Constraint/TR.cs rename Assets/VRM10/Runtime/Components/Constraint/{TRS.cs.meta => TR.cs.meta} (83%) delete mode 100644 Assets/VRM10/Runtime/Components/Constraint/TRS.cs diff --git a/Assets/VRM10/Editor/Components/Constraint/Coords.cs b/Assets/VRM10/Editor/Components/Constraint/TRExtensions.cs similarity index 85% rename from Assets/VRM10/Editor/Components/Constraint/Coords.cs rename to Assets/VRM10/Editor/Components/Constraint/TRExtensions.cs index 77a56fc74..1365ea074 100644 --- a/Assets/VRM10/Editor/Components/Constraint/Coords.cs +++ b/Assets/VRM10/Editor/Components/Constraint/TRExtensions.cs @@ -3,11 +3,11 @@ using UnityEngine; namespace UniVRM10 { - public static class Coords + public static class TRExtensions { - public static void Draw(Matrix4x4 m, float size) + public static void Draw(this TR tr, float size) { - Handles.matrix = m * Matrix4x4.Scale(new Vector3(size, size, size)); + Handles.matrix = tr.TRS(size); // Handles.CubeHandleCap(0, Vector3.zero, Quaternion.identity, size, EventType.Repaint); Handles.color = Color.red; diff --git a/Assets/VRM10/Editor/Components/Constraint/Coords.cs.meta b/Assets/VRM10/Editor/Components/Constraint/TRExtensions.cs.meta similarity index 83% rename from Assets/VRM10/Editor/Components/Constraint/Coords.cs.meta rename to Assets/VRM10/Editor/Components/Constraint/TRExtensions.cs.meta index c41246f85..d86fabc8d 100644 --- a/Assets/VRM10/Editor/Components/Constraint/Coords.cs.meta +++ b/Assets/VRM10/Editor/Components/Constraint/TRExtensions.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 069e3004cdd162e488caa0fab3983f05 +guid: f2d422c6e760aca49841f8acf44c0434 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index f5adc601e..e2beac9ca 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -33,7 +33,7 @@ namespace UniVRM10 var s = m_target.transform.lossyScale.x; // init - Coords.Draw(m_target.GetSourceModelCoords(), 0.2f / s); + m_target.GetSourceModelCoords().Draw(0.2f / s); } void SrcDrawLocalCoords() @@ -41,7 +41,7 @@ namespace UniVRM10 var s = m_target.transform.lossyScale.x; // init - Coords.Draw(m_target.GetSourceLocalCoords(), 0.2f / s); + m_target.GetSourceLocalCoords().Draw(0.2f / s); } #endregion @@ -62,7 +62,7 @@ namespace UniVRM10 var s = m_target.transform.lossyScale.x; // init - Coords.Draw(m_target.GetDstModelCoords(), 0.2f / s); + m_target.GetDstModelCoords().Draw(0.2f / s); } void DstDrawLocalCoords() @@ -70,7 +70,7 @@ namespace UniVRM10 var s = m_target.transform.lossyScale.x; // init - Coords.Draw(m_target.GetDstLocalCoords(), 0.2f / s); + m_target.GetDstLocalCoords().Draw(0.2f / s); } #endregion diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs index 56261bc5a..034130aeb 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs @@ -7,8 +7,8 @@ namespace UniVRM10 class ConstraintDestination { readonly Transform m_transform; - readonly TRS m_modelInitial; - public readonly TRS LocalInitial; + readonly TR m_modelInitial; + public readonly TR LocalInitial; public readonly Transform ModelRoot; public ConstraintDestination(Transform t, Transform modelRoot = null) @@ -16,8 +16,8 @@ namespace UniVRM10 ModelRoot = modelRoot; m_transform = t; - LocalInitial = TRS.GetLocal(t); - m_modelInitial = TRS.GetRelative(t, modelRoot.worldToLocalMatrix); + LocalInitial = TR.FromLocal(t); + m_modelInitial = TR.FromRelative(t, modelRoot); } public void ApplyTranslation(Vector3 delta, float weight, ObjectSpace coords, Transform modelRoot = null) diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs index a3809fc75..43d684393 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs @@ -12,12 +12,12 @@ namespace UniVRM10 /// /// initial: ModelRoot.localToWorldMatrix^-1 * t.localToWorldMatrix /// - public readonly TRS ModelInitial; + public readonly TR ModelInitial; /// /// initial: t.localPosition, t.localRotation, t.localScale /// - public readonly TRS LocalInitial; + public readonly TR LocalInitial; public Vector3 TranslationDelta(ObjectSpace coords) { @@ -47,17 +47,13 @@ namespace UniVRM10 m_transform = t; { - LocalInitial = TRS.GetLocal(t); + LocalInitial = TR.FromLocal(t); } { - var world = TRS.GetWorld(t); + var world = TR.FromWorld(t); ModelRoot = modelRoot; - ModelInitial = new TRS - { - Translation = modelRoot.worldToLocalMatrix.MultiplyPoint(world.Translation), - Rotation = world.Rotation * Quaternion.Inverse(ModelRoot.rotation), - }; + ModelInitial = new TR(world.Rotation * Quaternion.Inverse(ModelRoot.rotation), modelRoot.worldToLocalMatrix.MultiplyPoint(world.Translation)); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/TR.cs b/Assets/VRM10/Runtime/Components/Constraint/TR.cs new file mode 100644 index 000000000..d914f0e2f --- /dev/null +++ b/Assets/VRM10/Runtime/Components/Constraint/TR.cs @@ -0,0 +1,36 @@ +using UnityEngine; + +namespace UniVRM10 +{ + public struct TR + { + public Quaternion Rotation; + public Vector3 Translation; + + public static TR Identity => new TR(Quaternion.identity, Vector3.zero); + + public static TR FromWorld(Transform t) => new TR(t.rotation, t.position); + + public static TR FromLocal(Transform t) => new TR(t.localRotation, t.localPosition); + + public static TR FromRelative(Transform t, Transform from) + { + var toRelative = from.worldToLocalMatrix; + return new TR(toRelative.rotation * t.rotation, toRelative.MultiplyPoint(t.position)); + } + + public TR(Quaternion r, Vector3 t) + { + Rotation = r; + Translation = t; + } + + public TR(Quaternion r) : this(r, Vector3.zero) + { + } + + public Matrix4x4 TRS(float s) => Matrix4x4.TRS(Translation, Rotation, new Vector3(s, s, s)); + + public static TR operator *(TR a, TR b) => new TR(a.Rotation * b.Rotation, b.Rotation * a.Translation + b.Translation); + } +} diff --git a/Assets/VRM10/Runtime/Components/Constraint/TRS.cs.meta b/Assets/VRM10/Runtime/Components/Constraint/TR.cs.meta similarity index 83% rename from Assets/VRM10/Runtime/Components/Constraint/TRS.cs.meta rename to Assets/VRM10/Runtime/Components/Constraint/TR.cs.meta index 8d5e46b4b..b5f7a2033 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/TRS.cs.meta +++ b/Assets/VRM10/Runtime/Components/Constraint/TR.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: b00df4c7268fefe42a16416935bbceb5 +guid: 6ddd45ffb38279b488938471919fc089 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VRM10/Runtime/Components/Constraint/TRS.cs b/Assets/VRM10/Runtime/Components/Constraint/TRS.cs deleted file mode 100644 index eb5600d53..000000000 --- a/Assets/VRM10/Runtime/Components/Constraint/TRS.cs +++ /dev/null @@ -1,43 +0,0 @@ -using UnityEngine; - -namespace UniVRM10 -{ - struct TRS - { - public Vector3 Translation; - public Quaternion Rotation; - public Vector3 Scale; - - public Matrix4x4 Matrix => Matrix4x4.TRS(Translation, Rotation, Scale); - - public static TRS GetWorld(Transform t) - { - return new TRS - { - Translation = t.position, - Rotation = t.rotation, - Scale = t.lossyScale, - }; - } - - public static TRS GetLocal(Transform t) - { - return new TRS - { - Translation = t.localPosition, - Rotation = t.localRotation, - Scale = t.localScale, - }; - } - - public static TRS GetRelative(Transform t, Matrix4x4 toRelative) - { - return new TRS - { - Translation = toRelative.MultiplyPoint(t.position), - Rotation = toRelative.rotation * t.rotation, - Scale = toRelative.MultiplyVector(t.lossyScale), - }; - } - } -} diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index 6aea64770..115c563b7 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -39,48 +39,51 @@ namespace UniVRM10 /// Model の座標系 /// /// - public Matrix4x4 GetSourceModelCoords() + public TR GetSourceModelCoords() { - Matrix4x4 m = Matrix4x4.identity; + var r = Quaternion.identity; if (Source != null) { if (ModelRoot != null) { - m = Matrix4x4.Rotate(ModelRoot.rotation); + r = ModelRoot.rotation; } } + + var t = Vector3.zero; if (Source != null) { - m *= Matrix4x4.Translate(Source.position); + t = Source.position; } - return m; + + return new TR(r, t); } /// /// Local の座標系。つまり親座標系 /// /// - public Matrix4x4 GetSourceLocalCoords() + public TR GetSourceLocalCoords() { if (Source != null) { if (m_src != null) { // runtime - var parent = Matrix4x4.identity; + var parent = TR.Identity; if (Source.parent != null) { - parent = Source.parent.localToWorldMatrix; + parent = TR.FromWorld(Source.parent); } - return parent * m_src.LocalInitial.Matrix; + return parent * m_src.LocalInitial; } else { - return Source.localToWorldMatrix; + return TR.FromWorld(Source); } } - return Matrix4x4.identity; + return TR.Identity; } public Quaternion Delta @@ -90,30 +93,30 @@ namespace UniVRM10 } ConstraintDestination m_dst; - public Matrix4x4 GetDstModelCoords() + public TR GetDstModelCoords() { - Matrix4x4 m = Matrix4x4.identity; + var r = Quaternion.identity; if (ModelRoot != null) { - m = Matrix4x4.Rotate(ModelRoot.rotation); + r = ModelRoot.rotation; } - return m * Matrix4x4.Translate(transform.position); + return new TR(r, transform.position); } - public Matrix4x4 GetDstLocalCoords() + public TR GetDstLocalCoords() { if (m_src != null) { // runtime - var parent = Matrix4x4.identity; + var parent = TR.Identity; if (transform.parent != null) { - parent = transform.parent.localToWorldMatrix; + parent = TR.FromWorld(transform.parent); } - return parent * m_dst.LocalInitial.Matrix; + return parent * m_dst.LocalInitial; } else { - return transform.localToWorldMatrix; + return TR.FromWorld(transform); } } From 22b3613df98fe7c887bb28d12ada99b6ee7c7cdc Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 10 May 2021 19:59:25 +0900 Subject: [PATCH 10/37] refactoring --- .../VRM10RotationConstraintEditor.cs | 65 ++------ .../Constraint/VRM10RotationConstraint.cs | 141 ++++++++++-------- 2 files changed, 88 insertions(+), 118 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index e2beac9ca..08d430ba5 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -17,7 +17,7 @@ namespace UniVRM10 } #region SRC - void SrcDrawCurrent() + void DrawSourceCurrent() { var s = m_target.transform.lossyScale.x; @@ -28,25 +28,15 @@ namespace UniVRM10 Handles.DrawWireCube(Vector3.zero, new Vector3(size, size, size)); } - void SrcDrawModelCoords() + void DrawSourceCoords() { var s = m_target.transform.lossyScale.x; - - // init - m_target.GetSourceModelCoords().Draw(0.2f / s); - } - - void SrcDrawLocalCoords() - { - var s = m_target.transform.lossyScale.x; - - // init - m_target.GetSourceLocalCoords().Draw(0.2f / s); + m_target.GetSourceCoords().Draw(0.2f / s); } #endregion #region Dst - void DstDrawCurrent() + void DrawDstCurrent() { var s = m_target.transform.lossyScale.x; @@ -57,20 +47,10 @@ namespace UniVRM10 Handles.DrawWireCube(Vector3.zero, new Vector3(size, size, size)); } - void DstDrawModelCoords() + void DrawDstCoords() { var s = m_target.transform.lossyScale.x; - - // init - m_target.GetDstModelCoords().Draw(0.2f / s); - } - - void DstDrawLocalCoords() - { - var s = m_target.transform.lossyScale.x; - - // init - m_target.GetDstLocalCoords().Draw(0.2f / s); + m_target.GetDstCoords().Draw(0.2f / s); } #endregion @@ -134,35 +114,10 @@ namespace UniVRM10 Handles.Label(m_target.transform.position, sb.ToString(), s_style); } - switch (m_target.SourceCoordinate) - { - case ObjectSpace.model: - SrcDrawModelCoords(); - break; - - case ObjectSpace.local: - SrcDrawLocalCoords(); - break; - - default: - throw new NotImplementedException(); - } - SrcDrawCurrent(); - - switch (m_target.DestinationCoordinate) - { - case ObjectSpace.model: - DstDrawModelCoords(); - break; - - case ObjectSpace.local: - DstDrawLocalCoords(); - break; - - default: - throw new NotImplementedException(); - } - DstDrawCurrent(); + DrawSourceCoords(); + DrawSourceCurrent(); + DrawDstCoords(); + DrawDstCurrent(); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index 115c563b7..14ab8a139 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -1,4 +1,5 @@ -using UniGLTF.Extensions.VRMC_node_constraint; +using System; +using UniGLTF.Extensions.VRMC_node_constraint; using UnityEngine; @@ -36,54 +37,59 @@ namespace UniVRM10 ConstraintSource m_src; /// - /// Model の座標系 + /// Source の座標系 /// /// - public TR GetSourceModelCoords() + public TR GetSourceCoords() { - var r = Quaternion.identity; - if (Source != null) + switch (SourceCoordinate) { - if (ModelRoot != null) - { - r = ModelRoot.rotation; - } - } - - var t = Vector3.zero; - if (Source != null) - { - t = Source.position; - } - - return new TR(r, t); - } - - /// - /// Local の座標系。つまり親座標系 - /// - /// - public TR GetSourceLocalCoords() - { - if (Source != null) - { - if (m_src != null) - { - // runtime - var parent = TR.Identity; - if (Source.parent != null) + case ObjectSpace.model: { - parent = TR.FromWorld(Source.parent); + + var r = Quaternion.identity; + if (Source != null) + { + if (ModelRoot != null) + { + r = ModelRoot.rotation; + } + } + + var t = Vector3.zero; + if (Source != null) + { + t = Source.position; + } + + return new TR(r, t); + } + + case ObjectSpace.local: + { + if (Source != null) + { + if (m_src != null) + { + // runtime + var parent = TR.Identity; + if (Source.parent != null) + { + parent = TR.FromWorld(Source.parent); + } + return parent * m_src.LocalInitial; + } + else + { + return TR.FromWorld(Source); + } + } + + return TR.Identity; } - return parent * m_src.LocalInitial; - } - else - { - return TR.FromWorld(Source); - } } - return TR.Identity; + throw new NotImplementedException(); } public Quaternion Delta @@ -93,31 +99,40 @@ namespace UniVRM10 } ConstraintDestination m_dst; - public TR GetDstModelCoords() + public TR GetDstCoords() { - var r = Quaternion.identity; - if (ModelRoot != null) + switch (DestinationCoordinate) { - r = ModelRoot.rotation; - } - return new TR(r, transform.position); - } - public TR GetDstLocalCoords() - { - if (m_src != null) - { - // runtime - var parent = TR.Identity; - if (transform.parent != null) - { - parent = TR.FromWorld(transform.parent); - } - return parent * m_dst.LocalInitial; - } - else - { - return TR.FromWorld(transform); + case ObjectSpace.model: + { + var r = Quaternion.identity; + if (ModelRoot != null) + { + r = ModelRoot.rotation; + } + return new TR(r, transform.position); + } + + case ObjectSpace.local: + { + if (m_src != null) + { + // runtime + var parent = TR.Identity; + if (transform.parent != null) + { + parent = TR.FromWorld(transform.parent); + } + return parent * m_dst.LocalInitial; + } + else + { + return TR.FromWorld(transform); + } + } } + + throw new NotImplementedException(); } /// From c806dc61864981313647df4621b237808f2da281 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 10 May 2021 20:18:32 +0900 Subject: [PATCH 11/37] GetSourceCurrent --- .../VRM10RotationConstraintEditor.cs | 7 +-- .../Components/Constraint/ConstraintSource.cs | 3 +- .../Constraint/VRM10RotationConstraint.cs | 55 +++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index 08d430ba5..a937e80e9 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -20,12 +20,9 @@ namespace UniVRM10 void DrawSourceCurrent() { var s = m_target.transform.lossyScale.x; - - // current - Handles.matrix = m_target.Source.localToWorldMatrix; + Handles.matrix = m_target.GetSourceCurrent().TRS(0.05f * s); Handles.color = Color.yellow; - var size = 0.05f / s; - Handles.DrawWireCube(Vector3.zero, new Vector3(size, size, size)); + Handles.DrawWireCube(Vector3.zero, Vector3.one); } void DrawSourceCoords() diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs index 43d684393..0c0f3389d 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs @@ -34,10 +34,9 @@ namespace UniVRM10 { switch (coords) { - // 右からかけるか、左からかけるか、それが問題なのだ // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(LocalInitial.Rotation); - case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelRoot.rotation) * Quaternion.Inverse(ModelInitial.Rotation); + case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelInitial.Rotation) * Quaternion.Inverse(ModelRoot.rotation); default: throw new NotImplementedException(); } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index 14ab8a139..da13a5ea2 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -92,6 +92,61 @@ namespace UniVRM10 throw new NotImplementedException(); } + public TR GetSourceCurrent() + { + switch (SourceCoordinate) + { + case ObjectSpace.model: + { + var r = Quaternion.identity; + if (Source != null) + { + if (ModelRoot != null) + { + r = ModelRoot.rotation; + if (m_src != null) + { + r *= m_src.RotationDelta(ObjectSpace.model); + } + } + } + + var t = Vector3.zero; + if (Source != null) + { + t = Source.position; + } + + return new TR(r, t); + } + + case ObjectSpace.local: + { + if (Source != null) + { + if (m_src != null) + { + // runtime + var parent = TR.Identity; + if (Source.parent != null) + { + parent = TR.FromWorld(Source.parent); + } + return parent * m_src.LocalInitial; + } + else + { + return TR.FromWorld(Source); + } + } + + return TR.Identity; + } + } + + throw new NotImplementedException(); + } + public Quaternion Delta { get; From 46add881124e04e7ef597c7d483ccb52b18fda38 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 10 May 2021 20:31:46 +0900 Subject: [PATCH 12/37] fix GetSourceCurrent local --- .../Components/Constraint/VRM10RotationConstraint.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index da13a5ea2..b3cb56c36 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -127,12 +127,14 @@ namespace UniVRM10 if (m_src != null) { // runtime - var parent = TR.Identity; + var parent = Quaternion.identity; if (Source.parent != null) { - parent = TR.FromWorld(Source.parent); + parent = Source.parent.rotation; } - return parent * m_src.LocalInitial; + var delta = m_src.RotationDelta(ObjectSpace.local); + + return new TR(parent * m_src.LocalInitial.Rotation * delta, Source.position); } else { @@ -178,7 +180,7 @@ namespace UniVRM10 { parent = TR.FromWorld(transform.parent); } - return parent * m_dst.LocalInitial; + return m_dst.LocalInitial * parent; } else { From b3dd5dc1f3400797a4c1747915dbe057bc8def5c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 10 May 2021 20:59:52 +0900 Subject: [PATCH 13/37] fix operator* --- .../VRM10RotationConstraintEditor.cs | 8 +- .../VRM10/Runtime/Components/Constraint/TR.cs | 4 +- .../Constraint/VRM10RotationConstraint.cs | 105 +++++++++--------- 3 files changed, 57 insertions(+), 60 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index a937e80e9..317ceddda 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -1,6 +1,5 @@ using System; using System.Text; -using UniGLTF.Extensions.VRMC_node_constraint; using UnityEditor; using UnityEngine; @@ -36,12 +35,9 @@ namespace UniVRM10 void DrawDstCurrent() { var s = m_target.transform.lossyScale.x; - - // current - Handles.matrix = m_target.transform.localToWorldMatrix; + Handles.matrix = m_target.GetDstCurrent().TRS(0.05f * s); Handles.color = Color.yellow; - var size = 0.05f / s; - Handles.DrawWireCube(Vector3.zero, new Vector3(size, size, size)); + Handles.DrawWireCube(Vector3.zero, Vector3.one); } void DrawDstCoords() diff --git a/Assets/VRM10/Runtime/Components/Constraint/TR.cs b/Assets/VRM10/Runtime/Components/Constraint/TR.cs index d914f0e2f..c5c968c91 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/TR.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/TR.cs @@ -18,7 +18,7 @@ namespace UniVRM10 var toRelative = from.worldToLocalMatrix; return new TR(toRelative.rotation * t.rotation, toRelative.MultiplyPoint(t.position)); } - + public TR(Quaternion r, Vector3 t) { Rotation = r; @@ -31,6 +31,6 @@ namespace UniVRM10 public Matrix4x4 TRS(float s) => Matrix4x4.TRS(Translation, Rotation, new Vector3(s, s, s)); - public static TR operator *(TR a, TR b) => new TR(a.Rotation * b.Rotation, b.Rotation * a.Translation + b.Translation); + public static TR operator *(TR a, TR b) => new TR(a.Rotation * b.Rotation, a.Rotation * b.Translation + a.Translation); } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index b3cb56c36..24c80d31d 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -94,59 +94,15 @@ namespace UniVRM10 public TR GetSourceCurrent() { - switch (SourceCoordinate) + var coords = GetSourceCoords(); + if (m_src != null) { - case ObjectSpace.model: - { - var r = Quaternion.identity; - if (Source != null) - { - if (ModelRoot != null) - { - r = ModelRoot.rotation; - if (m_src != null) - { - r *= m_src.RotationDelta(ObjectSpace.model); - } - } - } - - var t = Vector3.zero; - if (Source != null) - { - t = Source.position; - } - - return new TR(r, t); - } - - case ObjectSpace.local: - { - if (Source != null) - { - if (m_src != null) - { - // runtime - var parent = Quaternion.identity; - if (Source.parent != null) - { - parent = Source.parent.rotation; - } - var delta = m_src.RotationDelta(ObjectSpace.local); - - return new TR(parent * m_src.LocalInitial.Rotation * delta, Source.position); - } - else - { - return TR.FromWorld(Source); - } - } - - return TR.Identity; - } + return coords * new TR(m_src.RotationDelta(SourceCoordinate)); + } + else + { + return coords; } - - throw new NotImplementedException(); } public Quaternion Delta @@ -180,7 +136,7 @@ namespace UniVRM10 { parent = TR.FromWorld(transform.parent); } - return m_dst.LocalInitial * parent; + return parent * m_dst.LocalInitial; } else { @@ -192,6 +148,51 @@ namespace UniVRM10 throw new NotImplementedException(); } + public TR GetDstCurrent() + { + var coords = GetDstCoords(); + if (m_src != null) + { + return coords * new TR(m_src.RotationDelta(SourceCoordinate)); + } + else + { + return coords; + } + // switch (DestinationCoordinate) + // { + // case ObjectSpace.model: + // { + // var r = Quaternion.identity; + // if (ModelRoot != null) + // { + // r = ModelRoot.rotation; + // } + // return new TR(r, transform.position); + // } + + // case ObjectSpace.local: + // { + // if (m_src != null) + // { + // // runtime + // var parent = TR.Identity; + // if (transform.parent != null) + // { + // parent = TR.FromWorld(transform.parent); + // } + // return parent * m_dst.LocalInitial; + // } + // else + // { + // return TR.FromWorld(transform); + // } + // } + // } + + // throw new NotImplementedException(); + } + /// /// Editorで設定値の変更を反映するために、クリアする /// From 2469d65cb29e7528ada8cedf5da0daf1ac71dcd3 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 13 May 2021 14:41:12 +0900 Subject: [PATCH 14/37] IVRM10SourceDestination --- ...10ConstraintSourceDestinationExtensions.cs | 59 +++++++++++++++ ...straintSourceDestinationExtensions.cs.meta | 11 +++ .../VRM10RotationConstraintEditor.cs | 74 ++++++------------- .../Constraint/IVRM10SourceDestination.cs | 11 +++ .../IVRM10SourceDestination.cs.meta | 11 +++ .../Constraint/VRM10RotationConstraint.cs | 6 +- 6 files changed, 117 insertions(+), 55 deletions(-) create mode 100644 Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs create mode 100644 Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs.meta create mode 100644 Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs create mode 100644 Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs.meta diff --git a/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs b/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs new file mode 100644 index 000000000..38e3c6a0b --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs @@ -0,0 +1,59 @@ +using UnityEditor; +using UnityEngine; + +namespace UniVRM10 +{ + public static class IVRM10ConstraintSourceDestinationExtensions + { + public static void DrawSourceCoords(this IVRM10ConstraintSourceDestination self) + { + try + { + self.GetSourceCoords().Draw(0.2f); + } + catch (ConstraintException) + { + + } + } + public static void DrawSourceCurrent(this IVRM10ConstraintSourceDestination self) + { + try + { + Handles.matrix = self.GetSourceCurrent().TRS(0.05f); + Handles.color = Color.yellow; + Handles.DrawWireCube(Vector3.zero, Vector3.one); + } + catch (ConstraintException) + { + + } + } + + public static void DrawDstCoords(this IVRM10ConstraintSourceDestination self) + { + try + { + self.GetDstCoords().Draw(0.2f); + } + catch (ConstraintException) + { + + } + } + + public static void DrawDstCurrent(this IVRM10ConstraintSourceDestination self) + { + try + { + Handles.matrix = self.GetDstCurrent().TRS(0.05f); + Handles.color = Color.yellow; + Handles.DrawWireCube(Vector3.zero, Vector3.one); + } + catch (ConstraintException) + { + + } + } + } +} diff --git a/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs.meta b/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs.meta new file mode 100644 index 000000000..a2836e116 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 59e1ae1f9d4fb9a459cd2c97a4351b1f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index 317ceddda..c0503738c 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -1,4 +1,3 @@ -using System; using System.Text; using UnityEditor; using UnityEngine; @@ -15,39 +14,18 @@ namespace UniVRM10 m_target = (VRM10RotationConstraint)target; } - #region SRC - void DrawSourceCurrent() - { - var s = m_target.transform.lossyScale.x; - Handles.matrix = m_target.GetSourceCurrent().TRS(0.05f * s); - Handles.color = Color.yellow; - Handles.DrawWireCube(Vector3.zero, Vector3.one); - } - - void DrawSourceCoords() - { - var s = m_target.transform.lossyScale.x; - m_target.GetSourceCoords().Draw(0.2f / s); - } - #endregion - - #region Dst - void DrawDstCurrent() - { - var s = m_target.transform.lossyScale.x; - Handles.matrix = m_target.GetDstCurrent().TRS(0.05f * s); - Handles.color = Color.yellow; - Handles.DrawWireCube(Vector3.zero, Vector3.one); - } - - void DrawDstCoords() - { - var s = m_target.transform.lossyScale.x; - m_target.GetDstCoords().Draw(0.2f / s); - } - #endregion - static GUIStyle s_style; + static GUIStyle Style + { + get + { + if (s_style == null) + { + s_style = new GUIStyle("box"); + } + return s_style; + } + } /// /// Euler各を +- 180 にクランプする @@ -74,16 +52,12 @@ namespace UniVRM10 { return; } - if (s_style == null) - { - s_style = new GUIStyle("box"); - } // this to target line Handles.color = Color.yellow; Handles.DrawLine(m_target.Source.position, m_target.transform.position); - var euler = Clamp180(m_target.Delta.eulerAngles); + var delta = Clamp180(m_target.Delta.eulerAngles); // show source { @@ -91,26 +65,26 @@ namespace UniVRM10 sb.AppendLine(); sb.AppendLine(); sb.AppendLine($"source: {m_target.SourceCoordinate}"); - sb.AppendLine($"{euler.x:0.}"); - sb.AppendLine($"{euler.y:0.}"); - sb.Append($"{euler.z:0.}"); - Handles.Label(m_target.Source.position, sb.ToString(), s_style); + sb.AppendLine($"{delta.x:0.}"); + sb.AppendLine($"{delta.y:0.}"); + sb.Append($"{delta.z:0.}"); + Handles.Label(m_target.Source.position, sb.ToString(), Style); } // show dst { var sb = new StringBuilder(); sb.AppendLine($"constraint: {m_target.DestinationCoordinate}"); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{euler.x:0.}"); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{euler.y:0.}"); - sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{euler.z:0.}"); - Handles.Label(m_target.transform.position, sb.ToString(), s_style); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.}"); + sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.}"); + Handles.Label(m_target.transform.position, sb.ToString(), Style); } - DrawSourceCoords(); - DrawSourceCurrent(); - DrawDstCoords(); - DrawDstCurrent(); + m_target.DrawSourceCoords(); + m_target.DrawSourceCurrent(); + m_target.DrawDstCoords(); + m_target.DrawDstCurrent(); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs b/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs new file mode 100644 index 000000000..d6aa2ca89 --- /dev/null +++ b/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs @@ -0,0 +1,11 @@ +namespace UniVRM10 +{ + public interface IVRM10ConstraintSourceDestination + { + TR GetSourceCoords(); + TR GetSourceCurrent(); + + TR GetDstCoords(); + TR GetDstCurrent(); + } +} diff --git a/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs.meta b/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs.meta new file mode 100644 index 000000000..ff49caec8 --- /dev/null +++ b/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4d1ea0f0f4d95a2499f9aa5a1d7a2235 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index 24c80d31d..635ff3782 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -9,7 +9,7 @@ namespace UniVRM10 /// 対象の初期回転と現在回転の差分(delta)を、自身の初期回転と自身の初期回転にdeltaを乗算したものに対してWeightでSlerpする。 /// [DisallowMultipleComponent] - public class VRM10RotationConstraint : VRM10Constraint + public class VRM10RotationConstraint : VRM10Constraint, IVRM10ConstraintSourceDestination { [SerializeField] public Transform Source = default; @@ -36,10 +36,6 @@ namespace UniVRM10 ConstraintSource m_src; - /// - /// Source の座標系 - /// - /// public TR GetSourceCoords() { switch (SourceCoordinate) From 61920922d4aef2669aec82ef54ab673ae9243c16 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 13 May 2021 15:05:43 +0900 Subject: [PATCH 15/37] VRM10PositionConstraintEditor --- .../VRM10PositionConstraintEditor.cs | 71 +++++++++++ .../VRM10PositionConstraintEditor.cs.meta | 11 ++ .../VRM10/Runtime/Components/Constraint/TR.cs | 4 + .../Constraint/VRM10PositionConstraint.cs | 117 +++++++++++++++++- 4 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs create mode 100644 Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs.meta diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs new file mode 100644 index 000000000..897497304 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs @@ -0,0 +1,71 @@ +using System.Text; +using UnityEditor; +using UnityEngine; + +namespace UniVRM10 +{ + [CustomEditor(typeof(VRM10PositionConstraint))] + public class VRM10PositionConstraintEditor : Editor + { + VRM10PositionConstraint m_target; + + void OnEnable() + { + m_target = (VRM10PositionConstraint)target; + } + + static GUIStyle s_style; + static GUIStyle Style + { + get + { + if (s_style == null) + { + s_style = new GUIStyle("box"); + } + return s_style; + } + } + + public void OnSceneGUI() + { + if (m_target.Source == null) + { + return; + } + + // this to target line + Handles.color = Color.yellow; + Handles.DrawLine(m_target.Source.position, m_target.transform.position); + + var delta = m_target.Delta; + + // show source + { + var sb = new StringBuilder(); + sb.AppendLine(); + sb.AppendLine(); + sb.AppendLine($"source: {m_target.SourceCoordinate}"); + sb.AppendLine($"{delta.x:0.00}"); + sb.AppendLine($"{delta.y:0.00}"); + sb.Append($"{delta.z:0.00}"); + Handles.Label(m_target.Source.position, sb.ToString(), Style); + } + + // show dst + { + var sb = new StringBuilder(); + sb.AppendLine($"constraint: {m_target.DestinationCoordinate}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.}"); + sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.}"); + Handles.Label(m_target.transform.position, sb.ToString(), Style); + } + + m_target.DrawSourceCoords(); + m_target.DrawSourceCurrent(); + m_target.DrawDstCoords(); + m_target.DrawDstCurrent(); + } + } +} diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs.meta b/Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs.meta new file mode 100644 index 000000000..7abff9c34 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d4c6d6f5f37e41e47bd02bdf1dbda864 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/Components/Constraint/TR.cs b/Assets/VRM10/Runtime/Components/Constraint/TR.cs index c5c968c91..14466898f 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/TR.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/TR.cs @@ -29,6 +29,10 @@ namespace UniVRM10 { } + public TR(Vector3 t) : this(Quaternion.identity, t) + { + } + public Matrix4x4 TRS(float s) => Matrix4x4.TRS(Translation, Rotation, new Vector3(s, s, s)); public static TR operator *(TR a, TR b) => new TR(a.Rotation * b.Rotation, a.Rotation * b.Translation + a.Translation); diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs index e69466a26..cf8bfa5a3 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs @@ -1,4 +1,5 @@ -using UniGLTF.Extensions.VRMC_node_constraint; +using System; +using UniGLTF.Extensions.VRMC_node_constraint; using UnityEngine; namespace UniVRM10 @@ -7,7 +8,7 @@ namespace UniVRM10 /// 対象の初期位置と現在位置の差分(delta)を、自身の初期位置に対してWeightを乗算して加算する。 /// [DisallowMultipleComponent] - public class VRM10PositionConstraint : VRM10Constraint + public class VRM10PositionConstraint : VRM10Constraint, IVRM10ConstraintSourceDestination { [SerializeField] public Transform Source = default; @@ -30,8 +31,116 @@ namespace UniVRM10 ConstraintSource m_src; + public TR GetSourceCoords() + { + if (Source == null) + { + throw new ConstraintException(ConstraintException.ExceptionTypes.NoSource); + } + + switch (SourceCoordinate) + { + case ObjectSpace.model: + { + if (ModelRoot == null) + { + throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); + } + + if (m_src == null) + { + return new TR(ModelRoot.rotation, Source.position); + } + + // runtime + return new TR(ModelRoot.rotation, m_src.ModelInitial.Translation); + } + + case ObjectSpace.local: + { + if (m_src == null) + { + return TR.FromWorld(Source); + } + + // runtime + var parent = TR.Identity; + if (Source.parent != null) + { + parent = TR.FromWorld(Source.parent); + } + return parent * m_src.LocalInitial; + } + + default: + throw new NotImplementedException(); + } + } + + public TR GetSourceCurrent() + { + var coords = GetSourceCoords(); + if (m_src == null) + { + return coords; + } + + return new TR(Delta) * coords; + } + + public Vector3 Delta + { + get; + private set; + } + ConstraintDestination m_dst; + public TR GetDstCoords() + { + switch (DestinationCoordinate) + { + case ObjectSpace.model: + { + if (ModelRoot == null) + { + throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); + } + return new TR(ModelRoot.rotation, transform.position); + } + + case ObjectSpace.local: + { + if (m_src == null) + { + return TR.FromWorld(transform); + } + + // runtime + var parent = TR.Identity; + if (transform.parent != null) + { + parent = TR.FromWorld(transform.parent); + } + return parent * m_dst.LocalInitial; + } + + default: + throw new NotImplementedException(); + } + } + + public TR GetDstCurrent() + { + var coords = GetDstCoords(); + if (m_src == null) + { + return coords; + } + + return new TR(Delta) * coords; + } + /// /// Editorで設定値の変更を反映するために、クリアする /// @@ -75,8 +184,8 @@ namespace UniVRM10 m_dst = new ConstraintDestination(transform, ModelRoot); } - var delta = FreezeAxes.Freeze(m_src.TranslationDelta(SourceCoordinate)); - m_dst.ApplyTranslation(delta, Weight, DestinationCoordinate, ModelRoot); + Delta = FreezeAxes.Freeze(m_src.TranslationDelta(SourceCoordinate)); + m_dst.ApplyTranslation(Delta, Weight, DestinationCoordinate, ModelRoot); } } } From 720bb3ed590114ab4721e7dd095c585e51e690a5 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 13 May 2021 16:24:39 +0900 Subject: [PATCH 16/37] WIP aim --- .../Constraint/VRM10AimConstraintEditor.cs | 121 ++++++++++++++++++ .../VRM10AimConstraintEditor.cs.meta | 11 ++ .../Constraint/VRM10AimConstraint.cs | 76 +++-------- Assets/VRM10/Runtime/IO/Vrm10Exporter.cs | 4 +- Assets/VRM10/Runtime/IO/Vrm10Importer.cs | 4 +- 5 files changed, 153 insertions(+), 63 deletions(-) create mode 100644 Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs create mode 100644 Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs.meta diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs new file mode 100644 index 000000000..fb9cee2f2 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs @@ -0,0 +1,121 @@ +using System.Text; +using UnityEditor; +using UnityEngine; + +namespace UniVRM10 +{ + [CustomEditor(typeof(VRM10AimConstraint))] + public class VRM10AimConstraintEditor : Editor + { + VRM10AimConstraint m_target; + + void OnEnable() + { + m_target = (VRM10AimConstraint)target; + } + + static GUIStyle s_style; + static GUIStyle Style + { + get + { + if (s_style == null) + { + s_style = new GUIStyle("box"); + } + return s_style; + } + } + + public void OnSceneGUI() + { + if (m_target.Source == null) + { + return; + } + + // this to target line + Handles.color = Color.yellow; + Handles.DrawLine(m_target.Source.position, m_target.transform.position); + + TR.FromWorld(m_target.transform).Draw(0.2f); + + Handles.matrix = Matrix4x4.identity; + EditorGUI.BeginChangeCheck(); + var pr = m_target.ParentRotation; + var pos = m_target.transform.position; + var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos); + if (EditorGUI.EndChangeCheck()) + { + Undo.RecordObject(m_target, "Rotated RotateAt Point"); + m_target.DestinationOffset = Quaternion.Inverse(pr) * rot; + } + + // aim + Handles.color = Color.yellow; + var aim = pos + rot * Vector3.forward * 0.3f; + Handles.DrawLine(pos, aim); + Handles.Label(aim, "aim"); + // up + var up = pos + rot * Vector3.up * 0.3f; + Handles.DrawLine(pos, up); + Handles.Label(up, "up"); + + // var delta = Clamp180(m_target.Delta.eulerAngles); + + // // show source + // { + // var sb = new StringBuilder(); + // sb.AppendLine(); + // sb.AppendLine(); + // sb.AppendLine($"source: {m_target.SourceCoordinate}"); + // sb.AppendLine($"{delta.x:0.}"); + // sb.AppendLine($"{delta.y:0.}"); + // sb.Append($"{delta.z:0.}"); + // Handles.Label(m_target.Source.position, sb.ToString(), Style); + // } + + // // show dst + // { + // var sb = new StringBuilder(); + // sb.AppendLine($"constraint: {m_target.DestinationCoordinate}"); + // sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.}"); + // sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.}"); + // sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.}"); + // Handles.Label(m_target.transform.position, sb.ToString(), Style); + // } + } + // void OnDrawGizmos() + // { + // if (Source == null) + // { + // return; + // } + + // var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position); + // var (yaw, yaw_, pitch, pitch_) = CalcYawPitch(m_coords, localPosition); + // switch (yaw_) + // { + // case 0: break; + // case 1: yaw = 180 - yaw; break; + // case 2: yaw = 180 + yaw; break; + // case 3: yaw = 360 - yaw; break; + // } + // switch (pitch_) + // { + // case 0: pitch = -pitch; break; + // case 1: pitch = -pitch; break; + // case 2: break; + // case 3: break; + // } + // // Debug.Log($"{yaw}({yaw_}), {pitch}({pitch_})"); + // // var rot = Quaternion.Euler(pitch, yaw, 0); + // var rot = Quaternion.AngleAxis(yaw, Vector3.up) * Quaternion.AngleAxis(pitch, Vector3.right); + // var p = rot * Vector3.forward; + + // Gizmos.matrix = transform.localToWorldMatrix; + // Gizmos.DrawLine(Vector3.zero, p * 5); + // } + + } +} diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs.meta b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs.meta new file mode 100644 index 000000000..324632787 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 53c7ae04ba8deea40acc6439a2fe11cd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs index eba3d3f5b..489a3ecf3 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs @@ -20,43 +20,33 @@ namespace UniVRM10 [Range(0, 10.0f)] public float Weight = 1.0f; - /// - /// Forward - /// [SerializeField] - public Vector3 AimVector = Vector3.forward; + public Quaternion DestinationOffset = Quaternion.identity; - [SerializeField] - public Vector3 UpVector = Vector3.up; + public Quaternion ParentRotation => transform.parent == null ? Quaternion.identity : transform.parent.rotation; - [SerializeField] - public Vector3 RightVector; - - Quaternion m_selfInitial; - Matrix4x4 m_coords; + public class AimLogic + { + public readonly Quaternion InitialLocalRotation; + public AimLogic(Quaternion initialLocalRotation) + { + InitialLocalRotation = initialLocalRotation; + } + } + AimLogic m_runtime; void Start() { if (Source == null) { + enabled = false; return; } - m_selfInitial = transform.rotation; - - // 正規直交座標を作る - // Y x Z => X - AimVector.Normalize(); - UpVector.Normalize(); - RightVector = Vector3.Cross(UpVector, AimVector).normalized; - // 直交するように再計算 - UpVector = Vector3.Cross(AimVector, RightVector).normalized; - m_coords = new Matrix4x4( - new Vector4(RightVector.x, RightVector.y, RightVector.z, 0), - new Vector4(UpVector.x, UpVector.y, UpVector.z, 0), - new Vector4(AimVector.x, AimVector.y, AimVector.z, 0), - new Vector4(0, 0, 0, 1) - ); + if (m_runtime == null) + { + m_runtime = new AimLogic(DestinationOffset); + } } static (float, int, float, int) CalcYawPitch(Matrix4x4 m, Vector3 target) @@ -135,40 +125,8 @@ namespace UniVRM10 return; } - var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position); + // var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position); // var (yaw, pitch) = CalcYawPitch(m_coords, localPosition); } - - void OnDrawGizmos() - { - if (Source == null) - { - return; - } - - var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position); - var (yaw, yaw_, pitch, pitch_) = CalcYawPitch(m_coords, localPosition); - switch (yaw_) - { - case 0: break; - case 1: yaw = 180 - yaw; break; - case 2: yaw = 180 + yaw; break; - case 3: yaw = 360 - yaw; break; - } - switch (pitch_) - { - case 0: pitch = -pitch; break; - case 1: pitch = -pitch; break; - case 2: break; - case 3: break; - } - // Debug.Log($"{yaw}({yaw_}), {pitch}({pitch_})"); - // var rot = Quaternion.Euler(pitch, yaw, 0); - var rot = Quaternion.AngleAxis(yaw, Vector3.up) * Quaternion.AngleAxis(pitch, Vector3.right); - var p = rot * Vector3.forward; - - Gizmos.matrix = transform.localToWorldMatrix; - Gizmos.DrawLine(Vector3.zero, p * 5); - } } } diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs index da0c5b722..eda93c2f9 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs @@ -444,8 +444,8 @@ namespace UniVRM10 Aim = new UniGLTF.Extensions.VRMC_node_constraint.AimConstraint { Source = model.Nodes.IndexOf(converter.Nodes[c.Source.gameObject]), - AimVector = ReverseX(c.AimVector), - UpVector = ReverseX(c.UpVector), + // AimVector = ReverseX(c.AimVector), + // UpVector = ReverseX(c.UpVector), Weight = c.Weight, }, }, diff --git a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs index 53768eaba..b90ec6a4f 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs @@ -501,8 +501,8 @@ namespace UniVRM10 var a = constraint.Aim; var aimConstraint = node.gameObject.AddComponent(); aimConstraint.Source = Nodes[a.Source.Value]; - aimConstraint.AimVector = Vector3InvertX(a.AimVector); - aimConstraint.UpVector = Vector3InvertX(a.UpVector); + // aimConstraint.AimVector = Vector3InvertX(a.AimVector); + // aimConstraint.UpVector = Vector3InvertX(a.UpVector); } } } From 673aac40e3558cd9e5f9657ea54dc7e8b96fbe26 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 13 May 2021 17:44:35 +0900 Subject: [PATCH 17/37] DrawAimUp --- .../Constraint/VRM10AimConstraintEditor.cs | 109 ++++++------------ .../Constraint/VRM10AimConstraint.cs | 20 +++- 2 files changed, 51 insertions(+), 78 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs index fb9cee2f2..1c7f4fd2f 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs @@ -27,6 +27,19 @@ namespace UniVRM10 } } + static void DrawAimUp(Quaternion rot, Vector3 pos, Color c) + { + Handles.color = c; + // aim + var aim = pos + rot * Vector3.forward * 0.3f; + Handles.DrawLine(pos, aim); + Handles.Label(aim, "aim"); + // up + var up = pos + rot * Vector3.up * 0.3f; + Handles.DrawLine(pos, up); + Handles.Label(up, "up"); + } + public void OnSceneGUI() { if (m_target.Source == null) @@ -38,84 +51,36 @@ namespace UniVRM10 Handles.color = Color.yellow; Handles.DrawLine(m_target.Source.position, m_target.transform.position); - TR.FromWorld(m_target.transform).Draw(0.2f); + var pos = m_target.transform.position; - Handles.matrix = Matrix4x4.identity; EditorGUI.BeginChangeCheck(); var pr = m_target.ParentRotation; - var pos = m_target.transform.position; - var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos); - if (EditorGUI.EndChangeCheck()) + + if (m_target.Logic == null) { - Undo.RecordObject(m_target, "Rotated RotateAt Point"); - m_target.DestinationOffset = Quaternion.Inverse(pr) * rot; + TR.FromWorld(m_target.transform).Draw(0.2f); + + Handles.matrix = Matrix4x4.identity; + var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos); + if (EditorGUI.EndChangeCheck()) + { + Undo.RecordObject(m_target, "Rotated RotateAt Point"); + m_target.DestinationOffset = Quaternion.Inverse(pr) * rot; + } + + DrawAimUp(rot, pos, Color.yellow); } + else + { + var rot = m_target.ParentRotation * m_target.Logic.InitialLocalRotation * m_target.DestinationOffset; + DrawAimUp(rot, m_target.transform.position, Color.yellow); + DrawAimUp(m_target.transform.rotation * m_target.Delta, m_target.transform.position, Color.magenta); - // aim - Handles.color = Color.yellow; - var aim = pos + rot * Vector3.forward * 0.3f; - Handles.DrawLine(pos, aim); - Handles.Label(aim, "aim"); - // up - var up = pos + rot * Vector3.up * 0.3f; - Handles.DrawLine(pos, up); - Handles.Label(up, "up"); - - // var delta = Clamp180(m_target.Delta.eulerAngles); - - // // show source - // { - // var sb = new StringBuilder(); - // sb.AppendLine(); - // sb.AppendLine(); - // sb.AppendLine($"source: {m_target.SourceCoordinate}"); - // sb.AppendLine($"{delta.x:0.}"); - // sb.AppendLine($"{delta.y:0.}"); - // sb.Append($"{delta.z:0.}"); - // Handles.Label(m_target.Source.position, sb.ToString(), Style); - // } - - // // show dst - // { - // var sb = new StringBuilder(); - // sb.AppendLine($"constraint: {m_target.DestinationCoordinate}"); - // sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.}"); - // sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.}"); - // sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.}"); - // Handles.Label(m_target.transform.position, sb.ToString(), Style); - // } + var sb = new StringBuilder(); + sb.AppendLine($"yaw: {m_target.Yaw:0.}"); + sb.AppendLine($"pitch: {m_target.Pitch:0.}"); + Handles.Label(m_target.transform.position, sb.ToString(), Style); + } } - // void OnDrawGizmos() - // { - // if (Source == null) - // { - // return; - // } - - // var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position); - // var (yaw, yaw_, pitch, pitch_) = CalcYawPitch(m_coords, localPosition); - // switch (yaw_) - // { - // case 0: break; - // case 1: yaw = 180 - yaw; break; - // case 2: yaw = 180 + yaw; break; - // case 3: yaw = 360 - yaw; break; - // } - // switch (pitch_) - // { - // case 0: pitch = -pitch; break; - // case 1: pitch = -pitch; break; - // case 2: break; - // case 3: break; - // } - // // Debug.Log($"{yaw}({yaw_}), {pitch}({pitch_})"); - // // var rot = Quaternion.Euler(pitch, yaw, 0); - // var rot = Quaternion.AngleAxis(yaw, Vector3.up) * Quaternion.AngleAxis(pitch, Vector3.right); - // var p = rot * Vector3.forward; - - // Gizmos.matrix = transform.localToWorldMatrix; - // Gizmos.DrawLine(Vector3.zero, p * 5); - // } - } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs index 489a3ecf3..3dc9f53d2 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs @@ -33,7 +33,8 @@ namespace UniVRM10 InitialLocalRotation = initialLocalRotation; } } - AimLogic m_runtime; + public AimLogic Logic { get; private set; } + void Start() { @@ -43,9 +44,9 @@ namespace UniVRM10 return; } - if (m_runtime == null) + if (Logic == null) { - m_runtime = new AimLogic(DestinationOffset); + Logic = new AimLogic(transform.localRotation); } } @@ -120,13 +121,20 @@ namespace UniVRM10 /// public override void Process() { - if (Source == null) + if (Logic == null) { return; } - // var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position); - // var (yaw, pitch) = CalcYawPitch(m_coords, localPosition); + var m = Matrix4x4.TRS(transform.position, ParentRotation * Logic.InitialLocalRotation, Vector3.one); + m.CalcYawPitch(Source.position, out Yaw, out Pitch); + // Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(Pitch, 0, 0); + Delta = m.YawPitchRotation(Yaw, Pitch); + transform.rotation = ParentRotation * Logic.InitialLocalRotation * Delta; } + + public float Yaw; + public float Pitch; + public Quaternion Delta; } } From ce8e0bff165d1f37bc9bb2adc72339999acc03a1 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 14 May 2021 13:43:13 +0900 Subject: [PATCH 18/37] CalcYawPitch --- .../Constraint/VRM10AimConstraintEditor.cs | 49 ++++---- .../Constraint/VRM10AimConstraint.cs | 114 ++++-------------- 2 files changed, 49 insertions(+), 114 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs index 1c7f4fd2f..1521bda5d 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs @@ -52,35 +52,36 @@ namespace UniVRM10 Handles.DrawLine(m_target.Source.position, m_target.transform.position); var pos = m_target.transform.position; + TR.FromWorld(m_target.transform).Draw(0.2f); - EditorGUI.BeginChangeCheck(); - var pr = m_target.ParentRotation; + // EditorGUI.BeginChangeCheck(); + // var pr = m_target.ParentRotation; - if (m_target.Logic == null) - { - TR.FromWorld(m_target.transform).Draw(0.2f); + // if (m_target.Logic == null) + // { + // TR.FromWorld(m_target.transform).Draw(0.2f); - Handles.matrix = Matrix4x4.identity; - var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos); - if (EditorGUI.EndChangeCheck()) - { - Undo.RecordObject(m_target, "Rotated RotateAt Point"); - m_target.DestinationOffset = Quaternion.Inverse(pr) * rot; - } + // Handles.matrix = Matrix4x4.identity; + // var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos); + // if (EditorGUI.EndChangeCheck()) + // { + // Undo.RecordObject(m_target, "Rotated RotateAt Point"); + // m_target.DestinationOffset = Quaternion.Inverse(pr) * rot; + // } - DrawAimUp(rot, pos, Color.yellow); - } - else - { - var rot = m_target.ParentRotation * m_target.Logic.InitialLocalRotation * m_target.DestinationOffset; - DrawAimUp(rot, m_target.transform.position, Color.yellow); - DrawAimUp(m_target.transform.rotation * m_target.Delta, m_target.transform.position, Color.magenta); + // DrawAimUp(rot, pos, Color.yellow); + // } + // else + // { + // var rot = m_target.ParentRotation * m_target.Logic.InitialLocalRotation * m_target.DestinationOffset; + // DrawAimUp(rot, m_target.transform.position, Color.yellow); + // DrawAimUp(m_target.transform.rotation * m_target.Delta, m_target.transform.position, Color.magenta); - var sb = new StringBuilder(); - sb.AppendLine($"yaw: {m_target.Yaw:0.}"); - sb.AppendLine($"pitch: {m_target.Pitch:0.}"); - Handles.Label(m_target.transform.position, sb.ToString(), Style); - } + // var sb = new StringBuilder(); + // sb.AppendLine($"yaw: {m_target.Yaw:0.}"); + // sb.AppendLine($"pitch: {m_target.Pitch:0.}"); + // Handles.Label(m_target.transform.position, sb.ToString(), Style); + // } } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs index 3dc9f53d2..c9d4feee8 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs @@ -20,20 +20,20 @@ namespace UniVRM10 [Range(0, 10.0f)] public float Weight = 1.0f; - [SerializeField] - public Quaternion DestinationOffset = Quaternion.identity; - public Quaternion ParentRotation => transform.parent == null ? Quaternion.identity : transform.parent.rotation; - public class AimLogic - { - public readonly Quaternion InitialLocalRotation; - public AimLogic(Quaternion initialLocalRotation) - { - InitialLocalRotation = initialLocalRotation; - } - } - public AimLogic Logic { get; private set; } + // [SerializeField] + // public Quaternion DestinationOffset = Quaternion.identity; + + // public class AimLogic + // { + // public readonly Quaternion InitialLocalRotation; + // public AimLogic(Quaternion initialLocalRotation) + // { + // InitialLocalRotation = initialLocalRotation; + // } + // } + // public AimLogic Logic { get; private set; } void Start() @@ -44,75 +44,10 @@ namespace UniVRM10 return; } - if (Logic == null) - { - Logic = new AimLogic(transform.localRotation); - } - } - - static (float, int, float, int) CalcYawPitch(Matrix4x4 m, Vector3 target) - { - var zaxis = Vector3.Project(target, m.GetColumn(2)); - var yaxis = Vector3.Project(target, m.GetColumn(1)); - var xaxis = Vector3.Project(target, m.GetColumn(0)); - - var xDot = Vector3.Dot(xaxis, m.GetColumn(0)) > 0; - var yDot = Vector3.Dot(yaxis, m.GetColumn(1)) > 0; - var zDot = Vector3.Dot(zaxis, m.GetColumn(2)) > 0; - - // xz - var yaw = (float)System.Math.Atan2(xaxis.magnitude, zaxis.magnitude) * Mathf.Rad2Deg; - var yawQuadrant = -1; - if (xDot && zDot) - { - // 1st(0-90) - yawQuadrant = 0; - } - else if (xDot && !zDot) - { - // 2nd(90-180) - yawQuadrant = 1; - } - else if (!xDot && !zDot) - { - // 3rd - yawQuadrant = 2; - } - else if (!xDot && zDot) - { - // 4th - yawQuadrant = 3; - } - else - { - throw new NotImplementedException(); - } - - // xy - var pitch = (float)System.Math.Atan2(yaxis.magnitude, (xaxis + zaxis).magnitude) * Mathf.Rad2Deg; - var pitchQuadrant = -1; - if (yDot && zDot) - { - // 1st - pitchQuadrant = 0; - } - else if (yDot & !zDot) - { - // 2nd - pitchQuadrant = 1; - } - else if (!yDot & !zDot) - { - // 3rd - pitchQuadrant = 2; - } - else if (!yDot & zDot) - { - // 4th - pitchQuadrant = 3; - } - - return (yaw, yawQuadrant, pitch, pitchQuadrant); + // if (Logic == null) + // { + // Logic = new AimLogic(transform.localRotation); + // } } /// @@ -121,16 +56,15 @@ namespace UniVRM10 /// public override void Process() { - if (Logic == null) - { - return; - } + // if (Logic == null) + // { + // return; + // } - var m = Matrix4x4.TRS(transform.position, ParentRotation * Logic.InitialLocalRotation, Vector3.one); - m.CalcYawPitch(Source.position, out Yaw, out Pitch); - // Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(Pitch, 0, 0); - Delta = m.YawPitchRotation(Yaw, Pitch); - transform.rotation = ParentRotation * Logic.InitialLocalRotation * Delta; + var m = Matrix4x4.Rotate(ParentRotation); + m.CalcYawPitch(Source.position - transform.position, out Yaw, out Pitch); + Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(-Pitch, 0, 0); + transform.rotation = ParentRotation * Delta; } public float Yaw; From 12e0cc0406026dd3474c55602a8ac3d3cbd93a59 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 14 May 2021 14:54:03 +0900 Subject: [PATCH 19/37] WIP aim --- .../Constraint/VRM10AimConstraintEditor.cs | 51 ++++++++++--------- .../Constraint/VRM10AimConstraint.cs | 46 ++++++++--------- .../Components/LookAt/Matrix4x4Extensions.cs | 24 +++++++++ 3 files changed, 73 insertions(+), 48 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs index 1521bda5d..cc162f34d 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs @@ -29,6 +29,7 @@ namespace UniVRM10 static void DrawAimUp(Quaternion rot, Vector3 pos, Color c) { + Handles.matrix = Matrix4x4.identity; Handles.color = c; // aim var aim = pos + rot * Vector3.forward * 0.3f; @@ -52,36 +53,36 @@ namespace UniVRM10 Handles.DrawLine(m_target.Source.position, m_target.transform.position); var pos = m_target.transform.position; - TR.FromWorld(m_target.transform).Draw(0.2f); - // EditorGUI.BeginChangeCheck(); - // var pr = m_target.ParentRotation; + var pr = m_target.ParentRotation; - // if (m_target.Logic == null) - // { - // TR.FromWorld(m_target.transform).Draw(0.2f); + if (m_target.Logic == null) + { + EditorGUI.BeginChangeCheck(); + TR.FromWorld(m_target.transform).Draw(0.2f); - // Handles.matrix = Matrix4x4.identity; - // var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos); - // if (EditorGUI.EndChangeCheck()) - // { - // Undo.RecordObject(m_target, "Rotated RotateAt Point"); - // m_target.DestinationOffset = Quaternion.Inverse(pr) * rot; - // } + Handles.matrix = Matrix4x4.identity; + var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos); + if (EditorGUI.EndChangeCheck()) + { + Undo.RecordObject(m_target, "Rotated RotateAt Point"); + m_target.DestinationOffset = Quaternion.Inverse(pr) * rot; + } - // DrawAimUp(rot, pos, Color.yellow); - // } - // else - // { - // var rot = m_target.ParentRotation * m_target.Logic.InitialLocalRotation * m_target.DestinationOffset; - // DrawAimUp(rot, m_target.transform.position, Color.yellow); - // DrawAimUp(m_target.transform.rotation * m_target.Delta, m_target.transform.position, Color.magenta); + DrawAimUp(rot, pos, Color.yellow); + } + else + { + var init = m_target.ParentRotation * m_target.Logic.InitialLocalRotation; + DrawAimUp(init * m_target.DestinationOffset, m_target.transform.position, Color.yellow); + new TR(init, m_target.transform.position).Draw(0.2f); + DrawAimUp(m_target.transform.rotation, m_target.transform.position, Color.magenta); - // var sb = new StringBuilder(); - // sb.AppendLine($"yaw: {m_target.Yaw:0.}"); - // sb.AppendLine($"pitch: {m_target.Pitch:0.}"); - // Handles.Label(m_target.transform.position, sb.ToString(), Style); - // } + var sb = new StringBuilder(); + sb.AppendLine($"yaw: {m_target.Yaw:0.}"); + sb.AppendLine($"pitch: {m_target.Pitch:0.}"); + Handles.Label(m_target.transform.position, sb.ToString(), Style); + } } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs index c9d4feee8..c70beaf15 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs @@ -22,19 +22,18 @@ namespace UniVRM10 public Quaternion ParentRotation => transform.parent == null ? Quaternion.identity : transform.parent.rotation; - // [SerializeField] - // public Quaternion DestinationOffset = Quaternion.identity; - - // public class AimLogic - // { - // public readonly Quaternion InitialLocalRotation; - // public AimLogic(Quaternion initialLocalRotation) - // { - // InitialLocalRotation = initialLocalRotation; - // } - // } - // public AimLogic Logic { get; private set; } + [SerializeField] + public Quaternion DestinationOffset = Quaternion.identity; + public class AimLogic + { + public readonly Quaternion InitialLocalRotation; + public AimLogic(Quaternion initialLocalRotation) + { + InitialLocalRotation = initialLocalRotation; + } + } + public AimLogic Logic { get; private set; } void Start() { @@ -44,10 +43,10 @@ namespace UniVRM10 return; } - // if (Logic == null) - // { - // Logic = new AimLogic(transform.localRotation); - // } + if (Logic == null) + { + Logic = new AimLogic(transform.localRotation); + } } /// @@ -56,14 +55,15 @@ namespace UniVRM10 /// public override void Process() { - // if (Logic == null) - // { - // return; - // } + if (Logic == null) + { + return; + } - var m = Matrix4x4.Rotate(ParentRotation); - m.CalcYawPitch(Source.position - transform.position, out Yaw, out Pitch); - Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(-Pitch, 0, 0); + // var init = ParentRotation * Logic.InitialLocalRotation; + var m = Matrix4x4.TRS(transform.position, ParentRotation * DestinationOffset, Vector3.one); + (Yaw, Pitch) = m.CalcYawPitch(Source.position); + Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(Pitch, 0, 0); transform.rotation = ParentRotation * Delta; } diff --git a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs index 0cd2da454..bf1806ca7 100644 --- a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs +++ b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs @@ -6,6 +6,30 @@ namespace UniVRM10 { public static class Matrix4x4Extensions { + /// + /// from AimConstraint + /// + /// + /// + /// + /// + public static (float Yaw, float Pitch) CalcYawPitch(this Matrix4x4 m, Vector3 target) + { + var p = m.GetColumn(3); + target -= new Vector3(p.x, p.y, p.z); + var zaxis = Vector3.Project(target, m.GetColumn(2)); + var yaxis = Vector3.Project(target, m.GetColumn(1)); + var xaxis = Vector3.Project(target, m.GetColumn(0)); + + var yawPlusMinus = Vector3.Dot(xaxis, m.GetColumn(0)) > 0 ? 1.0f : -1.0f; + var yaw = (float)Math.Atan2(xaxis.magnitude, zaxis.magnitude) * yawPlusMinus * Mathf.Rad2Deg; + + var pitchPlusMinus = Vector3.Dot(yaxis, m.GetColumn(1)) < 0 ? 1.0f : -1.0f; + var pitch = (float)Math.Atan2(yaxis.magnitude, (xaxis + zaxis).magnitude) * pitchPlusMinus * Mathf.Rad2Deg; + + return (yaw, pitch); + } + public static void CalcYawPitch(this Matrix4x4 m, Vector3 target, out float yaw, out float pitch) { var zaxis = Vector3.Project(target, m.GetColumn(2)); From f95123e2087d3c985d6a382fab3ebe3eb789bcee Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 14 May 2021 16:45:23 +0900 Subject: [PATCH 20/37] euler --- .../Constraint/VRM10AimConstraint.cs | 5 ++--- .../Components/LookAt/Matrix4x4Extensions.cs | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs index c70beaf15..e68cc765d 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs @@ -60,11 +60,10 @@ namespace UniVRM10 return; } - // var init = ParentRotation * Logic.InitialLocalRotation; - var m = Matrix4x4.TRS(transform.position, ParentRotation * DestinationOffset, Vector3.one); + var m = Matrix4x4.TRS(transform.position, ParentRotation * Logic.InitialLocalRotation * DestinationOffset, Vector3.one); (Yaw, Pitch) = m.CalcYawPitch(Source.position); Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(Pitch, 0, 0); - transform.rotation = ParentRotation * Delta; + transform.rotation = ParentRotation * Logic.InitialLocalRotation * DestinationOffset * Delta; } public float Yaw; diff --git a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs index bf1806ca7..1aa18e8a2 100644 --- a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs +++ b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs @@ -15,17 +15,19 @@ namespace UniVRM10 /// public static (float Yaw, float Pitch) CalcYawPitch(this Matrix4x4 m, Vector3 target) { - var p = m.GetColumn(3); - target -= new Vector3(p.x, p.y, p.z); - var zaxis = Vector3.Project(target, m.GetColumn(2)); - var yaxis = Vector3.Project(target, m.GetColumn(1)); - var xaxis = Vector3.Project(target, m.GetColumn(0)); + var localPosition = m.inverse.MultiplyPoint(target); - var yawPlusMinus = Vector3.Dot(xaxis, m.GetColumn(0)) > 0 ? 1.0f : -1.0f; - var yaw = (float)Math.Atan2(xaxis.magnitude, zaxis.magnitude) * yawPlusMinus * Mathf.Rad2Deg; + var zaxis = Vector3.Project(localPosition, Vector3.forward); + var yaxis = Vector3.Project(localPosition, Vector3.up); + var xaxis = Vector3.Project(localPosition, Vector3.right); + // y z plane var pitchPlusMinus = Vector3.Dot(yaxis, m.GetColumn(1)) < 0 ? 1.0f : -1.0f; - var pitch = (float)Math.Atan2(yaxis.magnitude, (xaxis + zaxis).magnitude) * pitchPlusMinus * Mathf.Rad2Deg; + var pitch = (float)Math.Atan2(yaxis.magnitude, zaxis.magnitude) * pitchPlusMinus * Mathf.Rad2Deg; + + // y+z x plane + var yawPlusMinus = Vector3.Dot(xaxis, m.GetColumn(0)) > 0 ? 1.0f : -1.0f; + var yaw = (float)Math.Atan2(xaxis.magnitude, (yaxis + zaxis).magnitude) * yawPlusMinus * Mathf.Rad2Deg; return (yaw, pitch); } From a1af1a943d81f5248573231f39c5c7a20de63144 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 14 May 2021 18:04:22 +0900 Subject: [PATCH 21/37] CalcYawPitch --- .../Constraint/VRM10AimConstraintEditor.cs | 2 +- .../Components/LookAt/Matrix4x4Extensions.cs | 42 ++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs index cc162f34d..17d2c13e1 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs @@ -76,7 +76,7 @@ namespace UniVRM10 var init = m_target.ParentRotation * m_target.Logic.InitialLocalRotation; DrawAimUp(init * m_target.DestinationOffset, m_target.transform.position, Color.yellow); new TR(init, m_target.transform.position).Draw(0.2f); - DrawAimUp(m_target.transform.rotation, m_target.transform.position, Color.magenta); + DrawAimUp(init * m_target.DestinationOffset * m_target.Delta, m_target.transform.position, Color.magenta); var sb = new StringBuilder(); sb.AppendLine($"yaw: {m_target.Yaw:0.}"); diff --git a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs index 1aa18e8a2..0271eedf5 100644 --- a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs +++ b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs @@ -21,13 +21,43 @@ namespace UniVRM10 var yaxis = Vector3.Project(localPosition, Vector3.up); var xaxis = Vector3.Project(localPosition, Vector3.right); - // y z plane - var pitchPlusMinus = Vector3.Dot(yaxis, m.GetColumn(1)) < 0 ? 1.0f : -1.0f; - var pitch = (float)Math.Atan2(yaxis.magnitude, zaxis.magnitude) * pitchPlusMinus * Mathf.Rad2Deg; + var xDot = Vector3.Dot(xaxis, Vector3.right) > 0; + var yDot = Vector3.Dot(yaxis, Vector3.up) > 0; + var zDot = Vector3.Dot(zaxis, Vector3.forward) > 0; - // y+z x plane - var yawPlusMinus = Vector3.Dot(xaxis, m.GetColumn(0)) > 0 ? 1.0f : -1.0f; - var yaw = (float)Math.Atan2(xaxis.magnitude, (yaxis + zaxis).magnitude) * yawPlusMinus * Mathf.Rad2Deg; + // x z plane + var yaw = (float)Math.Atan2(xaxis.magnitude, zaxis.magnitude) * Mathf.Rad2Deg; + if (xDot && zDot) + { + // 1st(0-90) + + } + else if (xDot && !zDot) + { + // 2nd(90-180) + yaw = 180 - yaw; + } + else if (!xDot && !zDot) + { + // 3rd + yaw = -180 + yaw; + } + else if (!xDot && zDot) + { + // 4th + yaw = -yaw; + } + else + { + throw new NotImplementedException(); + } + + // x+y z plane + var pitch = (float)Math.Atan2(yaxis.magnitude, (xaxis + zaxis).magnitude) * Mathf.Rad2Deg; + if (yDot) + { + pitch = -pitch; + } return (yaw, pitch); } From 79465de4adc6600d244fb0046a398bcb44769640 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 17 May 2021 15:10:55 +0900 Subject: [PATCH 22/37] impl rotationConstraint SourceOffset --- .../VRM10RotationConstraintEditor.cs | 12 ++ .../Components/Constraint/ConstraintSource.cs | 7 +- .../Constraint/VRM10PositionConstraint.cs | 3 + .../Constraint/VRM10RotationConstraint.cs | 148 ++++++------------ 4 files changed, 67 insertions(+), 103 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index c0503738c..fbc6329b9 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -53,6 +53,18 @@ namespace UniVRM10 return; } + // source offset + if (!Application.isPlaying) + { + EditorGUI.BeginChangeCheck(); + Quaternion sourceOffset = Handles.RotationHandle(m_target.SourceOffset.Rotation, m_target.Source.position); + if (EditorGUI.EndChangeCheck()) + { + Undo.RecordObject(m_target, "source offset"); + m_target.SourceOffset.Rotation = sourceOffset; + } + } + // this to target line Handles.color = Color.yellow; Handles.DrawLine(m_target.Source.position, m_target.transform.position); diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs index 0c0f3389d..15d6f1178 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs @@ -30,13 +30,13 @@ namespace UniVRM10 } } - public Quaternion RotationDelta(ObjectSpace coords) + public Quaternion RotationDelta(ObjectSpace coords, Quaternion sourceRotationOffset) { switch (coords) { // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); - case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(LocalInitial.Rotation); - case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelInitial.Rotation) * Quaternion.Inverse(ModelRoot.rotation); + case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(LocalInitial.Rotation * sourceRotationOffset); + case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelInitial.Rotation * sourceRotationOffset) * Quaternion.Inverse(ModelRoot.rotation); default: throw new NotImplementedException(); } } @@ -49,6 +49,7 @@ namespace UniVRM10 LocalInitial = TR.FromLocal(t); } + if (modelRoot != null) { var world = TR.FromWorld(t); ModelRoot = modelRoot; diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs index cf8bfa5a3..15c42b97d 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs @@ -16,6 +16,9 @@ namespace UniVRM10 [SerializeField] public ObjectSpace SourceCoordinate = default; + [SerializeField] + public VRM10RotationOffset SourceOffset = VRM10RotationOffset.Identity; + [SerializeField] public ObjectSpace DestinationCoordinate = default; diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index 635ff3782..a300fc66e 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -15,10 +15,10 @@ namespace UniVRM10 public Transform Source = default; [SerializeField] - public VRM10RotationOffset SourceOffset = VRM10RotationOffset.Identity; + public ObjectSpace SourceCoordinate = default; [SerializeField] - public ObjectSpace SourceCoordinate = default; + public VRM10RotationOffset SourceOffset = VRM10RotationOffset.Identity; [SerializeField] public ObjectSpace DestinationCoordinate = default; @@ -38,67 +38,52 @@ namespace UniVRM10 public TR GetSourceCoords() { + if (Source == null) + { + throw new ConstraintException(ConstraintException.ExceptionTypes.NoSource); + } + switch (SourceCoordinate) { case ObjectSpace.model: { - - var r = Quaternion.identity; - if (Source != null) + if (ModelRoot == null) { - if (ModelRoot != null) - { - r = ModelRoot.rotation; - } + throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); } - - var t = Vector3.zero; - if (Source != null) - { - t = Source.position; - } - - return new TR(r, t); + return new TR(ModelRoot.rotation * SourceOffset.Rotation, Source.position); } case ObjectSpace.local: { - if (Source != null) + if (m_src == null) { - if (m_src != null) - { - // runtime - var parent = TR.Identity; - if (Source.parent != null) - { - parent = TR.FromWorld(Source.parent); - } - return parent * m_src.LocalInitial; - } - else - { - return TR.FromWorld(Source); - } + return new TR(Source.rotation * SourceOffset.Rotation, Source.position); } - return TR.Identity; + // runtime + var parent = Quaternion.identity; + if (Source.parent != null) + { + parent = Source.parent.rotation; + } + return new TR(parent * m_src.LocalInitial.Rotation * SourceOffset.Rotation, Source.position); } - } - throw new NotImplementedException(); + default: + throw new NotImplementedException(); + } } public TR GetSourceCurrent() { var coords = GetSourceCoords(); - if (m_src != null) - { - return coords * new TR(m_src.RotationDelta(SourceCoordinate)); - } - else + if (m_src == null) { return coords; } + + return coords * new TR(Delta); } public Quaternion Delta @@ -114,79 +99,43 @@ namespace UniVRM10 { case ObjectSpace.model: { - var r = Quaternion.identity; - if (ModelRoot != null) + if (ModelRoot == null) { - r = ModelRoot.rotation; + throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); } - return new TR(r, transform.position); + return new TR(ModelRoot.rotation, transform.position); } case ObjectSpace.local: { - if (m_src != null) - { - // runtime - var parent = TR.Identity; - if (transform.parent != null) - { - parent = TR.FromWorld(transform.parent); - } - return parent * m_dst.LocalInitial; - } - else + if (m_src == null) { return TR.FromWorld(transform); } - } - } - throw new NotImplementedException(); + // runtime + var parent = TR.Identity; + if (transform.parent != null) + { + parent = TR.FromWorld(transform.parent); + } + return parent * m_dst.LocalInitial; + } + + default: + throw new NotImplementedException(); + } } public TR GetDstCurrent() { var coords = GetDstCoords(); - if (m_src != null) - { - return coords * new TR(m_src.RotationDelta(SourceCoordinate)); - } - else + if (m_src == null) { return coords; } - // switch (DestinationCoordinate) - // { - // case ObjectSpace.model: - // { - // var r = Quaternion.identity; - // if (ModelRoot != null) - // { - // r = ModelRoot.rotation; - // } - // return new TR(r, transform.position); - // } - // case ObjectSpace.local: - // { - // if (m_src != null) - // { - // // runtime - // var parent = TR.Identity; - // if (transform.parent != null) - // { - // parent = TR.FromWorld(transform.parent); - // } - // return parent * m_dst.LocalInitial; - // } - // else - // { - // return TR.FromWorld(transform); - // } - // } - // } - - // throw new NotImplementedException(); + return coords * new TR(Delta); } /// @@ -215,10 +164,6 @@ namespace UniVRM10 ModelRoot = current; } - /// - /// SourceのUpdateよりも先か後かはその時による。 - /// 厳密に制御するのは無理。 - /// public override void Process() { if (Source == null) @@ -236,10 +181,13 @@ namespace UniVRM10 m_dst = new ConstraintDestination(transform, ModelRoot); } - // 軸制限をしたオイラー角 - Delta = m_src.RotationDelta(SourceCoordinate); + // 回転差分 + Delta = m_src.RotationDelta(SourceCoordinate, SourceOffset.Rotation); + + // 軸制限 var fleezed = FreezeAxes.Freeze(Delta.eulerAngles); var rotation = Quaternion.Euler(fleezed); + // Debug.Log($"{delta} => {rotation}"); // オイラー角を再度Quaternionへ。weight を加味してSlerpする m_dst.ApplyRotation(rotation, Weight, DestinationCoordinate, ModelRoot); From 1e80c61af6dc91c7e011107b1694190779c916c6 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 17 May 2021 15:19:53 +0900 Subject: [PATCH 23/37] impl rotationConstraint DestinationOffset --- .../VRM10RotationConstraintEditor.cs | 16 +++++++- .../Constraint/VRM10RotationConstraint.cs | 41 +++++++++++-------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index fbc6329b9..ebd7d4c5e 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -57,11 +57,23 @@ namespace UniVRM10 if (!Application.isPlaying) { EditorGUI.BeginChangeCheck(); - Quaternion sourceOffset = Handles.RotationHandle(m_target.SourceOffset.Rotation, m_target.Source.position); + Quaternion offset = Handles.RotationHandle(m_target.SourceOffset.Rotation, m_target.Source.position); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(m_target, "source offset"); - m_target.SourceOffset.Rotation = sourceOffset; + m_target.SourceOffset.Rotation = offset; + } + } + + // dest offset + if (!Application.isPlaying) + { + EditorGUI.BeginChangeCheck(); + Quaternion offset = Handles.RotationHandle(m_target.DestinationOffset.Rotation, m_target.transform.position); + if (EditorGUI.EndChangeCheck()) + { + Undo.RecordObject(m_target, "dest offset"); + m_target.DestinationOffset.Rotation = offset; } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index a300fc66e..f3eeea7d3 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -11,18 +11,6 @@ namespace UniVRM10 [DisallowMultipleComponent] public class VRM10RotationConstraint : VRM10Constraint, IVRM10ConstraintSourceDestination { - [SerializeField] - public Transform Source = default; - - [SerializeField] - public ObjectSpace SourceCoordinate = default; - - [SerializeField] - public VRM10RotationOffset SourceOffset = VRM10RotationOffset.Identity; - - [SerializeField] - public ObjectSpace DestinationCoordinate = default; - [SerializeField] [EnumFlags] public AxisMask FreezeAxes = default; @@ -34,6 +22,23 @@ namespace UniVRM10 [SerializeField] public Transform ModelRoot = default; + [Header("Source")] + [SerializeField] + public Transform Source = default; + + [SerializeField] + public ObjectSpace SourceCoordinate = default; + + [SerializeField] + public VRM10RotationOffset SourceOffset = VRM10RotationOffset.Identity; + + [Header("Destination")] + [SerializeField] + public ObjectSpace DestinationCoordinate = default; + + [SerializeField] + public VRM10RotationOffset DestinationOffset = VRM10RotationOffset.Identity; + ConstraintSource m_src; public TR GetSourceCoords() @@ -103,23 +108,23 @@ namespace UniVRM10 { throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); } - return new TR(ModelRoot.rotation, transform.position); + return new TR(ModelRoot.rotation * DestinationOffset.Rotation, transform.position); } case ObjectSpace.local: { if (m_src == null) { - return TR.FromWorld(transform); + return new TR(transform.rotation * DestinationOffset.Rotation, transform.position); } // runtime - var parent = TR.Identity; + var parent = Quaternion.identity; if (transform.parent != null) { - parent = TR.FromWorld(transform.parent); + parent = transform.parent.rotation; } - return parent * m_dst.LocalInitial; + return new TR(parent * m_dst.LocalInitial.Rotation * DestinationOffset.Rotation, transform.position); } default: @@ -190,7 +195,7 @@ namespace UniVRM10 // Debug.Log($"{delta} => {rotation}"); // オイラー角を再度Quaternionへ。weight を加味してSlerpする - m_dst.ApplyRotation(rotation, Weight, DestinationCoordinate, ModelRoot); + m_dst.ApplyRotation(DestinationOffset.Rotation * rotation, Weight, DestinationCoordinate, ModelRoot); } } } From 253846698ce73712738fcb39bc2e47bd9042899c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 17 May 2021 16:20:42 +0900 Subject: [PATCH 24/37] =?UTF-8?q?position/rotation=20constraint=20?= =?UTF-8?q?=E3=81=AE=E5=85=B1=E9=80=9A=E9=83=A8=E5=88=86=E3=82=92=20VRM10P?= =?UTF-8?q?ostionRotationConstraintEditorBase=20=E3=81=AB=E3=81=BE?= =?UTF-8?q?=E3=81=A8=E3=82=81=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10ConstraintSourceDestinationExtensions.cs | 10 +- .../VRM10PositionConstraintEditor.cs | 62 +---- ...RM10PostionRotationConstraintEditorBase.cs | 113 +++++++++ ...stionRotationConstraintEditorBase.cs.meta} | 2 +- .../VRM10RotationConstraintEditor.cs | 105 +-------- .../Constraint/ConstraintDestination.cs | 2 +- .../Components/Constraint/ConstraintSource.cs | 2 +- .../Constraint/IVRM10SourceDestination.cs | 11 - .../Components/Constraint/VRM10Constraint.cs | 4 +- .../Constraint/VRM10PositionConstraint.cs | 189 +++------------ .../Constraint/VRM10RotationConstraint.cs | 178 +------------- .../VRM10RotationPositionConstraintBase.cs | 218 ++++++++++++++++++ ...RM10RotationPositionConstraintBase.cs.meta | 11 + 13 files changed, 390 insertions(+), 517 deletions(-) create mode 100644 Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs rename Assets/VRM10/{Runtime/Components/Constraint/IVRM10SourceDestination.cs.meta => Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs.meta} (83%) delete mode 100644 Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs create mode 100644 Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs create mode 100644 Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs.meta diff --git a/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs b/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs index 38e3c6a0b..2fc95decd 100644 --- a/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs +++ b/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs @@ -3,9 +3,9 @@ using UnityEngine; namespace UniVRM10 { - public static class IVRM10ConstraintSourceDestinationExtensions + public static class VRM10PositionRotationConstraintBaseExtensions { - public static void DrawSourceCoords(this IVRM10ConstraintSourceDestination self) + public static void DrawSourceCoords(this VRM10RotationPositionConstraintBase self) { try { @@ -16,7 +16,7 @@ namespace UniVRM10 } } - public static void DrawSourceCurrent(this IVRM10ConstraintSourceDestination self) + public static void DrawSourceCurrent(this VRM10RotationPositionConstraintBase self) { try { @@ -30,7 +30,7 @@ namespace UniVRM10 } } - public static void DrawDstCoords(this IVRM10ConstraintSourceDestination self) + public static void DrawDstCoords(this VRM10RotationPositionConstraintBase self) { try { @@ -42,7 +42,7 @@ namespace UniVRM10 } } - public static void DrawDstCurrent(this IVRM10ConstraintSourceDestination self) + public static void DrawDstCurrent(this VRM10RotationPositionConstraintBase self) { try { diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs index 897497304..78e9653c5 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10PositionConstraintEditor.cs @@ -5,67 +5,7 @@ using UnityEngine; namespace UniVRM10 { [CustomEditor(typeof(VRM10PositionConstraint))] - public class VRM10PositionConstraintEditor : Editor + public class VRM10PositionConstraintEditor : VRM10PositionRotationConstraintEditorBase { - VRM10PositionConstraint m_target; - - void OnEnable() - { - m_target = (VRM10PositionConstraint)target; - } - - static GUIStyle s_style; - static GUIStyle Style - { - get - { - if (s_style == null) - { - s_style = new GUIStyle("box"); - } - return s_style; - } - } - - public void OnSceneGUI() - { - if (m_target.Source == null) - { - return; - } - - // this to target line - Handles.color = Color.yellow; - Handles.DrawLine(m_target.Source.position, m_target.transform.position); - - var delta = m_target.Delta; - - // show source - { - var sb = new StringBuilder(); - sb.AppendLine(); - sb.AppendLine(); - sb.AppendLine($"source: {m_target.SourceCoordinate}"); - sb.AppendLine($"{delta.x:0.00}"); - sb.AppendLine($"{delta.y:0.00}"); - sb.Append($"{delta.z:0.00}"); - Handles.Label(m_target.Source.position, sb.ToString(), Style); - } - - // show dst - { - var sb = new StringBuilder(); - sb.AppendLine($"constraint: {m_target.DestinationCoordinate}"); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.}"); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.}"); - sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.}"); - Handles.Label(m_target.transform.position, sb.ToString(), Style); - } - - m_target.DrawSourceCoords(); - m_target.DrawSourceCurrent(); - m_target.DrawDstCoords(); - m_target.DrawDstCurrent(); - } } } diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs new file mode 100644 index 000000000..35b6f5f50 --- /dev/null +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs @@ -0,0 +1,113 @@ +using System.Text; +using UnityEditor; +using UnityEngine; + +namespace UniVRM10 +{ + public abstract class VRM10PositionRotationConstraintEditorBase : Editor + { + VRM10RotationPositionConstraintBase m_target; + + void OnEnable() + { + m_target = (VRM10RotationPositionConstraintBase)target; + } + + static GUIStyle s_style; + static GUIStyle Style + { + get + { + if (s_style == null) + { + s_style = new GUIStyle("box"); + } + return s_style; + } + } + + /// + /// Euler各を +- 180 にクランプする + /// + /// + /// + static Vector3 Clamp180(Vector3 v) + { + var x = v.x; + while (x < -180) x += 360; + while (x > 180) x -= 360; + var y = v.y; + while (y < -180) y += 360; + while (y > 180) y -= 360; + var z = v.z; + while (z < -180) z += 360; + while (z > 180) z -= 360; + return new Vector3(x, y, z); + } + + public void OnSceneGUI() + { + if (m_target.GetSource() == null) + { + return; + } + + // source offset + if (!Application.isPlaying) + { + EditorGUI.BeginChangeCheck(); + Quaternion offset = Handles.RotationHandle(m_target.SourceOffset, m_target.GetSource().position); + if (EditorGUI.EndChangeCheck()) + { + Undo.RecordObject(m_target.GetComponent(), "source offset"); + m_target.SourceOffset = offset; + } + } + + // dest offset + if (!Application.isPlaying) + { + EditorGUI.BeginChangeCheck(); + Quaternion offset = Handles.RotationHandle(m_target.DestinationOffset, m_target.GetComponent().transform.position); + if (EditorGUI.EndChangeCheck()) + { + Undo.RecordObject(m_target.GetComponent(), "dest offset"); + m_target.DestinationOffset = offset; + } + } + + // this to target line + Handles.color = Color.yellow; + Handles.DrawLine(m_target.GetSource().position, m_target.GetComponent().transform.position); + + var delta = Clamp180(m_target.Delta); + + // show source + { + var sb = new StringBuilder(); + sb.AppendLine(); + sb.AppendLine(); + sb.AppendLine($"source: {m_target.SourceCoordinate}"); + sb.AppendLine($"{delta.x:0.}"); + sb.AppendLine($"{delta.y:0.}"); + sb.Append($"{delta.z:0.}"); + Handles.Label(m_target.GetSource().position, sb.ToString(), Style); + } + + // show dst + { + var sb = new StringBuilder(); + sb.AppendLine($"constraint: {m_target.DestinationCoordinate}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.}"); + sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.}"); + Handles.Label(m_target.GetComponent().transform.position, sb.ToString(), Style); + } + + m_target.DrawSourceCoords(); + m_target.DrawSourceCurrent(); + m_target.DrawDstCoords(); + m_target.DrawDstCurrent(); + } + } +} diff --git a/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs.meta b/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs.meta similarity index 83% rename from Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs.meta rename to Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs.meta index ff49caec8..c162c586c 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs.meta +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 4d1ea0f0f4d95a2499f9aa5a1d7a2235 +guid: 46c1f13688fee134aa37a39a72433217 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs index ebd7d4c5e..870cb2f28 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10RotationConstraintEditor.cs @@ -5,110 +5,7 @@ using UnityEngine; namespace UniVRM10 { [CustomEditor(typeof(VRM10RotationConstraint))] - public class VRM10RotationConstraintEditor : Editor + public class VRM10RotationConstraintEditor : VRM10PositionRotationConstraintEditorBase { - VRM10RotationConstraint m_target; - - void OnEnable() - { - m_target = (VRM10RotationConstraint)target; - } - - static GUIStyle s_style; - static GUIStyle Style - { - get - { - if (s_style == null) - { - s_style = new GUIStyle("box"); - } - return s_style; - } - } - - /// - /// Euler各を +- 180 にクランプする - /// - /// - /// - static Vector3 Clamp180(Vector3 v) - { - var x = v.x; - while (x < -180) x += 360; - while (x > 180) x -= 360; - var y = v.y; - while (y < -180) y += 360; - while (y > 180) y -= 360; - var z = v.z; - while (z < -180) z += 360; - while (z > 180) z -= 360; - return new Vector3(x, y, z); - } - - public void OnSceneGUI() - { - if (m_target.Source == null) - { - return; - } - - // source offset - if (!Application.isPlaying) - { - EditorGUI.BeginChangeCheck(); - Quaternion offset = Handles.RotationHandle(m_target.SourceOffset.Rotation, m_target.Source.position); - if (EditorGUI.EndChangeCheck()) - { - Undo.RecordObject(m_target, "source offset"); - m_target.SourceOffset.Rotation = offset; - } - } - - // dest offset - if (!Application.isPlaying) - { - EditorGUI.BeginChangeCheck(); - Quaternion offset = Handles.RotationHandle(m_target.DestinationOffset.Rotation, m_target.transform.position); - if (EditorGUI.EndChangeCheck()) - { - Undo.RecordObject(m_target, "dest offset"); - m_target.DestinationOffset.Rotation = offset; - } - } - - // this to target line - Handles.color = Color.yellow; - Handles.DrawLine(m_target.Source.position, m_target.transform.position); - - var delta = Clamp180(m_target.Delta.eulerAngles); - - // show source - { - var sb = new StringBuilder(); - sb.AppendLine(); - sb.AppendLine(); - sb.AppendLine($"source: {m_target.SourceCoordinate}"); - sb.AppendLine($"{delta.x:0.}"); - sb.AppendLine($"{delta.y:0.}"); - sb.Append($"{delta.z:0.}"); - Handles.Label(m_target.Source.position, sb.ToString(), Style); - } - - // show dst - { - var sb = new StringBuilder(); - sb.AppendLine($"constraint: {m_target.DestinationCoordinate}"); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.}"); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.}"); - sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.}"); - Handles.Label(m_target.transform.position, sb.ToString(), Style); - } - - m_target.DrawSourceCoords(); - m_target.DrawSourceCurrent(); - m_target.DrawDstCoords(); - m_target.DrawDstCurrent(); - } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs index 034130aeb..f7c5b14c1 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs @@ -4,7 +4,7 @@ using UnityEngine; namespace UniVRM10 { - class ConstraintDestination + public class ConstraintDestination { readonly Transform m_transform; readonly TR m_modelInitial; diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs index 15d6f1178..8b88f3c59 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs @@ -4,7 +4,7 @@ using UniGLTF.Extensions.VRMC_node_constraint; namespace UniVRM10 { - class ConstraintSource + public class ConstraintSource { public readonly Transform ModelRoot; readonly Transform m_transform; diff --git a/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs b/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs deleted file mode 100644 index d6aa2ca89..000000000 --- a/Assets/VRM10/Runtime/Components/Constraint/IVRM10SourceDestination.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace UniVRM10 -{ - public interface IVRM10ConstraintSourceDestination - { - TR GetSourceCoords(); - TR GetSourceCurrent(); - - TR GetDstCoords(); - TR GetDstCurrent(); - } -} diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs index 7e6f11f0f..fe63c3d35 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs @@ -5,9 +5,7 @@ namespace UniVRM10 { public abstract class VRM10Constraint : MonoBehaviour { - public virtual void Process() - { + public abstract void Process(); - } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs index 15c42b97d..32177527e 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs @@ -8,169 +8,12 @@ namespace UniVRM10 /// 対象の初期位置と現在位置の差分(delta)を、自身の初期位置に対してWeightを乗算して加算する。 /// [DisallowMultipleComponent] - public class VRM10PositionConstraint : VRM10Constraint, IVRM10ConstraintSourceDestination + public class VRM10PositionConstraint : VRM10RotationPositionConstraintBase { - [SerializeField] - public Transform Source = default; + Vector3 m_delta; + public override Vector3 Delta => m_delta; - [SerializeField] - public ObjectSpace SourceCoordinate = default; - - [SerializeField] - public VRM10RotationOffset SourceOffset = VRM10RotationOffset.Identity; - - [SerializeField] - public ObjectSpace DestinationCoordinate = default; - - [SerializeField] - public AxisMask FreezeAxes = default; - - [SerializeField] - [Range(0, 10.0f)] - public float Weight = 1.0f; - - [SerializeField] - public Transform ModelRoot = default; - - ConstraintSource m_src; - - public TR GetSourceCoords() - { - if (Source == null) - { - throw new ConstraintException(ConstraintException.ExceptionTypes.NoSource); - } - - switch (SourceCoordinate) - { - case ObjectSpace.model: - { - if (ModelRoot == null) - { - throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); - } - - if (m_src == null) - { - return new TR(ModelRoot.rotation, Source.position); - } - - // runtime - return new TR(ModelRoot.rotation, m_src.ModelInitial.Translation); - } - - case ObjectSpace.local: - { - if (m_src == null) - { - return TR.FromWorld(Source); - } - - // runtime - var parent = TR.Identity; - if (Source.parent != null) - { - parent = TR.FromWorld(Source.parent); - } - return parent * m_src.LocalInitial; - } - - default: - throw new NotImplementedException(); - } - } - - public TR GetSourceCurrent() - { - var coords = GetSourceCoords(); - if (m_src == null) - { - return coords; - } - - return new TR(Delta) * coords; - } - - public Vector3 Delta - { - get; - private set; - } - - ConstraintDestination m_dst; - - public TR GetDstCoords() - { - switch (DestinationCoordinate) - { - case ObjectSpace.model: - { - if (ModelRoot == null) - { - throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); - } - return new TR(ModelRoot.rotation, transform.position); - } - - case ObjectSpace.local: - { - if (m_src == null) - { - return TR.FromWorld(transform); - } - - // runtime - var parent = TR.Identity; - if (transform.parent != null) - { - parent = TR.FromWorld(transform.parent); - } - return parent * m_dst.LocalInitial; - } - - default: - throw new NotImplementedException(); - } - } - - public TR GetDstCurrent() - { - var coords = GetDstCoords(); - if (m_src == null) - { - return coords; - } - - return new TR(Delta) * coords; - } - - /// - /// Editorで設定値の変更を反映するために、クリアする - /// - void OnValidate() - { - // Debug.Log("Validate"); - if (m_src != null && m_src.ModelRoot != ModelRoot) - { - m_src = null; - } - if (m_dst != null && m_dst.ModelRoot != ModelRoot) - { - m_dst = null; - } - } - - void Reset() - { - var current = transform; - while (current.parent != null) - { - current = current.parent; - } - ModelRoot = current; - } - - public override void Process() + protected override void UpdateDelta() { if (Source == null) { @@ -187,8 +30,30 @@ namespace UniVRM10 m_dst = new ConstraintDestination(transform, ModelRoot); } - Delta = FreezeAxes.Freeze(m_src.TranslationDelta(SourceCoordinate)); + m_delta = FreezeAxes.Freeze(m_src.TranslationDelta(SourceCoordinate)); m_dst.ApplyTranslation(Delta, Weight, DestinationCoordinate, ModelRoot); } + + public override TR GetSourceCurrent() + { + var coords = GetSourceCoords(); + if (m_src == null) + { + return coords; + } + + return new TR(Delta) * coords; + } + + public override TR GetDstCurrent() + { + var coords = GetDstCoords(); + if (m_src == null) + { + return coords; + } + + return new TR(Delta) * coords; + } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index f3eeea7d3..0dd4801e6 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -1,6 +1,4 @@ -using System; -using UniGLTF.Extensions.VRMC_node_constraint; -using UnityEngine; +using UnityEngine; namespace UniVRM10 @@ -9,78 +7,18 @@ namespace UniVRM10 /// 対象の初期回転と現在回転の差分(delta)を、自身の初期回転と自身の初期回転にdeltaを乗算したものに対してWeightでSlerpする。 /// [DisallowMultipleComponent] - public class VRM10RotationConstraint : VRM10Constraint, IVRM10ConstraintSourceDestination + public class VRM10RotationConstraint : VRM10RotationPositionConstraintBase { - [SerializeField] - [EnumFlags] - public AxisMask FreezeAxes = default; + Quaternion m_delta; - [SerializeField] - [Range(0, 10.0f)] - public float Weight = 1.0f; + public override Vector3 Delta => m_delta.eulerAngles; - [SerializeField] - public Transform ModelRoot = default; - - [Header("Source")] - [SerializeField] - public Transform Source = default; - - [SerializeField] - public ObjectSpace SourceCoordinate = default; - - [SerializeField] - public VRM10RotationOffset SourceOffset = VRM10RotationOffset.Identity; - - [Header("Destination")] - [SerializeField] - public ObjectSpace DestinationCoordinate = default; - - [SerializeField] - public VRM10RotationOffset DestinationOffset = VRM10RotationOffset.Identity; - - ConstraintSource m_src; - - public TR GetSourceCoords() + protected override void UpdateDelta() { - if (Source == null) - { - throw new ConstraintException(ConstraintException.ExceptionTypes.NoSource); - } - - switch (SourceCoordinate) - { - case ObjectSpace.model: - { - if (ModelRoot == null) - { - throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); - } - return new TR(ModelRoot.rotation * SourceOffset.Rotation, Source.position); - } - - case ObjectSpace.local: - { - if (m_src == null) - { - return new TR(Source.rotation * SourceOffset.Rotation, Source.position); - } - - // runtime - var parent = Quaternion.identity; - if (Source.parent != null) - { - parent = Source.parent.rotation; - } - return new TR(parent * m_src.LocalInitial.Rotation * SourceOffset.Rotation, Source.position); - } - - default: - throw new NotImplementedException(); - } + m_delta = m_src.RotationDelta(SourceCoordinate, SourceOffset); } - public TR GetSourceCurrent() + public override TR GetSourceCurrent() { var coords = GetSourceCoords(); if (m_src == null) @@ -88,51 +26,10 @@ namespace UniVRM10 return coords; } - return coords * new TR(Delta); + return coords * new TR(m_delta); } - public Quaternion Delta - { - get; - private set; - } - - ConstraintDestination m_dst; - public TR GetDstCoords() - { - switch (DestinationCoordinate) - { - case ObjectSpace.model: - { - if (ModelRoot == null) - { - throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); - } - return new TR(ModelRoot.rotation * DestinationOffset.Rotation, transform.position); - } - - case ObjectSpace.local: - { - if (m_src == null) - { - return new TR(transform.rotation * DestinationOffset.Rotation, transform.position); - } - - // runtime - var parent = Quaternion.identity; - if (transform.parent != null) - { - parent = transform.parent.rotation; - } - return new TR(parent * m_dst.LocalInitial.Rotation * DestinationOffset.Rotation, transform.position); - } - - default: - throw new NotImplementedException(); - } - } - - public TR GetDstCurrent() + public override TR GetDstCurrent() { var coords = GetDstCoords(); if (m_src == null) @@ -140,62 +37,7 @@ namespace UniVRM10 return coords; } - return coords * new TR(Delta); - } - - /// - /// Editorで設定値の変更を反映するために、クリアする - /// - void OnValidate() - { - // Debug.Log("Validate"); - if (m_src != null && m_src.ModelRoot != ModelRoot) - { - m_src = null; - } - if (m_dst != null && m_dst.ModelRoot != ModelRoot) - { - m_dst = null; - } - } - - void Reset() - { - var current = transform; - while (current.parent != null) - { - current = current.parent; - } - ModelRoot = current; - } - - public override void Process() - { - if (Source == null) - { - enabled = false; - return; - } - - if (m_src == null) - { - m_src = new ConstraintSource(Source, ModelRoot); - } - if (m_dst == null) - { - m_dst = new ConstraintDestination(transform, ModelRoot); - } - - // 回転差分 - Delta = m_src.RotationDelta(SourceCoordinate, SourceOffset.Rotation); - - // 軸制限 - var fleezed = FreezeAxes.Freeze(Delta.eulerAngles); - var rotation = Quaternion.Euler(fleezed); - - // Debug.Log($"{delta} => {rotation}"); - // オイラー角を再度Quaternionへ。weight を加味してSlerpする - m_dst.ApplyRotation(DestinationOffset.Rotation * rotation, Weight, DestinationCoordinate, ModelRoot); + return coords * new TR(m_delta); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs new file mode 100644 index 000000000..9988312c7 --- /dev/null +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs @@ -0,0 +1,218 @@ +using System; +using UniGLTF.Extensions.VRMC_node_constraint; +using UnityEngine; + +namespace UniVRM10 +{ + public abstract class VRM10RotationPositionConstraintBase : VRM10Constraint + { + [SerializeField] + [EnumFlags] + AxisMask m_freezeAxes = default; + public AxisMask FreezeAxes + { + get => m_freezeAxes; + set => m_freezeAxes = value; + } + + [SerializeField] + [Range(0, 10.0f)] + public float Weight = 1.0f; + + [SerializeField] + public Transform ModelRoot = default; + + #region Source + [Header("Source")] + [SerializeField] + public Transform Source = default; + + public Transform GetSource() => Source; + + [SerializeField] + ObjectSpace m_sourceCoordinate = default; + public ObjectSpace SourceCoordinate + { + get => m_sourceCoordinate; + set => m_sourceCoordinate = value; + } + + [SerializeField] + VRM10RotationOffset m_sourceOffset = VRM10RotationOffset.Identity; + + public Quaternion SourceOffset + { + get => m_sourceOffset.Rotation; + set => m_sourceOffset.Rotation = value; + } + #endregion + + + #region Destination + [Header("Destination")] + [SerializeField] + ObjectSpace m_destinationCoordinate = default; + public ObjectSpace DestinationCoordinate + { + get => m_destinationCoordinate; + set => m_destinationCoordinate = value; + } + + [SerializeField] + public VRM10RotationOffset m_destinationOffset = VRM10RotationOffset.Identity; + + public Quaternion DestinationOffset + { + get => m_destinationOffset.Rotation; + set => m_destinationOffset.Rotation = value; + } + #endregion + + public abstract Vector3 Delta { get; } + + protected ConstraintSource m_src; + + public TR GetSourceCoords() + { + if (Source == null) + { + throw new ConstraintException(ConstraintException.ExceptionTypes.NoSource); + } + + switch (SourceCoordinate) + { + case ObjectSpace.model: + { + if (ModelRoot == null) + { + throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); + } + return new TR(ModelRoot.rotation * SourceOffset, Source.position); + } + + case ObjectSpace.local: + { + if (m_src == null) + { + return new TR(Source.rotation * SourceOffset, Source.position); + } + + // runtime + var parent = Quaternion.identity; + if (Source.parent != null) + { + parent = Source.parent.rotation; + } + return new TR(parent * m_src.LocalInitial.Rotation * SourceOffset, Source.position); + } + + default: + throw new NotImplementedException(); + } + } + + public abstract TR GetSourceCurrent(); + + protected ConstraintDestination m_dst; + + public TR GetDstCoords() + { + switch (DestinationCoordinate) + { + case ObjectSpace.model: + { + if (ModelRoot == null) + { + throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); + } + return new TR(ModelRoot.rotation * DestinationOffset, transform.position); + } + + case ObjectSpace.local: + { + if (m_src == null) + { + return new TR(transform.rotation * DestinationOffset, transform.position); + } + + // runtime + var parent = Quaternion.identity; + if (transform.parent != null) + { + parent = transform.parent.rotation; + } + return new TR(parent * m_dst.LocalInitial.Rotation * DestinationOffset, transform.position); + } + + default: + throw new NotImplementedException(); + } + } + + public abstract TR GetDstCurrent(); + + + /// + /// Editorで設定値の変更を反映するために、クリアする + /// + void OnValidate() + { + // Debug.Log("Validate"); + if (m_src != null && m_src.ModelRoot != ModelRoot) + { + m_src = null; + } + if (m_dst != null && m_dst.ModelRoot != ModelRoot) + { + m_dst = null; + } + } + + void Reset() + { + var current = transform; + while (current.parent != null) + { + current = current.parent; + } + ModelRoot = current; + } + + public Component GetComponent() + { + return this; + } + + protected abstract void UpdateDelta(); + + public override void Process() + { + if (Source == null) + { + enabled = false; + return; + } + + if (m_src == null) + { + m_src = new ConstraintSource(Source, ModelRoot); + } + if (m_dst == null) + { + m_dst = new ConstraintDestination(transform, ModelRoot); + } + + // 回転差分 + UpdateDelta(); + + // 軸制限 + var fleezed = FreezeAxes.Freeze(Delta); + var rotation = Quaternion.Euler(fleezed); + + // Debug.Log($"{delta} => {rotation}"); + // オイラー角を再度Quaternionへ。weight を加味してSlerpする + m_dst.ApplyRotation(DestinationOffset * rotation, Weight, DestinationCoordinate, ModelRoot); + + } + } +} diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs.meta b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs.meta new file mode 100644 index 000000000..bac4e2e09 --- /dev/null +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 07919df684c7a4c47a9492a2de417f66 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From cc3568da14459150a229d80ff42b44a26a558df6 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 17 May 2021 17:44:38 +0900 Subject: [PATCH 25/37] Inverse --- Assets/VRM10/Runtime/Components/Constraint/TR.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Assets/VRM10/Runtime/Components/Constraint/TR.cs b/Assets/VRM10/Runtime/Components/Constraint/TR.cs index 14466898f..7a393f267 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/TR.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/TR.cs @@ -36,5 +36,11 @@ namespace UniVRM10 public Matrix4x4 TRS(float s) => Matrix4x4.TRS(Translation, Rotation, new Vector3(s, s, s)); public static TR operator *(TR a, TR b) => new TR(a.Rotation * b.Rotation, a.Rotation * b.Translation + a.Translation); + + public TR Inverse() + { + var inv = Quaternion.Inverse(Rotation); + return new TR(inv, inv * Translation); + } } } From da4ae3f0e5edb02e59ef7e4fe463d3c97fd17c9a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 17 May 2021 17:47:59 +0900 Subject: [PATCH 26/37] UpdateDelta --- .../Constraint/VRM10PositionConstraint.cs | 27 +++++-------------- .../Constraint/VRM10RotationConstraint.cs | 18 +++++++++---- .../VRM10RotationPositionConstraintBase.cs | 9 ------- 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs index 32177527e..a6d1a01f4 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs @@ -13,27 +13,6 @@ namespace UniVRM10 Vector3 m_delta; public override Vector3 Delta => m_delta; - protected override void UpdateDelta() - { - if (Source == null) - { - enabled = false; - return; - } - - if (m_src == null) - { - m_src = new ConstraintSource(Source, ModelRoot); - } - if (m_dst == null) - { - m_dst = new ConstraintDestination(transform, ModelRoot); - } - - m_delta = FreezeAxes.Freeze(m_src.TranslationDelta(SourceCoordinate)); - m_dst.ApplyTranslation(Delta, Weight, DestinationCoordinate, ModelRoot); - } - public override TR GetSourceCurrent() { var coords = GetSourceCoords(); @@ -55,5 +34,11 @@ namespace UniVRM10 return new TR(Delta) * coords; } + + protected override void UpdateDelta() + { + m_delta = FreezeAxes.Freeze(m_src.TranslationDelta(SourceCoordinate)); + m_dst.ApplyTranslation(Delta, Weight, DestinationCoordinate, ModelRoot); + } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index 0dd4801e6..5e3783a2e 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -13,11 +13,6 @@ namespace UniVRM10 public override Vector3 Delta => m_delta.eulerAngles; - protected override void UpdateDelta() - { - m_delta = m_src.RotationDelta(SourceCoordinate, SourceOffset); - } - public override TR GetSourceCurrent() { var coords = GetSourceCoords(); @@ -39,5 +34,18 @@ namespace UniVRM10 return coords * new TR(m_delta); } + + protected override void UpdateDelta() + { + m_delta = m_src.RotationDelta(SourceCoordinate, SourceOffset); + + // 軸制限 + var fleezed = FreezeAxes.Freeze(Delta); + var rotation = Quaternion.Euler(fleezed); + + // Debug.Log($"{delta} => {rotation}"); + // オイラー角を再度Quaternionへ。weight を加味してSlerpする + m_dst.ApplyRotation(DestinationOffset * rotation, Weight, DestinationCoordinate, ModelRoot); + } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs index 9988312c7..ee0c9638b 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs @@ -204,15 +204,6 @@ namespace UniVRM10 // 回転差分 UpdateDelta(); - - // 軸制限 - var fleezed = FreezeAxes.Freeze(Delta); - var rotation = Quaternion.Euler(fleezed); - - // Debug.Log($"{delta} => {rotation}"); - // オイラー角を再度Quaternionへ。weight を加味してSlerpする - m_dst.ApplyRotation(DestinationOffset * rotation, Weight, DestinationCoordinate, ModelRoot); - } } } From c2641b2fb4bc814704afd483fa7d24af03e5ea55 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 17 May 2021 17:56:16 +0900 Subject: [PATCH 27/37] Delta --- .../Components/Constraint/ConstraintSource.cs | 39 ++++++++++++------- .../Constraint/VRM10RotationConstraint.cs | 2 +- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs index 8b88f3c59..f4709b511 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs @@ -19,6 +19,17 @@ namespace UniVRM10 /// public readonly TR LocalInitial; + public TR Delta(ObjectSpace coords, Quaternion sourceRotationOffset) + { + switch (coords) + { + // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); + case ObjectSpace.local: return TR.FromLocal(m_transform) * (LocalInitial * new TR(sourceRotationOffset)).Inverse(); + case ObjectSpace.model: return TR.FromWorld(m_transform) * (TR.FromWorld(ModelRoot) * ModelInitial * new TR(sourceRotationOffset)).Inverse(); + default: throw new NotImplementedException(); + } + } + public Vector3 TranslationDelta(ObjectSpace coords) { switch (coords) @@ -30,30 +41,30 @@ namespace UniVRM10 } } - public Quaternion RotationDelta(ObjectSpace coords, Quaternion sourceRotationOffset) - { - switch (coords) - { - // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); - case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(LocalInitial.Rotation * sourceRotationOffset); - case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelInitial.Rotation * sourceRotationOffset) * Quaternion.Inverse(ModelRoot.rotation); - default: throw new NotImplementedException(); - } - } + // public Quaternion RotationDelta(ObjectSpace coords, Quaternion sourceRotationOffset) + // { + // return Delta(coords, sourceRotationOffset).Rotation; + + // switch (coords) + // { + // // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); + // case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(LocalInitial.Rotation * sourceRotationOffset); + // case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelInitial.Rotation * sourceRotationOffset) * Quaternion.Inverse(ModelRoot.rotation); + // default: throw new NotImplementedException(); + // } + // } public ConstraintSource(Transform t, Transform modelRoot = null) { - m_transform = t; - { + m_transform = t; LocalInitial = TR.FromLocal(t); } if (modelRoot != null) { - var world = TR.FromWorld(t); ModelRoot = modelRoot; - ModelInitial = new TR(world.Rotation * Quaternion.Inverse(ModelRoot.rotation), modelRoot.worldToLocalMatrix.MultiplyPoint(world.Translation)); + ModelInitial = TR.FromWorld(t) * TR.FromWorld(ModelRoot).Inverse(); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index 5e3783a2e..0c5281a55 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -37,7 +37,7 @@ namespace UniVRM10 protected override void UpdateDelta() { - m_delta = m_src.RotationDelta(SourceCoordinate, SourceOffset); + m_delta = m_src.Delta(SourceCoordinate, SourceOffset).Rotation; // 軸制限 var fleezed = FreezeAxes.Freeze(Delta); From cf1b684c56753be7347f8e3e69b0ccf099511808 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 17 May 2021 18:04:05 +0900 Subject: [PATCH 28/37] fix --- Assets/VRM10/Runtime/Components/Constraint/TR.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/TR.cs b/Assets/VRM10/Runtime/Components/Constraint/TR.cs index 7a393f267..72f930863 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/TR.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/TR.cs @@ -40,7 +40,7 @@ namespace UniVRM10 public TR Inverse() { var inv = Quaternion.Inverse(Rotation); - return new TR(inv, inv * Translation); + return new TR(inv, inv * -Translation); } } } From 962ee323856b0fb26a29043cb5130b08bb8e1500 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 17 May 2021 18:29:35 +0900 Subject: [PATCH 29/37] ApplyDelta --- .../Constraint/ConstraintDestination.cs | 16 ++++---- .../Components/Constraint/ConstraintSource.cs | 26 +----------- .../VRM10/Runtime/Components/Constraint/TR.cs | 14 +++++++ .../Constraint/VRM10PositionConstraint.cs | 13 +++--- .../Constraint/VRM10RotationConstraint.cs | 15 +++---- .../VRM10RotationPositionConstraintBase.cs | 41 +++++++++++++------ 6 files changed, 62 insertions(+), 63 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs index f7c5b14c1..550a4e358 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs @@ -7,7 +7,7 @@ namespace UniVRM10 public class ConstraintDestination { readonly Transform m_transform; - readonly TR m_modelInitial; + public readonly TR ModelInitial; public readonly TR LocalInitial; public readonly Transform ModelRoot; @@ -17,10 +17,10 @@ namespace UniVRM10 m_transform = t; LocalInitial = TR.FromLocal(t); - m_modelInitial = TR.FromRelative(t, modelRoot); + ModelInitial = TR.FromRelative(t, modelRoot); } - public void ApplyTranslation(Vector3 delta, float weight, ObjectSpace coords, Transform modelRoot = null) + public void ApplyTranslation(Vector3 delta, float weight, ObjectSpace coords, Quaternion offset, Transform modelRoot = null) { switch (coords) { @@ -30,14 +30,14 @@ namespace UniVRM10 case ObjectSpace.local: { - var value = LocalInitial.Translation + delta * weight; + var value = LocalInitial.Translation + offset * delta * weight; m_transform.localPosition = value; } break; case ObjectSpace.model: { - var value = m_modelInitial.Translation + delta * weight; + var value = ModelInitial.Translation + offset * delta * weight; m_transform.position = modelRoot.localToWorldMatrix.MultiplyPoint(value); } break; @@ -47,7 +47,7 @@ namespace UniVRM10 } } - public void ApplyRotation(Quaternion delta, float weight, ObjectSpace coords, Transform modelRoot = null) + public void ApplyRotation(Quaternion delta, float weight, ObjectSpace coords, Quaternion offset, Transform modelRoot = null) { // 0~1 で clamp しない slerp switch (coords) @@ -58,14 +58,14 @@ namespace UniVRM10 case ObjectSpace.local: { - var value = Quaternion.LerpUnclamped(Quaternion.identity, delta, weight) * LocalInitial.Rotation; + var value = Quaternion.LerpUnclamped(Quaternion.identity, delta * offset, weight) * LocalInitial.Rotation; m_transform.localRotation = value; } break; case ObjectSpace.model: { - var value = Quaternion.LerpUnclamped(Quaternion.identity, delta, weight) * m_modelInitial.Rotation; + var value = Quaternion.LerpUnclamped(Quaternion.identity, delta * offset, weight) * ModelInitial.Rotation; m_transform.rotation = modelRoot.rotation * value; } break; diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs index f4709b511..5a09be429 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs @@ -30,30 +30,6 @@ namespace UniVRM10 } } - public Vector3 TranslationDelta(ObjectSpace coords) - { - switch (coords) - { - // case ObjectSpace.World: return m_transform.position - m_initial.Translation; - case ObjectSpace.local: return m_transform.localPosition - LocalInitial.Translation; - case ObjectSpace.model: return ModelRoot.worldToLocalMatrix.MultiplyPoint(m_transform.position) - ModelInitial.Translation; - default: throw new NotImplementedException(); - } - } - - // public Quaternion RotationDelta(ObjectSpace coords, Quaternion sourceRotationOffset) - // { - // return Delta(coords, sourceRotationOffset).Rotation; - - // switch (coords) - // { - // // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); - // case ObjectSpace.local: return m_transform.localRotation * Quaternion.Inverse(LocalInitial.Rotation * sourceRotationOffset); - // case ObjectSpace.model: return m_transform.rotation * Quaternion.Inverse(ModelInitial.Rotation * sourceRotationOffset) * Quaternion.Inverse(ModelRoot.rotation); - // default: throw new NotImplementedException(); - // } - // } - public ConstraintSource(Transform t, Transform modelRoot = null) { { @@ -64,7 +40,7 @@ namespace UniVRM10 if (modelRoot != null) { ModelRoot = modelRoot; - ModelInitial = TR.FromWorld(t) * TR.FromWorld(ModelRoot).Inverse(); + ModelInitial = TR.FromLocal(t); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/TR.cs b/Assets/VRM10/Runtime/Components/Constraint/TR.cs index 72f930863..7e656487e 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/TR.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/TR.cs @@ -35,8 +35,22 @@ namespace UniVRM10 public Matrix4x4 TRS(float s) => Matrix4x4.TRS(Translation, Rotation, new Vector3(s, s, s)); + + /// + /// R1|T1 R2|T2 x R1R2|R1T2+T1 x + /// --+-- --+-- y => ----+------- y + /// 0| 1 0| 1 z 0| 1 z + /// + /// + /// + /// public static TR operator *(TR a, TR b) => new TR(a.Rotation * b.Rotation, a.Rotation * b.Translation + a.Translation); + /// + /// R|0 1|T R|RT + /// -+- -+- => -+-- + /// 0|1 0|1 0| 1 + /// public TR Inverse() { var inv = Quaternion.Inverse(Rotation); diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs index a6d1a01f4..b3dad7f52 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs @@ -10,8 +10,7 @@ namespace UniVRM10 [DisallowMultipleComponent] public class VRM10PositionConstraint : VRM10RotationPositionConstraintBase { - Vector3 m_delta; - public override Vector3 Delta => m_delta; + public override Vector3 Delta => m_delta.Translation; public override TR GetSourceCurrent() { @@ -21,7 +20,7 @@ namespace UniVRM10 return coords; } - return new TR(Delta) * coords; + return coords * new TR(m_delta.Translation); } public override TR GetDstCurrent() @@ -32,13 +31,13 @@ namespace UniVRM10 return coords; } - return new TR(Delta) * coords; + return coords * new TR(m_delta.Translation); } - protected override void UpdateDelta() + protected override void ApplyDelta() { - m_delta = FreezeAxes.Freeze(m_src.TranslationDelta(SourceCoordinate)); - m_dst.ApplyTranslation(Delta, Weight, DestinationCoordinate, ModelRoot); + var freezed = FreezeAxes.Freeze(Delta); + m_dst.ApplyTranslation(freezed, Weight, DestinationCoordinate, DestinationOffset, ModelRoot); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index 0c5281a55..28ca773eb 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -9,9 +9,7 @@ namespace UniVRM10 [DisallowMultipleComponent] public class VRM10RotationConstraint : VRM10RotationPositionConstraintBase { - Quaternion m_delta; - - public override Vector3 Delta => m_delta.eulerAngles; + public override Vector3 Delta => m_delta.Rotation.eulerAngles; public override TR GetSourceCurrent() { @@ -21,7 +19,7 @@ namespace UniVRM10 return coords; } - return coords * new TR(m_delta); + return coords * new TR(m_delta.Rotation); } public override TR GetDstCurrent() @@ -32,20 +30,17 @@ namespace UniVRM10 return coords; } - return coords * new TR(m_delta); + return coords * new TR(m_delta.Rotation); } - protected override void UpdateDelta() + protected override void ApplyDelta() { - m_delta = m_src.Delta(SourceCoordinate, SourceOffset).Rotation; - // 軸制限 var fleezed = FreezeAxes.Freeze(Delta); var rotation = Quaternion.Euler(fleezed); - // Debug.Log($"{delta} => {rotation}"); // オイラー角を再度Quaternionへ。weight を加味してSlerpする - m_dst.ApplyRotation(DestinationOffset * rotation, Weight, DestinationCoordinate, ModelRoot); + m_dst.ApplyRotation(rotation, Weight, DestinationCoordinate, DestinationOffset, ModelRoot); } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs index ee0c9638b..0e1995e0c 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs @@ -68,8 +68,6 @@ namespace UniVRM10 } #endregion - public abstract Vector3 Delta { get; } - protected ConstraintSource m_src; public TR GetSourceCoords() @@ -87,14 +85,21 @@ namespace UniVRM10 { throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); } - return new TR(ModelRoot.rotation * SourceOffset, Source.position); + + if (m_src == null) + { + return TR.FromWorld(Source) * new TR(SourceOffset); + } + + // runtime + return TR.FromWorld(ModelRoot) * new TR(SourceOffset, Source.position); } case ObjectSpace.local: { if (m_src == null) { - return new TR(Source.rotation * SourceOffset, Source.position); + return TR.FromWorld(Source) * new TR(SourceOffset); } // runtime @@ -125,23 +130,30 @@ namespace UniVRM10 { throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); } - return new TR(ModelRoot.rotation * DestinationOffset, transform.position); + + if (m_dst == null) + { + return TR.FromWorld(transform) * new TR(DestinationOffset); + } + + // runtime + return TR.FromWorld(ModelRoot) * m_dst.ModelInitial * new TR(DestinationOffset); } case ObjectSpace.local: { - if (m_src == null) + if (m_dst == null) { - return new TR(transform.rotation * DestinationOffset, transform.position); + return TR.FromWorld(transform) * new TR(DestinationOffset); } // runtime - var parent = Quaternion.identity; + var parent = TR.Identity; if (transform.parent != null) { - parent = transform.parent.rotation; + parent = TR.FromWorld(transform.parent); } - return new TR(parent * m_dst.LocalInitial.Rotation * DestinationOffset, transform.position); + return parent * m_dst.LocalInitial * new TR(DestinationOffset); } default: @@ -183,7 +195,10 @@ namespace UniVRM10 return this; } - protected abstract void UpdateDelta(); + protected TR m_delta; + public abstract Vector3 Delta { get; } + + protected abstract void ApplyDelta(); public override void Process() { @@ -202,8 +217,8 @@ namespace UniVRM10 m_dst = new ConstraintDestination(transform, ModelRoot); } - // 回転差分 - UpdateDelta(); + m_delta = m_src.Delta(SourceCoordinate, SourceOffset); + ApplyDelta(); } } } From 51d5144e34cfca8e96e1601971a8f27a7c18f6cd Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 18 May 2021 13:15:32 +0900 Subject: [PATCH 30/37] remove IVRM10ConstraintSourceDestinationExtensions --- ...10ConstraintSourceDestinationExtensions.cs | 59 ------------------- ...straintSourceDestinationExtensions.cs.meta | 11 ---- ...RM10PostionRotationConstraintEditorBase.cs | 19 ++++-- 3 files changed, 15 insertions(+), 74 deletions(-) delete mode 100644 Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs delete mode 100644 Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs.meta diff --git a/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs b/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs deleted file mode 100644 index 2fc95decd..000000000 --- a/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs +++ /dev/null @@ -1,59 +0,0 @@ -using UnityEditor; -using UnityEngine; - -namespace UniVRM10 -{ - public static class VRM10PositionRotationConstraintBaseExtensions - { - public static void DrawSourceCoords(this VRM10RotationPositionConstraintBase self) - { - try - { - self.GetSourceCoords().Draw(0.2f); - } - catch (ConstraintException) - { - - } - } - public static void DrawSourceCurrent(this VRM10RotationPositionConstraintBase self) - { - try - { - Handles.matrix = self.GetSourceCurrent().TRS(0.05f); - Handles.color = Color.yellow; - Handles.DrawWireCube(Vector3.zero, Vector3.one); - } - catch (ConstraintException) - { - - } - } - - public static void DrawDstCoords(this VRM10RotationPositionConstraintBase self) - { - try - { - self.GetDstCoords().Draw(0.2f); - } - catch (ConstraintException) - { - - } - } - - public static void DrawDstCurrent(this VRM10RotationPositionConstraintBase self) - { - try - { - Handles.matrix = self.GetDstCurrent().TRS(0.05f); - Handles.color = Color.yellow; - Handles.DrawWireCube(Vector3.zero, Vector3.one); - } - catch (ConstraintException) - { - - } - } - } -} diff --git a/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs.meta b/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs.meta deleted file mode 100644 index a2836e116..000000000 --- a/Assets/VRM10/Editor/Components/Constraint/IVRM10ConstraintSourceDestinationExtensions.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 59e1ae1f9d4fb9a459cd2c97a4351b1f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs index 35b6f5f50..e058dfb73 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs @@ -104,10 +104,21 @@ namespace UniVRM10 Handles.Label(m_target.GetComponent().transform.position, sb.ToString(), Style); } - m_target.DrawSourceCoords(); - m_target.DrawSourceCurrent(); - m_target.DrawDstCoords(); - m_target.DrawDstCurrent(); + m_target.GetSourceCoords().Draw(0.2f); + if (Application.isPlaying) + { + Handles.matrix = m_target.GetSourceCurrent().TRS(0.05f); + Handles.color = Color.yellow; + Handles.DrawWireCube(Vector3.zero, Vector3.one); + } + + m_target.GetDstCoords().Draw(0.2f); + if (Application.isPlaying) + { + Handles.matrix = m_target.GetDstCurrent().TRS(0.05f); + Handles.color = Color.yellow; + Handles.DrawWireCube(Vector3.zero, Vector3.one); + } } } } From b99b66f47b6b8269f1cef9859e5c683f55501e75 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 18 May 2021 13:38:29 +0900 Subject: [PATCH 31/37] GetSourceCoords --- .../VRM10/Runtime/Components/Constraint/TR.cs | 2 + .../VRM10RotationPositionConstraintBase.cs | 52 ++++++++++++------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/TR.cs b/Assets/VRM10/Runtime/Components/Constraint/TR.cs index 7e656487e..de7bfa81a 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/TR.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/TR.cs @@ -11,6 +11,8 @@ namespace UniVRM10 public static TR FromWorld(Transform t) => new TR(t.rotation, t.position); + public static TR FromParent(Transform t) => t.parent != null ? FromWorld(t.parent) : TR.Identity; + public static TR FromLocal(Transform t) => new TR(t.localRotation, t.localPosition); public static TR FromRelative(Transform t, Transform from) diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs index 0e1995e0c..8f8e5d4da 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs @@ -70,6 +70,36 @@ namespace UniVRM10 protected ConstraintSource m_src; + TR ModelCoords + { + get + { + if (m_src == null) + { + return TR.FromWorld(ModelRoot); + } + else + { + return TR.FromParent(ModelRoot) * m_src.ModelInitial; + } + } + } + + TR SourceCoords + { + get + { + if (m_src == null) + { + return TR.FromWorld(Source); + } + else + { + return TR.FromParent(Source) * m_src.LocalInitial; + } + } + } + public TR GetSourceCoords() { if (Source == null) @@ -85,30 +115,12 @@ namespace UniVRM10 { throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); } - - if (m_src == null) - { - return TR.FromWorld(Source) * new TR(SourceOffset); - } - - // runtime - return TR.FromWorld(ModelRoot) * new TR(SourceOffset, Source.position); + return new TR(ModelCoords.Rotation * SourceOffset, SourceCoords.Translation); } case ObjectSpace.local: { - if (m_src == null) - { - return TR.FromWorld(Source) * new TR(SourceOffset); - } - - // runtime - var parent = Quaternion.identity; - if (Source.parent != null) - { - parent = Source.parent.rotation; - } - return new TR(parent * m_src.LocalInitial.Rotation * SourceOffset, Source.position); + return new TR(SourceCoords.Rotation * SourceOffset, SourceCoords.Translation); } default: From a6f4e1cc52c72e994ae9d7cdd3fec3940c560f24 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 18 May 2021 13:42:20 +0900 Subject: [PATCH 32/37] GetDstCoords --- .../VRM10RotationPositionConstraintBase.cs | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs index 8f8e5d4da..6316b389b 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs @@ -70,7 +70,7 @@ namespace UniVRM10 protected ConstraintSource m_src; - TR ModelCoords + TR SourceModelCoords { get { @@ -115,7 +115,7 @@ namespace UniVRM10 { throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); } - return new TR(ModelCoords.Rotation * SourceOffset, SourceCoords.Translation); + return new TR(SourceModelCoords.Rotation * SourceOffset, SourceCoords.Translation); } case ObjectSpace.local: @@ -132,6 +132,36 @@ namespace UniVRM10 protected ConstraintDestination m_dst; + TR DestinationModelCoords + { + get + { + if (m_dst == null) + { + return TR.FromWorld(ModelRoot); + } + else + { + return TR.FromParent(ModelRoot) * m_dst.ModelInitial; + } + } + } + + TR DestinationCoords + { + get + { + if (m_dst == null) + { + return TR.FromWorld(transform); + } + else + { + return TR.FromParent(transform) * m_dst.LocalInitial; + } + } + } + public TR GetDstCoords() { switch (DestinationCoordinate) @@ -142,30 +172,12 @@ namespace UniVRM10 { throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); } - - if (m_dst == null) - { - return TR.FromWorld(transform) * new TR(DestinationOffset); - } - - // runtime - return TR.FromWorld(ModelRoot) * m_dst.ModelInitial * new TR(DestinationOffset); + return new TR(DestinationModelCoords.Rotation * DestinationOffset, DestinationCoords.Translation); } case ObjectSpace.local: { - if (m_dst == null) - { - return TR.FromWorld(transform) * new TR(DestinationOffset); - } - - // runtime - var parent = TR.Identity; - if (transform.parent != null) - { - parent = TR.FromWorld(transform.parent); - } - return parent * m_dst.LocalInitial * new TR(DestinationOffset); + return new TR(DestinationCoords.Rotation * DestinationOffset, DestinationCoords.Translation); } default: From 607d0e07b22b9027472bf63d9add975065905c6e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 18 May 2021 14:14:14 +0900 Subject: [PATCH 33/37] fix postition and rotation --- .../Constraint/ConstraintDestination.cs | 68 ++++--------------- .../Components/Constraint/ConstraintSource.cs | 14 ++-- .../Constraint/VRM10PositionConstraint.cs | 23 +++++-- .../Constraint/VRM10RotationConstraint.cs | 26 ++++--- .../VRM10RotationPositionConstraintBase.cs | 2 +- 5 files changed, 50 insertions(+), 83 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs index 550a4e358..10af3f818 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs @@ -1,78 +1,36 @@ -using System; -using UniGLTF.Extensions.VRMC_node_constraint; using UnityEngine; namespace UniVRM10 { public class ConstraintDestination { - readonly Transform m_transform; + public readonly Transform Destination; public readonly TR ModelInitial; public readonly TR LocalInitial; public readonly Transform ModelRoot; public ConstraintDestination(Transform t, Transform modelRoot = null) { - ModelRoot = modelRoot; - m_transform = t; - + Destination = t; LocalInitial = TR.FromLocal(t); - ModelInitial = TR.FromRelative(t, modelRoot); - } - public void ApplyTranslation(Vector3 delta, float weight, ObjectSpace coords, Quaternion offset, Transform modelRoot = null) - { - switch (coords) + if (modelRoot != null) { - // case DestinationCoordinates.World: - // m_transform.position = value; - // break; - - case ObjectSpace.local: - { - var value = LocalInitial.Translation + offset * delta * weight; - m_transform.localPosition = value; - } - break; - - case ObjectSpace.model: - { - var value = ModelInitial.Translation + offset * delta * weight; - m_transform.position = modelRoot.localToWorldMatrix.MultiplyPoint(value); - } - break; - - default: - throw new NotImplementedException(); + ModelRoot = modelRoot; + ModelInitial = TR.FromRelative(t, modelRoot); } } - public void ApplyRotation(Quaternion delta, float weight, ObjectSpace coords, Quaternion offset, Transform modelRoot = null) + public void ApplyLocal(TR tr) { - // 0~1 で clamp しない slerp - switch (coords) - { - // case DestinationCoordinates.World: - // m_transform.rotation = value; - // break; + Destination.localPosition = tr.Translation; + Destination.localRotation = tr.Rotation; + } - case ObjectSpace.local: - { - var value = Quaternion.LerpUnclamped(Quaternion.identity, delta * offset, weight) * LocalInitial.Rotation; - m_transform.localRotation = value; - } - break; - - case ObjectSpace.model: - { - var value = Quaternion.LerpUnclamped(Quaternion.identity, delta * offset, weight) * ModelInitial.Rotation; - m_transform.rotation = modelRoot.rotation * value; - } - break; - - default: - throw new NotImplementedException(); - } + public void ApplyModel(TR tr) + { + Destination.position = tr.Translation; + Destination.rotation = tr.Rotation; } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs index 5a09be429..3b0fa4a79 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintSource.cs @@ -7,16 +7,10 @@ namespace UniVRM10 public class ConstraintSource { public readonly Transform ModelRoot; - readonly Transform m_transform; + readonly Transform Source; - /// - /// initial: ModelRoot.localToWorldMatrix^-1 * t.localToWorldMatrix - /// public readonly TR ModelInitial; - /// - /// initial: t.localPosition, t.localRotation, t.localScale - /// public readonly TR LocalInitial; public TR Delta(ObjectSpace coords, Quaternion sourceRotationOffset) @@ -24,8 +18,8 @@ namespace UniVRM10 switch (coords) { // case SourceCoordinates.World: return m_transform.rotation * Quaternion.Inverse(m_initial.Rotation); - case ObjectSpace.local: return TR.FromLocal(m_transform) * (LocalInitial * new TR(sourceRotationOffset)).Inverse(); - case ObjectSpace.model: return TR.FromWorld(m_transform) * (TR.FromWorld(ModelRoot) * ModelInitial * new TR(sourceRotationOffset)).Inverse(); + case ObjectSpace.local: return TR.FromLocal(Source) * (LocalInitial * new TR(sourceRotationOffset)).Inverse(); + case ObjectSpace.model: return TR.FromWorld(Source) * (TR.FromWorld(ModelRoot) * ModelInitial * new TR(sourceRotationOffset)).Inverse(); default: throw new NotImplementedException(); } } @@ -33,7 +27,7 @@ namespace UniVRM10 public ConstraintSource(Transform t, Transform modelRoot = null) { { - m_transform = t; + Source = t; LocalInitial = TR.FromLocal(t); } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs index b3dad7f52..abeec4b9f 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs @@ -10,7 +10,7 @@ namespace UniVRM10 [DisallowMultipleComponent] public class VRM10PositionConstraint : VRM10RotationPositionConstraintBase { - public override Vector3 Delta => m_delta.Translation; + public override Vector3 Delta => FreezeAxes.Freeze(m_delta.Translation) * Weight; public override TR GetSourceCurrent() { @@ -19,8 +19,7 @@ namespace UniVRM10 { return coords; } - - return coords * new TR(m_delta.Translation); + return coords * new TR(Delta); } public override TR GetDstCurrent() @@ -30,14 +29,24 @@ namespace UniVRM10 { return coords; } - - return coords * new TR(m_delta.Translation); + return coords * new TR(Delta); } protected override void ApplyDelta() { - var freezed = FreezeAxes.Freeze(Delta); - m_dst.ApplyTranslation(freezed, Weight, DestinationCoordinate, DestinationOffset, ModelRoot); + switch (DestinationCoordinate) + { + case ObjectSpace.local: + m_dst.ApplyLocal(m_dst.LocalInitial * new TR(DestinationOffset) * new TR(Delta)); + break; + + case ObjectSpace.model: + m_dst.ApplyModel(DestinationCoords * new TR(DestinationOffset) * new TR(Delta)); + break; + + default: + throw new NotImplementedException(); + } } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index 28ca773eb..b78481eb1 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -1,5 +1,6 @@ -using UnityEngine; - +using System; +using UniGLTF.Extensions.VRMC_node_constraint; +using UnityEngine; namespace UniVRM10 { @@ -9,7 +10,7 @@ namespace UniVRM10 [DisallowMultipleComponent] public class VRM10RotationConstraint : VRM10RotationPositionConstraintBase { - public override Vector3 Delta => m_delta.Rotation.eulerAngles; + public override Vector3 Delta => FreezeAxes.Freeze(Quaternion.Slerp(Quaternion.identity, m_delta.Rotation, Weight).eulerAngles); public override TR GetSourceCurrent() { @@ -18,7 +19,6 @@ namespace UniVRM10 { return coords; } - return coords * new TR(m_delta.Rotation); } @@ -29,18 +29,24 @@ namespace UniVRM10 { return coords; } - return coords * new TR(m_delta.Rotation); } protected override void ApplyDelta() { - // 軸制限 - var fleezed = FreezeAxes.Freeze(Delta); - var rotation = Quaternion.Euler(fleezed); + switch (DestinationCoordinate) + { + case ObjectSpace.local: + m_dst.ApplyLocal(m_dst.LocalInitial * new TR(DestinationOffset) * new TR(Quaternion.Euler(Delta))); + break; - // オイラー角を再度Quaternionへ。weight を加味してSlerpする - m_dst.ApplyRotation(rotation, Weight, DestinationCoordinate, DestinationOffset, ModelRoot); + case ObjectSpace.model: + m_dst.ApplyModel(DestinationCoords * new TR(DestinationOffset) * new TR(Quaternion.Euler(Delta))); + break; + + default: + throw new NotImplementedException(); + } } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs index 6316b389b..552d45d91 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs @@ -147,7 +147,7 @@ namespace UniVRM10 } } - TR DestinationCoords + public TR DestinationCoords { get { From 2bb6d108610b84585197ba26407c97f1f15ea74d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 18 May 2021 14:21:08 +0900 Subject: [PATCH 34/37] rename --- .../Components/Constraint/VRM10PositionConstraint.cs | 2 +- .../Components/Constraint/VRM10RotationConstraint.cs | 2 +- .../VRM10RotationPositionConstraintBase.cs | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs index abeec4b9f..12d9f82ba 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs @@ -41,7 +41,7 @@ namespace UniVRM10 break; case ObjectSpace.model: - m_dst.ApplyModel(DestinationCoords * new TR(DestinationOffset) * new TR(Delta)); + m_dst.ApplyModel(DestinationInitialCoords * new TR(DestinationOffset) * new TR(Delta)); break; default: diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs index b78481eb1..31b1c509b 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationConstraint.cs @@ -41,7 +41,7 @@ namespace UniVRM10 break; case ObjectSpace.model: - m_dst.ApplyModel(DestinationCoords * new TR(DestinationOffset) * new TR(Quaternion.Euler(Delta))); + m_dst.ApplyModel(DestinationInitialCoords * new TR(DestinationOffset) * new TR(Quaternion.Euler(Delta))); break; default: diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs index 552d45d91..d3875e93a 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs @@ -85,7 +85,7 @@ namespace UniVRM10 } } - TR SourceCoords + TR SourceInitialCoords { get { @@ -115,12 +115,12 @@ namespace UniVRM10 { throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); } - return new TR(SourceModelCoords.Rotation * SourceOffset, SourceCoords.Translation); + return new TR(SourceModelCoords.Rotation * SourceOffset, SourceInitialCoords.Translation); } case ObjectSpace.local: { - return new TR(SourceCoords.Rotation * SourceOffset, SourceCoords.Translation); + return new TR(SourceInitialCoords.Rotation * SourceOffset, SourceInitialCoords.Translation); } default: @@ -147,7 +147,7 @@ namespace UniVRM10 } } - public TR DestinationCoords + public TR DestinationInitialCoords { get { @@ -172,12 +172,12 @@ namespace UniVRM10 { throw new ConstraintException(ConstraintException.ExceptionTypes.NoModelWithModelSpace); } - return new TR(DestinationModelCoords.Rotation * DestinationOffset, DestinationCoords.Translation); + return new TR(DestinationModelCoords.Rotation * DestinationOffset, DestinationInitialCoords.Translation); } case ObjectSpace.local: { - return new TR(DestinationCoords.Rotation * DestinationOffset, DestinationCoords.Translation); + return new TR(DestinationInitialCoords.Rotation * DestinationOffset, DestinationInitialCoords.Translation); } default: From 9a2fb090d62518578be7a9b658fe2028ab2cb087 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 18 May 2021 16:59:32 +0900 Subject: [PATCH 35/37] fix aim logic --- .../Components/Constraint/VRM10AimConstraint.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs index e68cc765d..2d7d22413 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs @@ -60,10 +60,12 @@ namespace UniVRM10 return; } - var m = Matrix4x4.TRS(transform.position, ParentRotation * Logic.InitialLocalRotation * DestinationOffset, Vector3.one); - (Yaw, Pitch) = m.CalcYawPitch(Source.position); - Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(Pitch, 0, 0); - transform.rotation = ParentRotation * Logic.InitialLocalRotation * DestinationOffset * Delta; + var zAxis = (Source.position - transform.position).normalized; + var xAxis = Vector3.Cross(Vector3.up, zAxis); + var yAxis = Vector3.Cross(zAxis, xAxis); + var m = new Matrix4x4(xAxis, yAxis, zAxis, new Vector4(0, 0, 0, 1)); + Delta = Quaternion.Inverse(ParentRotation * Logic.InitialLocalRotation * DestinationOffset) * m.rotation; + transform.rotation = ParentRotation * Logic.InitialLocalRotation * Delta; } public float Yaw; From f1e0a2f3f98c7acdb7244ce2e9f8b154fead9b16 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 18 May 2021 17:04:16 +0900 Subject: [PATCH 36/37] fix --- .../Constraint/ConstRaintException.cs | 20 +++++++++++++++++++ .../Constraint/ConstRaintException.cs.meta | 11 ++++++++++ 2 files changed, 31 insertions(+) create mode 100644 Assets/VRM10/Runtime/Components/Constraint/ConstRaintException.cs create mode 100644 Assets/VRM10/Runtime/Components/Constraint/ConstRaintException.cs.meta diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstRaintException.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstRaintException.cs new file mode 100644 index 000000000..5ec92109a --- /dev/null +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstRaintException.cs @@ -0,0 +1,20 @@ +using System; + +namespace UniVRM10 +{ + public class ConstraintException : Exception + { + public enum ExceptionTypes + { + NoSource, + NoModelWithModelSpace + } + + public readonly ExceptionTypes Type; + + public ConstraintException(ExceptionTypes type) + { + Type = type; + } + } +} diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstRaintException.cs.meta b/Assets/VRM10/Runtime/Components/Constraint/ConstRaintException.cs.meta new file mode 100644 index 000000000..afc5fb285 --- /dev/null +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstRaintException.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 13be84b73aeb40641978e083c750590d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 6d82fc188c431104018b296ee47fa91366c742e9 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 18 May 2021 17:06:32 +0900 Subject: [PATCH 37/37] =?UTF-8?q?=E6=A1=81=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VRM10PostionRotationConstraintEditorBase.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs index e058dfb73..99ee8fbd6 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs @@ -88,9 +88,9 @@ namespace UniVRM10 sb.AppendLine(); sb.AppendLine(); sb.AppendLine($"source: {m_target.SourceCoordinate}"); - sb.AppendLine($"{delta.x:0.}"); - sb.AppendLine($"{delta.y:0.}"); - sb.Append($"{delta.z:0.}"); + sb.AppendLine($"{delta.x:0.00}"); + sb.AppendLine($"{delta.y:0.00}"); + sb.Append($"{delta.z:0.00}"); Handles.Label(m_target.GetSource().position, sb.ToString(), Style); } @@ -98,9 +98,9 @@ namespace UniVRM10 { var sb = new StringBuilder(); sb.AppendLine($"constraint: {m_target.DestinationCoordinate}"); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.}"); - sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.}"); - sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.00}"); + sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.00}"); + sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.00}"); Handles.Label(m_target.GetComponent().transform.position, sb.ToString(), Style); }