From b3cbe06c6ebcb3cc09f27b8a90bc0c3277c4445f Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 18 May 2021 18:14:04 +0900 Subject: [PATCH] implement aim up space MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit aim のUPVectorを評価する空間を world から model/local に修正 --- .../Constraint/VRM10AimConstraintEditor.cs | 4 + ...RM10PostionRotationConstraintEditorBase.cs | 8 +- .../Constraint/VRM10AimConstraint.cs | 54 ++++---- .../Components/Constraint/VRM10Constraint.cs | 118 +++++++++++++++++- .../VRM10RotationPositionConstraintBase.cs | 104 +-------------- Assets/VRM10/Runtime/IO/Vrm10Importer.cs | 6 +- 6 files changed, 159 insertions(+), 135 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs index 71ff8f7b9..8ba5d8e95 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs @@ -78,6 +78,10 @@ namespace UniVRM10 new TR(init, m_target.transform.position).Draw(0.2f); DrawAimUp(init * m_target.DestinationOffset * m_target.Delta, m_target.transform.position, Color.magenta); } + + // Target UPVector + Handles.color = Color.red; + Handles.DrawLine(m_target.transform.position, m_target.transform.position + m_target.UpVector); } } } diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs index 99ee8fbd6..9250dc359 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10PostionRotationConstraintEditorBase.cs @@ -47,7 +47,7 @@ namespace UniVRM10 public void OnSceneGUI() { - if (m_target.GetSource() == null) + if (m_target.Source == null) { return; } @@ -56,7 +56,7 @@ namespace UniVRM10 if (!Application.isPlaying) { EditorGUI.BeginChangeCheck(); - Quaternion offset = Handles.RotationHandle(m_target.SourceOffset, m_target.GetSource().position); + Quaternion offset = Handles.RotationHandle(m_target.SourceOffset, m_target.Source.position); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(m_target.GetComponent(), "source offset"); @@ -78,7 +78,7 @@ namespace UniVRM10 // this to target line Handles.color = Color.yellow; - Handles.DrawLine(m_target.GetSource().position, m_target.GetComponent().transform.position); + Handles.DrawLine(m_target.Source.position, m_target.GetComponent().transform.position); var delta = Clamp180(m_target.Delta); @@ -91,7 +91,7 @@ namespace UniVRM10 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); + Handles.Label(m_target.Source.position, sb.ToString(), Style); } // show dst diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs index 9808b04e8..0c7d72606 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs @@ -8,16 +8,10 @@ namespace UniVRM10 [DisallowMultipleComponent] public class VRM10AimConstraint : VRM10Constraint { - [SerializeField] - [Range(0, 10.0f)] - public float Weight = 1.0f; - - [SerializeField] - public Transform ModelRoot = default; - [Header("Source")] [SerializeField] - public Transform Source = default; + public Transform m_source = default; + public override Transform Source => m_source; [Header("Destination")] [SerializeField] @@ -26,33 +20,41 @@ namespace UniVRM10 [SerializeField] public Quaternion DestinationOffset = Quaternion.identity; - public ConstraintSource m_src; + Quaternion m_delta; + public Quaternion Delta => m_delta; - public Quaternion Delta; - - /// - /// TargetのUpdateよりも先か後かはその時による。 - /// 厳密に制御するのは無理。 - /// - public override void Process() + public Vector3 UpVector { - if (Source == null) + get { - enabled = false; - return; - } + switch (m_destinationCoordinate) + { + case ObjectSpace.model: return ModelRoot.up; - if (m_src == null) - { - m_src = new ConstraintSource(Source, ModelRoot); - } + case ObjectSpace.local: + { + if (m_src == null) + { + return transform.up; + } + return (TR.FromParent(transform).Rotation * m_dst.LocalInitial.Rotation) * Vector3.up; + } + + default: + throw new NotImplementedException(); + } + } + } + + public override void OnProcess() + { var zAxis = (Source.position - transform.position).normalized; - var xAxis = Vector3.Cross(Vector3.up, zAxis); + var xAxis = Vector3.Cross(UpVector, zAxis); var yAxis = Vector3.Cross(zAxis, xAxis); var m = new Matrix4x4(xAxis, yAxis, zAxis, new Vector4(0, 0, 0, 1)); var parent = TR.FromParent(transform); - Delta = Quaternion.Inverse(parent.Rotation * m_src.LocalInitial.Rotation * DestinationOffset) * m.rotation; + m_delta = Quaternion.Inverse(parent.Rotation * m_src.LocalInitial.Rotation * DestinationOffset) * m.rotation; transform.rotation = parent.Rotation * m_src.LocalInitial.Rotation * Delta; } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs index fe63c3d35..b76ffb81d 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10Constraint.cs @@ -5,7 +5,123 @@ namespace UniVRM10 { public abstract class VRM10Constraint : MonoBehaviour { - public abstract void Process(); + [SerializeField] + [Range(0, 10.0f)] + public float Weight = 1.0f; + [SerializeField] + public Transform ModelRoot = default; + + protected virtual void Reset() + { + var current = transform; + while (true) + { + current = current.parent; + if (current.parent == null) + { + // root + break; + } + if (current.GetComponent() != null) + { + // model root + break; + } + } + ModelRoot = current; + } + + #region Source + public abstract Transform Source { get; } + + public ConstraintSource m_src; + + protected TR SourceModelCoords + { + get + { + if (m_src == null) + { + return TR.FromWorld(ModelRoot); + } + else + { + return TR.FromParent(ModelRoot) * m_src.ModelInitial; + } + } + } + + protected TR SourceInitialCoords + { + get + { + if (m_src == null) + { + return TR.FromWorld(Source); + } + else + { + return TR.FromParent(Source) * m_src.LocalInitial; + } + } + } + #endregion + + #region Destination + protected ConstraintDestination m_dst; + + protected TR DestinationModelCoords + { + get + { + if (m_dst == null) + { + return TR.FromWorld(ModelRoot); + } + else + { + return TR.FromParent(ModelRoot) * m_dst.ModelInitial; + } + } + } + + public TR DestinationInitialCoords + { + get + { + if (m_dst == null) + { + return TR.FromWorld(transform); + } + else + { + return TR.FromParent(transform) * m_dst.LocalInitial; + } + } + } + #endregion + + public 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); + } + + OnProcess(); + } + + public abstract void OnProcess(); } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs index d3875e93a..8bb02af0b 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10RotationPositionConstraintBase.cs @@ -15,19 +15,11 @@ namespace UniVRM10 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; + public Transform m_source = default; + public override Transform Source => m_source; [SerializeField] ObjectSpace m_sourceCoordinate = default; @@ -68,38 +60,6 @@ namespace UniVRM10 } #endregion - protected ConstraintSource m_src; - - TR SourceModelCoords - { - get - { - if (m_src == null) - { - return TR.FromWorld(ModelRoot); - } - else - { - return TR.FromParent(ModelRoot) * m_src.ModelInitial; - } - } - } - - TR SourceInitialCoords - { - get - { - if (m_src == null) - { - return TR.FromWorld(Source); - } - else - { - return TR.FromParent(Source) * m_src.LocalInitial; - } - } - } - public TR GetSourceCoords() { if (Source == null) @@ -130,38 +90,6 @@ namespace UniVRM10 public abstract TR GetSourceCurrent(); - protected ConstraintDestination m_dst; - - TR DestinationModelCoords - { - get - { - if (m_dst == null) - { - return TR.FromWorld(ModelRoot); - } - else - { - return TR.FromParent(ModelRoot) * m_dst.ModelInitial; - } - } - } - - public TR DestinationInitialCoords - { - get - { - if (m_dst == null) - { - return TR.FromWorld(transform); - } - else - { - return TR.FromParent(transform) * m_dst.LocalInitial; - } - } - } - public TR GetDstCoords() { switch (DestinationCoordinate) @@ -187,7 +115,6 @@ namespace UniVRM10 public abstract TR GetDstCurrent(); - /// /// Editorで設定値の変更を反映するために、クリアする /// @@ -204,16 +131,6 @@ namespace UniVRM10 } } - void Reset() - { - var current = transform; - while (current.parent != null) - { - current = current.parent; - } - ModelRoot = current; - } - public Component GetComponent() { return this; @@ -224,23 +141,8 @@ namespace UniVRM10 protected abstract void ApplyDelta(); - public override void Process() + public override void OnProcess() { - 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 = m_src.Delta(SourceCoordinate, SourceOffset); ApplyDelta(); } diff --git a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs index b90ec6a4f..3f365d8c5 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs @@ -479,7 +479,7 @@ namespace UniVRM10 var p = constraint.Position; var positionConstraint = node.gameObject.AddComponent(); positionConstraint.SourceCoordinate = p.SourceSpace; - positionConstraint.Source = Nodes[p.Source.Value]; + positionConstraint.m_source = Nodes[p.Source.Value]; positionConstraint.DestinationCoordinate = p.DestinationSpace; positionConstraint.FreezeAxes = FreezeAxis(p.FreezeAxes); positionConstraint.Weight = p.Weight.Value; @@ -490,7 +490,7 @@ namespace UniVRM10 var r = constraint.Rotation; var rotationConstraint = node.gameObject.AddComponent(); rotationConstraint.SourceCoordinate = r.SourceSpace; - rotationConstraint.Source = Nodes[r.Source.Value]; + rotationConstraint.m_source = Nodes[r.Source.Value]; rotationConstraint.DestinationCoordinate = r.DestinationSpace; rotationConstraint.FreezeAxes = FreezeAxis(r.FreezeAxes); rotationConstraint.Weight = r.Weight.Value; @@ -500,7 +500,7 @@ namespace UniVRM10 { var a = constraint.Aim; var aimConstraint = node.gameObject.AddComponent(); - aimConstraint.Source = Nodes[a.Source.Value]; + aimConstraint.m_source = Nodes[a.Source.Value]; // aimConstraint.AimVector = Vector3InvertX(a.AimVector); // aimConstraint.UpVector = Vector3InvertX(a.UpVector); }