mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-07 11:28:35 -05:00
add LookAtTargetSwitcher
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
78
Scripts/LookAt/LookAtTargetSwitcher.cs
Normal file
78
Scripts/LookAt/LookAtTargetSwitcher.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Scripts/LookAt/LookAtTargetSwitcher.cs.meta
Normal file
12
Scripts/LookAt/LookAtTargetSwitcher.cs.meta
Normal 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:
|
||||
@@ -19,6 +19,15 @@ namespace VRM
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 WorldForward
|
||||
{
|
||||
get
|
||||
{
|
||||
var m = WorldMatrix;
|
||||
return m.GetColumn(2); // zaxis
|
||||
}
|
||||
}
|
||||
|
||||
Matrix4x4 m_initialLocalMatrix;
|
||||
public void Setup()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user