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