BsScenePicking.h 6.3 KB

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