BsSceneGrid.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "Renderer/BsRendererExtension.h"
  6. #include "Math/BsVector2I.h"
  7. #include "Image/BsColor.h"
  8. #include "Material/BsMaterial.h"
  9. namespace bs
  10. {
  11. /** @addtogroup Scene-Editor-Internal
  12. * @{
  13. */
  14. namespace ct { class SceneGridRenderer; }
  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 SPtr<Camera>& 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 SPtr<EditorSettings>& 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. float mSpacing = 1.0f;
  48. UINT32 mSize = 256;
  49. GridMode mMode = GridMode::Perspective;
  50. bool mCoreDirty;
  51. SPtr<EditorSettings> mSettings;
  52. UINT32 mSettingsHash = 0;
  53. HMesh mGridMesh;
  54. SPtr<VertexDataDesc> mVertexDesc;
  55. SPtr<ct::SceneGridRenderer> mRenderer;
  56. };
  57. namespace ct
  58. {
  59. /** Handles scene grid rendering on the core thread. */
  60. class SceneGridRenderer : public RendererExtension
  61. {
  62. public:
  63. /** Structure used for initializing the renderer. */
  64. struct InitData
  65. {
  66. SPtr<Camera> camera;
  67. SPtr<Material> material;
  68. };
  69. SceneGridRenderer();
  70. private:
  71. friend class bs::SceneGrid;
  72. /** @copydoc RendererExtension::initialize */
  73. void initialize(const Any& data) override;
  74. /** @copydoc RendererExtension::check */
  75. RendererExtensionRequest check(const Camera& camera) override;
  76. /** @copydoc RendererExtension::render */
  77. void render(const Camera& camera, const RendererViewContext& viewContext) override;
  78. /**
  79. * Updates the grid mesh to render.
  80. *
  81. * @param[in] mesh Grid mesh to render.
  82. * @param[in] spacing Spacing between the grid lines.
  83. * @param[in] fadeGrid Determines should the grid fade out at larger distances.
  84. * @param[in] gridPlaneNormal Normal to the plane to render the grid on. Must be one of the basis vectors
  85. * (can't be arbitrary).
  86. */
  87. void updateData(const SPtr<Mesh>& mesh, float spacing, bool fadeGrid, const Vector3& gridPlaneNormal);
  88. SPtr<Camera> mCamera;
  89. SPtr<Mesh> mGridMesh;
  90. SPtr<Material> mGridMaterial;
  91. SPtr<GpuParamsSet> mMaterialParams;
  92. float mSpacing = 1.0f;
  93. bool mFadeGrid = true;
  94. Vector3 mGridPlaneNormal = Vector3::ZERO;
  95. MaterialParamMat4 mViewProjParam;
  96. MaterialParamVec4 mWorldCameraPosParam;
  97. MaterialParamColor mGridColorParam;
  98. MaterialParamFloat mGridSpacingParam;
  99. MaterialParamFloat mGridBorderWidthParam;
  100. MaterialParamFloat mGridFadeOutStartParam;
  101. MaterialParamFloat mGridFadeOutEndParam;
  102. MaterialParamVec3 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. }
  110. /** @} */
  111. }