BsScenePicking.h 5.4 KB

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