BsScenePicking.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsMatrix4.h"
  5. #include "BsGpuParam.h"
  6. namespace BansheeEngine
  7. {
  8. class ScenePickingCore;
  9. /**
  10. * @brief Handles picking of scene objects with a pointer in scene view.
  11. */
  12. class BS_ED_EXPORT ScenePicking : public Module<ScenePicking>
  13. {
  14. /**
  15. * @brief Contains information about a single pickable item (mesh).
  16. */
  17. struct RenderablePickData
  18. {
  19. SPtr<MeshCore> mesh;
  20. UINT32 index;
  21. Matrix4 wvpTransform;
  22. bool alpha;
  23. CullingMode cullMode;
  24. HTexture mainTexture;
  25. };
  26. public:
  27. ScenePicking();
  28. ~ScenePicking();
  29. /**
  30. * @brief Attempts to find a single nearest scene object under the provided position and area.
  31. *
  32. * @param cam Camera to perform the picking from.
  33. * @param position Pointer position relative to the camera viewport, in pixels.
  34. * @param area Width/height of the checked area in pixels. Use (1, 1) if you want the
  35. * exact position under the pointer.
  36. *
  37. * @return Nearest SceneObject under the provided area, or an empty handle if no object is found.
  38. */
  39. HSceneObject pickClosestObject(const CameraPtr& cam, const Vector2I& position, const Vector2I& area);
  40. /**
  41. * @brief Attempts to find all scene objects under the provided position and area. This does not mean
  42. * objects occluded by other objects.
  43. *
  44. * @param cam Camera to perform the picking from.
  45. * @param position Pointer position relative to the camera viewport, in pixels.
  46. * @param area Width/height of the checked area in pixels. Use (1, 1) if you want the
  47. * exact position under the pointer.
  48. *
  49. * @return A list of SceneObject%s under the provided area.
  50. */
  51. Vector<HSceneObject> pickObjects(const CameraPtr& cam, const Vector2I& position, const Vector2I& area);
  52. private:
  53. friend class ScenePickingCore;
  54. typedef Set<RenderablePickData, std::function<bool(const RenderablePickData&, const RenderablePickData&)>> RenderableSet;
  55. /**
  56. * @brief Encodes a pickable object identifier to a unique color.
  57. */
  58. static Color encodeIndex(UINT32 index);
  59. /**
  60. * @brief Decodes a color into a unique object identifier. Color should
  61. * have initially been encoded with ::encodeIndex.
  62. */
  63. static UINT32 decodeIndex(Color color);
  64. ScenePickingCore* mCore;
  65. };
  66. /**
  67. * @brief Core thread version of the ScenePicking manager. Handles
  68. * actual rendering.
  69. */
  70. class ScenePickingCore
  71. {
  72. /**
  73. * @brief A list of materials and their parameters to be used for rendering
  74. * of pickable objects.
  75. */
  76. struct MaterialData
  77. {
  78. SPtr<MaterialCore> mMatPickingCore;
  79. SPtr<MaterialCore> mMatPickingAlphaCore;
  80. SPtr<GpuParamsCore> mParamPickingVertParams;
  81. SPtr<GpuParamsCore> mParamPickingFragParams;
  82. SPtr<GpuParamsCore> mParamPickingAlphaVertParams;
  83. SPtr<GpuParamsCore> mParamPickingAlphaFragParams;
  84. GpuParamMat4Core mParamPickingWVP;
  85. GpuParamMat4Core mParamPickingAlphaWVP;
  86. GpuParamColorCore mParamPickingColor;
  87. GpuParamColorCore mParamPickingAlphaColor;
  88. GpuParamTextureCore mParamPickingAlphaTexture;
  89. };
  90. public:
  91. /**
  92. * @brief Initializes the manager. Must be called right after construction.
  93. */
  94. void initialize();
  95. /**
  96. * @brief Destroys the manager. Must be called right before destruction.
  97. */
  98. void destroy();
  99. /**
  100. * @brief Sets up the viewport, materials and their parameters as needed for picking. Also renders
  101. * all the provided renderable objects. Must be followed by ::corePickingEnd. You may call other methods
  102. * after this one, but you must ensure they render proper unique pickable colors that can be resolved
  103. * to SceneObject%s later.
  104. *
  105. * @param target Render target to render to.
  106. * @param viewportArea Normalized area of the render target to render in.
  107. * @param renderables A set of pickable Renderable objects to render.
  108. * @param position Position of the pointer where to pick objects, in pixels relative to viewport.
  109. * @param area Width/height of the area to pick objects, in pixels.
  110. */
  111. void corePickingBegin(const SPtr<RenderTargetCore>& target, const Rect2& viewportArea, const ScenePicking::RenderableSet& renderables,
  112. const Vector2I& position, const Vector2I& area);
  113. /**
  114. * @brief Ends picking operation started by ::corePickingBegin. Render target is resolved and objects in the picked area
  115. * are returned.
  116. *
  117. * @param target Render target we're rendering to.
  118. * @param viewportArea Normalized area of the render target we're rendering in.
  119. * @param position Position of the pointer where to pick objects, in pixels relative to viewport.
  120. * @param area Width/height of the area to pick objects, in pixels.
  121. * @param asyncOp Async operation handle that when complete will contain the results of the picking
  122. * operation in the form of Vector<SelectedObject>.
  123. */
  124. void corePickingEnd(const SPtr<RenderTargetCore>& target, const Rect2& viewportArea, const Vector2I& position,
  125. const Vector2I& area, AsyncOp& asyncOp);
  126. private:
  127. friend class ScenePicking;
  128. static const float ALPHA_CUTOFF;
  129. MaterialData mMaterialData[3];
  130. };
  131. }