BsSceneGrid.h 4.4 KB

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