BsScenePicking.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "Utility/BsModule.h"
  6. #include "Math/BsMatrix4.h"
  7. #include "RenderAPI/BsGpuParam.h"
  8. #include "Renderer/BsParamBlocks.h"
  9. namespace bs
  10. {
  11. struct GizmoDrawSettings;
  12. /** @addtogroup Scene-Editor
  13. * @{
  14. */
  15. /** Contains the data of a scene picking action. */
  16. struct SnapData
  17. {
  18. Vector3 normal;
  19. Vector3 pickPosition;
  20. };
  21. /** Contains the results of a scene picking action. */
  22. struct PickResults
  23. {
  24. Vector<UINT32> objects;
  25. Vector3 normal;
  26. float depth;
  27. };
  28. namespace ct { class ScenePicking; }
  29. /** Handles picking of scene objects with a pointer in scene view. */
  30. class BS_ED_EXPORT ScenePicking : public Module<ScenePicking>
  31. {
  32. /** Contains information about a single pickable item (mesh). */
  33. struct RenderablePickData
  34. {
  35. SPtr<ct::Mesh> mesh;
  36. UINT32 index;
  37. Matrix4 wvpTransform;
  38. bool alpha;
  39. CullingMode cullMode;
  40. HTexture mainTexture;
  41. };
  42. public:
  43. ScenePicking();
  44. ~ScenePicking();
  45. /**
  46. * Attempts to find a single nearest scene object under the provided position and area.
  47. *
  48. * @param[in] cam Camera to perform the picking from.
  49. * @param[in] gizmoDrawSettings Settings used for drawing pickable gizmos.
  50. * @param[in] position Pointer position relative to the camera viewport, in pixels.
  51. * @param[in] area Width/height of the checked area in pixels. Use (1, 1) if you want the exact
  52. * position under the pointer.
  53. * @param[in] ignoreRenderables A list of objects that should be ignored during scene picking.
  54. * @param[out] data Picking data regarding position and normal.
  55. * @return Nearest SceneObject under the provided area, or an empty handle if no object is
  56. * found.
  57. */
  58. HSceneObject pickClosestObject(const SPtr<Camera>& cam, const GizmoDrawSettings& gizmoDrawSettings,
  59. const Vector2I& position, const Vector2I& area, Vector<HSceneObject>& ignoreRenderables, SnapData* data = nullptr);
  60. /**
  61. * Attempts to find all scene objects under the provided position and area. This does not mean objects occluded by
  62. * other objects.
  63. *
  64. * @param[in] cam Camera to perform the picking from.
  65. * @param[in] gizmoDrawSettings Settings used for drawing pickable gizmos.
  66. * @param[in] position Pointer position relative to the camera viewport, in pixels.
  67. * @param[in] area Width/height of the checked area in pixels. Use (1, 1) if you want the exact
  68. * position under the pointer.
  69. * @param[in] ignoreRenderables A list of objects that should be ignored during scene picking.
  70. * @param[out] data Picking data regarding position and normal.
  71. * @return A list of SceneObject%s under the provided area.
  72. */
  73. Vector<HSceneObject> pickObjects(const SPtr<Camera>& cam, const GizmoDrawSettings& gizmoDrawSettings,
  74. const Vector2I& position, const Vector2I& area, Vector<HSceneObject>& ignoreRenderables,
  75. SnapData* data = nullptr);
  76. private:
  77. friend class ct::ScenePicking;
  78. typedef Set<RenderablePickData, std::function<bool(const RenderablePickData&, const RenderablePickData&)>> RenderableSet;
  79. /** Encodes a pickable object identifier to a unique color. */
  80. static Color encodeIndex(UINT32 index);
  81. /** Decodes a color into a unique object identifier. Color should have initially been encoded with encodeIndex(). */
  82. static UINT32 decodeIndex(Color color);
  83. ct::ScenePicking* mCore;
  84. };
  85. /** @} */
  86. namespace ct
  87. {
  88. /** @addtogroup Scene-Editor-Internal
  89. * @{
  90. */
  91. BS_PARAM_BLOCK_BEGIN(PickingParamBlockDef)
  92. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
  93. BS_PARAM_BLOCK_ENTRY(Color, gColorIndex)
  94. BS_PARAM_BLOCK_ENTRY(float, gAlphaCutoff)
  95. BS_PARAM_BLOCK_END
  96. extern PickingParamBlockDef gPickingParamBlockDef;
  97. /** Core thread version of the ScenePicking manager. Handles actual rendering. */
  98. class ScenePicking
  99. {
  100. public:
  101. /** Initializes the manager. Must be called right after construction. */
  102. void initialize();
  103. /** Destroys the manager. Must be called right before destruction. */
  104. void destroy();
  105. /**
  106. * Sets up the viewport, materials and their parameters as needed for picking. Also renders all the provided
  107. * renderable objects. Must be followed by corePickingEnd(). You may call other methods after this one, but you must
  108. * ensure they render proper unique pickable colors that can be resolved to SceneObject%s later.
  109. *
  110. * @param[in] target Render target to render to.
  111. * @param[in] viewportArea Normalized area of the render target to render in.
  112. * @param[in] renderables A set of pickable Renderable objects to render.
  113. * @param[in] position Position of the pointer where to pick objects, in pixels relative to viewport.
  114. * @param[in] area Width/height of the area to pick objects, in pixels.
  115. */
  116. void corePickingBegin(const SPtr<RenderTarget>& target, const Rect2& viewportArea,
  117. const bs::ScenePicking::RenderableSet& renderables, const Vector2I& position, const Vector2I& area);
  118. /**
  119. * Ends picking operation started by corePickingBegin(). Render target is resolved and objects in the picked area
  120. * are returned.
  121. *
  122. * @param[in] target Render target we're rendering to.
  123. * @param[in] viewportArea Normalized area of the render target we're rendering in.
  124. * @param[in] position Position of the pointer where to pick objects, in pixels relative to viewport.
  125. * @param[in] area Width/height of the area to pick objects, in pixels.
  126. * @param[in] gatherSnapData Determines whather normal & depth information will be recorded.
  127. * @param[out] asyncOp Async operation handle that when complete will contain the results of the picking
  128. * operation in the form of Vector<SelectedObject>.
  129. */
  130. void corePickingEnd(const SPtr<RenderTarget>& target, const Rect2& viewportArea, const Vector2I& position,
  131. const Vector2I& area, bool gatherSnapData, AsyncOp& asyncOp);
  132. private:
  133. friend class bs::ScenePicking;
  134. static const float ALPHA_CUTOFF;
  135. SPtr<RenderTexture> mPickingTexture;
  136. SPtr<Material> mMaterials[6];
  137. Vector<SPtr<GpuParamsSet>> mParamSets[6];
  138. Vector<SPtr<GpuParamBlockBuffer>> mParamBuffers;
  139. };
  140. /** @} */
  141. }
  142. }