DynamicBoneCollider.cs 6.0 KB

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