OvrHandAnimator.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class OvrHandAnimator : MonoBehaviour
  5. {
  6. protected virtual void Start()
  7. {
  8. for (int i = 0; i < this.m_aryFingerData.Length; i++)
  9. {
  10. this.m_aryFingerData[i] = new OvrHandAnimator.FingerMorph();
  11. }
  12. List<Transform> list = new List<Transform>();
  13. this.FindChild(list, this.m_trRootGrasp);
  14. for (int j = 0; j < this.m_listRoot.Length; j++)
  15. {
  16. List<OvrHandAnimator.BonePair> list2 = new List<OvrHandAnimator.BonePair>();
  17. List<Transform> list3 = new List<Transform>();
  18. this.FindChild(list3, this.m_trFingerRoot[j]);
  19. using (List<Transform>.Enumerator enumerator = list3.GetEnumerator())
  20. {
  21. while (enumerator.MoveNext())
  22. {
  23. Transform trBase = enumerator.Current;
  24. Transform transform = list.Find((Transform a) => a.name == trBase.name);
  25. if (transform != null)
  26. {
  27. list2.Add(new OvrHandAnimator.BonePair(trBase, transform));
  28. }
  29. else
  30. {
  31. Debug.LogWarning("指ペアが見つかりません。" + trBase.name);
  32. }
  33. }
  34. }
  35. this.m_listRoot[j] = list2;
  36. }
  37. }
  38. public virtual void Init(AVRControllerButtons f_CtrlBtn)
  39. {
  40. this.m_CtrlBtn = f_CtrlBtn;
  41. }
  42. protected virtual void FindChild(List<Transform> f_list, Transform f_trParent)
  43. {
  44. for (int i = 0; i < f_trParent.childCount; i++)
  45. {
  46. this.FindChild(f_list, f_trParent.GetChild(i));
  47. }
  48. f_list.Add(f_trParent);
  49. }
  50. public virtual void FingerRateTarget(OvrHandAnimator.Finger f_eFinger, float f_fRate)
  51. {
  52. this.m_fTargetRate[(int)f_eFinger] = f_fRate;
  53. }
  54. protected virtual void FingerRateUpdate(OvrHandAnimator.Finger f_eFinger, float f_fRate)
  55. {
  56. OvrHandAnimator.FingerMorph fingerMorph = this.m_aryFingerData[(int)f_eFinger];
  57. fingerMorph.fTarget = f_fRate;
  58. float num = fingerMorph.fTarget - fingerMorph.fNow;
  59. float num2 = num * this.m_fEaseRate;
  60. fingerMorph.fNow += num2;
  61. List<OvrHandAnimator.BonePair> list = this.m_listRoot[(int)f_eFinger];
  62. for (int i = 0; i < list.Count; i++)
  63. {
  64. OvrHandAnimator.BonePair bonePair = list[i];
  65. bonePair.trMain.localRotation = Quaternion.Slerp(bonePair.qBackRot, bonePair.trGrasp.localRotation, fingerMorph.fNow);
  66. }
  67. }
  68. protected virtual void Update()
  69. {
  70. if (this.m_CtrlBtn != null)
  71. {
  72. if (this.m_CtrlBtn.GetTouch(AVRControllerButtons.TOUCH.REST) || this.m_CtrlBtn.GetTouch(AVRControllerButtons.TOUCH.XA) || this.m_CtrlBtn.GetTouch(AVRControllerButtons.TOUCH.YB))
  73. {
  74. this.FingerRateTarget(OvrHandAnimator.Finger.Thumb, 1f);
  75. }
  76. else
  77. {
  78. this.FingerRateTarget(OvrHandAnimator.Finger.Thumb, 0f);
  79. }
  80. if (this.m_CtrlBtn.GetPress(AVRControllerButtons.BTN.TRIGGER))
  81. {
  82. this.FingerRateTarget(OvrHandAnimator.Finger.Index, 1f);
  83. }
  84. else if (this.m_CtrlBtn.GetTouch(AVRControllerButtons.TOUCH.TRIGGER))
  85. {
  86. this.FingerRateTarget(OvrHandAnimator.Finger.Index, 0.5f);
  87. }
  88. else
  89. {
  90. this.FingerRateTarget(OvrHandAnimator.Finger.Index, 0f);
  91. }
  92. if (this.m_CtrlBtn.GetPress(AVRControllerButtons.BTN.GRIP))
  93. {
  94. this.FingerRateTarget(OvrHandAnimator.Finger.Middle, 1f);
  95. this.FingerRateTarget(OvrHandAnimator.Finger.Ring, 1f);
  96. this.FingerRateTarget(OvrHandAnimator.Finger.Little, 1f);
  97. }
  98. else
  99. {
  100. this.FingerRateTarget(OvrHandAnimator.Finger.Middle, 0f);
  101. this.FingerRateTarget(OvrHandAnimator.Finger.Ring, 0f);
  102. this.FingerRateTarget(OvrHandAnimator.Finger.Little, 0f);
  103. }
  104. }
  105. for (int i = 0; i < 5; i++)
  106. {
  107. this.FingerRateUpdate((OvrHandAnimator.Finger)i, this.m_fTargetRate[i]);
  108. }
  109. }
  110. public Transform m_trRootMain;
  111. public Transform m_trRootGrasp;
  112. public Transform[] m_trFingerRoot = new Transform[5];
  113. public float m_fEaseRate = 0.1f;
  114. public float[] m_fTargetRate = new float[5];
  115. protected List<OvrHandAnimator.BonePair>[] m_listRoot = new List<OvrHandAnimator.BonePair>[5];
  116. protected OvrHandAnimator.FingerMorph[] m_aryFingerData = new OvrHandAnimator.FingerMorph[5];
  117. protected AVRControllerButtons m_CtrlBtn;
  118. public enum Finger
  119. {
  120. Thumb,
  121. Index,
  122. Middle,
  123. Ring,
  124. Little,
  125. Max
  126. }
  127. protected class BonePair
  128. {
  129. public BonePair(Transform main, Transform grasp)
  130. {
  131. this.trMain = main;
  132. this.trGrasp = grasp;
  133. this.vBackPos = this.trMain.localPosition;
  134. this.qBackRot = this.trMain.localRotation;
  135. }
  136. public Transform trMain;
  137. public Transform trGrasp;
  138. public Vector3 vBackPos;
  139. public Quaternion qBackRot;
  140. }
  141. protected class FingerMorph
  142. {
  143. public float fNow;
  144. public float fTarget;
  145. }
  146. }