Merge pull request #1762 from ousttrue/fix10/update_lookat

[1.0] LookAt を正規化しないことに対応させました
This commit is contained in:
ousttrue 2022-08-05 16:50:24 +09:00 committed by GitHub
commit e866effdf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 18 deletions

View File

@ -46,6 +46,10 @@ namespace UniVRM10
public override void OnToolGUI(EditorWindow window)
{
if(Selection.activeTransform==null)
{
return;
}
var root = Selection.activeTransform.GetComponent<Vrm10Instance>();
if (root == null)
{

View File

@ -6,7 +6,11 @@ namespace UniVRM10
internal sealed class LookAtEyeDirectionApplicableToBone : ILookAtEyeDirectionApplicable
{
private readonly Transform _leftEye;
private readonly Matrix4x4 _leftInit;
private readonly Transform _rightEye;
private readonly Matrix4x4 _rightInit;
private readonly CurveMapper _horizontalOuter;
private readonly CurveMapper _horizontalInner;
private readonly CurveMapper _verticalDown;
@ -16,7 +20,9 @@ namespace UniVRM10
CurveMapper horizontalOuter, CurveMapper horizontalInner, CurveMapper verticalDown, CurveMapper verticalUp)
{
_leftEye = leftEye;
_leftInit= Matrix4x4.Rotate(leftEye.localRotation);
_rightEye = rightEye;
_rightInit = Matrix4x4.Rotate(rightEye.localRotation);
_horizontalOuter = horizontalOuter;
_horizontalInner = horizontalInner;
_verticalDown = verticalDown;
@ -68,8 +74,8 @@ namespace UniVRM10
{
if (_leftEye != null && _rightEye != null)
{
_leftEye.localRotation = Matrix4x4.identity.YawPitchRotation(actualEyeDirection.LeftYaw, actualEyeDirection.LeftPitch);
_rightEye.localRotation = Matrix4x4.identity.YawPitchRotation(actualEyeDirection.RightYaw, actualEyeDirection.RightPitch);
_leftEye.localRotation = _leftInit.rotation * Matrix4x4.identity.YawPitchRotation(actualEyeDirection.LeftYaw, actualEyeDirection.LeftPitch);
_rightEye.localRotation = _rightInit.rotation * Matrix4x4.identity.YawPitchRotation(actualEyeDirection.RightYaw, actualEyeDirection.RightPitch);
}
}
}

View File

@ -62,19 +62,28 @@ namespace UniVRM10
return (yaw, pitch);
}
/// <summary>
/// yaw: 右が+
/// pitch: 上が+
/// という仕様。vrm-0.x から据え置き
/// </summary>
public static void CalcYawPitch(this Matrix4x4 m, Vector3 target, out float yaw, out float pitch)
{
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 z = Vector3.Dot(target, m.GetColumn(2));
var x = Vector3.Dot(target, m.GetColumn(0));
yaw = Mathf.Atan2(x, z) * Mathf.Rad2Deg;
var yawPlusMinus = Vector3.Dot(xaxis, m.GetColumn(0)) > 0 ? 1.0f : -1.0f;
yaw = (float)Math.Atan2(xaxis.magnitude, zaxis.magnitude) * yawPlusMinus * Mathf.Rad2Deg;
var pitchPlusMinus = Vector3.Dot(yaxis, m.GetColumn(1)) > 0 ? 1.0f : -1.0f;
pitch = (float)Math.Atan2(yaxis.magnitude, (xaxis + zaxis).magnitude) * pitchPlusMinus * Mathf.Rad2Deg;
// x+y z plane
var xz = Mathf.Sqrt(x * x + z * z);
var y = Vector3.Dot(target, m.GetColumn(1));
pitch = Mathf.Atan2(y, xz) * Mathf.Rad2Deg;
}
/// <summary>
/// yaw: 右が+
/// pitch: 上が+
/// という仕様。vrm-0.x から据え置き
/// </summary>
public static Quaternion YawPitchRotation(this Matrix4x4 m, float yaw, float pitch)
{
return Quaternion.AngleAxis(yaw, m.GetColumn(1)) * Quaternion.AngleAxis(-pitch, m.GetColumn(0));

View File

@ -83,6 +83,10 @@ namespace UniVRM10
void Start()
{
// cause new Vrm10Runtime.
// init LookAt init rotation.
var runtime = Runtime;
if (LookAtTargetType == VRM10ObjectLookAt.LookAtTargetTypes.CalcYawPitchToGaze)
{
if (Gaze == null)

View File

@ -20,19 +20,19 @@ namespace UniVRM10
#region LookAtTargetTypes.CalcYawPitchToGaze
// 座標計算用のempty
Transform m_lookAtOrigin;
Transform m_lookAtSpace;
public Transform GetLookAtOrigin(Transform head)
{
if (!Application.isPlaying)
{
return null;
}
if (m_lookAtOrigin == null)
if (m_lookAtSpace == null)
{
m_lookAtOrigin = new GameObject("_lookat_origin_").transform;
m_lookAtOrigin.SetParent(head);
m_lookAtSpace = new GameObject("_lookat_origin_").transform;
m_lookAtSpace.SetParent(head);
}
return m_lookAtOrigin;
return m_lookAtSpace;
}
/// <summary>
@ -40,9 +40,9 @@ namespace UniVRM10
/// </summary>
(float, float) CalcLookAtYawPitch(Vector3 targetWorldPosition, Transform head)
{
GetLookAtOrigin(head).localPosition = m_lookat.OffsetFromHead;
var localPosition = m_lookAtOrigin.worldToLocalMatrix.MultiplyPoint(targetWorldPosition);
var lookAtSpace = GetLookAtOrigin(head);
lookAtSpace.localPosition = m_lookat.OffsetFromHead;
var localPosition = lookAtSpace.worldToLocalMatrix.MultiplyPoint(targetWorldPosition);
float yaw, pitch;
Matrix4x4.identity.CalcYawPitch(localPosition, out yaw, out pitch);
return (yaw, pitch);
@ -87,6 +87,9 @@ namespace UniVRM10
internal Vrm10RuntimeLookAt(VRM10ObjectLookAt lookat, UniHumanoid.Humanoid humanoid, Transform head, VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform gaze)
{
// 初期姿勢で初期化する!
GetLookAtOrigin(head);
m_lookat = lookat;
m_head = head;