immediate_mesh.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #pragma once
  31. #include "core/templates/local_vector.h"
  32. #include "scene/resources/mesh.h"
  33. class ImmediateMesh : public Mesh {
  34. GDCLASS(ImmediateMesh, Mesh)
  35. RID mesh;
  36. bool uses_colors = false;
  37. bool uses_normals = false;
  38. bool uses_tangents = false;
  39. bool uses_uvs = false;
  40. bool uses_uv2s = false;
  41. Color current_color;
  42. Vector3 current_normal;
  43. Plane current_tangent;
  44. Vector2 current_uv;
  45. Vector2 current_uv2;
  46. LocalVector<Color> colors;
  47. LocalVector<Vector3> normals;
  48. LocalVector<Plane> tangents;
  49. LocalVector<Vector2> uvs;
  50. LocalVector<Vector2> uv2s;
  51. LocalVector<Vector3> vertices;
  52. struct Surface {
  53. PrimitiveType primitive;
  54. Ref<Material> material;
  55. bool vertex_2d = false;
  56. int array_len = 0;
  57. uint64_t format = 0;
  58. AABB aabb;
  59. };
  60. LocalVector<Surface> surfaces;
  61. bool surface_active = false;
  62. Surface active_surface_data;
  63. Vector<uint8_t> surface_vertex_create_cache;
  64. Vector<uint8_t> surface_attribute_create_cache;
  65. const Vector3 SMALL_VEC3 = Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON);
  66. protected:
  67. static void _bind_methods();
  68. public:
  69. void surface_begin(PrimitiveType p_primitive, const Ref<Material> &p_material = Ref<Material>());
  70. void surface_set_color(const Color &p_color);
  71. void surface_set_normal(const Vector3 &p_normal);
  72. void surface_set_tangent(const Plane &p_tangent);
  73. void surface_set_uv(const Vector2 &p_uv);
  74. void surface_set_uv2(const Vector2 &p_uv2);
  75. void surface_add_vertex(const Vector3 &p_vertex);
  76. void surface_add_vertex_2d(const Vector2 &p_vertex);
  77. void surface_end();
  78. void clear_surfaces();
  79. virtual int get_surface_count() const override;
  80. virtual int surface_get_array_len(int p_idx) const override;
  81. virtual int surface_get_array_index_len(int p_idx) const override;
  82. virtual Array surface_get_arrays(int p_surface) const override;
  83. virtual TypedArray<Array> surface_get_blend_shape_arrays(int p_surface) const override;
  84. virtual Dictionary surface_get_lods(int p_surface) const override;
  85. virtual BitField<ArrayFormat> surface_get_format(int p_idx) const override;
  86. virtual PrimitiveType surface_get_primitive_type(int p_idx) const override;
  87. virtual void surface_set_material(int p_idx, const Ref<Material> &p_material) override;
  88. virtual Ref<Material> surface_get_material(int p_idx) const override;
  89. virtual int get_blend_shape_count() const override;
  90. virtual StringName get_blend_shape_name(int p_index) const override;
  91. virtual void set_blend_shape_name(int p_index, const StringName &p_name) override;
  92. virtual AABB get_aabb() const override;
  93. virtual RID get_rid() const override;
  94. ImmediateMesh();
  95. ~ImmediateMesh();
  96. };