BsSceneGrid.h 4.5 KB

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