SnapSurface.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace O3DWB
  7. {
  8. [Serializable]
  9. public class SnapSurface
  10. {
  11. #region Private Variables
  12. [SerializeField]
  13. private bool _isValid;
  14. [SerializeField]
  15. private ObjectColliderSnapSurfaceGrid _objectBoxSnapSurfaceGrid = new ObjectColliderSnapSurfaceGrid();
  16. [SerializeField]
  17. private XZOrientedQuad3D _surfaceQuad;
  18. [SerializeField]
  19. private Vector3 _mouseCursorPickPoint;
  20. [SerializeField]
  21. private SnapSurfaceType _type;
  22. [SerializeField]
  23. private GameObject _surfaceObject;
  24. #endregion
  25. #region Public Static Properties
  26. public static Vector3 ModelSpaceRightAxis { get { return XZOrientedQuad3D.ModelSpaceRightAxis; } }
  27. public static Vector3 ModelSpacePlaneNormal { get { return XZOrientedQuad3D.ModelSpacePlaneNormal; } }
  28. public static Vector3 ModelSpaceLookAxis { get { return XZOrientedQuad3D.ModelSpaceLookAxis; } }
  29. #endregion
  30. #region Public Properties
  31. public bool IsValid { get { return _isValid; } }
  32. public Plane Plane { get { return _surfaceQuad != null ? _surfaceQuad.Plane : new Plane(); } }
  33. public XZOrientedQuad3D SurfaceQuad { get { return new XZOrientedQuad3D(_surfaceQuad); } }
  34. public XZGridRenderSettings RenderSettingsForColliderSnapSurfaceGrid { get { return _objectBoxSnapSurfaceGrid.RenderSettings; } }
  35. public GameObject SurfaceObject { get { return _surfaceObject; } }
  36. public Vector3 Center { get { return _surfaceQuad != null ? _surfaceQuad.Center : Vector3.zero; } }
  37. public Vector3 CursorPickPoint { get { return _mouseCursorPickPoint; } }
  38. public SnapSurfaceType SurfaceType { get { return _type; } }
  39. #endregion
  40. #region Public Methods
  41. public void RenderGizmos()
  42. {
  43. // Note: We will check if the surface object is null so that we don't render the surface
  44. // if the object has somehow been deleted from the scene.
  45. if (IsValid && _type == SnapSurfaceType.ObjectCollider && _surfaceObject != null && !ObjectSnapping.Get().Settings.EnableObjectToObjectSnap) _objectBoxSnapSurfaceGrid.RenderGizmos();
  46. }
  47. public void Refresh()
  48. {
  49. if (_isValid && _type == SnapSurfaceType.ObjectCollider)
  50. {
  51. _objectBoxSnapSurfaceGrid.FromXZOrientedQuad(_surfaceQuad);
  52. SceneView.RepaintAll();
  53. }
  54. }
  55. public void FromMouseCursorRayHit(MouseCursorRayHit cursorRayHit)
  56. {
  57. _isValid = false;
  58. if (!cursorRayHit.WasAnythingHit) return;
  59. else
  60. if (cursorRayHit.WasAnObjectHit && ObjectSnapSettings.Get().EnableObjectSurfaceGrid)
  61. {
  62. GameObjectRayHit gameObjectRayHit = FindClosestHitObjectWhichCanBeUsedAsSnapSurface(cursorRayHit);
  63. if (gameObjectRayHit != null)
  64. {
  65. if (!cursorRayHit.WasACellHit) ExtractData(gameObjectRayHit);
  66. else
  67. {
  68. if (gameObjectRayHit.HitEnter <= cursorRayHit.GridCellRayHit.HitEnter) ExtractData(gameObjectRayHit);
  69. }
  70. }
  71. }
  72. // If we didn't manage to build the surface up until now, we will give it one last chance
  73. // using the cursor grid cell ray hit.
  74. if (!_isValid && cursorRayHit.WasACellHit) ExtractData(cursorRayHit.GridCellRayHit);
  75. }
  76. public Vector3 GetSnapDestinationPointClosestToCursorPickPoint()
  77. {
  78. if(IsValid)
  79. {
  80. if (_type == SnapSurfaceType.GridCell)
  81. {
  82. List<Vector3> snapDestinationPoints = _surfaceQuad.GetCenterAndCornerPoints();
  83. snapDestinationPoints.AddRange(_surfaceQuad.GetMidEdgePoints());
  84. return Vector3Extensions.GetClosestPointToPoint(snapDestinationPoints, _mouseCursorPickPoint);
  85. }
  86. else
  87. {
  88. XZOrientedQuad3D gridCellQuad = _objectBoxSnapSurfaceGrid.GetCellFromPoint(_mouseCursorPickPoint).Quad;
  89. List<Vector3> snapDestinationPoints = gridCellQuad.GetCenterAndCornerPoints();
  90. snapDestinationPoints.AddRange(gridCellQuad.GetMidEdgePoints());
  91. return Vector3Extensions.GetClosestPointToPoint(snapDestinationPoints, _mouseCursorPickPoint);
  92. }
  93. }
  94. return Vector3.zero;
  95. }
  96. public TransformMatrix GetTransformMatrix()
  97. {
  98. if (IsValid) return _surfaceQuad.TransformMatrix;
  99. return TransformMatrix.GetIdentity();
  100. }
  101. public Vector3 GetLocalAxis(CoordinateSystemAxis axis)
  102. {
  103. if (IsValid) return _surfaceQuad.GetLocalAxis(axis);
  104. return Vector3.zero;
  105. }
  106. public Vector3 GetNormal()
  107. {
  108. if (IsValid) return _surfaceQuad.GetLocalAxis(CoordinateSystemAxis.PositiveUp);
  109. return Vector3.up;
  110. }
  111. #endregion
  112. #region Private Methods
  113. private GameObjectRayHit FindClosestHitObjectWhichCanBeUsedAsSnapSurface(MouseCursorRayHit cursorRayHit)
  114. {
  115. ObjectMask objectSnapMask = ObjectSnapping.Get().ObjectSnapMask;
  116. List<GameObjectRayHit> gameObjectHits = cursorRayHit.SortedObjectRayHits;
  117. for(int hitIndex = 0; hitIndex < gameObjectHits.Count; ++hitIndex)
  118. {
  119. GameObject hitObject = gameObjectHits[hitIndex].HitObject;
  120. if (!hitObject.HasMesh()) continue;
  121. if (objectSnapMask.IsGameObjectMasked(hitObject)) continue;
  122. return gameObjectHits[hitIndex];
  123. }
  124. return null;
  125. }
  126. private void ExtractData(GameObjectRayHit objectRayHit)
  127. {
  128. if (objectRayHit.WasBoxHit)
  129. {
  130. _isValid = true;
  131. _mouseCursorPickPoint = objectRayHit.HitPoint;
  132. CalculateSurfaceQuad(objectRayHit.ObjectBoxHit);
  133. _objectBoxSnapSurfaceGrid.FromXZOrientedQuad(_surfaceQuad);
  134. _type = SnapSurfaceType.ObjectCollider;
  135. _surfaceObject = objectRayHit.HitObject;
  136. }
  137. }
  138. private void CalculateSurfaceQuad(OrientedBoxRayHit boxRayHit)
  139. {
  140. OrientedBox hitBox = boxRayHit.HitBox;
  141. CoordinateSystem pickedFaceCoordSystem = hitBox.GetBoxFaceCoordinateSystem(boxRayHit.HitFace);
  142. _surfaceQuad = new XZOrientedQuad3D(pickedFaceCoordSystem.GetOriginPosition(),
  143. hitBox.GetBoxFaceSizeAlongFaceLocalXZAxes(boxRayHit.HitFace),
  144. pickedFaceCoordSystem.GetRotation());
  145. }
  146. private void ExtractData(GridCellRayHit gridCellRayHit)
  147. {
  148. _isValid = true;
  149. _surfaceQuad = gridCellRayHit.HitCell.Quad;
  150. _mouseCursorPickPoint = gridCellRayHit.HitPoint;
  151. _type = SnapSurfaceType.GridCell;
  152. _surfaceObject = null;
  153. }
  154. #endregion
  155. }
  156. }
  157. #endif