From 962ee323856b0fb26a29043cb5130b08bb8e1500 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 17 May 2021 18:29:35 +0900 Subject: [PATCH] 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(); } } }