mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-10-01 07:11:02 -05:00
ApplyDelta
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,22 @@ namespace UniVRM10
|
||||
|
||||
public Matrix4x4 TRS(float s) => Matrix4x4.TRS(Translation, Rotation, new Vector3(s, s, s));
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// R1|T1 R2|T2 x R1R2|R1T2+T1 x
|
||||
/// --+-- --+-- y => ----+------- y
|
||||
/// 0| 1 0| 1 z 0| 1 z
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static TR operator *(TR a, TR b) => new TR(a.Rotation * b.Rotation, a.Rotation * b.Translation + a.Translation);
|
||||
|
||||
/// <summary>
|
||||
/// R|0 1|T R|RT
|
||||
/// -+- -+- => -+--
|
||||
/// 0|1 0|1 0| 1
|
||||
/// </summary>
|
||||
public TR Inverse()
|
||||
{
|
||||
var inv = Quaternion.Inverse(Rotation);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user