jiggleBone.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using UnityEngine;
  3. public class jiggleBone : MonoBehaviour
  4. {
  5. private void Awake()
  6. {
  7. Vector3 vector = base.transform.position + base.transform.TransformDirection(new Vector3(this.boneAxis.x * this.targetDistance, this.boneAxis.y * this.targetDistance, this.boneAxis.z * this.targetDistance));
  8. this.dynamicPos = vector;
  9. this.defQtn = base.transform.localRotation;
  10. if (base.transform.name == "Mune_R")
  11. {
  12. this.MuneL_y = -1f;
  13. }
  14. this.m_trParent = base.transform.parent;
  15. this.m_trSub = base.transform.Find(base.transform.name + "_sub");
  16. if (this.m_trSub == null)
  17. {
  18. NDebug.Assert("transform.name の下に" + base.transform.name + "_sub がありません。", false);
  19. }
  20. }
  21. public void LateUpdateSelf()
  22. {
  23. if (!base.enabled)
  24. {
  25. return;
  26. }
  27. if (0f < this.BlendValueON)
  28. {
  29. if (0.5f <= this.m_fMuneYawaraka)
  30. {
  31. float num = (this.m_fMuneYawaraka - 0.5f) / 0.5f;
  32. this.bMass = 0.4f + 1.1f * num;
  33. this.bDamping = 0.3f - 0.290000021f * num;
  34. this.BlendValueON2 = 1f;
  35. }
  36. else
  37. {
  38. float blendValueON = this.m_fMuneYawaraka / 0.5f;
  39. this.bMass = 0.4f;
  40. this.bDamping = 0.3f;
  41. this.BlendValueON2 = blendValueON;
  42. }
  43. base.transform.localRotation = this.defQtn;
  44. Vector3 dir = base.transform.TransformDirection(new Vector3(this.boneAxis.x * this.targetDistance, this.boneAxis.y * this.targetDistance, this.boneAxis.z * this.targetDistance));
  45. Vector3 vector = base.transform.TransformDirection(new Vector3(0f, 1f, 0f));
  46. Vector3 vector2 = base.transform.position + base.transform.TransformDirection(new Vector3(this.boneAxis.x * this.targetDistance, this.boneAxis.y * this.targetDistance, this.boneAxis.z * this.targetDistance));
  47. float p = 0.5f;
  48. float num2 = this.bStiffness[1];
  49. float num3 = this.bStiffness[0];
  50. if (this.boBRA)
  51. {
  52. num2 = this.bStiffnessBRA[1];
  53. num3 = this.bStiffnessBRA[0];
  54. p = 0.7f;
  55. }
  56. float num4 = Mathf.Pow(this.BlendValue / 1.3f, p);
  57. this.force.x = (vector2.x - this.dynamicPos.x) * (num2 * num4 + (1f - num4) * num3);
  58. this.acc.x = this.force.x / this.bMass;
  59. this.vel.x = this.vel.x + this.acc.x * (1f - this.bDamping);
  60. this.force.y = (vector2.y - this.dynamicPos.y) * (num2 * num4 + (1f - num4) * num3);
  61. this.force.y = this.force.y - this.bGravity / 10f;
  62. this.acc.y = this.force.y / this.bMass;
  63. this.vel.y = this.vel.y + this.acc.y * (1f - this.bDamping);
  64. this.force.z = (vector2.z - this.dynamicPos.z) * (num2 * num4 + (1f - num4) * num3);
  65. this.acc.z = this.force.z / this.bMass;
  66. this.vel.z = this.vel.z + this.acc.z * (1f - this.bDamping);
  67. if (this.m_fBackBlendValueON2 != this.BlendValueON2)
  68. {
  69. this.force = Vector3.zero;
  70. this.acc = Vector3.zero;
  71. this.vel = Vector3.zero;
  72. this.m_fBackBlendValueON2 = this.BlendValueON2;
  73. }
  74. if (this.boWarpInit)
  75. {
  76. this.force = Vector3.zero;
  77. this.acc = Vector3.zero;
  78. this.vel = Vector3.zero;
  79. vector2 = (this.dynamicPos = base.transform.position + base.transform.TransformDirection(new Vector3(this.boneAxis.x * this.targetDistance, this.boneAxis.y * this.targetDistance, this.boneAxis.z * this.targetDistance)));
  80. }
  81. Vector3 a = this.dynamicPos;
  82. a += this.force;
  83. a += this.vel;
  84. if (Vector3.Dot(a - base.transform.position, vector2 - base.transform.position) < this.m_fLimitRot)
  85. {
  86. this.force *= this.m_fLimitRotDecay;
  87. this.vel *= this.m_fLimitRotDecay;
  88. }
  89. this.dynamicPos += this.force;
  90. this.dynamicPos += this.vel;
  91. this.dynamicPos = Vector3.Slerp(vector2, this.dynamicPos, this.BlendValueON2);
  92. base.transform.LookAt(this.dynamicPos, vector);
  93. base.transform.Rotate(0f, 90f - this.MuneYori * this.BlendValue, -this.MuneUpDown * this.MuneL_y * this.BlendValue);
  94. Quaternion localRotation = base.transform.localRotation;
  95. base.transform.localRotation = Quaternion.Slerp(this.defQtn, localRotation, this.BlendValueON);
  96. if (this.SquashAndStretch)
  97. {
  98. float magnitude = (this.dynamicPos - vector2).magnitude;
  99. float x;
  100. if (this.boneAxis.x == 0f)
  101. {
  102. x = 1f + -magnitude * this.sideStretch;
  103. }
  104. else
  105. {
  106. x = 1f + magnitude * this.frontStretch;
  107. }
  108. float y;
  109. if (this.boneAxis.y == 0f)
  110. {
  111. y = 1f + -magnitude * this.sideStretch;
  112. }
  113. else
  114. {
  115. y = 1f + magnitude * this.frontStretch;
  116. }
  117. float z;
  118. if (this.boneAxis.z == 0f)
  119. {
  120. z = 1f + -magnitude * this.sideStretch;
  121. }
  122. else
  123. {
  124. z = 1f + magnitude * this.frontStretch;
  125. }
  126. base.transform.localScale = new Vector3(x, y, z);
  127. if (this.m_bEnableScaleX)
  128. {
  129. float num5 = Vector3.Dot(Vector3.up, this.m_trParent.up);
  130. if (this.m_fScaleDotRateLimit <= num5 && 0.7f < this.BlendValue)
  131. {
  132. this.m_fMuneScaleRate = (this.BlendValue - 0.7f) / 0.599999964f;
  133. this.m_fScaleX = 1f - this.m_fScaleMinus * ((num5 - this.m_fScaleDotRateLimit) / (1f - this.m_fScaleDotRateLimit)) * this.m_fMuneScaleRate * this.BlendValueON;
  134. }
  135. else
  136. {
  137. this.m_fScaleX = 1f;
  138. }
  139. base.transform.localScale = new Vector3(base.transform.localScale.x * this.m_fScaleX, base.transform.localScale.y, base.transform.localScale.z);
  140. }
  141. }
  142. if (this.debugMode)
  143. {
  144. Debug.DrawRay(base.transform.position, dir, Color.blue);
  145. Debug.DrawRay(base.transform.position, vector, Color.green);
  146. Debug.DrawRay(vector2, Vector3.up * 0.2f, Color.yellow);
  147. Debug.DrawRay(this.dynamicPos, Vector3.up * 0.2f, Color.red);
  148. }
  149. if (this.boWarpInit)
  150. {
  151. this.boWarpInit = false;
  152. }
  153. }
  154. else
  155. {
  156. this.m_fBackBlendValueON2 = -1f;
  157. this.boWarpInit = true;
  158. }
  159. Transform parent = base.transform.parent;
  160. float num6 = (1f - Mathf.Abs(Vector3.Dot(parent.up, Vector3.down))) * -this.MuneL_y * 0.8f;
  161. num6 += this.BlendValue2 * (this.BlendValue * 0.5f);
  162. float num7 = this.BlendValue;
  163. if (num7 > 1f)
  164. {
  165. num7 = 1f;
  166. }
  167. num6 *= num7 * this.BlendValueON;
  168. num6 += 3.14159274f;
  169. this.m_trSub.localRotation = Quaternion.EulerRotation(0f, 0f, num6);
  170. }
  171. public bool debugMode;
  172. private Vector3 dynamicPos = default(Vector3);
  173. public bool boBRA = true;
  174. public float BlendValue;
  175. public float BlendValue2;
  176. public float BlendValueON = 1f;
  177. public float BlendValueON2 = 1f;
  178. public Vector3 boneAxis = new Vector3(-1f, 0f, 0f);
  179. public float targetDistance = 0.7f;
  180. public float[] bStiffness = new float[]
  181. {
  182. 0.22f,
  183. 0.055f
  184. };
  185. public float[] bStiffnessBRA = new float[]
  186. {
  187. 0.27f,
  188. 0.07f
  189. };
  190. public float bMass = 0.4f;
  191. public float bDamping = 0.3f;
  192. public float bGravity = 0.1f;
  193. private Vector3 force = default(Vector3);
  194. private Vector3 acc = default(Vector3);
  195. private Vector3 vel = default(Vector3);
  196. public bool SquashAndStretch = true;
  197. public float sideStretch = -0.2f;
  198. public float frontStretch = 0.3f;
  199. public float MuneL_y = 1f;
  200. public float MuneYori;
  201. public float MuneUpDown;
  202. public float MuneYori_f;
  203. public float MuneUpDown_f;
  204. private Quaternion defQtn;
  205. public float m_fMuneYawaraka = 0.5f;
  206. public float m_fLimitRot = 0.3f;
  207. public float m_fLimitRotDecay = 0.8f;
  208. public bool m_bEnableScaleX;
  209. public float m_fScaleX = 1f;
  210. public float m_fMuneScaleRate;
  211. public float m_fScaleDotRateLimit = 0.8f;
  212. public float m_fScaleMinus = 0.2f;
  213. private Transform m_trParent;
  214. private Transform m_trSub;
  215. public bool boWarpInit;
  216. private float m_fBackBlendValueON2;
  217. }