csg_shape.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. #pragma once
  31. #include "csg.h"
  32. #include "scene/3d/path_3d.h"
  33. #include "scene/3d/visual_instance_3d.h"
  34. #ifndef PHYSICS_3D_DISABLED
  35. #include "scene/resources/3d/concave_polygon_shape_3d.h"
  36. #endif // PHYSICS_3D_DISABLED
  37. #include "thirdparty/misc/mikktspace.h"
  38. class NavigationMesh;
  39. class NavigationMeshSourceGeometryData3D;
  40. class CSGShape3D : public GeometryInstance3D {
  41. GDCLASS(CSGShape3D, GeometryInstance3D);
  42. public:
  43. enum Operation {
  44. OPERATION_UNION,
  45. OPERATION_INTERSECTION,
  46. OPERATION_SUBTRACTION,
  47. };
  48. private:
  49. Operation operation = OPERATION_UNION;
  50. CSGShape3D *parent_shape = nullptr;
  51. CSGBrush *brush = nullptr;
  52. AABB node_aabb;
  53. bool dirty = false;
  54. bool last_visible = false;
  55. float snap = 0.001;
  56. #ifndef PHYSICS_3D_DISABLED
  57. bool use_collision = false;
  58. uint32_t collision_layer = 1;
  59. uint32_t collision_mask = 1;
  60. real_t collision_priority = 1.0;
  61. Ref<ConcavePolygonShape3D> root_collision_shape;
  62. RID root_collision_instance;
  63. RID root_collision_debug_instance;
  64. Transform3D debug_shape_old_transform;
  65. #endif // PHYSICS_3D_DISABLED
  66. bool calculate_tangents = true;
  67. Ref<ArrayMesh> root_mesh;
  68. struct ShapeUpdateSurface {
  69. Vector<Vector3> vertices;
  70. Vector<Vector3> normals;
  71. Vector<Vector2> uvs;
  72. Vector<real_t> tans;
  73. Ref<Material> material;
  74. int last_added = 0;
  75. Vector3 *verticesw = nullptr;
  76. Vector3 *normalsw = nullptr;
  77. Vector2 *uvsw = nullptr;
  78. real_t *tansw = nullptr;
  79. };
  80. //mikktspace callbacks
  81. static int mikktGetNumFaces(const SMikkTSpaceContext *pContext);
  82. static int mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace);
  83. static void mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert);
  84. static void mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert);
  85. static void mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert);
  86. static void mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  87. const tbool bIsOrientationPreserving, const int iFace, const int iVert);
  88. #ifndef PHYSICS_3D_DISABLED
  89. void _update_collision_faces();
  90. bool _is_debug_collision_shape_visible();
  91. void _update_debug_collision_shape();
  92. void _clear_debug_collision_shape();
  93. void _on_transform_changed();
  94. Vector<Vector3> _get_brush_collision_faces();
  95. #endif // PHYSICS_3D_DISABLED
  96. protected:
  97. void _notification(int p_what);
  98. virtual CSGBrush *_build_brush() = 0;
  99. void _make_dirty(bool p_parent_removing = false);
  100. PackedStringArray get_configuration_warnings() const override;
  101. static void _bind_methods();
  102. friend class CSGCombiner3D;
  103. CSGBrush *_get_brush();
  104. void _validate_property(PropertyInfo &p_property) const;
  105. public:
  106. Array get_meshes() const;
  107. void update_shape();
  108. void set_operation(Operation p_operation);
  109. Operation get_operation() const;
  110. virtual Vector<Vector3> get_brush_faces();
  111. virtual AABB get_aabb() const override;
  112. void set_use_collision(bool p_enable);
  113. bool is_using_collision() const;
  114. void set_collision_layer(uint32_t p_layer);
  115. uint32_t get_collision_layer() const;
  116. void set_collision_mask(uint32_t p_mask);
  117. uint32_t get_collision_mask() const;
  118. void set_collision_layer_value(int p_layer_number, bool p_value);
  119. bool get_collision_layer_value(int p_layer_number) const;
  120. void set_collision_mask_value(int p_layer_number, bool p_value);
  121. bool get_collision_mask_value(int p_layer_number) const;
  122. RID _get_root_collision_instance() const;
  123. void set_collision_priority(real_t p_priority);
  124. real_t get_collision_priority() const;
  125. #ifndef DISABLE_DEPRECATED
  126. void set_snap(float p_snap);
  127. float get_snap() const;
  128. #endif // DISABLE_DEPRECATED
  129. void set_calculate_tangents(bool p_calculate_tangents);
  130. bool is_calculating_tangents() const;
  131. bool is_root_shape() const;
  132. Ref<ArrayMesh> bake_static_mesh();
  133. #ifndef PHYSICS_3D_DISABLED
  134. Ref<ConcavePolygonShape3D> bake_collision_shape();
  135. #endif // PHYSICS_3D_DISABLED
  136. virtual Ref<TriangleMesh> generate_triangle_mesh() const override;
  137. #ifndef NAVIGATION_3D_DISABLED
  138. private:
  139. static Callable _navmesh_source_geometry_parsing_callback;
  140. static RID _navmesh_source_geometry_parser;
  141. public:
  142. static void navmesh_parse_init();
  143. static void navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  144. #endif // NAVIGATION_3D_DISABLED
  145. CSGShape3D();
  146. ~CSGShape3D();
  147. };
  148. VARIANT_ENUM_CAST(CSGShape3D::Operation)
  149. class CSGCombiner3D : public CSGShape3D {
  150. GDCLASS(CSGCombiner3D, CSGShape3D);
  151. private:
  152. virtual CSGBrush *_build_brush() override;
  153. public:
  154. CSGCombiner3D();
  155. };
  156. class CSGPrimitive3D : public CSGShape3D {
  157. GDCLASS(CSGPrimitive3D, CSGShape3D);
  158. protected:
  159. bool flip_faces;
  160. 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);
  161. static void _bind_methods();
  162. public:
  163. void set_flip_faces(bool p_invert);
  164. bool get_flip_faces();
  165. CSGPrimitive3D();
  166. };
  167. class CSGMesh3D : public CSGPrimitive3D {
  168. GDCLASS(CSGMesh3D, CSGPrimitive3D);
  169. virtual CSGBrush *_build_brush() override;
  170. Ref<Mesh> mesh;
  171. Ref<Material> material;
  172. void _mesh_changed();
  173. protected:
  174. static void _bind_methods();
  175. public:
  176. void set_mesh(const Ref<Mesh> &p_mesh);
  177. Ref<Mesh> get_mesh();
  178. void set_material(const Ref<Material> &p_material);
  179. Ref<Material> get_material() const;
  180. };
  181. class CSGSphere3D : public CSGPrimitive3D {
  182. GDCLASS(CSGSphere3D, CSGPrimitive3D);
  183. virtual CSGBrush *_build_brush() override;
  184. Ref<Material> material;
  185. bool smooth_faces;
  186. float radius;
  187. int radial_segments;
  188. int rings;
  189. protected:
  190. static void _bind_methods();
  191. public:
  192. void set_radius(const float p_radius);
  193. float get_radius() const;
  194. void set_radial_segments(const int p_radial_segments);
  195. int get_radial_segments() const;
  196. void set_rings(const int p_rings);
  197. int get_rings() const;
  198. void set_material(const Ref<Material> &p_material);
  199. Ref<Material> get_material() const;
  200. void set_smooth_faces(bool p_smooth_faces);
  201. bool get_smooth_faces() const;
  202. CSGSphere3D();
  203. };
  204. class CSGBox3D : public CSGPrimitive3D {
  205. GDCLASS(CSGBox3D, CSGPrimitive3D);
  206. virtual CSGBrush *_build_brush() override;
  207. Ref<Material> material;
  208. Vector3 size = Vector3(1, 1, 1);
  209. protected:
  210. static void _bind_methods();
  211. #ifndef DISABLE_DEPRECATED
  212. // Kept for compatibility from 3.x to 4.0.
  213. bool _set(const StringName &p_name, const Variant &p_value);
  214. #endif
  215. public:
  216. void set_size(const Vector3 &p_size);
  217. Vector3 get_size() const;
  218. void set_material(const Ref<Material> &p_material);
  219. Ref<Material> get_material() const;
  220. CSGBox3D() {}
  221. };
  222. class CSGCylinder3D : public CSGPrimitive3D {
  223. GDCLASS(CSGCylinder3D, CSGPrimitive3D);
  224. virtual CSGBrush *_build_brush() override;
  225. Ref<Material> material;
  226. float radius;
  227. float height;
  228. int sides;
  229. bool cone;
  230. bool smooth_faces;
  231. protected:
  232. static void _bind_methods();
  233. public:
  234. void set_radius(const float p_radius);
  235. float get_radius() const;
  236. void set_height(const float p_height);
  237. float get_height() const;
  238. void set_sides(const int p_sides);
  239. int get_sides() const;
  240. void set_cone(const bool p_cone);
  241. bool is_cone() const;
  242. void set_smooth_faces(bool p_smooth_faces);
  243. bool get_smooth_faces() const;
  244. void set_material(const Ref<Material> &p_material);
  245. Ref<Material> get_material() const;
  246. CSGCylinder3D();
  247. };
  248. class CSGTorus3D : public CSGPrimitive3D {
  249. GDCLASS(CSGTorus3D, CSGPrimitive3D);
  250. virtual CSGBrush *_build_brush() override;
  251. Ref<Material> material;
  252. float inner_radius;
  253. float outer_radius;
  254. int sides;
  255. int ring_sides;
  256. bool smooth_faces;
  257. protected:
  258. static void _bind_methods();
  259. public:
  260. void set_inner_radius(const float p_inner_radius);
  261. float get_inner_radius() const;
  262. void set_outer_radius(const float p_outer_radius);
  263. float get_outer_radius() const;
  264. void set_sides(const int p_sides);
  265. int get_sides() const;
  266. void set_ring_sides(const int p_ring_sides);
  267. int get_ring_sides() const;
  268. void set_smooth_faces(bool p_smooth_faces);
  269. bool get_smooth_faces() const;
  270. void set_material(const Ref<Material> &p_material);
  271. Ref<Material> get_material() const;
  272. CSGTorus3D();
  273. };
  274. class CSGPolygon3D : public CSGPrimitive3D {
  275. GDCLASS(CSGPolygon3D, CSGPrimitive3D);
  276. public:
  277. enum Mode {
  278. MODE_DEPTH,
  279. MODE_SPIN,
  280. MODE_PATH
  281. };
  282. enum PathIntervalType {
  283. PATH_INTERVAL_DISTANCE,
  284. PATH_INTERVAL_SUBDIVIDE
  285. };
  286. enum PathRotation {
  287. PATH_ROTATION_POLYGON,
  288. PATH_ROTATION_PATH,
  289. PATH_ROTATION_PATH_FOLLOW,
  290. };
  291. private:
  292. virtual CSGBrush *_build_brush() override;
  293. Vector<Vector2> polygon;
  294. Ref<Material> material;
  295. Mode mode;
  296. float depth;
  297. float spin_degrees;
  298. int spin_sides;
  299. NodePath path_node;
  300. PathIntervalType path_interval_type;
  301. float path_interval;
  302. float path_simplify_angle;
  303. PathRotation path_rotation;
  304. bool path_rotation_accurate;
  305. bool path_local;
  306. Path3D *path = nullptr;
  307. bool smooth_faces;
  308. bool path_continuous_u;
  309. real_t path_u_distance;
  310. bool path_joined;
  311. bool _is_editable_3d_polygon() const;
  312. bool _has_editable_3d_polygon_no_depth() const;
  313. void _path_changed();
  314. void _path_exited();
  315. protected:
  316. static void _bind_methods();
  317. void _validate_property(PropertyInfo &p_property) const;
  318. void _notification(int p_what);
  319. public:
  320. void set_polygon(const Vector<Vector2> &p_polygon);
  321. Vector<Vector2> get_polygon() const;
  322. void set_mode(Mode p_mode);
  323. Mode get_mode() const;
  324. void set_depth(float p_depth);
  325. float get_depth() const;
  326. void set_spin_degrees(float p_spin_degrees);
  327. float get_spin_degrees() const;
  328. void set_spin_sides(int p_spin_sides);
  329. int get_spin_sides() const;
  330. void set_path_node(const NodePath &p_path);
  331. NodePath get_path_node() const;
  332. void set_path_interval_type(PathIntervalType p_interval_type);
  333. PathIntervalType get_path_interval_type() const;
  334. void set_path_interval(float p_interval);
  335. float get_path_interval() const;
  336. void set_path_simplify_angle(float p_angle);
  337. float get_path_simplify_angle() const;
  338. void set_path_rotation(PathRotation p_rotation);
  339. PathRotation get_path_rotation() const;
  340. void set_path_rotation_accurate(bool p_enable);
  341. bool get_path_rotation_accurate() const;
  342. void set_path_local(bool p_enable);
  343. bool is_path_local() const;
  344. void set_path_continuous_u(bool p_enable);
  345. bool is_path_continuous_u() const;
  346. void set_path_u_distance(real_t p_path_u_distance);
  347. real_t get_path_u_distance() const;
  348. void set_path_joined(bool p_enable);
  349. bool is_path_joined() const;
  350. void set_smooth_faces(bool p_smooth_faces);
  351. bool get_smooth_faces() const;
  352. void set_material(const Ref<Material> &p_material);
  353. Ref<Material> get_material() const;
  354. CSGPolygon3D();
  355. };
  356. VARIANT_ENUM_CAST(CSGPolygon3D::Mode)
  357. VARIANT_ENUM_CAST(CSGPolygon3D::PathRotation)
  358. VARIANT_ENUM_CAST(CSGPolygon3D::PathIntervalType)