CustomGeometry.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Graphics/Drawable.h"
  5. namespace Urho3D
  6. {
  7. /// Custom geometry vertex.
  8. /// @nocount
  9. struct CustomGeometryVertex
  10. {
  11. /// Position.
  12. Vector3 position_;
  13. /// Normal.
  14. Vector3 normal_;
  15. /// Color.
  16. color32 color_;
  17. /// Texture coordinates.
  18. Vector2 texCoord_;
  19. /// Tangent.
  20. Vector4 tangent_;
  21. };
  22. class VertexBuffer;
  23. /// Custom geometry component.
  24. class URHO3D_API CustomGeometry : public Drawable
  25. {
  26. URHO3D_OBJECT(CustomGeometry, Drawable);
  27. public:
  28. /// Construct.
  29. explicit CustomGeometry(Context* context);
  30. /// Destruct.
  31. ~CustomGeometry() override;
  32. /// Register object factory. Drawable must be registered first.
  33. /// @nobind
  34. static void RegisterObject(Context* context);
  35. /// Process octree raycast. May be called from a worker thread.
  36. void ProcessRayQuery(const RayOctreeQuery& query, Vector<RayQueryResult>& results) override;
  37. /// Return the geometry for a specific LOD level.
  38. Geometry* GetLodGeometry(i32 batchIndex, i32 level) override;
  39. /// Return number of occlusion geometry triangles.
  40. i32 GetNumOccluderTriangles() override;
  41. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  42. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  43. /// Clear all geometries.
  44. void Clear();
  45. /// Set number of geometries.
  46. /// @property
  47. void SetNumGeometries(unsigned num);
  48. /// Set vertex buffer dynamic mode. A dynamic buffer should be faster to update frequently. Effective at the next Commit() call.
  49. /// @property
  50. void SetDynamic(bool enable);
  51. /// Begin defining a geometry. Clears existing vertices in that index.
  52. void BeginGeometry(unsigned index, PrimitiveType type);
  53. /// Define a vertex position. This begins a new vertex.
  54. void DefineVertex(const Vector3& position);
  55. /// Define a vertex normal.
  56. void DefineNormal(const Vector3& normal);
  57. /// Define a vertex color.
  58. void DefineColor(const Color& color);
  59. /// Define a vertex UV coordinate.
  60. void DefineTexCoord(const Vector2& texCoord);
  61. /// Define a vertex tangent.
  62. void DefineTangent(const Vector4& tangent);
  63. /// Set the primitive type, number of vertices and elements in a geometry, after which the vertices can be edited with GetVertex(). An alternative to BeginGeometry() / DefineVertex().
  64. void DefineGeometry
  65. (unsigned index, PrimitiveType type, unsigned numVertices, bool hasNormals, bool hasColors, bool hasTexCoords,
  66. bool hasTangents);
  67. /// Update vertex buffer and calculate the bounding box. Call after finishing defining geometry.
  68. void Commit();
  69. /// Set material on all geometries.
  70. /// @property
  71. void SetMaterial(Material* material);
  72. /// Set material on one geometry. Return true if successful.
  73. /// @property{set_materials}
  74. bool SetMaterial(unsigned index, Material* material);
  75. /// Return number of geometries.
  76. /// @property
  77. unsigned GetNumGeometries() const { return geometries_.Size(); }
  78. /// Return number of vertices in a geometry.
  79. /// @property
  80. unsigned GetNumVertices(unsigned index) const;
  81. /// Return whether vertex buffer dynamic mode is enabled.
  82. /// @property
  83. bool IsDynamic() const { return dynamic_; }
  84. /// Return material by geometry index.
  85. /// @property{get_materials}
  86. Material* GetMaterial(unsigned index = 0) const;
  87. /// Return all vertices. These can be edited; calling Commit() updates the vertex buffer.
  88. Vector<Vector<CustomGeometryVertex>>& GetVertices() { return vertices_; }
  89. /// Return a vertex in a geometry for editing, or null if out of bounds. After the edits are finished, calling Commit() updates the vertex buffer.
  90. CustomGeometryVertex* GetVertex(unsigned geometryIndex, unsigned vertexNum);
  91. /// Set geometry data attribute.
  92. void SetGeometryDataAttr(const Vector<byte>& value);
  93. /// Set materials attribute.
  94. void SetMaterialsAttr(const ResourceRefList& value);
  95. /// Return geometry data attribute.
  96. Vector<byte> GetGeometryDataAttr() const;
  97. /// Return materials attribute.
  98. const ResourceRefList& GetMaterialsAttr() const;
  99. protected:
  100. /// Recalculate the world-space bounding box.
  101. void OnWorldBoundingBoxUpdate() override;
  102. private:
  103. /// Primitive type per geometry.
  104. Vector<PrimitiveType> primitiveTypes_;
  105. /// Source vertices per geometry.
  106. Vector<Vector<CustomGeometryVertex>> vertices_;
  107. /// All geometries.
  108. Vector<SharedPtr<Geometry>> geometries_;
  109. /// Vertex buffer.
  110. SharedPtr<VertexBuffer> vertexBuffer_;
  111. /// Element mask used so far.
  112. VertexElements elementMask_;
  113. /// Current geometry being updated.
  114. unsigned geometryIndex_;
  115. /// Material list attribute.
  116. mutable ResourceRefList materialsAttr_;
  117. /// Vertex buffer dynamic flag.
  118. bool dynamic_;
  119. };
  120. }