DynamicBoneMuneCollider.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. [AddComponentMenu("Dynamic Bone/Dynamic Bone Mune Collider")]
  5. public class DynamicBoneMuneCollider : DynamicBoneColliderBase
  6. {
  7. public override string TypeName
  8. {
  9. get
  10. {
  11. return "dbm";
  12. }
  13. }
  14. public override void Serialize(BinaryWriter f_bw, int f_nVersion)
  15. {
  16. base.Serialize(f_bw, f_nVersion);
  17. f_bw.Write(this.m_Radius);
  18. f_bw.Write(this.m_Height);
  19. f_bw.Write(this.m_fScaleRateMulMax);
  20. f_bw.Write(this.m_CenterRateMax.x);
  21. f_bw.Write(this.m_CenterRateMax.y);
  22. f_bw.Write(this.m_CenterRateMax.z);
  23. }
  24. public override void Deserialize(BinaryReader f_br, int f_nVersion, Transform f_trBodyBoneRoot)
  25. {
  26. base.Deserialize(f_br, f_nVersion, f_trBodyBoneRoot);
  27. this.m_Radius = f_br.ReadSingle();
  28. this.m_Height = f_br.ReadSingle();
  29. this.m_fScaleRateMulMax = f_br.ReadSingle();
  30. this.m_CenterRateMax.Set(f_br.ReadSingle(), f_br.ReadSingle(), f_br.ReadSingle());
  31. }
  32. private void OnValidate()
  33. {
  34. this.m_Radius = Mathf.Max(this.m_Radius, 0f);
  35. this.m_Height = Mathf.Max(this.m_Height, 0f);
  36. }
  37. public override bool Collide(ref Vector3 particlePosition, float particleRadius)
  38. {
  39. float num = 0f;
  40. if (this.m_maid != null)
  41. {
  42. num = this.m_maid.body0.jbMuneL.BlendValue / 1.3f;
  43. }
  44. float num2 = 1f + this.m_fScaleRateMulMax * num;
  45. Vector3 vector = Vector3.Lerp(this.m_Center, this.m_CenterRateMax, num);
  46. float num3 = this.m_Radius * Mathf.Abs(base.transform.lossyScale.x * num2);
  47. float num4 = this.m_Height * 0.5f - this.m_Radius;
  48. if (num4 <= 0f)
  49. {
  50. if (this.m_Bound == DynamicBoneColliderBase.Bound.Outside)
  51. {
  52. DynamicBoneMuneCollider.OutsideSphere(ref particlePosition, particleRadius, base.transform.TransformPoint(vector), num3);
  53. }
  54. else
  55. {
  56. DynamicBoneMuneCollider.InsideSphere(ref particlePosition, particleRadius, base.transform.TransformPoint(vector), num3);
  57. }
  58. }
  59. else
  60. {
  61. Vector3 position = vector;
  62. Vector3 position2 = vector;
  63. DynamicBoneColliderBase.Direction direction = this.m_Direction;
  64. if (direction != DynamicBoneColliderBase.Direction.X)
  65. {
  66. if (direction != DynamicBoneColliderBase.Direction.Y)
  67. {
  68. if (direction == DynamicBoneColliderBase.Direction.Z)
  69. {
  70. position.z -= num4;
  71. position2.z += num4;
  72. }
  73. }
  74. else
  75. {
  76. position.y -= num4;
  77. position2.y += num4;
  78. }
  79. }
  80. else
  81. {
  82. position.x -= num4;
  83. position2.x += num4;
  84. }
  85. if (this.m_Bound == DynamicBoneColliderBase.Bound.Outside)
  86. {
  87. DynamicBoneMuneCollider.OutsideCapsule(ref particlePosition, particleRadius, base.transform.TransformPoint(position), base.transform.TransformPoint(position2), num3);
  88. }
  89. else
  90. {
  91. DynamicBoneMuneCollider.InsideCapsule(ref particlePosition, particleRadius, base.transform.TransformPoint(position), base.transform.TransformPoint(position2), num3);
  92. }
  93. }
  94. return false;
  95. }
  96. private static void OutsideSphere(ref Vector3 particlePosition, float particleRadius, Vector3 sphereCenter, float sphereRadius)
  97. {
  98. float num = sphereRadius + particleRadius;
  99. float num2 = num * num;
  100. Vector3 a = particlePosition - sphereCenter;
  101. float sqrMagnitude = a.sqrMagnitude;
  102. if (sqrMagnitude > 0f && sqrMagnitude < num2)
  103. {
  104. float num3 = Mathf.Sqrt(sqrMagnitude);
  105. particlePosition = sphereCenter + a * (num / num3);
  106. }
  107. }
  108. private static void InsideSphere(ref Vector3 particlePosition, float particleRadius, Vector3 sphereCenter, float sphereRadius)
  109. {
  110. float num = sphereRadius - particleRadius;
  111. float num2 = num * num;
  112. Vector3 a = particlePosition - sphereCenter;
  113. float sqrMagnitude = a.sqrMagnitude;
  114. if (sqrMagnitude > num2)
  115. {
  116. float num3 = Mathf.Sqrt(sqrMagnitude);
  117. particlePosition = sphereCenter + a * (num / num3);
  118. }
  119. }
  120. private static void OutsideCapsule(ref Vector3 particlePosition, float particleRadius, Vector3 capsuleP0, Vector3 capsuleP1, float capsuleRadius)
  121. {
  122. float num = capsuleRadius + particleRadius;
  123. float num2 = num * num;
  124. Vector3 vector = capsuleP1 - capsuleP0;
  125. Vector3 vector2 = particlePosition - capsuleP0;
  126. float num3 = Vector3.Dot(vector2, vector);
  127. if (num3 <= 0f)
  128. {
  129. float sqrMagnitude = vector2.sqrMagnitude;
  130. if (sqrMagnitude > 0f && sqrMagnitude < num2)
  131. {
  132. float num4 = Mathf.Sqrt(sqrMagnitude);
  133. particlePosition = capsuleP0 + vector2 * (num / num4);
  134. }
  135. }
  136. else
  137. {
  138. float sqrMagnitude2 = vector.sqrMagnitude;
  139. if (num3 >= sqrMagnitude2)
  140. {
  141. vector2 = particlePosition - capsuleP1;
  142. float sqrMagnitude3 = vector2.sqrMagnitude;
  143. if (sqrMagnitude3 > 0f && sqrMagnitude3 < num2)
  144. {
  145. float num5 = Mathf.Sqrt(sqrMagnitude3);
  146. particlePosition = capsuleP1 + vector2 * (num / num5);
  147. }
  148. }
  149. else if (sqrMagnitude2 > 0f)
  150. {
  151. num3 /= sqrMagnitude2;
  152. vector2 -= vector * num3;
  153. float sqrMagnitude4 = vector2.sqrMagnitude;
  154. if (sqrMagnitude4 > 0f && sqrMagnitude4 < num2)
  155. {
  156. float num6 = Mathf.Sqrt(sqrMagnitude4);
  157. particlePosition += vector2 * ((num - num6) / num6);
  158. }
  159. }
  160. }
  161. }
  162. private static void InsideCapsule(ref Vector3 particlePosition, float particleRadius, Vector3 capsuleP0, Vector3 capsuleP1, float capsuleRadius)
  163. {
  164. float num = capsuleRadius - particleRadius;
  165. float num2 = num * num;
  166. Vector3 vector = capsuleP1 - capsuleP0;
  167. Vector3 vector2 = particlePosition - capsuleP0;
  168. float num3 = Vector3.Dot(vector2, vector);
  169. if (num3 <= 0f)
  170. {
  171. float sqrMagnitude = vector2.sqrMagnitude;
  172. if (sqrMagnitude > num2)
  173. {
  174. float num4 = Mathf.Sqrt(sqrMagnitude);
  175. particlePosition = capsuleP0 + vector2 * (num / num4);
  176. }
  177. }
  178. else
  179. {
  180. float sqrMagnitude2 = vector.sqrMagnitude;
  181. if (num3 >= sqrMagnitude2)
  182. {
  183. vector2 = particlePosition - capsuleP1;
  184. float sqrMagnitude3 = vector2.sqrMagnitude;
  185. if (sqrMagnitude3 > num2)
  186. {
  187. float num5 = Mathf.Sqrt(sqrMagnitude3);
  188. particlePosition = capsuleP1 + vector2 * (num / num5);
  189. }
  190. }
  191. else if (sqrMagnitude2 > 0f)
  192. {
  193. num3 /= sqrMagnitude2;
  194. vector2 -= vector * num3;
  195. float sqrMagnitude4 = vector2.sqrMagnitude;
  196. if (sqrMagnitude4 > num2)
  197. {
  198. float num6 = Mathf.Sqrt(sqrMagnitude4);
  199. particlePosition += vector2 * ((num - num6) / num6);
  200. }
  201. }
  202. }
  203. }
  204. [Tooltip("The radius of the sphere or capsule.")]
  205. public float m_Radius = 0.5f;
  206. [Tooltip("The height of the capsule.")]
  207. public float m_Height;
  208. public float m_fScaleRateMulMax = 1f;
  209. public Vector3 m_CenterRateMax = Vector3.zero;
  210. public Maid m_maid;
  211. }