Drawable.h 14 KB

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