BsSceneGrid.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "BsVector3.h"
  4. #include "BsColor.h"
  5. #include "BsMaterial.h"
  6. namespace BansheeEngine
  7. {
  8. class SceneGridCore;
  9. /** Determines how is the scene grid drawn. */
  10. enum class GridMode
  11. {
  12. Perspective, /**< Grid is drawn in XZ plane, at Y = 0. */
  13. OrthoX, /**< Grid is drawn in YZ plane, always visible along positive X. */
  14. OrthoY, /**< Grid is drawn in XZ plane, always visible along positive Y. */
  15. OrthoZ, /**< Grid is drawn in XY plane, always visible along positive Z. */
  16. OrthoNegX, /**< Grid is drawn in YZ plane, always visible along negative X. */
  17. OrthoNegY, /**< Grid is drawn in XZ plane, always visible along negative Y. */
  18. OrthoNegZ /**< Grid is drawn in XY plane, always visible along negative Z. */
  19. };
  20. /** Handles rendering of the grid in the scene view. */
  21. class BS_ED_EXPORT SceneGrid
  22. {
  23. public:
  24. SceneGrid(const CameraPtr& camera);
  25. ~SceneGrid();
  26. /** Sets the total width/height of the grid in XZ plane. */
  27. void setSize(UINT32 size);
  28. /** Sets the spacing between grid lines. */
  29. void setSpacing(float spacing);
  30. /** Determines in what position and orientation is the grid drawn. */
  31. void setMode(GridMode mode);
  32. /** Changes the active editor settings. Grid properties will be updated internally when editor settings change. */
  33. void setSettings(const EditorSettingsPtr& settings);
  34. /** Called once per frame. */
  35. void _update();
  36. private:
  37. /** Updates internal grid parameters from the attached settings object. */
  38. void updateFromEditorSettings();
  39. /** Rebuilds the scene grid mesh. Call this whenever grid parameters change. */
  40. void updateGridMesh();
  41. /**
  42. * Initializes the core thread portion of the scene grid renderer.
  43. *
  44. * @param[in] material Material used for drawing the grid.
  45. * @param[in] camera Camera to render the scene grid to.
  46. */
  47. void initializeCore(const SPtr<CameraCore>& camera, const SPtr<MaterialCore>& material);
  48. /** Destroys the core thread portion of the draw manager. */
  49. void destroyCore(SceneGridCore* core);
  50. float mSpacing = 1.0f;
  51. UINT32 mSize = 256;
  52. GridMode mMode = GridMode::Perspective;
  53. bool mCoreDirty;
  54. EditorSettingsPtr mSettings;
  55. UINT32 mSettingsHash = 0;
  56. HMesh mGridMesh;
  57. VertexDataDescPtr mVertexDesc;
  58. std::atomic<SceneGridCore*> mCore;
  59. };
  60. /** Handles scene grid rendering on the core thread. */
  61. class SceneGridCore
  62. {
  63. public:
  64. SceneGridCore() { }
  65. ~SceneGridCore();
  66. private:
  67. friend class SceneGrid;
  68. /**
  69. * Initializes the object. Must be called right after construction and before any use.
  70. *
  71. * @param[in] material Material used for drawing the grid.
  72. * @param[in] camera Camera to render the scene grid to.
  73. */
  74. void initialize(const SPtr<CameraCore>& camera, const SPtr<MaterialCore>& material);
  75. /**
  76. * Updates the grid mesh to render.
  77. *
  78. * @param[in] mesh Grid mesh to render.
  79. * @param[in] spacing Spacing between the grid lines.
  80. * @param[in] fadeGrid Determines should the grid fade out at larger distances.
  81. */
  82. void updateData(const SPtr<MeshCore>& mesh, float spacing, bool fadeGrid);
  83. /** Callback triggered by the renderer, actually draws the grid mesh. */
  84. void render();
  85. SPtr<CameraCore> mCamera;
  86. SPtr<MeshCore> mGridMesh;
  87. SPtr<MaterialCore> mGridMaterial;
  88. float mSpacing = 1.0f;
  89. bool mFadeGrid = true;
  90. MaterialParamMat4Core mViewProjParam;
  91. MaterialParamVec4Core mWorldCameraPosParam;
  92. MaterialParamColorCore mGridColorParam;
  93. MaterialParamFloatCore mGridSpacingParam;
  94. MaterialParamFloatCore mGridBorderWidthParam;
  95. MaterialParamFloatCore mGridFadeOutStartParam;
  96. MaterialParamFloatCore mGridFadeOutEndParam;
  97. static const Color GRID_LINE_COLOR;
  98. static const float LINE_WIDTH;
  99. static const float LINE_BORDER_WIDTH;
  100. static const float FADE_OUT_START;
  101. static const float FADE_OUT_END;
  102. };
  103. }