SkyCloud.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Kamgam.SkyClouds
  5. {
  6. public class SkyCloud : MonoBehaviour
  7. {
  8. public float ObserverFadeStartDistance = 0.5f;
  9. public float ObserverFadeEndDistance = 0.1f;
  10. public Color ObserverFadeColor = Color.white;
  11. public float ObserverMaskScale = 1f;
  12. public float ObserverMaskPadding = 0.2f;
  13. public static List<SkyCloud> Clouds = new List<SkyCloud>();
  14. public static float GetMinDistance(Vector3 position, out Vector3 closestPosition, out SkyCloud closestCloud, float distanceLimit = 10f)
  15. {
  16. float distance = distanceLimit;
  17. Vector3 tmpClosestPosition = new Vector3(0f, -999_999f, 0f);
  18. closestPosition = tmpClosestPosition;
  19. closestCloud = null;
  20. foreach (var cloud in Clouds)
  21. {
  22. if (cloud == null || cloud.gameObject == null)
  23. continue;
  24. float d = cloud.GetDistance(position, out tmpClosestPosition, distanceLimit);
  25. if (d < distance)
  26. {
  27. distance = d;
  28. closestPosition = tmpClosestPosition;
  29. closestCloud = cloud;
  30. }
  31. }
  32. return distance;
  33. }
  34. private RaycastHit[] _hitsUp;
  35. protected Collider[] _colliders;
  36. public Collider[] Colliders
  37. {
  38. get
  39. {
  40. if (_colliders == null)
  41. {
  42. _colliders = this.GetComponents<Collider>();
  43. }
  44. return _colliders;
  45. }
  46. }
  47. protected Material _couldMaterial;
  48. public Material CloudMaterial
  49. {
  50. get
  51. {
  52. if (_couldMaterial == null)
  53. {
  54. var renderer = this.GetComponent<MeshRenderer>();
  55. if (renderer != null)
  56. _couldMaterial = renderer.sharedMaterial;
  57. }
  58. return _couldMaterial;
  59. }
  60. }
  61. public void OnEnable()
  62. {
  63. Clouds.Add(this);
  64. }
  65. public void OnDisable()
  66. {
  67. Clouds.Remove(this);
  68. }
  69. //Vector3 _closestPointForGizmo;
  70. public float GetDistance(Vector3 position, out Vector3 closestPosition, float distanceLimit = 10f)
  71. {
  72. closestPosition = transform.position;
  73. if (Colliders == null)
  74. {
  75. Debug.LogWarning("SkyCloud does not have a collider. No distance calculations can be done.", this.gameObject);
  76. return distanceLimit;
  77. }
  78. bool isInBounds = false;
  79. foreach (var collider in Colliders)
  80. {
  81. if (collider == null || !collider.enabled)
  82. continue;
  83. var closestPoint = collider.ClosestPointOnBounds(position);
  84. var sqrDistance = Vector3.SqrMagnitude(closestPoint - position);
  85. if (sqrDistance < distanceLimit * distanceLimit)
  86. {
  87. isInBounds = true;
  88. break;
  89. }
  90. }
  91. float minDistance = distanceLimit;
  92. if (isInBounds)
  93. {
  94. foreach (var collider in Colliders)
  95. {
  96. if (collider == null || !collider.enabled)
  97. continue;
  98. var closestPoint = collider.ClosestPointOnBounds(position);
  99. var sqrDistance = Vector3.SqrMagnitude(closestPoint - position);
  100. if (sqrDistance < distanceLimit * distanceLimit)
  101. {
  102. // Shortcut for when the observer is inside one of the colliders.
  103. if (IsInsideMesh(position))
  104. {
  105. return 0f;
  106. }
  107. if (collider.isTrigger)
  108. {
  109. closestPoint = collider.ClosestPoint(position);
  110. //_closestPointForGizmo = closestPoint;
  111. float distance = Vector3.Magnitude(closestPoint - position);
  112. if (distance < minDistance)
  113. {
  114. closestPosition = closestPoint;
  115. minDistance = distance;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. return minDistance;
  122. }
  123. //private void OnDrawGizmos()
  124. //{
  125. // Gizmos.DrawSphere(_closestPointForGizmo, 0.1f);
  126. //}
  127. public bool IsInsideMesh(Vector3 point)
  128. {
  129. Physics.queriesHitBackfaces = true;
  130. int hitsUp = Physics.RaycastNonAlloc(new Ray(point, Vector3.up), _hitsUp);
  131. Physics.queriesHitBackfaces = false;
  132. // If the hit normal y value is positive then this means we have hit a back face from the inside.
  133. for (var i = 0; i < hitsUp; i++)
  134. if (_hitsUp[i].normal.y > 0)
  135. return true;
  136. // For planes we would have to another test but we skip this because we assume it's always a mesh collider with a volume > 0.
  137. return false;
  138. }
  139. public static bool CheckSphereIntersection(Collider targetCollider, SphereCollider sphereCollider, out Vector3 closestPoint, out Vector3 surfaceNormal)
  140. {
  141. closestPoint = Vector3.zero;
  142. surfaceNormal = Vector3.zero;
  143. float penetrationDepth = 0f;
  144. Vector3 sphere_pos = sphereCollider.transform.position;
  145. if (Physics.ComputePenetration(targetCollider, targetCollider.transform.position, targetCollider.transform.rotation, sphereCollider, sphere_pos, Quaternion.identity, out surfaceNormal, out penetrationDepth))
  146. {
  147. closestPoint = sphere_pos + (surfaceNormal * (sphereCollider.radius - penetrationDepth));
  148. surfaceNormal = -surfaceNormal;
  149. return true;
  150. }
  151. return false;
  152. }
  153. }
  154. }