BsScenePicking.h 6.3 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 "BsModule.h"
  6. #include "BsMatrix4.h"
  7. #include "BsGpuParam.h"
  8. namespace BansheeEngine
  9. {
  10. /** @addtogroup Scene-Editor
  11. * @{
  12. */
  13. /** Contains the data of a scene picking action. */
  14. struct SnapData
  15. {
  16. Vector3 normal;
  17. Vector3 pickPosition;
  18. };
  19. /** Contains the results of a scene picking action. */
  20. struct PickResults
  21. {
  22. Vector<UINT32> objects;
  23. Vector3 normal;
  24. float depth;
  25. };
  26. class ScenePickingCore;
  27. /** Handles picking of scene objects with a pointer in scene view. */
  28. class BS_ED_EXPORT ScenePicking : public Module<ScenePicking>
  29. {
  30. /** Contains information about a single pickable item (mesh). */
  31. struct RenderablePickData
  32. {
  33. SPtr<MeshCore> mesh;
  34. UINT32 index;
  35. Matrix4 wvpTransform;
  36. bool alpha;
  37. CullingMode cullMode;
  38. HTexture mainTexture;
  39. };
  40. public:
  41. ScenePicking();
  42. ~ScenePicking();
  43. /**
  44. * Attempts to find a single nearest scene object under the provided position and area.
  45. *
  46. * @param[in] cam Camera to perform the picking from.
  47. * @param[in] position Pointer position relative to the camera viewport, in pixels.
  48. * @param[in] area Width/height of the checked area in pixels. Use (1, 1) if you want the exact
  49. * position under the pointer.
  50. * @param[in] ignoreRenderables A list of objects that should be ignored during scene picking.
  51. * @param[out] data Picking data regarding position and normal.
  52. * @return Nearest SceneObject under the provided area, or an empty handle if no object is
  53. * found.
  54. */
  55. HSceneObject pickClosestObject(const SPtr<Camera>& cam, const Vector2I& position, const Vector2I& area,
  56. Vector<HSceneObject>& ignoreRenderables, SnapData* data = nullptr);
  57. /**
  58. * Attempts to find all scene objects under the provided position and area. This does not mean objects occluded by
  59. * other objects.
  60. *
  61. * @param[in] cam Camera to perform the picking from.
  62. * @param[in] position Pointer position relative to the camera viewport, in pixels.
  63. * @param[in] area Width/height of the checked area in pixels. Use (1, 1) if you want the exact
  64. * position under the pointer.
  65. * @param[in] ignoreRenderables A list of objects that should be ignored during scene picking.
  66. * @param[out] data Picking data regarding position and normal.
  67. * @return A list of SceneObject%s under the provided area.
  68. */
  69. Vector<HSceneObject> pickObjects(const SPtr<Camera>& cam, const Vector2I& position, const Vector2I& area,
  70. Vector<HSceneObject>& ignoreRenderables, SnapData* data = nullptr);
  71. private:
  72. friend class ScenePickingCore;
  73. typedef Set<RenderablePickData, std::function<bool(const RenderablePickData&, const RenderablePickData&)>> RenderableSet;
  74. /** Encodes a pickable object identifier to a unique color. */
  75. static Color encodeIndex(UINT32 index);
  76. /** Decodes a color into a unique object identifier. Color should have initially been encoded with encodeIndex(). */
  77. static UINT32 decodeIndex(Color color);
  78. ScenePickingCore* mCore;
  79. };
  80. /** @} */
  81. /** @addtogroup Scene-Editor-Internal
  82. * @{
  83. */
  84. /** Core thread version of the ScenePicking manager. Handles actual rendering. */
  85. class ScenePickingCore
  86. {
  87. /** A list of materials and their parameters to be used for rendering of pickable objects. */
  88. struct MaterialData
  89. {
  90. SPtr<MaterialCore> mMatPickingCore;
  91. SPtr<MaterialCore> mMatPickingAlphaCore;
  92. SPtr<GpuParamsSetCore> mPickingParams;
  93. SPtr<GpuParamsSetCore> mPickingAlphaParams;
  94. GpuParamMat4Core mParamPickingWVP;
  95. GpuParamMat4Core mParamPickingAlphaWVP;
  96. GpuParamColorCore mParamPickingColor;
  97. GpuParamColorCore mParamPickingAlphaColor;
  98. GpuParamTextureCore mParamPickingAlphaTexture;
  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<RenderTargetCore>& target, const Rect2& viewportArea,
  117. const 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<RenderTargetCore>& target, const Rect2& viewportArea, const Vector2I& position,
  131. const Vector2I& area, bool gatherSnapData, AsyncOp& asyncOp);
  132. private:
  133. friend class ScenePicking;
  134. static const float ALPHA_CUTOFF;
  135. MaterialData mMaterialData[3];
  136. SPtr<MultiRenderTextureCore> mPickingTexture;
  137. };
  138. /** @} */
  139. }