add LookAtTargetSwitcher

This commit is contained in:
ousttrue
2018-03-27 23:26:34 +09:00
parent 6c065e9109
commit a6efc0784b
5 changed files with 129 additions and 5 deletions

View File

@@ -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;

View File

@@ -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";
}
}

View File

@@ -0,0 +1,78 @@
using System.Collections.Generic;
using UnityEngine;
namespace VRM
{
public class LookAtTargetSwitcher : MonoBehaviour
{
[SerializeField]
List<Transform> m_targets = new List<Transform>();
[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<VRMLookAtHead>();
m_blinker = GameObject.FindObjectOfType<Blinker>();
}
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;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: bcd30950d78a34d48a749efb962224e6
timeCreated: 1522156957
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -19,6 +19,15 @@ namespace VRM
}
}
public Vector3 WorldForward
{
get
{
var m = WorldMatrix;
return m.GetColumn(2); // zaxis
}
}
Matrix4x4 m_initialLocalMatrix;
public void Setup()
{