diff --git a/Scripts/BlendShape/Blinker.cs b/Scripts/BlendShape/Blinker.cs index 1942c509f..071ea83d7 100644 --- a/Scripts/BlendShape/Blinker.cs +++ b/Scripts/BlendShape/Blinker.cs @@ -29,11 +29,36 @@ namespace VRM static readonly string BLINK_NAME = BlendShapePreset.Blink.ToString(); + float m_nextRequest; + bool m_request; + public bool Request + { + get { return m_request; } + set + { + if (Time.time < m_nextRequest) + { + return; + } + m_request = value; + m_nextRequest = Time.time + 1.0f; + } + } + IEnumerator BlinkRoutine() { while (true) { - yield return new WaitForSeconds(Random.value * m_interVal); + var waitTime = Time.time + Random.value * m_interVal; + while (waitTime>Time.time) + { + if (Request) + { + m_request = false; + break; + } + yield return null; + } // close var value = 0.0f; diff --git a/Scripts/Format/VRMVersion.cs b/Scripts/Format/VRMVersion.cs index 42f5a9c43..011883701 100644 --- a/Scripts/Format/VRMVersion.cs +++ b/Scripts/Format/VRMVersion.cs @@ -4,11 +4,11 @@ namespace VRM public static class VRMVersion { public const int MAJOR = 0; - public const int MINOR = 1; + public const int MINOR = 2; - public const string VERSION = "0.1"; + public const string VERSION = "0.2"; - public const string DecrementMenuName = "VRM/Version(0.1) Decrement"; - public const string IncrementMenuName = "VRM/Version(0.1) Increment"; + public const string DecrementMenuName = "VRM/Version(0.2) Decrement"; + public const string IncrementMenuName = "VRM/Version(0.2) Increment"; } } diff --git a/Scripts/LookAt/LookAtTargetSwitcher.cs b/Scripts/LookAt/LookAtTargetSwitcher.cs new file mode 100644 index 000000000..2f74aa850 --- /dev/null +++ b/Scripts/LookAt/LookAtTargetSwitcher.cs @@ -0,0 +1,78 @@ +using System.Collections.Generic; +using UnityEngine; + + +namespace VRM +{ + public class LookAtTargetSwitcher : MonoBehaviour + { + [SerializeField] + List m_targets = new List(); + + [SerializeField, Range(0, 90.0f)] + float m_thresholdDegrees = 60.0f; + + [SerializeField] + VRMLookAtHead m_lookAtHead; + + [SerializeField] + Blinker m_blinker; + + private void Reset() + { + m_lookAtHead = GameObject.FindObjectOfType(); + m_blinker = GameObject.FindObjectOfType(); + } + + float CalcScore(Transform target) + { + return Vector3.Dot(m_lookAtHead.Head.WorldForward, target.position - m_lookAtHead.Head.Transform.position); + } + + Transform ChooseTarget() + { + Transform target = null; + float maxScore = 0; + var min = System.Math.Cos(m_thresholdDegrees * Mathf.Deg2Rad); + foreach (var x in m_targets) + { + var score = CalcScore(x); + if (score > min && score > maxScore) + { + maxScore = score; + target = x; + } + } + return target; + } + + Transform m_lastTarget; + + private void Update() + { + if (m_targets == null || m_targets.Count == 0) return; + + var target = ChooseTarget(); + if (target != m_lastTarget) + { + // blink + //Debug.Log("request"); + m_lastTarget = target; + m_blinker.Request = true; + } + + Vector3 targetPosition; + if (target == null) + { + // forward + targetPosition = m_lookAtHead.Head.Transform.position + m_lookAtHead.Head.WorldForward * 20.0f; + } + else + { + targetPosition = target.position; + } + // half move + transform.position += (targetPosition - transform.position) * 0.5f; + } + } +} diff --git a/Scripts/LookAt/LookAtTargetSwitcher.cs.meta b/Scripts/LookAt/LookAtTargetSwitcher.cs.meta new file mode 100644 index 000000000..f02c2c726 --- /dev/null +++ b/Scripts/LookAt/LookAtTargetSwitcher.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: bcd30950d78a34d48a749efb962224e6 +timeCreated: 1522156957 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/LookAt/OffsetOnTransform.cs b/Scripts/LookAt/OffsetOnTransform.cs index f20519542..d2e6d15fb 100644 --- a/Scripts/LookAt/OffsetOnTransform.cs +++ b/Scripts/LookAt/OffsetOnTransform.cs @@ -19,6 +19,15 @@ namespace VRM } } + public Vector3 WorldForward + { + get + { + var m = WorldMatrix; + return m.GetColumn(2); // zaxis + } + } + Matrix4x4 m_initialLocalMatrix; public void Setup() {