ObjectInteraction3DShape.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace O3DWB
  6. {
  7. [Serializable]
  8. public abstract class ObjectInteraction3DShape
  9. {
  10. #region Private Constant Variables
  11. private const float _allowedObjectBoxDistanceFromSurfacePlaneForOverlapDetection = 0.12f;
  12. #endregion
  13. #region Protected Variables
  14. protected List<Vector3> _renderPoints = new List<Vector3>();
  15. protected bool _renderPointsMustBeCalculated = true;
  16. protected Vector3 _furthestRenderPointInFrontOfShapePlane;
  17. protected Vector3 _furthestRenderPointBehindShapePlane;
  18. protected MouseCursorRayHit _cursorRayHit;
  19. #endregion
  20. #region Public Properties
  21. public Plane Plane { get { return new Plane(Normal, Center); } }
  22. public Vector3 FurthestRenderPointInFrontOfShapePlane
  23. {
  24. get
  25. {
  26. if (_renderPointsMustBeCalculated) CalculateRenderPoints();
  27. return _furthestRenderPointInFrontOfShapePlane;
  28. }
  29. }
  30. public Vector3 FurthestRenderPointBehindShapePlane
  31. {
  32. get
  33. {
  34. if (_renderPointsMustBeCalculated) CalculateRenderPoints();
  35. return _furthestRenderPointBehindShapePlane;
  36. }
  37. }
  38. public MouseCursorRayHit CursorRayHit { get { return _cursorRayHit; } }
  39. #endregion
  40. #region Public Abstract Properties
  41. public abstract Vector3 Center { get; set; }
  42. public abstract Vector3 Normal { get; set; }
  43. #endregion
  44. #region Public Methods
  45. public List<Vector3> GetRenderPoints()
  46. {
  47. if (_renderPointsMustBeCalculated) CalculateRenderPoints();
  48. return new List<Vector3>(_renderPoints);
  49. }
  50. public void AcquireCursorRayHit(ObjectMask objectMask, MouseCursorObjectPickFlags cursorPickMaskFlags)
  51. {
  52. MouseCursor.Instance.PushObjectMask(objectMask);
  53. MouseCursor.Instance.PushObjectPickMaskFlags(cursorPickMaskFlags);
  54. _cursorRayHit = MouseCursor.Instance.GetRayHit();
  55. MouseCursor.Instance.PopObjectMask();
  56. MouseCursor.Instance.PopObjectPickMaskFlags();
  57. }
  58. public void AcquireCursorRayHitForTerrainObject(GameObject terrainObject)
  59. {
  60. _cursorRayHit = MouseCursor.Instance.GetCursorRayHitForTerrainObject(terrainObject);
  61. }
  62. public void AdjustShapeCenterAndNormalBasedOnCursorRayHit()
  63. {
  64. if (HasCursorPickedObject())
  65. {
  66. GameObjectRayHit objectRayHit = _cursorRayHit.ClosestObjectRayHit;
  67. if (objectRayHit.WasTerrainHit) AdjustShapeWhenSittingOnTerrain(objectRayHit);
  68. else if (objectRayHit.WasMeshHit) AdjustShapeWhenSittingOnMesh(objectRayHit);
  69. else if (objectRayHit.WasSpriteHit) AdjustShapeWhenSittingOnSprite(objectRayHit);
  70. }
  71. else
  72. if (HasCursorPickedGridCell()) AdjustShapeWhenSittingOnGridCell();
  73. }
  74. public List<GameObject> GetPotentialyOverlappedGameObjects()
  75. {
  76. Sphere overlapSphere = new Sphere(Center, CalculateObjectOverlapSphereRadius());
  77. return Octave3DScene.Get().OverlapSphere(overlapSphere);
  78. }
  79. public List<GameObject> GetOverlappedGameObjects(bool allowPartialOverlap)
  80. {
  81. if (!HasCursorPickedAnything()) return new List<GameObject>();
  82. List<GameObject> potentialyOverlappedObjects = GetPotentialyOverlappedGameObjects();
  83. if (allowPartialOverlap) return GetOverlappedGameObjectsForPartialOverlap(potentialyOverlappedObjects);
  84. else return GetOverlappedGameObjectsForFullOverlap(potentialyOverlappedObjects);
  85. }
  86. public bool HasCursorPickedAnything()
  87. {
  88. return IsCursorRayHitAvailable() && _cursorRayHit.WasAnythingHit;
  89. }
  90. public bool HasCursorPickedSprite()
  91. {
  92. return HasCursorPickedObject() && _cursorRayHit.ClosestObjectRayHit.WasSpriteHit;
  93. }
  94. public bool HasCursorPickedGridCell()
  95. {
  96. return IsCursorRayHitAvailable() && _cursorRayHit.WasACellHit;
  97. }
  98. public bool HasCursorPickedObject()
  99. {
  100. return IsCursorRayHitAvailable() && _cursorRayHit.WasAnObjectHit;
  101. }
  102. public bool HasCursorPickedTerrainObject()
  103. {
  104. return HasCursorPickedObject() && _cursorRayHit.ClosestObjectRayHit.WasTerrainHit;
  105. }
  106. public bool HasCursorPickedMeshObject()
  107. {
  108. return HasCursorPickedObject() && _cursorRayHit.ClosestObjectRayHit.WasMeshHit;
  109. }
  110. public bool IsCursorRayHitAvailable()
  111. {
  112. return _cursorRayHit != null;
  113. }
  114. public GameObject GetGameObjectPickedByCursor()
  115. {
  116. if (HasCursorPickedObject()) return _cursorRayHit.ClosestObjectRayHit.HitObject;
  117. return null;
  118. }
  119. #endregion
  120. #region Public Abstract Methods
  121. public abstract void RenderGizmos();
  122. public abstract bool OverlapsPolygon(Polygon3D polygon);
  123. public abstract bool ContainsPoint(Vector3 point);
  124. public abstract bool ContainsAllPoints(List<Vector3> points);
  125. public abstract bool ContainsAnyPoint(List<Vector3> points);
  126. #endregion
  127. #region Protected Methods
  128. protected void CalculateRenderPoints()
  129. {
  130. if (_renderPointsMustBeCalculated)
  131. {
  132. _renderPoints = GenerateRenderPoints();
  133. _renderPointsMustBeCalculated = false;
  134. if (HasCursorPickedObject()) ProjectRenderPointsOnHoveredObject();
  135. CalculateRenderPointsFurthestFromShapePlane();
  136. }
  137. }
  138. #endregion
  139. #region Protected Methods
  140. protected abstract List<Vector3> GenerateRenderPoints();
  141. #endregion
  142. #region Private Methods
  143. private void CalculateRenderPointsFurthestFromShapePlane()
  144. {
  145. if (!Plane.GetFurthestPointInFront(_renderPoints, out _furthestRenderPointInFrontOfShapePlane)) _furthestRenderPointInFrontOfShapePlane = _renderPoints[0];
  146. if (!Plane.GetFurthestPointBehind(_renderPoints, out _furthestRenderPointBehindShapePlane)) _furthestRenderPointBehindShapePlane = _renderPoints[0];
  147. }
  148. private float CalculateObjectOverlapSphereRadius()
  149. {
  150. float distanceFromCenterToFurthestPointInFrontOfPlane = (Center - FurthestRenderPointInFrontOfShapePlane).magnitude;
  151. float distanceFromCenterToFurthestPointBehindPlane = (Center - FurthestRenderPointBehindShapePlane).magnitude;
  152. float sphereRadius = distanceFromCenterToFurthestPointInFrontOfPlane;
  153. if (sphereRadius < distanceFromCenterToFurthestPointBehindPlane) sphereRadius = distanceFromCenterToFurthestPointBehindPlane;
  154. return sphereRadius;
  155. }
  156. private void AdjustShapeWhenSittingOnTerrain(GameObjectRayHit objectRayHit)
  157. {
  158. Normal = Vector3.up;
  159. Center = objectRayHit.HitPoint;
  160. }
  161. private void AdjustShapeWhenSittingOnMesh(GameObjectRayHit objectRayHit)
  162. {
  163. Normal = objectRayHit.HitNormal;
  164. Center = objectRayHit.HitPoint;
  165. }
  166. private void AdjustShapeWhenSittingOnSprite(GameObjectRayHit objectRayHit)
  167. {
  168. Normal = objectRayHit.HitNormal;
  169. Center = objectRayHit.HitPoint;
  170. }
  171. private void AdjustShapeWhenSittingOnGridCell()
  172. {
  173. Normal = _cursorRayHit.GridCellRayHit.HitCell.ParentGrid.TransformMatrix.GetNormalizedUpAxis();
  174. Center = _cursorRayHit.GridCellRayHit.HitPoint;
  175. }
  176. private List<GameObject> GetOverlappedGameObjectsForPartialOverlap(List<GameObject> potentialyOverlappedObjects)
  177. {
  178. var overlappedGameObjects = new List<GameObject>(potentialyOverlappedObjects.Count);
  179. foreach (GameObject gameObject in potentialyOverlappedObjects)
  180. {
  181. if (ShouldGameObjectBeIgnoredForOverlapDetection(gameObject)) continue;
  182. if (IsGameObjectPartiallyOverlapped(gameObject)) overlappedGameObjects.Add(gameObject);
  183. }
  184. return overlappedGameObjects;
  185. }
  186. private bool ShouldGameObjectBeIgnoredForOverlapDetection(GameObject gameObject)
  187. {
  188. if (gameObject == GetGameObjectPickedByCursor()) return true;
  189. if (ObjectQueries.IsGameObjectEmpty(gameObject)) return true;
  190. return false;
  191. }
  192. private bool IsGameObjectPartiallyOverlapped(GameObject gameObject)
  193. {
  194. OrientedBox worldOrientedBox = gameObject.GetWorldOrientedBox();
  195. if (worldOrientedBox.IsValid())
  196. {
  197. Polygon3D projectedBoxCornerPointsPoly = worldOrientedBox.Get3DPolygonFromCornerPointsProjectedOnPlane(Plane);
  198. if (OverlapsPolygon(projectedBoxCornerPointsPoly) &&
  199. DoesWorldOrientedBoxIntersectOrResideOnShapeSurface(worldOrientedBox)) return true;
  200. }
  201. return false;
  202. }
  203. private bool DoesWorldOrientedBoxIntersectOrResideOnShapeSurface(OrientedBox worldOrientedBox)
  204. {
  205. return worldOrientedBox.IsSpanningOrIsCloseInFrontOrOnAnyPlane(GetPlanesOnWhichOrientedBoxResides(worldOrientedBox), _allowedObjectBoxDistanceFromSurfacePlaneForOverlapDetection);
  206. }
  207. private List<Plane> GetPlanesOnWhichOrientedBoxResides(OrientedBox worldOrientedBox)
  208. {
  209. if (HasCursorPickedTerrainObject())
  210. {
  211. List<Vector3> boxCenterAndCornerPoints = worldOrientedBox.GetCenterAndCornerPoints();
  212. var planes = new List<Plane>(boxCenterAndCornerPoints.Count);
  213. Octave3DColliderRayHit colliderRayHit;
  214. Octave3DCollider hitCollider = _cursorRayHit.ClosestObjectRayHit.HitCollider;
  215. foreach (Vector3 boxPoint in boxCenterAndCornerPoints)
  216. {
  217. Ray ray = new Ray(boxPoint, Normal);
  218. if (hitCollider.RaycastBothDirections(ray, out colliderRayHit)) planes.Add(new Plane(colliderRayHit.HitNormal, colliderRayHit.HitPoint));
  219. else planes.Add(Plane);
  220. }
  221. return planes;
  222. }
  223. return new List<Plane> { Plane };
  224. }
  225. private List<GameObject> GetOverlappedGameObjectsForFullOverlap(List<GameObject> potentialyOverlappedObjects)
  226. {
  227. var overlappedGameObjects = new List<GameObject>(potentialyOverlappedObjects.Count);
  228. foreach (GameObject gameObject in potentialyOverlappedObjects)
  229. {
  230. if (ShouldGameObjectBeIgnoredForOverlapDetection(gameObject)) continue;
  231. if (IsGameObjectFullyOverlappedByCircle(gameObject)) overlappedGameObjects.Add(gameObject);
  232. }
  233. return overlappedGameObjects;
  234. }
  235. private bool IsGameObjectFullyOverlappedByCircle(GameObject gameObject)
  236. {
  237. OrientedBox worldOrientedBox = gameObject.GetWorldOrientedBox();
  238. return IsWorldOrientedBoxFullyOverlappedByCircle(worldOrientedBox);
  239. }
  240. private bool IsWorldOrientedBoxFullyOverlappedByCircle(OrientedBox worldOrientedBox)
  241. {
  242. if (worldOrientedBox.IsValid())
  243. {
  244. if (ContainsAllPoints(worldOrientedBox.GetCornerPointsProjectedOnPlane(Plane)) &&
  245. DoesWorldOrientedBoxIntersectOrResideOnShapeSurface(worldOrientedBox)) return true;
  246. }
  247. return false;
  248. }
  249. private void ProjectRenderPointsOnHoveredObject()
  250. {
  251. if(HasCursorPickedMeshObject() || HasCursorPickedTerrainObject())
  252. {
  253. var pointsProjector = new PointsOnColliderProjector(_cursorRayHit.ClosestObjectRayHit.HitCollider, Plane);
  254. _renderPoints = pointsProjector.ProjectPoints(_renderPoints);
  255. }
  256. }
  257. #endregion
  258. }
  259. }
  260. #endif