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
31#ifndef MESH_H
32#define MESH_H
33
34#include "core/io/resource.h"
35#include "core/math/face3.h"
36#include "core/math/triangle_mesh.h"
37#include "scene/resources/material.h"
38#include "servers/rendering_server.h"
39
40class ConcavePolygonShape3D;
41class ConvexPolygonShape3D;
42class MeshConvexDecompositionSettings;
43class Shape3D;
44
45class Mesh : public Resource {
46 GDCLASS(Mesh, Resource);
47
48 mutable Ref<TriangleMesh> triangle_mesh; //cached
49 mutable Vector<Ref<TriangleMesh>> surface_triangle_meshes; //cached
50 mutable Vector<Vector3> debug_lines;
51 Size2i lightmap_size_hint;
52
53 Vector<Vector3> _get_faces() const;
54
55public:
56 enum PrimitiveType {
57 PRIMITIVE_POINTS = RenderingServer::PRIMITIVE_POINTS,
58 PRIMITIVE_LINES = RenderingServer::PRIMITIVE_LINES,
59 PRIMITIVE_LINE_STRIP = RenderingServer::PRIMITIVE_LINE_STRIP,
60 PRIMITIVE_TRIANGLES = RenderingServer::PRIMITIVE_TRIANGLES,
61 PRIMITIVE_TRIANGLE_STRIP = RenderingServer::PRIMITIVE_TRIANGLE_STRIP,
62 PRIMITIVE_MAX = RenderingServer::PRIMITIVE_MAX,
63 };
64
65protected:
66 static void _bind_methods();
67
68 GDVIRTUAL0RC(int, _get_surface_count)
69 GDVIRTUAL1RC(int, _surface_get_array_len, int)
70 GDVIRTUAL1RC(int, _surface_get_array_index_len, int)
71 GDVIRTUAL1RC(Array, _surface_get_arrays, int)
72 GDVIRTUAL1RC(TypedArray<Array>, _surface_get_blend_shape_arrays, int)
73 GDVIRTUAL1RC(Dictionary, _surface_get_lods, int)
74 GDVIRTUAL1RC(uint32_t, _surface_get_format, int)
75 GDVIRTUAL1RC(uint32_t, _surface_get_primitive_type, int)
76 GDVIRTUAL2(_surface_set_material, int, Ref<Material>)
77 GDVIRTUAL1RC(Ref<Material>, _surface_get_material, int)
78 GDVIRTUAL0RC(int, _get_blend_shape_count)
79 GDVIRTUAL1RC(StringName, _get_blend_shape_name, int)
80 GDVIRTUAL2(_set_blend_shape_name, int, StringName)
81 GDVIRTUAL0RC(AABB, _get_aabb)
82
83public:
84 enum {
85 NO_INDEX_ARRAY = RenderingServer::NO_INDEX_ARRAY,
86 ARRAY_WEIGHTS_SIZE = RenderingServer::ARRAY_WEIGHTS_SIZE
87 };
88 enum BlendShapeMode {
89 BLEND_SHAPE_MODE_NORMALIZED = RS::BLEND_SHAPE_MODE_NORMALIZED,
90 BLEND_SHAPE_MODE_RELATIVE = RS::BLEND_SHAPE_MODE_RELATIVE,
91 };
92 enum ArrayType {
93 ARRAY_VERTEX = RenderingServer::ARRAY_VERTEX,
94 ARRAY_NORMAL = RenderingServer::ARRAY_NORMAL,
95 ARRAY_TANGENT = RenderingServer::ARRAY_TANGENT,
96 ARRAY_COLOR = RenderingServer::ARRAY_COLOR,
97 ARRAY_TEX_UV = RenderingServer::ARRAY_TEX_UV,
98 ARRAY_TEX_UV2 = RenderingServer::ARRAY_TEX_UV2,
99 ARRAY_CUSTOM0 = RenderingServer::ARRAY_CUSTOM0,
100 ARRAY_CUSTOM1 = RenderingServer::ARRAY_CUSTOM1,
101 ARRAY_CUSTOM2 = RenderingServer::ARRAY_CUSTOM2,
102 ARRAY_CUSTOM3 = RenderingServer::ARRAY_CUSTOM3,
103 ARRAY_BONES = RenderingServer::ARRAY_BONES,
104 ARRAY_WEIGHTS = RenderingServer::ARRAY_WEIGHTS,
105 ARRAY_INDEX = RenderingServer::ARRAY_INDEX,
106 ARRAY_MAX = RenderingServer::ARRAY_MAX
107
108 };
109
110 enum ArrayCustomFormat {
111 ARRAY_CUSTOM_RGBA8_UNORM,
112 ARRAY_CUSTOM_RGBA8_SNORM,
113 ARRAY_CUSTOM_RG_HALF,
114 ARRAY_CUSTOM_RGBA_HALF,
115 ARRAY_CUSTOM_R_FLOAT,
116 ARRAY_CUSTOM_RG_FLOAT,
117 ARRAY_CUSTOM_RGB_FLOAT,
118 ARRAY_CUSTOM_RGBA_FLOAT,
119 ARRAY_CUSTOM_MAX
120 };
121
122 enum ArrayFormat {
123 ARRAY_FORMAT_VERTEX = RS::ARRAY_FORMAT_VERTEX,
124 ARRAY_FORMAT_NORMAL = RS::ARRAY_FORMAT_NORMAL,
125 ARRAY_FORMAT_TANGENT = RS::ARRAY_FORMAT_TANGENT,
126 ARRAY_FORMAT_COLOR = RS::ARRAY_FORMAT_COLOR,
127 ARRAY_FORMAT_TEX_UV = RS::ARRAY_FORMAT_TEX_UV,
128 ARRAY_FORMAT_TEX_UV2 = RS::ARRAY_FORMAT_TEX_UV2,
129 ARRAY_FORMAT_CUSTOM0 = RS::ARRAY_FORMAT_CUSTOM0,
130 ARRAY_FORMAT_CUSTOM1 = RS::ARRAY_FORMAT_CUSTOM1,
131 ARRAY_FORMAT_CUSTOM2 = RS::ARRAY_FORMAT_CUSTOM2,
132 ARRAY_FORMAT_CUSTOM3 = RS::ARRAY_FORMAT_CUSTOM3,
133 ARRAY_FORMAT_BONES = RS::ARRAY_FORMAT_BONES,
134 ARRAY_FORMAT_WEIGHTS = RS::ARRAY_FORMAT_WEIGHTS,
135 ARRAY_FORMAT_INDEX = RS::ARRAY_FORMAT_INDEX,
136
137 ARRAY_FORMAT_BLEND_SHAPE_MASK = RS::ARRAY_FORMAT_BLEND_SHAPE_MASK,
138
139 ARRAY_FORMAT_CUSTOM_BASE = RS::ARRAY_FORMAT_CUSTOM_BASE,
140 ARRAY_FORMAT_CUSTOM_BITS = RS::ARRAY_FORMAT_CUSTOM_BITS,
141 ARRAY_FORMAT_CUSTOM0_SHIFT = RS::ARRAY_FORMAT_CUSTOM0_SHIFT,
142 ARRAY_FORMAT_CUSTOM1_SHIFT = RS::ARRAY_FORMAT_CUSTOM1_SHIFT,
143 ARRAY_FORMAT_CUSTOM2_SHIFT = RS::ARRAY_FORMAT_CUSTOM2_SHIFT,
144 ARRAY_FORMAT_CUSTOM3_SHIFT = RS::ARRAY_FORMAT_CUSTOM3_SHIFT,
145
146 ARRAY_FORMAT_CUSTOM_MASK = RS::ARRAY_FORMAT_CUSTOM_MASK,
147 ARRAY_COMPRESS_FLAGS_BASE = RS::ARRAY_COMPRESS_FLAGS_BASE,
148
149 ARRAY_FLAG_USE_2D_VERTICES = RS::ARRAY_FLAG_USE_2D_VERTICES,
150 ARRAY_FLAG_USE_DYNAMIC_UPDATE = RS::ARRAY_FLAG_USE_DYNAMIC_UPDATE,
151 ARRAY_FLAG_USE_8_BONE_WEIGHTS = RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS,
152
153 ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY = RS::ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY,
154 };
155
156 virtual int get_surface_count() const;
157 virtual int surface_get_array_len(int p_idx) const;
158 virtual int surface_get_array_index_len(int p_idx) const;
159 virtual Array surface_get_arrays(int p_surface) const;
160 virtual TypedArray<Array> surface_get_blend_shape_arrays(int p_surface) const;
161 virtual Dictionary surface_get_lods(int p_surface) const;
162 virtual BitField<ArrayFormat> surface_get_format(int p_idx) const;
163 virtual PrimitiveType surface_get_primitive_type(int p_idx) const;
164 virtual void surface_set_material(int p_idx, const Ref<Material> &p_material);
165 virtual Ref<Material> surface_get_material(int p_idx) const;
166 virtual int get_blend_shape_count() const;
167 virtual StringName get_blend_shape_name(int p_index) const;
168 virtual void set_blend_shape_name(int p_index, const StringName &p_name);
169 virtual AABB get_aabb() const;
170
171 Vector<Face3> get_faces() const;
172 Vector<Face3> get_surface_faces(int p_surface) const;
173 Ref<TriangleMesh> generate_triangle_mesh() const;
174 Ref<TriangleMesh> generate_surface_triangle_mesh(int p_surface) const;
175 void generate_debug_mesh_lines(Vector<Vector3> &r_lines);
176 void generate_debug_mesh_indices(Vector<Vector3> &r_points);
177
178 Ref<Mesh> create_outline(float p_margin) const;
179
180 void set_lightmap_size_hint(const Size2i &p_size);
181 Size2i get_lightmap_size_hint() const;
182 void clear_cache() const;
183
184 typedef Vector<Vector<Vector3>> (*ConvexDecompositionFunc)(const real_t *p_vertices, int p_vertex_count, const uint32_t *p_triangles, int p_triangle_count, const Ref<MeshConvexDecompositionSettings> &p_settings, Vector<Vector<uint32_t>> *r_convex_indices);
185
186 static ConvexDecompositionFunc convex_decomposition_function;
187
188 Vector<Ref<Shape3D>> convex_decompose(const Ref<MeshConvexDecompositionSettings> &p_settings) const;
189 Ref<ConvexPolygonShape3D> create_convex_shape(bool p_clean = true, bool p_simplify = false) const;
190 Ref<ConcavePolygonShape3D> create_trimesh_shape() const;
191
192 virtual int get_builtin_bind_pose_count() const;
193 virtual Transform3D get_builtin_bind_pose(int p_index) const;
194
195 virtual Ref<Resource> create_placeholder() const;
196
197 Mesh();
198};
199
200class MeshConvexDecompositionSettings : public RefCounted {
201 GDCLASS(MeshConvexDecompositionSettings, RefCounted);
202
203public:
204 enum Mode : int {
205 CONVEX_DECOMPOSITION_MODE_VOXEL = 0,
206 CONVEX_DECOMPOSITION_MODE_TETRAHEDRON = 1
207 };
208
209private:
210 Mode mode = CONVEX_DECOMPOSITION_MODE_VOXEL;
211
212 /// Maximum concavity. [Range: 0.0 -> 1.0]
213 real_t max_concavity = 1.0;
214 /// Controls the bias toward clipping along symmetry planes. [Range: 0.0 -> 1.0]
215 real_t symmetry_planes_clipping_bias = 0.05;
216 /// Controls the bias toward clipping along revolution axes. [Range: 0.0 -> 1.0]
217 real_t revolution_axes_clipping_bias = 0.05;
218 real_t min_volume_per_convex_hull = 0.0001;
219 /// Maximum number of voxels generated during the voxelization stage.
220 uint32_t resolution = 10'000;
221 uint32_t max_num_vertices_per_convex_hull = 32;
222 /// Controls the granularity of the search for the "best" clipping plane.
223 /// [Range: 1 -> 16]
224 uint32_t plane_downsampling = 4;
225 /// Controls the precision of the convex-hull generation process during the
226 /// clipping plane selection stage.
227 /// [Range: 1 -> 16]
228 uint32_t convex_hull_downsampling = 4;
229 /// enable/disable normalizing the mesh before applying the convex decomposition.
230 bool normalize_mesh = false;
231
232 bool convex_hull_approximation = true;
233 /// This is the maximum number of convex hulls to produce from the merge operation.
234 uint32_t max_convex_hulls = 1;
235 bool project_hull_vertices = true;
236
237protected:
238 static void _bind_methods();
239
240public:
241 void set_max_concavity(real_t p_max_concavity);
242 real_t get_max_concavity() const;
243
244 void set_symmetry_planes_clipping_bias(real_t p_symmetry_planes_clipping_bias);
245 real_t get_symmetry_planes_clipping_bias() const;
246
247 void set_revolution_axes_clipping_bias(real_t p_revolution_axes_clipping_bias);
248 real_t get_revolution_axes_clipping_bias() const;
249
250 void set_min_volume_per_convex_hull(real_t p_min_volume_per_convex_hull);
251 real_t get_min_volume_per_convex_hull() const;
252
253 void set_resolution(uint32_t p_resolution);
254 uint32_t get_resolution() const;
255
256 void set_max_num_vertices_per_convex_hull(uint32_t p_max_num_vertices_per_convex_hull);
257 uint32_t get_max_num_vertices_per_convex_hull() const;
258
259 void set_plane_downsampling(uint32_t p_plane_downsampling);
260 uint32_t get_plane_downsampling() const;
261
262 void set_convex_hull_downsampling(uint32_t p_convex_hull_downsampling);
263 uint32_t get_convex_hull_downsampling() const;
264
265 void set_normalize_mesh(bool p_normalize_mesh);
266 bool get_normalize_mesh() const;
267
268 void set_mode(Mode p_mode);
269 Mode get_mode() const;
270
271 void set_convex_hull_approximation(bool p_convex_hull_approximation);
272 bool get_convex_hull_approximation() const;
273
274 void set_max_convex_hulls(uint32_t p_max_convex_hulls);
275 uint32_t get_max_convex_hulls() const;
276
277 void set_project_hull_vertices(bool p_project_hull_vertices);
278 bool get_project_hull_vertices() const;
279};
280
281VARIANT_ENUM_CAST(MeshConvexDecompositionSettings::Mode);
282
283class ArrayMesh : public Mesh {
284 GDCLASS(ArrayMesh, Mesh);
285 RES_BASE_EXTENSION("mesh");
286
287 PackedStringArray _get_blend_shape_names() const;
288 void _set_blend_shape_names(const PackedStringArray &p_names);
289
290 Array _get_surfaces() const;
291 void _set_surfaces(const Array &p_data);
292 Ref<ArrayMesh> shadow_mesh;
293
294private:
295 struct Surface {
296 uint32_t format = 0;
297 int array_length = 0;
298 int index_array_length = 0;
299 PrimitiveType primitive = PrimitiveType::PRIMITIVE_MAX;
300
301 String name;
302 AABB aabb;
303 Ref<Material> material;
304 bool is_2d = false;
305 };
306 Vector<Surface> surfaces;
307 mutable RID mesh;
308 AABB aabb;
309 BlendShapeMode blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE;
310 Vector<StringName> blend_shapes;
311 AABB custom_aabb;
312
313 _FORCE_INLINE_ void _create_if_empty() const;
314 void _recompute_aabb();
315
316protected:
317 virtual bool _is_generated() const { return false; }
318
319 bool _set(const StringName &p_name, const Variant &p_value);
320 bool _get(const StringName &p_name, Variant &r_ret) const;
321 void _get_property_list(List<PropertyInfo> *p_list) const;
322 bool surface_index_0 = false;
323
324 virtual void reset_state() override;
325
326 static void _bind_methods();
327
328public:
329 void add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const TypedArray<Array> &p_blend_shapes = TypedArray<Array>(), const Dictionary &p_lods = Dictionary(), BitField<ArrayFormat> p_flags = 0);
330
331 void add_surface(BitField<ArrayFormat> 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>());
332
333 Array surface_get_arrays(int p_surface) const override;
334 TypedArray<Array> surface_get_blend_shape_arrays(int p_surface) const override;
335 Dictionary surface_get_lods(int p_surface) const override;
336
337 void add_blend_shape(const StringName &p_name);
338 int get_blend_shape_count() const override;
339 StringName get_blend_shape_name(int p_index) const override;
340 void set_blend_shape_name(int p_index, const StringName &p_name) override;
341 void clear_blend_shapes();
342
343 void set_blend_shape_mode(BlendShapeMode p_mode);
344 BlendShapeMode get_blend_shape_mode() const;
345
346 void surface_update_vertex_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data);
347 void surface_update_attribute_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data);
348 void surface_update_skin_region(int p_surface, int p_offset, const Vector<uint8_t> &p_data);
349
350 int get_surface_count() const override;
351
352 void clear_surfaces();
353
354 void surface_set_custom_aabb(int p_idx, const AABB &p_aabb); //only recognized by driver
355
356 int surface_get_array_len(int p_idx) const override;
357 int surface_get_array_index_len(int p_idx) const override;
358 BitField<ArrayFormat> surface_get_format(int p_idx) const override;
359 PrimitiveType surface_get_primitive_type(int p_idx) const override;
360
361 virtual void surface_set_material(int p_idx, const Ref<Material> &p_material) override;
362 virtual Ref<Material> surface_get_material(int p_idx) const override;
363
364 int surface_find_by_name(const String &p_name) const;
365 void surface_set_name(int p_idx, const String &p_name);
366 String surface_get_name(int p_idx) const;
367
368 void set_custom_aabb(const AABB &p_custom);
369 AABB get_custom_aabb() const;
370
371 AABB get_aabb() const override;
372 virtual RID get_rid() const override;
373
374 void regen_normal_maps();
375
376 Error lightmap_unwrap(const Transform3D &p_base_transform = Transform3D(), float p_texel_size = 0.05);
377 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);
378
379 virtual void reload_from_file() override;
380
381 void set_shadow_mesh(const Ref<ArrayMesh> &p_mesh);
382 Ref<ArrayMesh> get_shadow_mesh() const;
383
384 ArrayMesh();
385
386 ~ArrayMesh();
387};
388
389VARIANT_ENUM_CAST(Mesh::ArrayType);
390VARIANT_BITFIELD_CAST(Mesh::ArrayFormat);
391VARIANT_ENUM_CAST(Mesh::ArrayCustomFormat);
392VARIANT_ENUM_CAST(Mesh::PrimitiveType);
393VARIANT_ENUM_CAST(Mesh::BlendShapeMode);
394
395class PlaceholderMesh : public Mesh {
396 GDCLASS(PlaceholderMesh, Mesh);
397
398 RID rid;
399 AABB aabb;
400
401protected:
402 static void _bind_methods();
403
404public:
405 virtual int get_surface_count() const override { return 0; }
406 virtual int surface_get_array_len(int p_idx) const override { return 0; }
407 virtual int surface_get_array_index_len(int p_idx) const override { return 0; }
408 virtual Array surface_get_arrays(int p_surface) const override { return Array(); }
409 virtual TypedArray<Array> surface_get_blend_shape_arrays(int p_surface) const override { return TypedArray<Array>(); }
410 virtual Dictionary surface_get_lods(int p_surface) const override { return Dictionary(); }
411 virtual BitField<ArrayFormat> surface_get_format(int p_idx) const override { return 0; }
412 virtual PrimitiveType surface_get_primitive_type(int p_idx) const override { return PRIMITIVE_TRIANGLES; }
413 virtual void surface_set_material(int p_idx, const Ref<Material> &p_material) override {}
414 virtual Ref<Material> surface_get_material(int p_idx) const override { return Ref<Material>(); }
415 virtual int get_blend_shape_count() const override { return 0; }
416 virtual StringName get_blend_shape_name(int p_index) const override { return StringName(); }
417 virtual void set_blend_shape_name(int p_index, const StringName &p_name) override {}
418 virtual RID get_rid() const override { return rid; }
419 virtual AABB get_aabb() const override { return aabb; }
420 void set_aabb(const AABB &p_aabb) { aabb = p_aabb; }
421
422 virtual int get_builtin_bind_pose_count() const override { return 0; }
423 virtual Transform3D get_builtin_bind_pose(int p_index) const override { return Transform3D(); }
424
425 PlaceholderMesh();
426 ~PlaceholderMesh();
427};
428
429#endif // MESH_H
430