CustomGeometry.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #pragma once
  23. #include "../Graphics/Drawable.h"
  24. namespace Urho3D
  25. {
  26. /// Custom geometry vertex.
  27. struct CustomGeometryVertex
  28. {
  29. /// Position.
  30. Vector3 position_;
  31. /// Normal.
  32. Vector3 normal_;
  33. /// Color.
  34. unsigned color_;
  35. /// Texture coordinates.
  36. Vector2 texCoord_;
  37. /// Tangent.
  38. Vector4 tangent_;
  39. };
  40. class VertexBuffer;
  41. /// Custom geometry component.
  42. class URHO3D_API CustomGeometry : public Drawable
  43. {
  44. URHO3D_OBJECT(CustomGeometry, Drawable);
  45. public:
  46. /// Construct.
  47. explicit CustomGeometry(Context* context);
  48. /// Destruct.
  49. ~CustomGeometry() override;
  50. /// Register object factory. Drawable must be registered first.
  51. static void RegisterObject(Context* context);
  52. /// Process octree raycast. May be called from a worker thread.
  53. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  54. /// Return the geometry for a specific LOD level.
  55. Geometry* GetLodGeometry(unsigned batchIndex, unsigned level) override;
  56. /// Return number of occlusion geometry triangles.
  57. unsigned GetNumOccluderTriangles() override;
  58. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  59. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  60. // TODO: Extract this code from CustomGeometry
  61. // {
  62. /// Geometry Shape Helper Functions (normal defaults to (0,1,0)
  63. /// Shape Creation Functions, can be used as input into protrude shape
  64. static Vector<Vector3> GetCircleShape(float radius = 1, size_t iterations = 100, float startTheta = 0, float endTheta = 2 * M_PI);
  65. static Vector<Vector3> GetSquareShape(float size);
  66. /// Make the custom geometry into a circle, change start and stop to make a segment instead
  67. /// Set clear=false and geomNum for making multiple circle segments
  68. void MakeCircle(float radius = 1, size_t iterations = 300, float startTheta = 0, float endTheta = 2 * M_PI,
  69. bool clear = true, int geomNum = 0);
  70. /// Draws a shape generated by connecting the points in the input vector (the end point will connect to the start)
  71. void MakeShape(const Vector<Urho3D::Vector3>& pointList, bool connectTail = true);
  72. /// Makes this custom geometry into a square shape
  73. void MakeSquare(float size);
  74. /// Produces a circle graph given a vector of paired (weights and materials)
  75. void MakeCircleGraph(const Vector<Pair<float, Urho3D::SharedPtr<Urho3D::Material> > >& parts,
  76. int radius = 1, int iterations = 300);
  77. void MakeSphere(float radius = 1, size_t iterations = 200);
  78. /// Protrode a shape along a line (note: the input vector must be a complete shape with the tail connecting to head)
  79. /// This function makes this object into the newly generated 3D mesh (works best if the line (point list) is also complete)
  80. void ProtrudeShape(const Vector<Vector3> &mShapeList, const Vector<Vector3> &mPointList, bool connectTail = false);
  81. /// Helper Function Creating 3D Meshes (connectTail = true), connect the endLinePoint to the start)
  82. void CreateQuadsFromBuffer(const Vector<Vector3>& pointList, size_t zIterations, size_t thetaIterations, bool connectTail = false);
  83. /// Fills a point List shape with triangles
  84. void FillShape(const Vector<Vector3>& shapeList, bool connectTail = true, bool clear = true, int geomNum = 0);
  85. /// End Shape Generation Helper Functions
  86. // }
  87. /// Clear all geometries.
  88. void Clear();
  89. /// Set number of geometries.
  90. void SetNumGeometries(unsigned num);
  91. /// Set vertex buffer dynamic mode. A dynamic buffer should be faster to update frequently. Effective at the next Commit() call.
  92. void SetDynamic(bool enable);
  93. /// Begin defining a geometry. Clears existing vertices in that index.
  94. void BeginGeometry(unsigned index, PrimitiveType type);
  95. /// Define a vertex position. This begins a new vertex.
  96. void DefineVertex(const Vector3& position);
  97. /// Define a vertex normal.
  98. void DefineNormal(const Vector3& normal);
  99. /// Define a vertex color.
  100. void DefineColor(const Color& color);
  101. /// Define a vertex UV coordinate.
  102. void DefineTexCoord(const Vector2& texCoord);
  103. /// Define a vertex tangent.
  104. void DefineTangent(const Vector4& tangent);
  105. /// 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().
  106. void DefineGeometry
  107. (unsigned index, PrimitiveType type, unsigned numVertices, bool hasNormals, bool hasColors, bool hasTexCoords,
  108. bool hasTangents);
  109. /// Update vertex buffer and calculate the bounding box. Call after finishing defining geometry.
  110. void Commit();
  111. /// Set material on all geometries.
  112. void SetMaterial(Material* material);
  113. /// Set material on one geometry. Return true if successful.
  114. bool SetMaterial(unsigned index, Material* material);
  115. /// Return number of geometries.
  116. unsigned GetNumGeometries() const { return geometries_.Size(); }
  117. /// Return number of vertices in a geometry.
  118. unsigned GetNumVertices(unsigned index) const;
  119. /// Return whether vertex buffer dynamic mode is enabled.
  120. bool IsDynamic() const { return dynamic_; }
  121. /// Return material by geometry index.
  122. Material* GetMaterial(unsigned index = 0) const;
  123. /// Return all vertices. These can be edited; calling Commit() updates the vertex buffer.
  124. Vector<PODVector<CustomGeometryVertex> >& GetVertices() { return vertices_; }
  125. /// 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.
  126. CustomGeometryVertex* GetVertex(unsigned geometryIndex, unsigned vertexNum);
  127. /// Set geometry data attribute.
  128. void SetGeometryDataAttr(const PODVector<unsigned char>& value);
  129. /// Set materials attribute.
  130. void SetMaterialsAttr(const ResourceRefList& value);
  131. /// Return geometry data attribute.
  132. PODVector<unsigned char> GetGeometryDataAttr() const;
  133. /// Return materials attribute.
  134. const ResourceRefList& GetMaterialsAttr() const;
  135. protected:
  136. /// Recalculate the world-space bounding box.
  137. void OnWorldBoundingBoxUpdate() override;
  138. private:
  139. /// Primitive type per geometry.
  140. PODVector<PrimitiveType> primitiveTypes_;
  141. /// Source vertices per geometry.
  142. Vector<PODVector<CustomGeometryVertex> > vertices_;
  143. /// All geometries.
  144. Vector<SharedPtr<Geometry> > geometries_;
  145. /// Vertex buffer.
  146. SharedPtr<VertexBuffer> vertexBuffer_;
  147. /// Element mask used so far.
  148. VertexMaskFlags elementMask_;
  149. /// Current geometry being updated.
  150. unsigned geometryIndex_;
  151. /// Material list attribute.
  152. mutable ResourceRefList materialsAttr_;
  153. /// Vertex buffer dynamic flag.
  154. bool dynamic_;
  155. };
  156. }