BsScenePicking.h 6.0 KB

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