Blinker.cs
3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System.Collections;
using UnityEngine;
namespace VRM
{
public class Blinker : MonoBehaviour
{
[SerializeField]
public VRMBlendShapeProxy BlendShapes;
private void Reset()
{
BlendShapes = GetComponent<VRMBlendShapeProxy>();
}
[SerializeField]
float m_interVal = 5.0f;
[SerializeField]
float m_closingTime = 0.06f;
[SerializeField]
float m_openingSeconds = 0.03f;
[SerializeField]
float m_closeSeconds = 0.1f;
protected Coroutine m_coroutine;
//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;
}
}
protected IEnumerator BlinkRoutine()
{
while (true)
{
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;
var closeSpeed = 1.0f / m_closeSeconds;
while (true)
{
value += Time.deltaTime * closeSpeed;
if (value >= 1.0f)
{
break;
}
BlendShapes.ImmediatelySetValue(BlendShapePreset.Blink, value);
yield return null;
}
BlendShapes.ImmediatelySetValue(BlendShapePreset.Blink, 1.0f);
// wait...
yield return new WaitForSeconds(m_closingTime);
// open
value = 1.0f;
var openSpeed = 1.0f / m_openingSeconds;
while (true)
{
value -= Time.deltaTime * openSpeed;
if (value < 0)
{
break;
}
BlendShapes.ImmediatelySetValue(BlendShapePreset.Blink, value);
yield return null;
}
BlendShapes.ImmediatelySetValue(BlendShapePreset.Blink, 0);
}
}
private void Awake()
{
if (BlendShapes == null) BlendShapes = GetComponent<VRM.VRMBlendShapeProxy>();
}
private void OnEnable()
{
m_coroutine = StartCoroutine(BlinkRoutine());
}
private void OnDisable()
{
if (m_coroutine != null)
{
StopCoroutine(m_coroutine);
m_coroutine = null;
}
}
}
}