implement aim up space

aim のUPVectorを評価する空間を world から model/local に修正
This commit is contained in:
ousttrue
2021-05-18 18:14:04 +09:00
parent 00814c0274
commit b3cbe06c6e
6 changed files with 159 additions and 135 deletions

View File

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

View File

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

View File

@@ -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;
/// <summary>
/// TargetのUpdateよりも先か後かはその時による。
/// 厳密に制御するのは無理。
/// </summary>
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;
}
}

View File

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

View File

@@ -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();
/// <summary>
/// Editorで設定値の変更を反映するために、クリアする
/// </summary>
@@ -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();
}

View File

@@ -479,7 +479,7 @@ namespace UniVRM10
var p = constraint.Position;
var positionConstraint = node.gameObject.AddComponent<VRM10PositionConstraint>();
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<VRM10RotationConstraint>();
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<VRM10AimConstraint>();
aimConstraint.Source = Nodes[a.Source.Value];
aimConstraint.m_source = Nodes[a.Source.Value];
// aimConstraint.AimVector = Vector3InvertX(a.AimVector);
// aimConstraint.UpVector = Vector3InvertX(a.UpVector);
}