Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using UnityEngine.Serialization;
- namespace VRM
- {
- /// <summary>
- /// VRMBlendShapeProxy によるランダムに瞬きするサンプル。
- /// VRMBlendShapeProxy のある GameObject にアタッチする。
- /// </summary>
- public class Blinker : MonoBehaviour
- {
- VRMBlendShapeProxy m_blendShapes;
- public SkinnedMeshRenderer Face;
- [FormerlySerializedAs("m_interVal")]
- [SerializeField]
- public float Interval = 5.0f;
- [FormerlySerializedAs("m_closingTime")]
- [SerializeField]
- public float ClosingTime = 0.06f;
- [FormerlySerializedAs("m_openingSeconds")]
- [SerializeField]
- public float OpeningSeconds = 0.03f;
- [FormerlySerializedAs("m_closeSeconds")]
- [SerializeField]
- public float CloseSeconds = 0.1f;
- [FormerlySerializedAs("m_randomTiming")]
- [SerializeField]
- public float RandomTiming = 2;
- Coroutine m_coroutine;
- 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)
- {
- var waitTime = Time.time + Interval + RandomTiming * 2 * (Random.value - 0.5);
- while (waitTime > Time.time)
- {
- if (Request)
- {
- m_request = false;
- break;
- }
- yield return null;
- }
- // close
- var value = 0.0f;
- var closeSpeed = 1.0f / ClosingTime;
- var FunVal = Face.GetBlendShapeWeight(1);
- var JoyVal = Face.GetBlendShapeWeight(2);
- var EyeAngryVal = Face.GetBlendShapeWeight(11);
- var EyeCloseVal = Face.GetBlendShapeWeight(12);
- var EyeJoyVal = Face.GetBlendShapeWeight(15);
- var EyeSorrowVal = Face.GetBlendShapeWeight(18);
- float ClosedVal = (FunVal * 0.20f + JoyVal * 1.0f + EyeAngryVal * 0.10f + EyeCloseVal * 1.0f + EyeJoyVal * 1.0f + EyeSorrowVal * 0.7f)/100.0f;
- float BlinkValue = 0.9f - ClosedVal;
- if (BlinkValue < 0.15f){
- BlinkValue = 0.0f;
- }
- Debug.Log(BlinkValue);
- while (true)
- {
- value += Time.deltaTime * closeSpeed;
- if (value >= BlinkValue)
- {
- break;
- }
- m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), value);
- yield return null;
- }
- // m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), value);
- // wait...
- yield return new WaitForSeconds(CloseSeconds);
- Debug.Log("Finished waiting");
- // open
- value = BlinkValue;
- var openSpeed = 1.0f / OpeningSeconds;
- while (true)
- {
- value -= Time.deltaTime * openSpeed;
- if (value < 0)
- {
- break;
- }
- m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), value);
- yield return null;
- }
- m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), 0);
- }
- }
- private void OnEnable()
- {
- m_blendShapes = GetComponent<VRM.VRMBlendShapeProxy>();
- m_coroutine = StartCoroutine(BlinkRoutine());
- }
- private void OnDisable()
- {
- if (m_coroutine != null)
- {
- StopCoroutine(m_coroutine);
- m_coroutine = null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment