BsSceneGrid.h 4.5 KB

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