BsScenePicking.h 5.4 KB

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