mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-10-01 07:11:02 -05:00
WIP aim
This commit is contained in:
@@ -29,6 +29,7 @@ namespace UniVRM10
|
||||
|
||||
static void DrawAimUp(Quaternion rot, Vector3 pos, Color c)
|
||||
{
|
||||
Handles.matrix = Matrix4x4.identity;
|
||||
Handles.color = c;
|
||||
// aim
|
||||
var aim = pos + rot * Vector3.forward * 0.3f;
|
||||
@@ -52,36 +53,36 @@ namespace UniVRM10
|
||||
Handles.DrawLine(m_target.Source.position, m_target.transform.position);
|
||||
|
||||
var pos = m_target.transform.position;
|
||||
TR.FromWorld(m_target.transform).Draw(0.2f);
|
||||
|
||||
// EditorGUI.BeginChangeCheck();
|
||||
// var pr = m_target.ParentRotation;
|
||||
var pr = m_target.ParentRotation;
|
||||
|
||||
// if (m_target.Logic == null)
|
||||
// {
|
||||
// TR.FromWorld(m_target.transform).Draw(0.2f);
|
||||
if (m_target.Logic == null)
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
TR.FromWorld(m_target.transform).Draw(0.2f);
|
||||
|
||||
// Handles.matrix = Matrix4x4.identity;
|
||||
// var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos);
|
||||
// if (EditorGUI.EndChangeCheck())
|
||||
// {
|
||||
// Undo.RecordObject(m_target, "Rotated RotateAt Point");
|
||||
// m_target.DestinationOffset = Quaternion.Inverse(pr) * rot;
|
||||
// }
|
||||
Handles.matrix = Matrix4x4.identity;
|
||||
var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos);
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
Undo.RecordObject(m_target, "Rotated RotateAt Point");
|
||||
m_target.DestinationOffset = Quaternion.Inverse(pr) * rot;
|
||||
}
|
||||
|
||||
// DrawAimUp(rot, pos, Color.yellow);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var rot = m_target.ParentRotation * m_target.Logic.InitialLocalRotation * m_target.DestinationOffset;
|
||||
// DrawAimUp(rot, m_target.transform.position, Color.yellow);
|
||||
// DrawAimUp(m_target.transform.rotation * m_target.Delta, m_target.transform.position, Color.magenta);
|
||||
DrawAimUp(rot, pos, Color.yellow);
|
||||
}
|
||||
else
|
||||
{
|
||||
var init = m_target.ParentRotation * m_target.Logic.InitialLocalRotation;
|
||||
DrawAimUp(init * m_target.DestinationOffset, m_target.transform.position, Color.yellow);
|
||||
new TR(init, m_target.transform.position).Draw(0.2f);
|
||||
DrawAimUp(m_target.transform.rotation, m_target.transform.position, Color.magenta);
|
||||
|
||||
// var sb = new StringBuilder();
|
||||
// sb.AppendLine($"yaw: {m_target.Yaw:0.}");
|
||||
// sb.AppendLine($"pitch: {m_target.Pitch:0.}");
|
||||
// Handles.Label(m_target.transform.position, sb.ToString(), Style);
|
||||
// }
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine($"yaw: {m_target.Yaw:0.}");
|
||||
sb.AppendLine($"pitch: {m_target.Pitch:0.}");
|
||||
Handles.Label(m_target.transform.position, sb.ToString(), Style);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,19 +22,18 @@ namespace UniVRM10
|
||||
|
||||
public Quaternion ParentRotation => transform.parent == null ? Quaternion.identity : transform.parent.rotation;
|
||||
|
||||
// [SerializeField]
|
||||
// public Quaternion DestinationOffset = Quaternion.identity;
|
||||
|
||||
// public class AimLogic
|
||||
// {
|
||||
// public readonly Quaternion InitialLocalRotation;
|
||||
// public AimLogic(Quaternion initialLocalRotation)
|
||||
// {
|
||||
// InitialLocalRotation = initialLocalRotation;
|
||||
// }
|
||||
// }
|
||||
// public AimLogic Logic { get; private set; }
|
||||
[SerializeField]
|
||||
public Quaternion DestinationOffset = Quaternion.identity;
|
||||
|
||||
public class AimLogic
|
||||
{
|
||||
public readonly Quaternion InitialLocalRotation;
|
||||
public AimLogic(Quaternion initialLocalRotation)
|
||||
{
|
||||
InitialLocalRotation = initialLocalRotation;
|
||||
}
|
||||
}
|
||||
public AimLogic Logic { get; private set; }
|
||||
|
||||
void Start()
|
||||
{
|
||||
@@ -44,10 +43,10 @@ namespace UniVRM10
|
||||
return;
|
||||
}
|
||||
|
||||
// if (Logic == null)
|
||||
// {
|
||||
// Logic = new AimLogic(transform.localRotation);
|
||||
// }
|
||||
if (Logic == null)
|
||||
{
|
||||
Logic = new AimLogic(transform.localRotation);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -56,14 +55,15 @@ namespace UniVRM10
|
||||
/// </summary>
|
||||
public override void Process()
|
||||
{
|
||||
// if (Logic == null)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
if (Logic == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var m = Matrix4x4.Rotate(ParentRotation);
|
||||
m.CalcYawPitch(Source.position - transform.position, out Yaw, out Pitch);
|
||||
Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(-Pitch, 0, 0);
|
||||
// var init = ParentRotation * Logic.InitialLocalRotation;
|
||||
var m = Matrix4x4.TRS(transform.position, ParentRotation * DestinationOffset, Vector3.one);
|
||||
(Yaw, Pitch) = m.CalcYawPitch(Source.position);
|
||||
Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(Pitch, 0, 0);
|
||||
transform.rotation = ParentRotation * Delta;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,30 @@ namespace UniVRM10
|
||||
{
|
||||
public static class Matrix4x4Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// from AimConstraint
|
||||
/// </summary>
|
||||
/// <param name="Yaw"></param>
|
||||
/// <param name="m"></param>
|
||||
/// <param name="target"></param>
|
||||
/// <returns></returns>
|
||||
public static (float Yaw, float Pitch) CalcYawPitch(this Matrix4x4 m, Vector3 target)
|
||||
{
|
||||
var p = m.GetColumn(3);
|
||||
target -= new Vector3(p.x, p.y, p.z);
|
||||
var zaxis = Vector3.Project(target, m.GetColumn(2));
|
||||
var yaxis = Vector3.Project(target, m.GetColumn(1));
|
||||
var xaxis = Vector3.Project(target, m.GetColumn(0));
|
||||
|
||||
var yawPlusMinus = Vector3.Dot(xaxis, m.GetColumn(0)) > 0 ? 1.0f : -1.0f;
|
||||
var yaw = (float)Math.Atan2(xaxis.magnitude, zaxis.magnitude) * yawPlusMinus * Mathf.Rad2Deg;
|
||||
|
||||
var pitchPlusMinus = Vector3.Dot(yaxis, m.GetColumn(1)) < 0 ? 1.0f : -1.0f;
|
||||
var pitch = (float)Math.Atan2(yaxis.magnitude, (xaxis + zaxis).magnitude) * pitchPlusMinus * Mathf.Rad2Deg;
|
||||
|
||||
return (yaw, pitch);
|
||||
}
|
||||
|
||||
public static void CalcYawPitch(this Matrix4x4 m, Vector3 target, out float yaw, out float pitch)
|
||||
{
|
||||
var zaxis = Vector3.Project(target, m.GetColumn(2));
|
||||
|
||||
Reference in New Issue
Block a user