This commit is contained in:
ousttrue 2021-05-13 16:24:39 +09:00
parent 61920922d4
commit 720bb3ed59
5 changed files with 153 additions and 63 deletions

View File

@ -0,0 +1,121 @@
using System.Text;
using UnityEditor;
using UnityEngine;
namespace UniVRM10
{
[CustomEditor(typeof(VRM10AimConstraint))]
public class VRM10AimConstraintEditor : Editor
{
VRM10AimConstraint m_target;
void OnEnable()
{
m_target = (VRM10AimConstraint)target;
}
static GUIStyle s_style;
static GUIStyle Style
{
get
{
if (s_style == null)
{
s_style = new GUIStyle("box");
}
return s_style;
}
}
public void OnSceneGUI()
{
if (m_target.Source == null)
{
return;
}
// this to target line
Handles.color = Color.yellow;
Handles.DrawLine(m_target.Source.position, m_target.transform.position);
TR.FromWorld(m_target.transform).Draw(0.2f);
Handles.matrix = Matrix4x4.identity;
EditorGUI.BeginChangeCheck();
var pr = m_target.ParentRotation;
var pos = m_target.transform.position;
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;
}
// aim
Handles.color = Color.yellow;
var aim = pos + rot * Vector3.forward * 0.3f;
Handles.DrawLine(pos, aim);
Handles.Label(aim, "aim");
// up
var up = pos + rot * Vector3.up * 0.3f;
Handles.DrawLine(pos, up);
Handles.Label(up, "up");
// var delta = Clamp180(m_target.Delta.eulerAngles);
// // show source
// {
// var sb = new StringBuilder();
// sb.AppendLine();
// sb.AppendLine();
// sb.AppendLine($"source: {m_target.SourceCoordinate}");
// sb.AppendLine($"{delta.x:0.}");
// sb.AppendLine($"{delta.y:0.}");
// sb.Append($"{delta.z:0.}");
// Handles.Label(m_target.Source.position, sb.ToString(), Style);
// }
// // show dst
// {
// var sb = new StringBuilder();
// sb.AppendLine($"constraint: {m_target.DestinationCoordinate}");
// sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.}");
// sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.}");
// sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.}");
// Handles.Label(m_target.transform.position, sb.ToString(), Style);
// }
}
// void OnDrawGizmos()
// {
// if (Source == null)
// {
// return;
// }
// var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position);
// var (yaw, yaw_, pitch, pitch_) = CalcYawPitch(m_coords, localPosition);
// switch (yaw_)
// {
// case 0: break;
// case 1: yaw = 180 - yaw; break;
// case 2: yaw = 180 + yaw; break;
// case 3: yaw = 360 - yaw; break;
// }
// switch (pitch_)
// {
// case 0: pitch = -pitch; break;
// case 1: pitch = -pitch; break;
// case 2: break;
// case 3: break;
// }
// // Debug.Log($"{yaw}({yaw_}), {pitch}({pitch_})");
// // var rot = Quaternion.Euler(pitch, yaw, 0);
// var rot = Quaternion.AngleAxis(yaw, Vector3.up) * Quaternion.AngleAxis(pitch, Vector3.right);
// var p = rot * Vector3.forward;
// Gizmos.matrix = transform.localToWorldMatrix;
// Gizmos.DrawLine(Vector3.zero, p * 5);
// }
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 53c7ae04ba8deea40acc6439a2fe11cd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -20,43 +20,33 @@ namespace UniVRM10
[Range(0, 10.0f)]
public float Weight = 1.0f;
/// <summary>
/// Forward
/// </summary>
[SerializeField]
public Vector3 AimVector = Vector3.forward;
public Quaternion DestinationOffset = Quaternion.identity;
[SerializeField]
public Vector3 UpVector = Vector3.up;
public Quaternion ParentRotation => transform.parent == null ? Quaternion.identity : transform.parent.rotation;
[SerializeField]
public Vector3 RightVector;
Quaternion m_selfInitial;
Matrix4x4 m_coords;
public class AimLogic
{
public readonly Quaternion InitialLocalRotation;
public AimLogic(Quaternion initialLocalRotation)
{
InitialLocalRotation = initialLocalRotation;
}
}
AimLogic m_runtime;
void Start()
{
if (Source == null)
{
enabled = false;
return;
}
m_selfInitial = transform.rotation;
// 正規直交座標を作る
// Y x Z => X
AimVector.Normalize();
UpVector.Normalize();
RightVector = Vector3.Cross(UpVector, AimVector).normalized;
// 直交するように再計算
UpVector = Vector3.Cross(AimVector, RightVector).normalized;
m_coords = new Matrix4x4(
new Vector4(RightVector.x, RightVector.y, RightVector.z, 0),
new Vector4(UpVector.x, UpVector.y, UpVector.z, 0),
new Vector4(AimVector.x, AimVector.y, AimVector.z, 0),
new Vector4(0, 0, 0, 1)
);
if (m_runtime == null)
{
m_runtime = new AimLogic(DestinationOffset);
}
}
static (float, int, float, int) CalcYawPitch(Matrix4x4 m, Vector3 target)
@ -135,40 +125,8 @@ namespace UniVRM10
return;
}
var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position);
// var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position);
// var (yaw, pitch) = CalcYawPitch(m_coords, localPosition);
}
void OnDrawGizmos()
{
if (Source == null)
{
return;
}
var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position);
var (yaw, yaw_, pitch, pitch_) = CalcYawPitch(m_coords, localPosition);
switch (yaw_)
{
case 0: break;
case 1: yaw = 180 - yaw; break;
case 2: yaw = 180 + yaw; break;
case 3: yaw = 360 - yaw; break;
}
switch (pitch_)
{
case 0: pitch = -pitch; break;
case 1: pitch = -pitch; break;
case 2: break;
case 3: break;
}
// Debug.Log($"{yaw}({yaw_}), {pitch}({pitch_})");
// var rot = Quaternion.Euler(pitch, yaw, 0);
var rot = Quaternion.AngleAxis(yaw, Vector3.up) * Quaternion.AngleAxis(pitch, Vector3.right);
var p = rot * Vector3.forward;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawLine(Vector3.zero, p * 5);
}
}
}

View File

@ -444,8 +444,8 @@ namespace UniVRM10
Aim = new UniGLTF.Extensions.VRMC_node_constraint.AimConstraint
{
Source = model.Nodes.IndexOf(converter.Nodes[c.Source.gameObject]),
AimVector = ReverseX(c.AimVector),
UpVector = ReverseX(c.UpVector),
// AimVector = ReverseX(c.AimVector),
// UpVector = ReverseX(c.UpVector),
Weight = c.Weight,
},
},

View File

@ -501,8 +501,8 @@ namespace UniVRM10
var a = constraint.Aim;
var aimConstraint = node.gameObject.AddComponent<VRM10AimConstraint>();
aimConstraint.Source = Nodes[a.Source.Value];
aimConstraint.AimVector = Vector3InvertX(a.AimVector);
aimConstraint.UpVector = Vector3InvertX(a.UpVector);
// aimConstraint.AimVector = Vector3InvertX(a.AimVector);
// aimConstraint.UpVector = Vector3InvertX(a.UpVector);
}
}
}