fix postition and rotation

This commit is contained in:
ousttrue 2021-05-18 14:14:14 +09:00
parent a6f4e1cc52
commit 607d0e07b2
5 changed files with 50 additions and 83 deletions

View File

@ -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;
}
}
}

View File

@ -7,16 +7,10 @@ namespace UniVRM10
public class ConstraintSource
{
public readonly Transform ModelRoot;
readonly Transform m_transform;
readonly Transform Source;
/// <summary>
/// initial: ModelRoot.localToWorldMatrix^-1 * t.localToWorldMatrix
/// </summary>
public readonly TR ModelInitial;
/// <summary>
/// initial: t.localPosition, t.localRotation, t.localScale
/// </summary>
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);
}

View File

@ -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();
}
}
}
}

View File

@ -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();
}
}
}
}

View File

@ -147,7 +147,7 @@ namespace UniVRM10
}
}
TR DestinationCoords
public TR DestinationCoords
{
get
{