mesh.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*************************************************************************/
  2. /* mesh.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/io/resource.h"
  33. #include "core/math/face3.h"
  34. #include "core/math/triangle_mesh.h"
  35. #include "scene/resources/material.h"
  36. #include "scene/resources/shape_3d.h"
  37. #include "servers/rendering_server.h"
  38. class Mesh : public Resource {
  39. GDCLASS(Mesh, Resource);
  40. mutable Ref<TriangleMesh> triangle_mesh; //cached
  41. mutable Vector<Ref<TriangleMesh>> surface_triangle_meshes; //cached
  42. mutable Vector<Vector3> debug_lines;
  43. Size2i lightmap_size_hint;
  44. public:
  45. enum PrimitiveType {
  46. PRIMITIVE_POINTS = RenderingServer::PRIMITIVE_POINTS,
  47. PRIMITIVE_LINES = RenderingServer::PRIMITIVE_LINES,
  48. PRIMITIVE_LINE_STRIP = RenderingServer::PRIMITIVE_LINE_STRIP,
  49. PRIMITIVE_TRIANGLES = RenderingServer::PRIMITIVE_TRIANGLES,
  50. PRIMITIVE_TRIANGLE_STRIP = RenderingServer::PRIMITIVE_TRIANGLE_STRIP,
  51. PRIMITIVE_MAX = RenderingServer::PRIMITIVE_MAX,
  52. };
  53. protected:
  54. static void _bind_methods();
  55. GDVIRTUAL0RC(int, _get_surface_count)
  56. GDVIRTUAL1RC(int, _surface_get_array_len, int)
  57. GDVIRTUAL1RC(int, _surface_get_array_index_len, int)
  58. GDVIRTUAL1RC(Array, _surface_get_arrays, int)
  59. GDVIRTUAL1RC(TypedArray<Array>, _surface_get_blend_shape_arrays, int)
  60. GDVIRTUAL1RC(Dictionary, _surface_get_lods, int)
  61. GDVIRTUAL1RC(uint32_t, _surface_get_format, int)
  62. GDVIRTUAL1RC(uint32_t, _surface_get_primitive_type, int)
  63. GDVIRTUAL2(_surface_set_material, int, Ref<Material>)
  64. GDVIRTUAL1RC(Ref<Material>, _surface_get_material, int)
  65. GDVIRTUAL0RC(int, _get_blend_shape_count)
  66. GDVIRTUAL1RC(StringName, _get_blend_shape_name, int)
  67. GDVIRTUAL2(_set_blend_shape_name, int, StringName)
  68. GDVIRTUAL0RC(AABB, _get_aabb)
  69. public:
  70. enum {
  71. NO_INDEX_ARRAY = RenderingServer::NO_INDEX_ARRAY,
  72. ARRAY_WEIGHTS_SIZE = RenderingServer::ARRAY_WEIGHTS_SIZE
  73. };
  74. enum BlendShapeMode {
  75. BLEND_SHAPE_MODE_NORMALIZED = RS::BLEND_SHAPE_MODE_NORMALIZED,
  76. BLEND_SHAPE_MODE_RELATIVE = RS::BLEND_SHAPE_MODE_RELATIVE,
  77. };
  78. enum ArrayType {
  79. ARRAY_VERTEX = RenderingServer::ARRAY_VERTEX,
  80. ARRAY_NORMAL = RenderingServer::ARRAY_NORMAL,
  81. ARRAY_TANGENT = RenderingServer::ARRAY_TANGENT,
  82. ARRAY_COLOR = RenderingServer::ARRAY_COLOR,
  83. ARRAY_TEX_UV = RenderingServer::ARRAY_TEX_UV,
  84. ARRAY_TEX_UV2 = RenderingServer::ARRAY_TEX_UV2,
  85. ARRAY_CUSTOM0 = RenderingServer::ARRAY_CUSTOM0,
  86. ARRAY_CUSTOM1 = RenderingServer::ARRAY_CUSTOM1,
  87. ARRAY_CUSTOM2 = RenderingServer::ARRAY_CUSTOM2,
  88. ARRAY_CUSTOM3 = RenderingServer::ARRAY_CUSTOM3,
  89. ARRAY_BONES = RenderingServer::ARRAY_BONES,
  90. ARRAY_WEIGHTS = RenderingServer::ARRAY_WEIGHTS,
  91. ARRAY_INDEX = RenderingServer::ARRAY_INDEX,
  92. ARRAY_MAX = RenderingServer::ARRAY_MAX
  93. };
  94. enum ArrayCustomFormat {
  95. ARRAY_CUSTOM_RGBA8_UNORM,
  96. ARRAY_CUSTOM_RGBA8_SNORM,
  97. ARRAY_CUSTOM_RG_HALF,
  98. ARRAY_CUSTOM_RGBA_HALF,
  99. ARRAY_CUSTOM_R_FLOAT,
  100. ARRAY_CUSTOM_RG_FLOAT,
  101. ARRAY_CUSTOM_RGB_FLOAT,
  102. ARRAY_CUSTOM_RGBA_FLOAT,
  103. ARRAY_CUSTOM_MAX
  104. };
  105. enum ArrayFormat {
  106. ARRAY_FORMAT_VERTEX = RS::ARRAY_FORMAT_VERTEX,
  107. ARRAY_FORMAT_NORMAL = RS::ARRAY_FORMAT_NORMAL,
  108. ARRAY_FORMAT_TANGENT = RS::ARRAY_FORMAT_TANGENT,
  109. ARRAY_FORMAT_COLOR = RS::ARRAY_FORMAT_COLOR,
  110. ARRAY_FORMAT_TEX_UV = RS::ARRAY_FORMAT_TEX_UV,
  111. ARRAY_FORMAT_TEX_UV2 = RS::ARRAY_FORMAT_TEX_UV2,
  112. ARRAY_FORMAT_CUSTOM0 = RS::ARRAY_FORMAT_CUSTOM0,
  113. ARRAY_FORMAT_CUSTOM1 = RS::ARRAY_FORMAT_CUSTOM1,
  114. ARRAY_FORMAT_CUSTOM2 = RS::ARRAY_FORMAT_CUSTOM2,
  115. ARRAY_FORMAT_CUSTOM3 = RS::ARRAY_FORMAT_CUSTOM3,
  116. ARRAY_FORMAT_BONES = RS::ARRAY_FORMAT_BONES,
  117. ARRAY_FORMAT_WEIGHTS = RS::ARRAY_FORMAT_WEIGHTS,
  118. ARRAY_FORMAT_INDEX = RS::ARRAY_FORMAT_INDEX,
  119. ARRAY_FORMAT_BLEND_SHAPE_MASK = RS::ARRAY_FORMAT_BLEND_SHAPE_MASK,
  120. ARRAY_FORMAT_CUSTOM_BASE = RS::ARRAY_FORMAT_CUSTOM_BASE,
  121. ARRAY_FORMAT_CUSTOM_BITS = RS::ARRAY_FORMAT_CUSTOM_BITS,
  122. ARRAY_FORMAT_CUSTOM0_SHIFT = RS::ARRAY_FORMAT_CUSTOM0_SHIFT,
  123. ARRAY_FORMAT_CUSTOM1_SHIFT = RS::ARRAY_FORMAT_CUSTOM1_SHIFT,
  124. ARRAY_FORMAT_CUSTOM2_SHIFT = RS::ARRAY_FORMAT_CUSTOM2_SHIFT,
  125. ARRAY_FORMAT_CUSTOM3_SHIFT = RS::ARRAY_FORMAT_CUSTOM3_SHIFT,
  126. ARRAY_FORMAT_CUSTOM_MASK = RS::ARRAY_FORMAT_CUSTOM_MASK,
  127. ARRAY_COMPRESS_FLAGS_BASE = RS::ARRAY_COMPRESS_FLAGS_BASE,
  128. ARRAY_FLAG_USE_2D_VERTICES = RS::ARRAY_FLAG_USE_2D_VERTICES,
  129. ARRAY_FLAG_USE_DYNAMIC_UPDATE = RS::ARRAY_FLAG_USE_DYNAMIC_UPDATE,
  130. ARRAY_FLAG_USE_8_BONE_WEIGHTS = RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS,
  131. ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY = RS::ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY,
  132. };
  133. virtual int get_surface_count() const;
  134. virtual int surface_get_array_len(int p_idx) const;
  135. virtual int surface_get_array_index_len(int p_idx) const;
  136. virtual Array surface_get_arrays(int p_surface) const;
  137. virtual TypedArray<Array> surface_get_blend_shape_arrays(int p_surface) const;
  138. virtual Dictionary surface_get_lods(int p_surface) const;
  139. virtual uint32_t surface_get_format(int p_idx) const;
  140. virtual PrimitiveType surface_get_primitive_type(int p_idx) const;
  141. virtual void surface_set_material(int p_idx, const Ref<Material> &p_material);
  142. virtual Ref<Material> surface_get_material(int p_idx) const;
  143. virtual int get_blend_shape_count() const;
  144. virtual StringName get_blend_shape_name(int p_index) const;
  145. virtual void set_blend_shape_name(int p_index, const StringName &p_name);
  146. virtual AABB get_aabb() const;
  147. Vector<Face3> get_faces() const;
  148. Vector<Face3> get_surface_faces(int p_surface) const;
  149. Ref<TriangleMesh> generate_triangle_mesh() const;
  150. Ref<TriangleMesh> generate_surface_triangle_mesh(int p_surface) const;
  151. void generate_debug_mesh_lines(Vector<Vector3> &r_lines);
  152. void generate_debug_mesh_indices(Vector<Vector3> &r_points);
  153. Ref<Mesh> create_outline(float p_margin) const;
  154. void set_lightmap_size_hint(const Size2i &p_size);
  155. Size2i get_lightmap_size_hint() const;
  156. void clear_cache() const;
  157. struct ConvexDecompositionSettings {
  158. enum Mode : int {
  159. CONVEX_DECOMPOSITION_MODE_VOXEL = 0,
  160. CONVEX_DECOMPOSITION_MODE_TETRAHEDRON
  161. };
  162. /// Maximum concavity. [Range: 0.0 -> 1.0]
  163. real_t max_concavity = 1.0;
  164. /// Controls the bias toward clipping along symmetry planes. [Range: 0.0 -> 1.0]
  165. real_t symmetry_planes_clipping_bias = 0.05;
  166. /// Controls the bias toward clipping along revolution axes. [Range: 0.0 -> 1.0]
  167. real_t revolution_axes_clipping_bias = 0.05;
  168. real_t min_volume_per_convex_hull = 0.0001;
  169. /// Maximum number of voxels generated during the voxelization stage.
  170. uint32_t resolution = 10'000;
  171. uint32_t max_num_vertices_per_convex_hull = 32;
  172. /// Controls the granularity of the search for the "best" clipping plane.
  173. /// [Range: 1 -> 16]
  174. uint32_t plane_downsampling = 4;
  175. /// Controls the precision of the convex-hull generation process during the
  176. /// clipping plane selection stage.
  177. /// [Range: 1 -> 16]
  178. uint32_t convexhull_downsampling = 4;
  179. /// enable/disable normalizing the mesh before applying the convex decomposition.
  180. bool normalize_mesh = false;
  181. Mode mode = CONVEX_DECOMPOSITION_MODE_VOXEL;
  182. bool convexhull_approximation = true;
  183. /// This is the maximum number of convex hulls to produce from the merge operation.
  184. uint32_t max_convex_hulls = 1;
  185. bool project_hull_vertices = true;
  186. };
  187. typedef Vector<Vector<Vector3>> (*ConvexDecompositionFunc)(const real_t *p_vertices, int p_vertex_count, const uint32_t *p_triangles, int p_triangle_count, const ConvexDecompositionSettings &p_settings, Vector<Vector<uint32_t>> *r_convex_indices);
  188. static ConvexDecompositionFunc convex_decomposition_function;
  189. Vector<Ref<Shape3D>> convex_decompose(const ConvexDecompositionSettings &p_settings) const;
  190. Ref<Shape3D> create_convex_shape(bool p_clean = true, bool p_simplify = false) const;
  191. Ref<Shape3D> create_trimesh_shape() const;
  192. virtual int get_builtin_bind_pose_count() const;
  193. virtual Transform3D get_builtin_bind_pose(int p_index) const;
  194. Mesh();
  195. };
  196. class ArrayMesh : public Mesh {
  197. GDCLASS(ArrayMesh, Mesh);
  198. RES_BASE_EXTENSION("mesh");
  199. PackedStringArray _get_blend_shape_names() const;
  200. void _set_blend_shape_names(const PackedStringArray &p_names);
  201. Array _get_surfaces() const;
  202. void _set_surfaces(const Array &p_data);
  203. Ref<ArrayMesh> shadow_mesh;
  204. private:
  205. struct Surface {
  206. uint32_t format = 0;
  207. int array_length = 0;
  208. int index_array_length = 0;
  209. PrimitiveType primitive = PrimitiveType::PRIMITIVE_MAX;
  210. String name;
  211. AABB aabb;
  212. Ref<Material> material;
  213. bool is_2d = false;
  214. };
  215. Vector<Surface> surfaces;
  216. mutable RID mesh;
  217. AABB aabb;
  218. BlendShapeMode blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE;
  219. Vector<StringName> blend_shapes;
  220. AABB custom_aabb;
  221. _FORCE_INLINE_ void _create_if_empty() const;
  222. void _recompute_aabb();
  223. protected:
  224. virtual bool _is_generated() const { return false; }
  225. bool _set(const StringName &p_name, const Variant &p_value);
  226. bool _get(const StringName &p_name, Variant &r_ret) const;
  227. void _get_property_list(List<PropertyInfo> *p_list) const;
  228. virtual void reset_state() override;
  229. static void _bind_methods();
  230. public:
  231. void add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes = Array(), const Dictionary &p_lods = Dictionary(), uint32_t p_flags = 0);
  232. void add_surface(uint32_t p_format, PrimitiveType p_primitive, const Vector<uint8_t> &p_array, const Vector<uint8_t> &p_attribute_array, const Vector<uint8_t> &p_skin_array, int p_vertex_count, const Vector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<uint8_t> &p_blend_shape_data = Vector<uint8_t>(), const Vector<AABB> &p_bone_aabbs = Vector<AABB>(), const Vector<RS::SurfaceData::LOD> &p_lods = Vector<RS::SurfaceData::LOD>());
  233. Array surface_get_arrays(int p_surface) const override;
  234. TypedArray<Array> surface_get_blend_shape_arrays(int p_surface) const override;
  235. Dictionary surface_get_lods(int p_surface) const override;
  236. void add_blend_shape(const StringName &p_name);
  237. int get_blend_shape_count() const override;
  238. StringName get_blend_shape_name(int p_index) const override;
  239. void set_blend_shape_name(int p_index, const StringName &p_name) override;
  240. void clear_blend_shapes();
  241. void set_blend_shape_mode(BlendShapeMode p_mode);
  242. BlendShapeMode get_blend_shape_mode() const;
  243. void surface_update_vertex_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data);
  244. void surface_update_attribute_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data);
  245. void surface_update_skin_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data);
  246. int get_surface_count() const override;
  247. void clear_surfaces();
  248. void surface_set_custom_aabb(int p_idx, const AABB &p_aabb); //only recognized by driver
  249. int surface_get_array_len(int p_idx) const override;
  250. int surface_get_array_index_len(int p_idx) const override;
  251. uint32_t surface_get_format(int p_idx) const override;
  252. PrimitiveType surface_get_primitive_type(int p_idx) const override;
  253. virtual void surface_set_material(int p_idx, const Ref<Material> &p_material) override;
  254. virtual Ref<Material> surface_get_material(int p_idx) const override;
  255. int surface_find_by_name(const String &p_name) const;
  256. void surface_set_name(int p_idx, const String &p_name);
  257. String surface_get_name(int p_idx) const;
  258. void set_custom_aabb(const AABB &p_custom);
  259. AABB get_custom_aabb() const;
  260. AABB get_aabb() const override;
  261. virtual RID get_rid() const override;
  262. void regen_normal_maps();
  263. Error lightmap_unwrap(const Transform3D &p_base_transform = Transform3D(), float p_texel_size = 0.05);
  264. Error lightmap_unwrap_cached(const Transform3D &p_base_transform, float p_texel_size, const Vector<uint8_t> &p_src_cache, Vector<uint8_t> &r_dst_cache, bool p_generate_cache = true);
  265. virtual void reload_from_file() override;
  266. void set_shadow_mesh(const Ref<ArrayMesh> &p_mesh);
  267. Ref<ArrayMesh> get_shadow_mesh() const;
  268. ArrayMesh();
  269. ~ArrayMesh();
  270. };
  271. VARIANT_ENUM_CAST(Mesh::ArrayType);
  272. VARIANT_ENUM_CAST(Mesh::ArrayFormat);
  273. VARIANT_ENUM_CAST(Mesh::ArrayCustomFormat);
  274. VARIANT_ENUM_CAST(Mesh::PrimitiveType);
  275. VARIANT_ENUM_CAST(Mesh::BlendShapeMode);
  276. class PlaceholderMesh : public Mesh {
  277. GDCLASS(PlaceholderMesh, Mesh);
  278. RID rid;
  279. AABB aabb;
  280. protected:
  281. static void _bind_methods();
  282. public:
  283. virtual int get_surface_count() const override { return 0; }
  284. virtual int surface_get_array_len(int p_idx) const override { return 0; }
  285. virtual int surface_get_array_index_len(int p_idx) const override { return 0; }
  286. virtual Array surface_get_arrays(int p_surface) const override { return Array(); }
  287. virtual TypedArray<Array> surface_get_blend_shape_arrays(int p_surface) const override { return TypedArray<Array>(); }
  288. virtual Dictionary surface_get_lods(int p_surface) const override { return Dictionary(); }
  289. virtual uint32_t surface_get_format(int p_idx) const override { return 0; }
  290. virtual PrimitiveType surface_get_primitive_type(int p_idx) const override { return PRIMITIVE_TRIANGLES; }
  291. virtual void surface_set_material(int p_idx, const Ref<Material> &p_material) override {}
  292. virtual Ref<Material> surface_get_material(int p_idx) const override { return Ref<Material>(); }
  293. virtual int get_blend_shape_count() const override { return 0; }
  294. virtual StringName get_blend_shape_name(int p_index) const override { return StringName(); }
  295. virtual void set_blend_shape_name(int p_index, const StringName &p_name) override {}
  296. virtual RID get_rid() const override { return rid; }
  297. virtual AABB get_aabb() const override { return aabb; }
  298. void set_aabb(const AABB &p_aabb) { aabb = p_aabb; }
  299. virtual int get_builtin_bind_pose_count() const override { return 0; }
  300. virtual Transform3D get_builtin_bind_pose(int p_index) const override { return Transform3D(); }
  301. PlaceholderMesh();
  302. ~PlaceholderMesh();
  303. };
  304. #endif // MESH_H