csg_shape.h 13 KB

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