Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/csg/csg_shape.h
10277 views
1
/**************************************************************************/
2
/* csg_shape.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
#pragma once
32
33
#include "csg.h"
34
35
#include "scene/3d/path_3d.h"
36
#include "scene/3d/visual_instance_3d.h"
37
38
#ifndef PHYSICS_3D_DISABLED
39
#include "scene/resources/3d/concave_polygon_shape_3d.h"
40
#endif // PHYSICS_3D_DISABLED
41
42
#include "thirdparty/misc/mikktspace.h"
43
44
class NavigationMesh;
45
class NavigationMeshSourceGeometryData3D;
46
47
class CSGShape3D : public GeometryInstance3D {
48
GDCLASS(CSGShape3D, GeometryInstance3D);
49
50
public:
51
enum Operation {
52
OPERATION_UNION,
53
OPERATION_INTERSECTION,
54
OPERATION_SUBTRACTION,
55
56
};
57
58
private:
59
Operation operation = OPERATION_UNION;
60
CSGShape3D *parent_shape = nullptr;
61
62
CSGBrush *brush = nullptr;
63
64
AABB node_aabb;
65
66
bool dirty = false;
67
bool last_visible = false;
68
float snap = 0.001;
69
70
#ifndef PHYSICS_3D_DISABLED
71
bool use_collision = false;
72
uint32_t collision_layer = 1;
73
uint32_t collision_mask = 1;
74
real_t collision_priority = 1.0;
75
Ref<ConcavePolygonShape3D> root_collision_shape;
76
RID root_collision_instance;
77
RID root_collision_debug_instance;
78
Transform3D debug_shape_old_transform;
79
#endif // PHYSICS_3D_DISABLED
80
81
bool calculate_tangents = true;
82
83
Ref<ArrayMesh> root_mesh;
84
85
struct Vector3Hasher {
86
_ALWAYS_INLINE_ uint32_t hash(const Vector3 &p_vec3) const {
87
uint32_t h = hash_murmur3_one_float(p_vec3.x);
88
h = hash_murmur3_one_float(p_vec3.y, h);
89
h = hash_murmur3_one_float(p_vec3.z, h);
90
return h;
91
}
92
};
93
94
struct ShapeUpdateSurface {
95
Vector<Vector3> vertices;
96
Vector<Vector3> normals;
97
Vector<Vector2> uvs;
98
Vector<real_t> tans;
99
Ref<Material> material;
100
int last_added = 0;
101
102
Vector3 *verticesw = nullptr;
103
Vector3 *normalsw = nullptr;
104
Vector2 *uvsw = nullptr;
105
real_t *tansw = nullptr;
106
};
107
108
//mikktspace callbacks
109
static int mikktGetNumFaces(const SMikkTSpaceContext *pContext);
110
static int mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace);
111
static void mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert);
112
static void mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert);
113
static void mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert);
114
static void mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
115
const tbool bIsOrientationPreserving, const int iFace, const int iVert);
116
117
#ifndef PHYSICS_3D_DISABLED
118
void _update_collision_faces();
119
bool _is_debug_collision_shape_visible();
120
void _update_debug_collision_shape();
121
void _clear_debug_collision_shape();
122
void _on_transform_changed();
123
Vector<Vector3> _get_brush_collision_faces();
124
#endif // PHYSICS_3D_DISABLED
125
126
protected:
127
void _notification(int p_what);
128
virtual CSGBrush *_build_brush() = 0;
129
void _make_dirty(bool p_parent_removing = false);
130
PackedStringArray get_configuration_warnings() const override;
131
132
static void _bind_methods();
133
134
friend class CSGCombiner3D;
135
CSGBrush *_get_brush();
136
137
void _validate_property(PropertyInfo &p_property) const;
138
139
public:
140
Array get_meshes() const;
141
void update_shape();
142
143
void set_operation(Operation p_operation);
144
Operation get_operation() const;
145
146
virtual Vector<Vector3> get_brush_faces();
147
148
virtual AABB get_aabb() const override;
149
150
void set_use_collision(bool p_enable);
151
bool is_using_collision() const;
152
153
void set_collision_layer(uint32_t p_layer);
154
uint32_t get_collision_layer() const;
155
156
void set_collision_mask(uint32_t p_mask);
157
uint32_t get_collision_mask() const;
158
159
void set_collision_layer_value(int p_layer_number, bool p_value);
160
bool get_collision_layer_value(int p_layer_number) const;
161
162
void set_collision_mask_value(int p_layer_number, bool p_value);
163
bool get_collision_mask_value(int p_layer_number) const;
164
165
RID _get_root_collision_instance() const;
166
167
void set_collision_priority(real_t p_priority);
168
real_t get_collision_priority() const;
169
170
#ifndef DISABLE_DEPRECATED
171
void set_snap(float p_snap);
172
float get_snap() const;
173
#endif // DISABLE_DEPRECATED
174
175
void set_calculate_tangents(bool p_calculate_tangents);
176
bool is_calculating_tangents() const;
177
178
bool is_root_shape() const;
179
180
Ref<ArrayMesh> bake_static_mesh();
181
#ifndef PHYSICS_3D_DISABLED
182
Ref<ConcavePolygonShape3D> bake_collision_shape();
183
#endif // PHYSICS_3D_DISABLED
184
185
virtual Ref<TriangleMesh> generate_triangle_mesh() const override;
186
187
#ifndef NAVIGATION_3D_DISABLED
188
private:
189
static Callable _navmesh_source_geometry_parsing_callback;
190
static RID _navmesh_source_geometry_parser;
191
192
public:
193
static void navmesh_parse_init();
194
static void navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
195
#endif // NAVIGATION_3D_DISABLED
196
197
CSGShape3D();
198
~CSGShape3D();
199
};
200
201
VARIANT_ENUM_CAST(CSGShape3D::Operation)
202
203
class CSGCombiner3D : public CSGShape3D {
204
GDCLASS(CSGCombiner3D, CSGShape3D);
205
206
private:
207
virtual CSGBrush *_build_brush() override;
208
209
public:
210
CSGCombiner3D();
211
};
212
213
class CSGPrimitive3D : public CSGShape3D {
214
GDCLASS(CSGPrimitive3D, CSGShape3D);
215
216
protected:
217
bool flip_faces;
218
CSGBrush *_create_brush_from_arrays(const Vector<Vector3> &p_vertices, const Vector<Vector2> &p_uv, const Vector<bool> &p_smooth, const Vector<Ref<Material>> &p_materials);
219
static void _bind_methods();
220
221
public:
222
void set_flip_faces(bool p_invert);
223
bool get_flip_faces();
224
225
CSGPrimitive3D();
226
};
227
228
class CSGMesh3D : public CSGPrimitive3D {
229
GDCLASS(CSGMesh3D, CSGPrimitive3D);
230
231
virtual CSGBrush *_build_brush() override;
232
233
Ref<Mesh> mesh;
234
Ref<Material> material;
235
236
void _mesh_changed();
237
238
protected:
239
static void _bind_methods();
240
241
public:
242
void set_mesh(const Ref<Mesh> &p_mesh);
243
Ref<Mesh> get_mesh();
244
245
void set_material(const Ref<Material> &p_material);
246
Ref<Material> get_material() const;
247
};
248
249
class CSGSphere3D : public CSGPrimitive3D {
250
GDCLASS(CSGSphere3D, CSGPrimitive3D);
251
virtual CSGBrush *_build_brush() override;
252
253
Ref<Material> material;
254
bool smooth_faces;
255
float radius;
256
int radial_segments;
257
int rings;
258
259
protected:
260
static void _bind_methods();
261
262
public:
263
void set_radius(const float p_radius);
264
float get_radius() const;
265
266
void set_radial_segments(const int p_radial_segments);
267
int get_radial_segments() const;
268
269
void set_rings(const int p_rings);
270
int get_rings() const;
271
272
void set_material(const Ref<Material> &p_material);
273
Ref<Material> get_material() const;
274
275
void set_smooth_faces(bool p_smooth_faces);
276
bool get_smooth_faces() const;
277
278
CSGSphere3D();
279
};
280
281
class CSGBox3D : public CSGPrimitive3D {
282
GDCLASS(CSGBox3D, CSGPrimitive3D);
283
virtual CSGBrush *_build_brush() override;
284
285
Ref<Material> material;
286
Vector3 size = Vector3(1, 1, 1);
287
288
protected:
289
static void _bind_methods();
290
#ifndef DISABLE_DEPRECATED
291
// Kept for compatibility from 3.x to 4.0.
292
bool _set(const StringName &p_name, const Variant &p_value);
293
#endif
294
295
public:
296
void set_size(const Vector3 &p_size);
297
Vector3 get_size() const;
298
299
void set_material(const Ref<Material> &p_material);
300
Ref<Material> get_material() const;
301
302
CSGBox3D() {}
303
};
304
305
class CSGCylinder3D : public CSGPrimitive3D {
306
GDCLASS(CSGCylinder3D, CSGPrimitive3D);
307
virtual CSGBrush *_build_brush() override;
308
309
Ref<Material> material;
310
float radius;
311
float height;
312
int sides;
313
bool cone;
314
bool smooth_faces;
315
316
protected:
317
static void _bind_methods();
318
319
public:
320
void set_radius(const float p_radius);
321
float get_radius() const;
322
323
void set_height(const float p_height);
324
float get_height() const;
325
326
void set_sides(const int p_sides);
327
int get_sides() const;
328
329
void set_cone(const bool p_cone);
330
bool is_cone() const;
331
332
void set_smooth_faces(bool p_smooth_faces);
333
bool get_smooth_faces() const;
334
335
void set_material(const Ref<Material> &p_material);
336
Ref<Material> get_material() const;
337
338
CSGCylinder3D();
339
};
340
341
class CSGTorus3D : public CSGPrimitive3D {
342
GDCLASS(CSGTorus3D, CSGPrimitive3D);
343
virtual CSGBrush *_build_brush() override;
344
345
Ref<Material> material;
346
float inner_radius;
347
float outer_radius;
348
int sides;
349
int ring_sides;
350
bool smooth_faces;
351
352
protected:
353
static void _bind_methods();
354
355
public:
356
void set_inner_radius(const float p_inner_radius);
357
float get_inner_radius() const;
358
359
void set_outer_radius(const float p_outer_radius);
360
float get_outer_radius() const;
361
362
void set_sides(const int p_sides);
363
int get_sides() const;
364
365
void set_ring_sides(const int p_ring_sides);
366
int get_ring_sides() const;
367
368
void set_smooth_faces(bool p_smooth_faces);
369
bool get_smooth_faces() const;
370
371
void set_material(const Ref<Material> &p_material);
372
Ref<Material> get_material() const;
373
374
CSGTorus3D();
375
};
376
377
class CSGPolygon3D : public CSGPrimitive3D {
378
GDCLASS(CSGPolygon3D, CSGPrimitive3D);
379
380
public:
381
enum Mode {
382
MODE_DEPTH,
383
MODE_SPIN,
384
MODE_PATH
385
};
386
387
enum PathIntervalType {
388
PATH_INTERVAL_DISTANCE,
389
PATH_INTERVAL_SUBDIVIDE
390
};
391
392
enum PathRotation {
393
PATH_ROTATION_POLYGON,
394
PATH_ROTATION_PATH,
395
PATH_ROTATION_PATH_FOLLOW,
396
};
397
398
private:
399
virtual CSGBrush *_build_brush() override;
400
401
Vector<Vector2> polygon;
402
Ref<Material> material;
403
404
Mode mode;
405
406
float depth;
407
408
float spin_degrees;
409
int spin_sides;
410
411
NodePath path_node;
412
PathIntervalType path_interval_type;
413
float path_interval;
414
float path_simplify_angle;
415
PathRotation path_rotation;
416
bool path_rotation_accurate;
417
bool path_local;
418
419
Path3D *path = nullptr;
420
421
bool smooth_faces;
422
bool path_continuous_u;
423
real_t path_u_distance;
424
bool path_joined;
425
426
bool _is_editable_3d_polygon() const;
427
bool _has_editable_3d_polygon_no_depth() const;
428
429
void _path_changed();
430
void _path_exited();
431
432
protected:
433
static void _bind_methods();
434
void _validate_property(PropertyInfo &p_property) const;
435
void _notification(int p_what);
436
437
public:
438
void set_polygon(const Vector<Vector2> &p_polygon);
439
Vector<Vector2> get_polygon() const;
440
441
void set_mode(Mode p_mode);
442
Mode get_mode() const;
443
444
void set_depth(float p_depth);
445
float get_depth() const;
446
447
void set_spin_degrees(float p_spin_degrees);
448
float get_spin_degrees() const;
449
450
void set_spin_sides(int p_spin_sides);
451
int get_spin_sides() const;
452
453
void set_path_node(const NodePath &p_path);
454
NodePath get_path_node() const;
455
456
void set_path_interval_type(PathIntervalType p_interval_type);
457
PathIntervalType get_path_interval_type() const;
458
459
void set_path_interval(float p_interval);
460
float get_path_interval() const;
461
462
void set_path_simplify_angle(float p_angle);
463
float get_path_simplify_angle() const;
464
465
void set_path_rotation(PathRotation p_rotation);
466
PathRotation get_path_rotation() const;
467
468
void set_path_rotation_accurate(bool p_enable);
469
bool get_path_rotation_accurate() const;
470
471
void set_path_local(bool p_enable);
472
bool is_path_local() const;
473
474
void set_path_continuous_u(bool p_enable);
475
bool is_path_continuous_u() const;
476
477
void set_path_u_distance(real_t p_path_u_distance);
478
real_t get_path_u_distance() const;
479
480
void set_path_joined(bool p_enable);
481
bool is_path_joined() const;
482
483
void set_smooth_faces(bool p_smooth_faces);
484
bool get_smooth_faces() const;
485
486
void set_material(const Ref<Material> &p_material);
487
Ref<Material> get_material() const;
488
489
CSGPolygon3D();
490
};
491
492
VARIANT_ENUM_CAST(CSGPolygon3D::Mode)
493
VARIANT_ENUM_CAST(CSGPolygon3D::PathRotation)
494
VARIANT_ENUM_CAST(CSGPolygon3D::PathIntervalType)
495
496