BsSceneGrid.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  10. * @brief Handles rendering of the grid in the scene view.
  11. */
  12. class SceneGrid
  13. {
  14. public:
  15. SceneGrid(const CameraPtr& camera);
  16. ~SceneGrid();
  17. /**
  18. * @brief Sets the grid origin in world coordinates.
  19. */
  20. void setOrigin(const Vector3& origin);
  21. /**
  22. * @brief Sets the total width/height of the grid in XZ plane.
  23. */
  24. void setSize(UINT32 size);
  25. /**
  26. * @brief Sets the spacing between grid lines.
  27. */
  28. void setSpacing(float spacing);
  29. /**
  30. * @brief Changes the active editor settings. Grid properties
  31. * will be updated internally when editor settings change.
  32. */
  33. void setSettings(const EditorSettingsPtr& settings);
  34. /**
  35. * @brief Called once per frame.
  36. *
  37. * @note Internal method.
  38. */
  39. void update();
  40. private:
  41. /**
  42. * @brief Updates internal grid parameters from the attached settings object.
  43. */
  44. void updateFromEditorSettings();
  45. /**
  46. * @brief Rebuilds the scene grid mesh. Call this whenever grid parameters change.
  47. */
  48. void updateGridMesh();
  49. /**
  50. * @brief Initializes the core thread portion of the scene grid renderer.
  51. *
  52. * @param material Material used for drawing the grid.
  53. * @param camera Camera to render the scene grid to.
  54. */
  55. void initializeCore(const SPtr<CameraCore>& camera, const SPtr<MaterialCore>& material);
  56. /**
  57. * @brief Destroys the core thread portion of the draw manager.
  58. */
  59. void destroyCore(SceneGridCore* core);
  60. Vector3 mOrigin;
  61. float mSpacing = 1.0f;
  62. UINT32 mSize = 256;
  63. bool mCoreDirty;
  64. EditorSettingsPtr mSettings;
  65. UINT32 mSettingsHash = 0;
  66. HMesh mGridMesh;
  67. VertexDataDescPtr mVertexDesc;
  68. std::atomic<SceneGridCore*> mCore;
  69. };
  70. /**
  71. * @brief Handles scene grid rendering on the core thread.
  72. */
  73. class SceneGridCore
  74. {
  75. public:
  76. SceneGridCore() { }
  77. ~SceneGridCore();
  78. private:
  79. friend class SceneGrid;
  80. /**
  81. * @brief Initializes the object. Must be called right after construction and before any use.
  82. *
  83. * @param material Material used for drawing the grid.
  84. * @param camera Camera to render the scene grid to.
  85. */
  86. void initialize(const SPtr<CameraCore>& camera, const SPtr<MaterialCore>& material);
  87. /**
  88. * @brief Updates the grid mesh to render.
  89. *
  90. * @param mesh Grid mesh to render.
  91. * @param spacing Spacing between the grid lines.
  92. */
  93. void updateData(const SPtr<MeshCore>& mesh, float spacing);
  94. /**
  95. * @brief Callback triggered by the renderer, actually draws the grid mesh.
  96. */
  97. void render();
  98. SPtr<CameraCore> mCamera;
  99. SPtr<MeshCore> mGridMesh;
  100. SPtr<MaterialCore> mGridMaterial;
  101. float mSpacing = 1.0f;
  102. MaterialParamMat4Core mViewProjParam;
  103. MaterialParamVec4Core mWorldCameraPosParam;
  104. MaterialParamColorCore mGridColorParam;
  105. MaterialParamFloatCore mGridSpacingParam;
  106. MaterialParamFloatCore mGridBorderWidthParam;
  107. MaterialParamFloatCore mGridFadeOutStartParam;
  108. MaterialParamFloatCore mGridFadeOutEndParam;
  109. static const Color GRID_LINE_COLOR;
  110. static const float LINE_WIDTH;
  111. static const float LINE_BORDER_WIDTH;
  112. static const float FADE_OUT_START;
  113. static const float FADE_OUT_END;
  114. };
  115. }