mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-08 11:59:26 -05:00
VRMLookAtHead.Head to Transform
This commit is contained in:
@@ -106,7 +106,7 @@ namespace VRM
|
||||
public override void OnPreviewGUI(Rect r, GUIStyle background)
|
||||
{
|
||||
m_previewRenderUtility.BeginPreview(r, background);
|
||||
var target = m_target.Head.Transform;
|
||||
var target = m_target.Head;
|
||||
if (target != null)
|
||||
{
|
||||
#if UNITY_2017_1_OR_NEWER
|
||||
@@ -145,7 +145,7 @@ namespace VRM
|
||||
//if (!Application.isPlaying) return;
|
||||
if (!m_target.DrawGizmo) return;
|
||||
if (m_target.Target == null) return;
|
||||
if (m_target.Head.Transform == null) return;
|
||||
if (m_target.Head == null) return;
|
||||
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
@@ -158,25 +158,25 @@ namespace VRM
|
||||
}
|
||||
|
||||
Handles.color = new Color(1, 1, 1, 0.6f);
|
||||
Handles.DrawDottedLine(m_target.Head.Transform.position, m_target.Target.position, 4.0f);
|
||||
Handles.DrawDottedLine(m_target.Head.position, m_target.Target.position, 4.0f);
|
||||
|
||||
Handles.matrix = m_target.Head.InitialWorldMatrix;
|
||||
Handles.matrix = m_target.Head.localToWorldMatrix;
|
||||
Handles.Label(Vector3.zero, string.Format("Yaw: {0:0.}degree\nPitch: {1:0.}degree",
|
||||
m_target.Yaw,
|
||||
m_target.Pitch));
|
||||
|
||||
Handles.color = new Color(0, 1, 0, 0.2f);
|
||||
Handles.DrawSolidArc(Vector3.zero,
|
||||
m_target.Head.OffsetRotation.GetColumn(1),
|
||||
m_target.Head.OffsetRotation.GetColumn(2),
|
||||
Matrix4x4.identity.GetColumn(1),
|
||||
Matrix4x4.identity.GetColumn(2),
|
||||
m_target.Yaw,
|
||||
RADIUS);
|
||||
|
||||
Handles.matrix = m_target.Head.InitialWorldMatrix * m_target.YawMatrix;
|
||||
Handles.matrix = m_target.Head.localToWorldMatrix * m_target.YawMatrix;
|
||||
Handles.color = new Color(1, 0, 0, 0.2f);
|
||||
Handles.DrawSolidArc(Vector3.zero,
|
||||
m_target.Head.OffsetRotation.GetColumn(0),
|
||||
m_target.Head.OffsetRotation.GetColumn(2),
|
||||
Matrix4x4.identity.GetColumn(0),
|
||||
Matrix4x4.identity.GetColumn(2),
|
||||
-m_target.Pitch,
|
||||
RADIUS);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace VRM
|
||||
|
||||
float CalcScore(Transform target)
|
||||
{
|
||||
return Vector3.Dot(m_lookAtHead.Head.WorldForward, target.position - m_lookAtHead.Head.Transform.position);
|
||||
return Vector3.Dot(m_lookAtHead.Head.forward, target.position - m_lookAtHead.Head.position);
|
||||
}
|
||||
|
||||
Transform ChooseTarget()
|
||||
@@ -65,7 +65,7 @@ namespace VRM
|
||||
if (target == null)
|
||||
{
|
||||
// forward
|
||||
targetPosition = m_lookAtHead.Head.Transform.position + m_lookAtHead.Head.WorldForward * 20.0f;
|
||||
targetPosition = m_lookAtHead.Head.position + m_lookAtHead.Head.forward * 20.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -123,8 +123,8 @@ namespace VRM
|
||||
if (LeftEye.Transform != null && RightEye.Transform != null)
|
||||
{
|
||||
// 目に値を適用する
|
||||
LeftEye.Transform.rotation = LeftEye.InitialWorldMatrix.ExtractRotation() * m_head.Head.OffsetRotation.YawPitchRotation(leftYaw, pitch);
|
||||
RightEye.Transform.rotation = RightEye.InitialWorldMatrix.ExtractRotation() * m_head.Head.OffsetRotation.YawPitchRotation(rightYaw, pitch);
|
||||
LeftEye.Transform.rotation = LeftEye.InitialWorldMatrix.ExtractRotation() * Matrix4x4.identity.YawPitchRotation(leftYaw, pitch);
|
||||
RightEye.Transform.rotation = RightEye.InitialWorldMatrix.ExtractRotation() * Matrix4x4.identity.YawPitchRotation(rightYaw, pitch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,13 @@ namespace VRM
|
||||
LateUpdate,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Headボーンローカルで目標物のYaw, Pitchを求めて目線に適用する
|
||||
///
|
||||
/// * VRMLookAtBoneApplyer
|
||||
/// * VRMLookAtBlendShapeApplyer
|
||||
///
|
||||
/// </summary>
|
||||
public class VRMLookAtHead : MonoBehaviour, IVRMComponent
|
||||
{
|
||||
public bool DrawGizmo = true;
|
||||
@@ -24,7 +31,7 @@ namespace VRM
|
||||
public Transform Target;
|
||||
|
||||
[SerializeField]
|
||||
public OffsetOnTransform Head;
|
||||
public Transform Head;
|
||||
|
||||
#region Thumbnail
|
||||
public Texture2D CreateThumbnail()
|
||||
@@ -64,15 +71,14 @@ namespace VRM
|
||||
|
||||
public void LookFace(Transform t)
|
||||
{
|
||||
if (Head.Transform == null) return;
|
||||
var head = Head.Transform;
|
||||
var headPosition = head.position + new Vector3(0, 0.05f, 0);
|
||||
t.position = headPosition + Head.WorldMatrix.ExtractRotation() * new Vector3(0, 0, 0.7f);
|
||||
if (Head == null) return;
|
||||
var headPosition = Head.position + new Vector3(0, 0.05f, 0);
|
||||
t.position = headPosition + Head.localToWorldMatrix.ExtractRotation() * new Vector3(0, 0, 0.7f);
|
||||
t.LookAt(headPosition);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void OnImported(VRMImporterContext context)
|
||||
void Awake()
|
||||
{
|
||||
var animator = GetComponent<Animator>();
|
||||
if (animator == null)
|
||||
@@ -88,8 +94,11 @@ namespace VRM
|
||||
return;
|
||||
}
|
||||
|
||||
Head = OffsetOnTransform.Create(head);
|
||||
Head = head;
|
||||
}
|
||||
|
||||
public void OnImported(VRMImporterContext context)
|
||||
{
|
||||
var gltfFirstPerson = context.GLTF.extensions.VRM.firstPerson;
|
||||
switch (gltfFirstPerson.lookAtType)
|
||||
{
|
||||
@@ -109,11 +118,6 @@ namespace VRM
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Head.Setup();
|
||||
}
|
||||
|
||||
static Matrix4x4 LookAtMatrixFromWorld(Vector3 from, Vector3 target)
|
||||
{
|
||||
var pos = new Vector4(from.x, from.y, from.z, 1);
|
||||
@@ -137,7 +141,7 @@ namespace VRM
|
||||
{
|
||||
get
|
||||
{
|
||||
var yaw = Quaternion.AngleAxis(m_yaw, Head.OffsetRotation.GetColumn(1));
|
||||
var yaw = Quaternion.AngleAxis(m_yaw, Vector3.up);
|
||||
var m = default(Matrix4x4);
|
||||
m.SetTRS(Vector3.zero, yaw, Vector3.one);
|
||||
return m;
|
||||
@@ -159,7 +163,7 @@ namespace VRM
|
||||
}
|
||||
|
||||
public event Action<float, float> YawPitchChanged;
|
||||
void RaiseYawPitchChanged(float yaw, float pitch)
|
||||
public void RaiseYawPitchChanged(float yaw, float pitch)
|
||||
{
|
||||
m_yaw = yaw;
|
||||
m_pitch = pitch;
|
||||
@@ -172,7 +176,7 @@ namespace VRM
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if(UpdateType==UpdateType.Update)
|
||||
if (UpdateType == UpdateType.Update)
|
||||
{
|
||||
LookWorldPosition();
|
||||
}
|
||||
@@ -196,8 +200,8 @@ namespace VRM
|
||||
|
||||
public void LookWorldPosition(Vector3 targetPosition, out float yaw, out float pitch)
|
||||
{
|
||||
var localPosition = Head.InitialWorldMatrix.inverse.MultiplyPoint(targetPosition);
|
||||
Head.OffsetRotation.CalcYawPitch(localPosition, out yaw, out pitch);
|
||||
var localPosition = Head.worldToLocalMatrix.MultiplyPoint(targetPosition);
|
||||
Matrix4x4.identity.CalcYawPitch(localPosition, out yaw, out pitch);
|
||||
RaiseYawPitchChanged(yaw, pitch);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user