Guest User

blinker.cs

a guest
Feb 27th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4.  
  5. namespace VRM
  6. {
  7.     /// <summary>
  8.     /// VRMBlendShapeProxy によるランダムに瞬きするサンプル。
  9.     /// VRMBlendShapeProxy のある GameObject にアタッチする。
  10.     /// </summary>
  11.     public class Blinker : MonoBehaviour
  12.     {
  13.         VRMBlendShapeProxy m_blendShapes;
  14.        
  15.         public SkinnedMeshRenderer Face;
  16.        
  17.  
  18.         [FormerlySerializedAs("m_interVal")]
  19.         [SerializeField]
  20.         public float Interval = 5.0f;
  21.  
  22.         [FormerlySerializedAs("m_closingTime")]
  23.         [SerializeField]
  24.         public float ClosingTime = 0.06f;
  25.  
  26.         [FormerlySerializedAs("m_openingSeconds")]
  27.         [SerializeField]
  28.         public float OpeningSeconds = 0.03f;
  29.  
  30.         [FormerlySerializedAs("m_closeSeconds")]
  31.         [SerializeField]
  32.         public float CloseSeconds = 0.1f;
  33.        
  34.         [FormerlySerializedAs("m_randomTiming")]
  35.         [SerializeField]
  36.         public float RandomTiming = 2;
  37.  
  38.         Coroutine m_coroutine;
  39.  
  40.         float m_nextRequest;
  41.         bool m_request;
  42.         public bool Request
  43.         {
  44.             get { return m_request; }
  45.             set
  46.             {
  47.                 if (Time.time < m_nextRequest)
  48.                 {
  49.                     return;
  50.                 }
  51.                 m_request = value;
  52.                 m_nextRequest = Time.time + 1.0f;
  53.             }
  54.         }
  55.  
  56.         IEnumerator BlinkRoutine()
  57.         {
  58.             while (true)
  59.             {
  60.                 var waitTime = Time.time +  Interval + RandomTiming * 2 * (Random.value - 0.5);
  61.                 while (waitTime > Time.time)
  62.                 {
  63.                     if (Request)
  64.                     {
  65.                         m_request = false;
  66.                         break;
  67.                     }
  68.                     yield return null;
  69.                 }
  70.  
  71.                 // close
  72.                 var value = 0.0f;
  73.                 var closeSpeed = 1.0f / ClosingTime;
  74.                 var FunVal = Face.GetBlendShapeWeight(1);
  75.                 var JoyVal = Face.GetBlendShapeWeight(2);
  76.                 var EyeAngryVal = Face.GetBlendShapeWeight(11);
  77.                 var EyeCloseVal = Face.GetBlendShapeWeight(12);
  78.                 var EyeJoyVal = Face.GetBlendShapeWeight(15);
  79.                 var EyeSorrowVal = Face.GetBlendShapeWeight(18);
  80.                
  81.                 float ClosedVal = (FunVal * 0.20f + JoyVal * 1.0f + EyeAngryVal * 0.10f + EyeCloseVal * 1.0f + EyeJoyVal * 1.0f + EyeSorrowVal * 0.7f)/100.0f;
  82.                
  83.                 float BlinkValue = 0.9f - ClosedVal;
  84.                 if (BlinkValue < 0.15f){
  85.                     BlinkValue = 0.0f;
  86.                 }
  87.  
  88.                 Debug.Log(BlinkValue);
  89.                 while (true)
  90.                 {
  91.                     value += Time.deltaTime * closeSpeed;
  92.                     if (value >= BlinkValue)
  93.                     {
  94.                         break;
  95.                     }
  96.  
  97.                     m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), value);
  98.                     yield return null;
  99.                 }
  100.                 // m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), value);
  101.  
  102.                 // wait...
  103.                 yield return new WaitForSeconds(CloseSeconds);
  104.                 Debug.Log("Finished waiting");
  105.  
  106.                 // open
  107.                 value = BlinkValue;
  108.                 var openSpeed = 1.0f / OpeningSeconds;
  109.                 while (true)
  110.                 {
  111.                     value -= Time.deltaTime * openSpeed;
  112.                     if (value < 0)
  113.                     {
  114.                         break;
  115.                     }
  116.                     m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), value);
  117.                     yield return null;
  118.                 }
  119.                 m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), 0);
  120.             }
  121.         }
  122.  
  123.         private void OnEnable()
  124.         {
  125.             m_blendShapes = GetComponent<VRM.VRMBlendShapeProxy>();
  126.             m_coroutine = StartCoroutine(BlinkRoutine());
  127.         }
  128.  
  129.         private void OnDisable()
  130.         {
  131.             if (m_coroutine != null)
  132.             {
  133.                 StopCoroutine(m_coroutine);
  134.                 m_coroutine = null;
  135.             }
  136.         }
  137.     }
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment