mesh.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************/
  2. /* 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 MESH_H
  31. #define MESH_H
  32. #include "core/math/face3.h"
  33. #include "core/math/triangle_mesh.h"
  34. #include "core/resource.h"
  35. #include "scene/resources/material.h"
  36. #include "scene/resources/shape.h"
  37. #include "servers/visual_server.h"
  38. class Mesh : public Resource {
  39. GDCLASS(Mesh, Resource);
  40. mutable Ref<TriangleMesh> triangle_mesh; //cached
  41. mutable Vector<Vector3> debug_lines;
  42. Size2 lightmap_size_hint;
  43. protected:
  44. static void _bind_methods();
  45. public:
  46. enum {
  47. NO_INDEX_ARRAY = VisualServer::NO_INDEX_ARRAY,
  48. ARRAY_WEIGHTS_SIZE = VisualServer::ARRAY_WEIGHTS_SIZE
  49. };
  50. enum ArrayType {
  51. ARRAY_VERTEX = VisualServer::ARRAY_VERTEX,
  52. ARRAY_NORMAL = VisualServer::ARRAY_NORMAL,
  53. ARRAY_TANGENT = VisualServer::ARRAY_TANGENT,
  54. ARRAY_COLOR = VisualServer::ARRAY_COLOR,
  55. ARRAY_TEX_UV = VisualServer::ARRAY_TEX_UV,
  56. ARRAY_TEX_UV2 = VisualServer::ARRAY_TEX_UV2,
  57. ARRAY_BONES = VisualServer::ARRAY_BONES,
  58. ARRAY_WEIGHTS = VisualServer::ARRAY_WEIGHTS,
  59. ARRAY_INDEX = VisualServer::ARRAY_INDEX,
  60. ARRAY_MAX = VisualServer::ARRAY_MAX
  61. };
  62. enum ArrayFormat {
  63. /* ARRAY FORMAT FLAGS */
  64. ARRAY_FORMAT_VERTEX = 1 << ARRAY_VERTEX, // mandatory
  65. ARRAY_FORMAT_NORMAL = 1 << ARRAY_NORMAL,
  66. ARRAY_FORMAT_TANGENT = 1 << ARRAY_TANGENT,
  67. ARRAY_FORMAT_COLOR = 1 << ARRAY_COLOR,
  68. ARRAY_FORMAT_TEX_UV = 1 << ARRAY_TEX_UV,
  69. ARRAY_FORMAT_TEX_UV2 = 1 << ARRAY_TEX_UV2,
  70. ARRAY_FORMAT_BONES = 1 << ARRAY_BONES,
  71. ARRAY_FORMAT_WEIGHTS = 1 << ARRAY_WEIGHTS,
  72. ARRAY_FORMAT_INDEX = 1 << ARRAY_INDEX,
  73. ARRAY_COMPRESS_BASE = (ARRAY_INDEX + 1),
  74. ARRAY_COMPRESS_VERTEX = 1 << (ARRAY_VERTEX + ARRAY_COMPRESS_BASE), // mandatory
  75. ARRAY_COMPRESS_NORMAL = 1 << (ARRAY_NORMAL + ARRAY_COMPRESS_BASE),
  76. ARRAY_COMPRESS_TANGENT = 1 << (ARRAY_TANGENT + ARRAY_COMPRESS_BASE),
  77. ARRAY_COMPRESS_COLOR = 1 << (ARRAY_COLOR + ARRAY_COMPRESS_BASE),
  78. ARRAY_COMPRESS_TEX_UV = 1 << (ARRAY_TEX_UV + ARRAY_COMPRESS_BASE),
  79. ARRAY_COMPRESS_TEX_UV2 = 1 << (ARRAY_TEX_UV2 + ARRAY_COMPRESS_BASE),
  80. ARRAY_COMPRESS_BONES = 1 << (ARRAY_BONES + ARRAY_COMPRESS_BASE),
  81. ARRAY_COMPRESS_WEIGHTS = 1 << (ARRAY_WEIGHTS + ARRAY_COMPRESS_BASE),
  82. ARRAY_COMPRESS_INDEX = 1 << (ARRAY_INDEX + ARRAY_COMPRESS_BASE),
  83. ARRAY_FLAG_USE_2D_VERTICES = ARRAY_COMPRESS_INDEX << 1,
  84. ARRAY_FLAG_USE_16_BIT_BONES = ARRAY_COMPRESS_INDEX << 2,
  85. ARRAY_FLAG_USE_DYNAMIC_UPDATE = ARRAY_COMPRESS_INDEX << 3,
  86. ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION = ARRAY_COMPRESS_INDEX << 4,
  87. ARRAY_COMPRESS_DEFAULT = ARRAY_COMPRESS_NORMAL | ARRAY_COMPRESS_TANGENT | ARRAY_COMPRESS_COLOR | ARRAY_COMPRESS_TEX_UV | ARRAY_COMPRESS_TEX_UV2 | ARRAY_COMPRESS_WEIGHTS | ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION
  88. };
  89. enum PrimitiveType {
  90. PRIMITIVE_POINTS = VisualServer::PRIMITIVE_POINTS,
  91. PRIMITIVE_LINES = VisualServer::PRIMITIVE_LINES,
  92. PRIMITIVE_LINE_STRIP = VisualServer::PRIMITIVE_LINE_STRIP,
  93. PRIMITIVE_LINE_LOOP = VisualServer::PRIMITIVE_LINE_LOOP,
  94. PRIMITIVE_TRIANGLES = VisualServer::PRIMITIVE_TRIANGLES,
  95. PRIMITIVE_TRIANGLE_STRIP = VisualServer::PRIMITIVE_TRIANGLE_STRIP,
  96. PRIMITIVE_TRIANGLE_FAN = VisualServer::PRIMITIVE_TRIANGLE_FAN,
  97. };
  98. enum BlendShapeMode {
  99. BLEND_SHAPE_MODE_NORMALIZED = VS::BLEND_SHAPE_MODE_NORMALIZED,
  100. BLEND_SHAPE_MODE_RELATIVE = VS::BLEND_SHAPE_MODE_RELATIVE,
  101. };
  102. virtual int get_surface_count() const = 0;
  103. virtual int surface_get_array_len(int p_idx) const = 0;
  104. virtual int surface_get_array_index_len(int p_idx) const = 0;
  105. virtual bool surface_is_softbody_friendly(int p_idx) const;
  106. virtual Array surface_get_arrays(int p_surface) const = 0;
  107. virtual Array surface_get_blend_shape_arrays(int p_surface) const = 0;
  108. virtual uint32_t surface_get_format(int p_idx) const = 0;
  109. virtual PrimitiveType surface_get_primitive_type(int p_idx) const = 0;
  110. virtual void surface_set_material(int p_idx, const Ref<Material> &p_material) = 0;
  111. virtual Ref<Material> surface_get_material(int p_idx) const = 0;
  112. virtual int get_blend_shape_count() const = 0;
  113. virtual StringName get_blend_shape_name(int p_index) const = 0;
  114. virtual void set_blend_shape_name(int p_index, const StringName &p_name) = 0;
  115. PoolVector<Face3> get_faces() const;
  116. Ref<TriangleMesh> generate_triangle_mesh() const;
  117. void generate_debug_mesh_lines(Vector<Vector3> &r_lines);
  118. void generate_debug_mesh_indices(Vector<Vector3> &r_points);
  119. Ref<Shape> create_trimesh_shape() const;
  120. Ref<Shape> create_convex_shape(bool p_clean = true, bool p_simplify = false) const;
  121. Ref<Mesh> create_outline(float p_margin) const;
  122. virtual AABB get_aabb() const = 0;
  123. void set_lightmap_size_hint(const Vector2 &p_size);
  124. Size2 get_lightmap_size_hint() const;
  125. void clear_cache() const;
  126. typedef Vector<PoolVector<Vector3>> (*ConvexDecompositionFunc)(const real_t *p_vertices, int p_vertex_count, const uint32_t *p_triangles, int p_triangle_count, int p_max_convex_hulls, Vector<PoolVector<uint32_t>> *r_convex_indices);
  127. static ConvexDecompositionFunc convex_decomposition_function;
  128. Vector<Ref<Shape>> convex_decompose(int p_max_convex_hulls = -1) const;
  129. Mesh();
  130. };
  131. class ArrayMesh : public Mesh {
  132. GDCLASS(ArrayMesh, Mesh);
  133. RES_BASE_EXTENSION("mesh");
  134. private:
  135. struct Surface {
  136. String name;
  137. AABB aabb;
  138. Ref<Material> material;
  139. bool is_2d;
  140. };
  141. Vector<Surface> surfaces;
  142. RID mesh;
  143. AABB aabb;
  144. BlendShapeMode blend_shape_mode;
  145. Vector<StringName> blend_shapes;
  146. AABB custom_aabb;
  147. void _recompute_aabb();
  148. protected:
  149. virtual bool _is_generated() const { return false; }
  150. bool _set(const StringName &p_name, const Variant &p_value);
  151. bool _get(const StringName &p_name, Variant &r_ret) const;
  152. void _get_property_list(List<PropertyInfo> *p_list) const;
  153. static void _bind_methods();
  154. public:
  155. void add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes = Array(), uint32_t p_flags = ARRAY_COMPRESS_DEFAULT);
  156. void add_surface(uint32_t p_format, PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<PoolVector<uint8_t>> &p_blend_shapes = Vector<PoolVector<uint8_t>>(), const Vector<AABB> &p_bone_aabbs = Vector<AABB>());
  157. Array surface_get_arrays(int p_surface) const;
  158. Array surface_get_blend_shape_arrays(int p_surface) const;
  159. void add_blend_shape(const StringName &p_name);
  160. int get_blend_shape_count() const;
  161. StringName get_blend_shape_name(int p_index) const;
  162. void set_blend_shape_name(int p_index, const StringName &p_name);
  163. void clear_blend_shapes();
  164. void set_blend_shape_mode(BlendShapeMode p_mode);
  165. BlendShapeMode get_blend_shape_mode() const;
  166. void surface_update_region(int p_surface, int p_offset, const PoolVector<uint8_t> &p_data);
  167. int get_surface_count() const;
  168. void surface_remove(int p_idx);
  169. void clear_surfaces();
  170. void surface_set_custom_aabb(int p_idx, const AABB &p_aabb); //only recognized by driver
  171. int surface_get_array_len(int p_idx) const;
  172. int surface_get_array_index_len(int p_idx) const;
  173. uint32_t surface_get_format(int p_idx) const;
  174. PrimitiveType surface_get_primitive_type(int p_idx) const;
  175. virtual void surface_set_material(int p_idx, const Ref<Material> &p_material);
  176. virtual Ref<Material> surface_get_material(int p_idx) const;
  177. int surface_find_by_name(const String &p_name) const;
  178. void surface_set_name(int p_idx, const String &p_name);
  179. String surface_get_name(int p_idx) const;
  180. void add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data);
  181. void set_custom_aabb(const AABB &p_custom);
  182. AABB get_custom_aabb() const;
  183. AABB get_aabb() const;
  184. virtual RID get_rid() const;
  185. void regen_normalmaps();
  186. Error lightmap_unwrap(const Transform &p_base_transform = Transform(), float p_texel_size = 0.05);
  187. Error lightmap_unwrap_cached(int *&r_cache_data, unsigned int &r_cache_size, bool &r_used_cache, const Transform &p_base_transform = Transform(), float p_texel_size = 0.05);
  188. virtual void reload_from_file();
  189. ArrayMesh();
  190. ~ArrayMesh();
  191. };
  192. VARIANT_ENUM_CAST(Mesh::ArrayType);
  193. VARIANT_ENUM_CAST(Mesh::ArrayFormat);
  194. VARIANT_ENUM_CAST(Mesh::PrimitiveType);
  195. VARIANT_ENUM_CAST(Mesh::BlendShapeMode);
  196. #endif // MESH_H