immediate_mesh.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**************************************************************************/
  2. /* immediate_mesh.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef IMMEDIATE_MESH_H
  31. #define IMMEDIATE_MESH_H
  32. #include "core/templates/local_vector.h"
  33. #include "scene/resources/mesh.h"
  34. class ImmediateMesh : public Mesh {
  35. GDCLASS(ImmediateMesh, Mesh)
  36. RID mesh;
  37. bool uses_colors = false;
  38. bool uses_normals = false;
  39. bool uses_tangents = false;
  40. bool uses_uvs = false;
  41. bool uses_uv2s = false;
  42. Color current_color;
  43. Vector3 current_normal;
  44. Plane current_tangent;
  45. Vector2 current_uv;
  46. Vector2 current_uv2;
  47. LocalVector<Color> colors;
  48. LocalVector<Vector3> normals;
  49. LocalVector<Plane> tangents;
  50. LocalVector<Vector2> uvs;
  51. LocalVector<Vector2> uv2s;
  52. LocalVector<Vector3> vertices;
  53. struct Surface {
  54. PrimitiveType primitive;
  55. Ref<Material> material;
  56. bool vertex_2d = false;
  57. int array_len = 0;
  58. uint64_t format = 0;
  59. AABB aabb;
  60. };
  61. LocalVector<Surface> surfaces;
  62. bool surface_active = false;
  63. Surface active_surface_data;
  64. Vector<uint8_t> surface_vertex_create_cache;
  65. Vector<uint8_t> surface_attribute_create_cache;
  66. const Vector3 SMALL_VEC3 = Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON);
  67. protected:
  68. static void _bind_methods();
  69. public:
  70. void surface_begin(PrimitiveType p_primitive, const Ref<Material> &p_material = Ref<Material>());
  71. void surface_set_color(const Color &p_color);
  72. void surface_set_normal(const Vector3 &p_normal);
  73. void surface_set_tangent(const Plane &p_tangent);
  74. void surface_set_uv(const Vector2 &p_uv);
  75. void surface_set_uv2(const Vector2 &p_uv2);
  76. void surface_add_vertex(const Vector3 &p_vertex);
  77. void surface_add_vertex_2d(const Vector2 &p_vertex);
  78. void surface_end();
  79. void clear_surfaces();
  80. virtual int get_surface_count() const override;
  81. virtual int surface_get_array_len(int p_idx) const override;
  82. virtual int surface_get_array_index_len(int p_idx) const override;
  83. virtual Array surface_get_arrays(int p_surface) const override;
  84. virtual TypedArray<Array> surface_get_blend_shape_arrays(int p_surface) const override;
  85. virtual Dictionary surface_get_lods(int p_surface) const override;
  86. virtual BitField<ArrayFormat> surface_get_format(int p_idx) const override;
  87. virtual PrimitiveType surface_get_primitive_type(int p_idx) const override;
  88. virtual void surface_set_material(int p_idx, const Ref<Material> &p_material) override;
  89. virtual Ref<Material> surface_get_material(int p_idx) const override;
  90. virtual int get_blend_shape_count() const override;
  91. virtual StringName get_blend_shape_name(int p_index) const override;
  92. virtual void set_blend_shape_name(int p_index, const StringName &p_name) override;
  93. virtual AABB get_aabb() const override;
  94. virtual RID get_rid() const override;
  95. ImmediateMesh();
  96. ~ImmediateMesh();
  97. };
  98. #endif // IMMEDIATE_MESH_H