ObjectColliderSnapSurfaceGrid.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System;
  4. namespace O3DWB
  5. {
  6. [Serializable]
  7. public class ObjectColliderSnapSurfaceGrid
  8. {
  9. #region Private Variables
  10. [SerializeField]
  11. private XZGrid _grid;
  12. #endregion
  13. #region Private Properties
  14. private XZGrid Grid
  15. {
  16. get
  17. {
  18. if(_grid == null)
  19. {
  20. _grid = Octave3DWorldBuilder.ActiveInstance.CreateScriptableObject<XZGrid>();
  21. _grid.DimensionSettings.DimensionType = XZGridDimensionType.Finite;
  22. _grid.RenderSettings.CellLineThickness = 0.01f;
  23. _grid.RenderSettings.PlaneColor = new Color(0.3254f, 1.0f, 0.2f, 0.3019f);
  24. _grid.RenderableCoordinateSystem.RenderSettings.IsVisible = false;
  25. }
  26. return _grid;
  27. }
  28. }
  29. #endregion
  30. #region Public Properties
  31. public XZGridRenderSettings RenderSettings { get { return Grid.RenderSettings; } }
  32. #endregion
  33. #region Public Methods
  34. public void RenderGizmos()
  35. {
  36. Vector3 oldOriginPosition = Grid.GetOriginPosition();
  37. // Note: This should not be necessary, but on a couple of ocasions this was reset to 'Infinite' probably due to a serialization issue.
  38. // Can not reproduce this anymore. Moreover, the 'XZGrid' class has been made into a ScripatbleObject which most likely fixed the
  39. // issue. However, it's better to be safe :)
  40. Grid.DimensionSettings.DimensionType = XZGridDimensionType.Finite;
  41. Grid.Translate(Grid.Plane.normal * 0.002f);
  42. Grid.RenderGizmos();
  43. Grid.SetOriginPosition(oldOriginPosition);
  44. }
  45. public XZGridCell GetCellFromPoint(Vector3 point)
  46. {
  47. return Grid.GetCellFromPoint(point);
  48. }
  49. public void FromXZOrientedQuad(XZOrientedQuad3D orientedQuad)
  50. {
  51. float desiredCellSize = ObjectSnapSettings.Get().ObjectColliderSnapSurfaceGridSettings.DesiredCellSize;
  52. // As a first step, ensure that the grid sits in the middle of the quad and has the same orientation
  53. Grid.SetOriginPosition(orientedQuad.GetOriginPosition());
  54. Grid.SetRotation(orientedQuad.GetRotation());
  55. Vector2 quadSize = orientedQuad.ScaledXZSize;
  56. float cellSizeX = desiredCellSize;
  57. float cellSizeZ = desiredCellSize;
  58. int cellCountX, cellCountZ;
  59. if(cellSizeX > quadSize.x)
  60. {
  61. cellSizeX = quadSize.x;
  62. cellCountX = 1;
  63. }
  64. else
  65. {
  66. // Adjust the cell size in such a way that we get an integer number of cells along each dimension
  67. float divisionResult = quadSize.x / cellSizeX;
  68. cellCountX = (int)divisionResult;
  69. float fractionalValue = divisionResult - cellCountX;
  70. if (fractionalValue != 0.0f) cellSizeX += (fractionalValue / cellCountX) * desiredCellSize; // If the fractional value is not 0, it means the cell size needs to be adjusted
  71. // in such a way that the calculated number of cells ('cellCountX') covers the entire
  72. // quad area along the corresponding dimension.
  73. }
  74. if(cellSizeZ > quadSize.y)
  75. {
  76. cellSizeZ = quadSize.y;
  77. cellCountZ = 1;
  78. }
  79. else
  80. {
  81. float divisionResult = quadSize.y / cellSizeZ;
  82. cellCountZ = (int)divisionResult;
  83. float fractionalValue = divisionResult - cellCountZ;
  84. if (fractionalValue != 0.0f) cellSizeZ += (fractionalValue / cellCountZ) * desiredCellSize;
  85. }
  86. // Make sure the cell size is not larger than the quad
  87. if (cellSizeX > quadSize.x) cellSizeX = quadSize.x;
  88. if (cellSizeZ > quadSize.y) cellSizeZ = quadSize.y;
  89. Grid.CellSizeSettings.CellSizeX = cellSizeX;
  90. Grid.CellSizeSettings.CellSizeZ = cellSizeZ;
  91. // Store the cell count without taking into consideration the cell which sits at the origin of the grid
  92. int cellCountXNoMiddle = cellCountX - 1;
  93. int cellCountZNoMiddle = cellCountZ - 1;
  94. if(cellCountXNoMiddle % 2 == 0)
  95. {
  96. int halfCount = cellCountXNoMiddle / 2;
  97. Grid.DimensionSettings.FiniteDimensionSettings.XAxisCellIndexRange.Min = -halfCount;
  98. Grid.DimensionSettings.FiniteDimensionSettings.XAxisCellIndexRange.Max = halfCount;
  99. }
  100. else
  101. {
  102. int halfCount = cellCountXNoMiddle / 2;
  103. Grid.DimensionSettings.FiniteDimensionSettings.XAxisCellIndexRange.Min = -halfCount;
  104. Grid.DimensionSettings.FiniteDimensionSettings.XAxisCellIndexRange.Max = halfCount + 1;
  105. }
  106. if (cellCountZNoMiddle % 2 == 0)
  107. {
  108. int halfCount = cellCountZNoMiddle / 2;
  109. Grid.DimensionSettings.FiniteDimensionSettings.ZAxisCellIndexRange.Min = -halfCount;
  110. Grid.DimensionSettings.FiniteDimensionSettings.ZAxisCellIndexRange.Max = halfCount;
  111. }
  112. else
  113. {
  114. int halfCount = cellCountZNoMiddle / 2;
  115. Grid.DimensionSettings.FiniteDimensionSettings.ZAxisCellIndexRange.Min = -halfCount;
  116. Grid.DimensionSettings.FiniteDimensionSettings.ZAxisCellIndexRange.Max = halfCount + 1;
  117. }
  118. // We need to make sure that the grid nicely sits within the boundaries of the quad. In order to do this, we will
  119. // align the top left corners of the quad and the grid's top left cell.
  120. Vector3 quadTopLeftCornerPoint = orientedQuad.GetCornerPoints()[(int)XZOrientedQuad3DCornerPoint.TopLeft];
  121. XZOrientedQuad3D topLeftCellQuad = Grid.CalculateCellQuad(Grid.DimensionSettings.FiniteDimensionSettings.XAxisCellIndexRange.Min, Grid.DimensionSettings.FiniteDimensionSettings.ZAxisCellIndexRange.Max);
  122. Vector3 cellQuadTopLeftCornerPoint = topLeftCellQuad.GetCornerPoints()[(int)XZOrientedQuad3DCornerPoint.TopLeft];
  123. Vector3 gridOriginMoveVector = quadTopLeftCornerPoint - cellQuadTopLeftCornerPoint;
  124. Grid.Translate(gridOriginMoveVector);
  125. }
  126. #endregion
  127. }
  128. }
  129. #endif