Drawable.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../GraphicsAPI/GraphicsDefs.h"
  6. #include "../Math/BoundingBox.h"
  7. #include "../Scene/Component.h"
  8. namespace Urho3D
  9. {
  10. enum class DrawableTypes : u8
  11. {
  12. Undefined = 0,
  13. Geometry = 1 << 0,
  14. Light = 1 << 1,
  15. Zone = 1 << 2,
  16. Geometry2D = 1 << 3,
  17. Any = 0xFF
  18. };
  19. URHO3D_FLAGS(DrawableTypes);
  20. inline constexpr mask32 DEFAULT_VIEWMASK = M_U32_MASK_ALL_BITS;
  21. inline constexpr mask32 DEFAULT_LIGHTMASK = M_U32_MASK_ALL_BITS;
  22. inline constexpr mask32 DEFAULT_SHADOWMASK = M_U32_MASK_ALL_BITS;
  23. inline constexpr mask32 DEFAULT_ZONEMASK = M_U32_MASK_ALL_BITS;
  24. inline constexpr i32 MAX_VERTEX_LIGHTS = 4;
  25. inline constexpr float ANIMATION_LOD_BASESCALE = 2500.0f;
  26. class Camera;
  27. class File;
  28. class Geometry;
  29. class Light;
  30. class Material;
  31. class OcclusionBuffer;
  32. class Octant;
  33. class RayOctreeQuery;
  34. class Zone;
  35. struct RayQueryResult;
  36. struct WorkItem;
  37. /// Geometry update type.
  38. enum UpdateGeometryType
  39. {
  40. UPDATE_NONE = 0,
  41. UPDATE_MAIN_THREAD,
  42. UPDATE_WORKER_THREAD
  43. };
  44. /// Rendering frame update parameters.
  45. struct FrameInfo
  46. {
  47. /// Frame number.
  48. i32 frameNumber_;
  49. /// Time elapsed since last frame.
  50. float timeStep_;
  51. /// Viewport size.
  52. IntVector2 viewSize_;
  53. /// Camera being used.
  54. Camera* camera_;
  55. };
  56. /// Source data for a 3D geometry draw call.
  57. struct URHO3D_API SourceBatch
  58. {
  59. /// Construct with defaults.
  60. SourceBatch();
  61. /// Copy-construct.
  62. SourceBatch(const SourceBatch& batch);
  63. /// Destruct.
  64. ~SourceBatch();
  65. /// Assignment operator.
  66. SourceBatch& operator =(const SourceBatch& rhs);
  67. /// Distance from camera.
  68. float distance_{};
  69. /// Geometry.
  70. Geometry* geometry_{};
  71. /// Material.
  72. SharedPtr<Material> material_;
  73. /// World transform(s). For a skinned model, these are the bone transforms.
  74. const Matrix3x4* worldTransform_{&Matrix3x4::IDENTITY};
  75. /// Number of world transforms.
  76. i32 numWorldTransforms_{1};
  77. /// Per-instance data. If not null, must contain enough data to fill instancing buffer.
  78. void* instancingData_{};
  79. /// %Geometry type.
  80. GeometryType geometryType_{GEOM_STATIC};
  81. };
  82. /// Base class for visible components.
  83. class URHO3D_API Drawable : public Component
  84. {
  85. URHO3D_OBJECT(Drawable, Component);
  86. friend class Octant;
  87. friend class Octree;
  88. friend void UpdateDrawablesWork(const WorkItem* item, i32 threadIndex);
  89. public:
  90. /// Construct.
  91. Drawable(Context* context, DrawableTypes drawableType);
  92. /// Destruct.
  93. ~Drawable() override;
  94. /// Register object attributes. Drawable must be registered first.
  95. /// @nobind
  96. static void RegisterObject(Context* context);
  97. /// Handle enabled/disabled state change.
  98. void OnSetEnabled() override;
  99. /// Process octree raycast. May be called from a worker thread.
  100. virtual void ProcessRayQuery(const RayOctreeQuery& query, Vector<RayQueryResult>& results);
  101. /// Update before octree reinsertion. Is called from a worker thread.
  102. virtual void Update(const FrameInfo& frame) { }
  103. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  104. virtual void UpdateBatches(const FrameInfo& frame);
  105. /// Prepare geometry for rendering.
  106. virtual void UpdateGeometry(const FrameInfo& frame) { }
  107. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  108. virtual UpdateGeometryType GetUpdateGeometryType() { return UPDATE_NONE; }
  109. /// Return the geometry for a specific LOD level.
  110. virtual Geometry* GetLodGeometry(i32 batchIndex, i32 level);
  111. /// Return number of occlusion geometry triangles.
  112. virtual i32 GetNumOccluderTriangles() { return 0; }
  113. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  114. virtual bool DrawOcclusion(OcclusionBuffer* buffer);
  115. /// Visualize the component as debug geometry.
  116. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  117. /// Set draw distance.
  118. /// @property
  119. void SetDrawDistance(float distance);
  120. /// Set shadow draw distance.
  121. /// @property
  122. void SetShadowDistance(float distance);
  123. /// Set LOD bias.
  124. /// @property
  125. void SetLodBias(float bias);
  126. /// Set view mask. Is and'ed with camera's view mask to see if the object should be rendered.
  127. /// @property
  128. void SetViewMask(mask32 mask);
  129. /// Set light mask. Is and'ed with light's and zone's light mask to see if the object should be lit.
  130. /// @property
  131. void SetLightMask(mask32 mask);
  132. /// Set shadow mask. Is and'ed with light's light mask and zone's shadow mask to see if the object should be rendered to a shadow map.
  133. /// @property
  134. void SetShadowMask(mask32 mask);
  135. /// Set zone mask. Is and'ed with zone's zone mask to see if the object should belong to the zone.
  136. /// @property
  137. void SetZoneMask(mask32 mask);
  138. /// Set maximum number of per-pixel lights. Default 0 is unlimited.
  139. /// @property
  140. void SetMaxLights(i32 num);
  141. /// Set shadowcaster flag.
  142. /// @property
  143. void SetCastShadows(bool enable);
  144. /// Set occlusion flag.
  145. /// @property
  146. void SetOccluder(bool enable);
  147. /// Set occludee flag.
  148. /// @property
  149. void SetOccludee(bool enable);
  150. /// Mark for update and octree reinsertion. Update is automatically queued when the drawable's scene node moves or changes scale.
  151. void MarkForUpdate();
  152. /// Return local space bounding box. May not be applicable or properly updated on all drawables.
  153. /// @property
  154. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  155. /// Return world-space bounding box.
  156. /// @property
  157. const BoundingBox& GetWorldBoundingBox();
  158. /// Return drawable type.
  159. DrawableTypes GetDrawableType() const { return drawableType_; }
  160. /// Return draw distance.
  161. /// @property
  162. float GetDrawDistance() const { return drawDistance_; }
  163. /// Return shadow draw distance.
  164. /// @property
  165. float GetShadowDistance() const { return shadowDistance_; }
  166. /// Return LOD bias.
  167. /// @property
  168. float GetLodBias() const { return lodBias_; }
  169. /// Return view mask.
  170. /// @property
  171. mask32 GetViewMask() const { return viewMask_; }
  172. /// Return light mask.
  173. /// @property
  174. mask32 GetLightMask() const { return lightMask_; }
  175. /// Return shadow mask.
  176. /// @property
  177. mask32 GetShadowMask() const { return shadowMask_; }
  178. /// Return zone mask.
  179. /// @property
  180. mask32 GetZoneMask() const { return zoneMask_; }
  181. /// Return maximum number of per-pixel lights.
  182. /// @property
  183. i32 GetMaxLights() const { return maxLights_; }
  184. /// Return shadowcaster flag.
  185. /// @property
  186. bool GetCastShadows() const { return castShadows_; }
  187. /// Return occluder flag.
  188. /// @property
  189. bool IsOccluder() const { return occluder_; }
  190. /// Return occludee flag.
  191. /// @property
  192. bool IsOccludee() const { return occludee_; }
  193. /// Return whether is in view this frame from any viewport camera. Excludes shadow map cameras.
  194. /// @property
  195. bool IsInView() const;
  196. /// Return whether is in view of a specific camera this frame. Pass in a null camera to allow any camera, including shadow map cameras.
  197. bool IsInView(Camera* camera) const;
  198. /// Return draw call source data.
  199. const Vector<SourceBatch>& GetBatches() const { return batches_; }
  200. /// Set new zone. Zone assignment may optionally be temporary, meaning it needs to be re-evaluated on the next frame.
  201. void SetZone(Zone* zone, bool temporary = false);
  202. /// Set sorting value.
  203. void SetSortValue(float value);
  204. /// Set view-space depth bounds.
  205. void SetMinMaxZ(float minZ, float maxZ)
  206. {
  207. minZ_ = minZ;
  208. maxZ_ = maxZ;
  209. }
  210. /// Mark in view. Also clear the light list.
  211. void MarkInView(const FrameInfo& frame);
  212. /// Mark in view without specifying a camera. Used for shadow casters.
  213. void MarkInView(i32 frameNumber);
  214. /// Sort and limit per-pixel lights to maximum allowed. Convert extra lights into vertex lights.
  215. void LimitLights();
  216. /// Sort and limit per-vertex lights to maximum allowed.
  217. void LimitVertexLights(bool removeConvertedLights);
  218. /// Set base pass flag for a batch.
  219. void SetBasePass(i32 batchIndex)
  220. {
  221. assert(batchIndex >= 0 && batchIndex < 32);
  222. basePassFlags_ |= (1u << batchIndex);
  223. }
  224. /// Return octree octant.
  225. Octant* GetOctant() const { return octant_; }
  226. /// Return current zone.
  227. /// @property
  228. Zone* GetZone() const { return zone_; }
  229. /// Return whether current zone is inconclusive or dirty due to the drawable moving.
  230. bool IsZoneDirty() const { return zoneDirty_; }
  231. /// Return distance from camera.
  232. float GetDistance() const { return distance_; }
  233. /// Return LOD scaled distance from camera.
  234. float GetLodDistance() const { return lodDistance_; }
  235. /// Return sorting value.
  236. float GetSortValue() const { return sortValue_; }
  237. /// Return whether is in view on the current frame. Called by View.
  238. bool IsInView(const FrameInfo& frame, bool anyCamera = false) const;
  239. /// Return whether has a base pass.
  240. bool HasBasePass(i32 batchIndex) const
  241. {
  242. assert(batchIndex >= 0 && batchIndex < 32);
  243. return (basePassFlags_ & (1u << batchIndex)) != 0;
  244. }
  245. /// Return per-pixel lights.
  246. const Vector<Light*>& GetLights() const { return lights_; }
  247. /// Return per-vertex lights.
  248. const Vector<Light*>& GetVertexLights() const { return vertexLights_; }
  249. /// Return the first added per-pixel light.
  250. Light* GetFirstLight() const { return firstLight_; }
  251. /// Return the minimum view-space depth.
  252. float GetMinZ() const { return minZ_; }
  253. /// Return the maximum view-space depth.
  254. float GetMaxZ() const { return maxZ_; }
  255. /// Add a per-pixel light affecting the object this frame.
  256. void AddLight(Light* light)
  257. {
  258. if (!firstLight_)
  259. firstLight_ = light;
  260. // Need to store into the light list only if the per-pixel lights are being limited
  261. // Otherwise recording the first light is enough
  262. if (maxLights_)
  263. lights_.Push(light);
  264. }
  265. /// Add a per-vertex light affecting the object this frame.
  266. void AddVertexLight(Light* light)
  267. {
  268. vertexLights_.Push(light);
  269. }
  270. protected:
  271. /// Handle node being assigned.
  272. void OnNodeSet(Node* node) override;
  273. /// Handle scene being assigned.
  274. void OnSceneSet(Scene* scene) override;
  275. /// Handle node transform being dirtied.
  276. void OnMarkedDirty(Node* node) override;
  277. /// Recalculate the world-space bounding box.
  278. virtual void OnWorldBoundingBoxUpdate() = 0;
  279. /// Handle removal from octree.
  280. virtual void OnRemoveFromOctree() { }
  281. /// Add to octree.
  282. void AddToOctree();
  283. /// Remove from octree.
  284. void RemoveFromOctree();
  285. /// Move into another octree octant.
  286. void SetOctant(Octant* octant) { octant_ = octant; }
  287. /// World-space bounding box.
  288. BoundingBox worldBoundingBox_;
  289. /// Local-space bounding box.
  290. BoundingBox boundingBox_;
  291. /// Draw call source data.
  292. Vector<SourceBatch> batches_;
  293. /// Drawable type
  294. DrawableTypes drawableType_;
  295. /// Bounding box dirty flag.
  296. bool worldBoundingBoxDirty_;
  297. /// Shadowcaster flag.
  298. bool castShadows_;
  299. /// Occluder flag.
  300. bool occluder_;
  301. /// Occludee flag.
  302. bool occludee_;
  303. /// Octree update queued flag.
  304. bool updateQueued_;
  305. /// Zone inconclusive or dirtied flag.
  306. bool zoneDirty_;
  307. /// Octree octant.
  308. Octant* octant_;
  309. /// Current zone.
  310. Zone* zone_;
  311. /// View mask.
  312. mask32 viewMask_;
  313. /// Light mask.
  314. mask32 lightMask_;
  315. /// Shadow mask.
  316. mask32 shadowMask_;
  317. /// Zone mask.
  318. mask32 zoneMask_;
  319. /// Last visible frame number.
  320. i32 viewFrameNumber_;
  321. /// Current distance to camera.
  322. float distance_;
  323. /// LOD scaled distance.
  324. float lodDistance_;
  325. /// Draw distance.
  326. float drawDistance_;
  327. /// Shadow distance.
  328. float shadowDistance_;
  329. /// Current sort value.
  330. float sortValue_;
  331. /// Current minimum view space depth.
  332. float minZ_;
  333. /// Current maximum view space depth.
  334. float maxZ_;
  335. /// LOD bias.
  336. float lodBias_;
  337. /// Base pass flags, bit per batch.
  338. flagset32 basePassFlags_;
  339. /// Maximum per-pixel lights.
  340. i32 maxLights_;
  341. /// List of cameras from which is seen on the current frame.
  342. Vector<Camera*> viewCameras_;
  343. /// First per-pixel light added this frame.
  344. Light* firstLight_;
  345. /// Per-pixel lights affecting this drawable.
  346. Vector<Light*> lights_;
  347. /// Per-vertex lights affecting this drawable.
  348. Vector<Light*> vertexLights_;
  349. };
  350. inline bool CompareDrawables(Drawable* lhs, Drawable* rhs)
  351. {
  352. return lhs->GetSortValue() < rhs->GetSortValue();
  353. }
  354. URHO3D_API bool WriteDrawablesToOBJ(const Vector<Drawable*>& drawables, File* outputFile, bool asZUp, bool asRightHanded, bool writeLightmapUV = false);
  355. }