XZGridRenderer.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace O3DWB
  5. {
  6. public class XZGridRenderer
  7. {
  8. public void RenderGizmos(XZGrid grid, CameraViewVolume cameraViewVolume)
  9. {
  10. if (!grid.RenderSettings.IsVisible) return;
  11. // Note: Can not figure out how to render a finite grid inside a shader yet... :D
  12. if(grid.DimensionSettings.DimensionType == XZGridDimensionType.Finite)
  13. {
  14. RenderGizmos_Obsolete(grid, cameraViewVolume);
  15. return;
  16. }
  17. Plane gridPlane = grid.Plane;
  18. Vector3 gridPlaneCenter = gridPlane.ProjectPoint(SceneViewCamera.Camera.transform.position);
  19. Box camVolumeAABB = cameraViewVolume.WorldSpaceAABB;
  20. List<Vector3> projectedVolumeAABBPts = gridPlane.ProjectAllPoints(camVolumeAABB.GetCornerPoints());
  21. List<Vector3> modelSpacePrjPts = Vector3Extensions.GetTransformedPoints(projectedVolumeAABBPts, grid.TransformMatrix.ToMatrix4x4x.inverse);
  22. Box modelSpacePtsBox = Box.FromPoints(modelSpacePrjPts);
  23. Vector3 gridPlaneSize = modelSpacePtsBox.Size;
  24. Matrix4x4 planeTransformMatrix = Matrix4x4.TRS(gridPlaneCenter, grid.Rotation, gridPlaneSize);
  25. Material xzGridMaterial = MaterialPool.Get().XZGridMaterial;
  26. xzGridMaterial.SetFloat("_CellSizeX", grid.CellSizeSettings.CellSizeX);
  27. xzGridMaterial.SetFloat("_CellSizeZ", grid.CellSizeSettings.CellSizeZ);
  28. xzGridMaterial.SetVector("_CellOffset", grid.GetOriginPosition());
  29. xzGridMaterial.SetColor("_LineColor", grid.RenderSettings.CellLineColor);
  30. xzGridMaterial.SetColor("_PlaneColor", grid.RenderSettings.PlaneColor);
  31. xzGridMaterial.SetFloat("_CamFarPlaneDist", SceneViewCamera.Camera.farClipPlane);
  32. xzGridMaterial.SetVector("_CamWorldPos", SceneViewCamera.Camera.transform.position);
  33. xzGridMaterial.SetMatrix("_InvRotMatrix", Matrix4x4.TRS(Vector3.zero, grid.Rotation, Vector3.one).inverse);
  34. xzGridMaterial.SetMatrix("_PlaneTransformMtx", planeTransformMatrix);
  35. int numPasses = xzGridMaterial.passCount;
  36. for (int passIndex = 0; passIndex < numPasses; ++passIndex)
  37. {
  38. xzGridMaterial.SetPass(passIndex);
  39. Graphics.DrawMeshNow(GizmosEx.XZRectangleMesh, planeTransformMatrix);
  40. }
  41. GizmosMatrix.Push(grid.TransformMatrix.ToMatrix4x4x);
  42. grid.RenderableCoordinateSystem.RenderGizmos();
  43. GizmosMatrix.Pop();
  44. }
  45. private void RenderGizmos_Obsolete(XZGrid grid, CameraViewVolume cameraViewVolume)
  46. {
  47. if (!grid.RenderSettings.IsVisible) return;
  48. var visibleCellRangeCalculator = new XZVisibleGridCellRangeCalculator();
  49. XZVisibleGridCellRange visibleCellRange = visibleCellRangeCalculator.Calculate(grid, cameraViewVolume);
  50. GizmosMatrix.Push(grid.TransformMatrix.ToMatrix4x4x);
  51. RenderGridPlane_Obsolete(grid, visibleCellRange);
  52. RenderGridCellLines_Obsolete(grid, visibleCellRange);
  53. grid.RenderableCoordinateSystem.RenderGizmos();
  54. GizmosMatrix.Pop();
  55. }
  56. private void RenderGridPlane_Obsolete(XZGrid grid, XZVisibleGridCellRange visibleCellRange)
  57. {
  58. int minCellIndexX = visibleCellRange.XAxisVisibleCellRange.Min;
  59. int maxCellIndexX = visibleCellRange.XAxisVisibleCellRange.Max;
  60. int minCellIndexZ = visibleCellRange.ZAxisVisibleCellRange.Min;
  61. int maxCellIndexZ = visibleCellRange.ZAxisVisibleCellRange.Max;
  62. float numberOfCellsOnX = maxCellIndexX - minCellIndexX + 1.0f;
  63. float numberOfCellsOnZ = maxCellIndexZ - minCellIndexZ + 1.0f;
  64. XZGridCellSizeSettings gridCellSizeSettings = grid.CellSizeSettings;
  65. Vector3 planeCenter = (grid.GetCellHrzStart(minCellIndexX) + grid.GetCellDepthStart(minCellIndexZ) +
  66. grid.GetCellHrzStart(maxCellIndexX + 1) + grid.GetCellDepthStart(maxCellIndexZ + 1)) * 0.5f;
  67. Vector3 planeSize = new Vector3(numberOfCellsOnX * gridCellSizeSettings.CellSizeX, 0.0f, numberOfCellsOnZ * gridCellSizeSettings.CellSizeZ);
  68. GizmosColor.Push(grid.RenderSettings.PlaneColor);
  69. Gizmos.DrawCube(planeCenter, planeSize);
  70. GizmosColor.Pop();
  71. }
  72. private void RenderGridCellLines_Obsolete(XZGrid grid, XZVisibleGridCellRange visibleCellRange)
  73. {
  74. int minCellIndexX = visibleCellRange.XAxisVisibleCellRange.Min;
  75. int maxCellIndexX = visibleCellRange.XAxisVisibleCellRange.Max;
  76. float numberOfCellsOnX = maxCellIndexX - minCellIndexX + 1.0f;
  77. int minCellIndexZ = visibleCellRange.ZAxisVisibleCellRange.Min;
  78. int maxCellIndexZ = visibleCellRange.ZAxisVisibleCellRange.Max;
  79. float numberOfCellsOnZ = maxCellIndexZ - minCellIndexZ + 1.0f;
  80. XZGridCellSizeSettings gridCellSizeSettings = grid.CellSizeSettings;
  81. XZGridRenderSettings gridRenderSettings = grid.RenderSettings;
  82. Vector3 startPointOnX, startPointOnZ;
  83. Vector3 lineCubeSize = Vector3.zero;
  84. // Render the grid lines which extend along the grid's X axis
  85. GizmosColor.Push(grid.RenderSettings.CellLineColor);
  86. startPointOnX = grid.GetCellHrzStart(minCellIndexX);
  87. lineCubeSize.z = gridRenderSettings.CellLineThickness;
  88. int maxLineIndex = maxCellIndexZ + 1;
  89. for (int lineIndex = minCellIndexZ; lineIndex <= maxLineIndex; ++lineIndex)
  90. {
  91. startPointOnZ = grid.GetCellDepthStart(lineIndex);
  92. Vector3 firstPoint = startPointOnX + startPointOnZ;
  93. Vector3 secondPoint = firstPoint + XZGrid.ModelSpaceRightAxis * (numberOfCellsOnX * gridCellSizeSettings.CellSizeX);
  94. lineCubeSize.x = (firstPoint - secondPoint).magnitude + gridRenderSettings.CellLineThickness;
  95. Gizmos.DrawCube((firstPoint + secondPoint) * 0.5f, lineCubeSize);
  96. }
  97. // Render the grid lines which extend along the grid's Z axis
  98. startPointOnZ = grid.GetCellDepthStart(minCellIndexZ);
  99. lineCubeSize.x = gridRenderSettings.CellLineThickness;
  100. maxLineIndex = maxCellIndexX + 1;
  101. for (int lineIndex = minCellIndexX; lineIndex <= maxLineIndex; ++lineIndex)
  102. {
  103. startPointOnX = grid.GetCellHrzStart(lineIndex);
  104. Vector3 firstPoint = startPointOnX + startPointOnZ;
  105. Vector3 secondPoint = firstPoint + XZGrid.ModelSpaceLookAxis * (numberOfCellsOnZ * gridCellSizeSettings.CellSizeZ);
  106. lineCubeSize.z = (firstPoint - secondPoint).magnitude;
  107. Gizmos.DrawCube((firstPoint + secondPoint) * 0.5f, lineCubeSize);
  108. }
  109. GizmosColor.Pop();
  110. }
  111. }
  112. }
  113. #endif