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. #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. void set_collision_priority(real_t p_priority);
  126. real_t get_collision_priority() const;
  127. #ifndef DISABLE_DEPRECATED
  128. void set_snap(float p_snap);
  129. float get_snap() const;
  130. #endif // DISABLE_DEPRECATED
  131. void set_calculate_tangents(bool p_calculate_tangents);
  132. bool is_calculating_tangents() const;
  133. bool is_root_shape() const;
  134. Ref<ArrayMesh> bake_static_mesh();
  135. Ref<ConcavePolygonShape3D> bake_collision_shape();
  136. virtual Ref<TriangleMesh> generate_triangle_mesh() const override;
  137. private:
  138. static Callable _navmesh_source_geometry_parsing_callback;
  139. static RID _navmesh_source_geometry_parser;
  140. public:
  141. static void navmesh_parse_init();
  142. static void navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  143. CSGShape3D();
  144. ~CSGShape3D();
  145. };
  146. VARIANT_ENUM_CAST(CSGShape3D::Operation)
  147. class CSGCombiner3D : public CSGShape3D {
  148. GDCLASS(CSGCombiner3D, CSGShape3D);
  149. private:
  150. virtual CSGBrush *_build_brush() override;
  151. public:
  152. CSGCombiner3D();
  153. };
  154. class CSGPrimitive3D : public CSGShape3D {
  155. GDCLASS(CSGPrimitive3D, CSGShape3D);
  156. protected:
  157. bool flip_faces;
  158. 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);
  159. static void _bind_methods();
  160. public:
  161. void set_flip_faces(bool p_invert);
  162. bool get_flip_faces();
  163. CSGPrimitive3D();
  164. };
  165. class CSGMesh3D : public CSGPrimitive3D {
  166. GDCLASS(CSGMesh3D, CSGPrimitive3D);
  167. virtual CSGBrush *_build_brush() override;
  168. Ref<Mesh> mesh;
  169. Ref<Material> material;
  170. void _mesh_changed();
  171. protected:
  172. static void _bind_methods();
  173. public:
  174. void set_mesh(const Ref<Mesh> &p_mesh);
  175. Ref<Mesh> get_mesh();
  176. void set_material(const Ref<Material> &p_material);
  177. Ref<Material> get_material() const;
  178. };
  179. class CSGSphere3D : public CSGPrimitive3D {
  180. GDCLASS(CSGSphere3D, CSGPrimitive3D);
  181. virtual CSGBrush *_build_brush() override;
  182. Ref<Material> material;
  183. bool smooth_faces;
  184. float radius;
  185. int radial_segments;
  186. int rings;
  187. protected:
  188. static void _bind_methods();
  189. public:
  190. void set_radius(const float p_radius);
  191. float get_radius() const;
  192. void set_radial_segments(const int p_radial_segments);
  193. int get_radial_segments() const;
  194. void set_rings(const int p_rings);
  195. int get_rings() const;
  196. void set_material(const Ref<Material> &p_material);
  197. Ref<Material> get_material() const;
  198. void set_smooth_faces(bool p_smooth_faces);
  199. bool get_smooth_faces() const;
  200. CSGSphere3D();
  201. };
  202. class CSGBox3D : public CSGPrimitive3D {
  203. GDCLASS(CSGBox3D, CSGPrimitive3D);
  204. virtual CSGBrush *_build_brush() override;
  205. Ref<Material> material;
  206. Vector3 size = Vector3(1, 1, 1);
  207. protected:
  208. static void _bind_methods();
  209. #ifndef DISABLE_DEPRECATED
  210. // Kept for compatibility from 3.x to 4.0.
  211. bool _set(const StringName &p_name, const Variant &p_value);
  212. #endif
  213. public:
  214. void set_size(const Vector3 &p_size);
  215. Vector3 get_size() const;
  216. void set_material(const Ref<Material> &p_material);
  217. Ref<Material> get_material() const;
  218. CSGBox3D() {}
  219. };
  220. class CSGCylinder3D : public CSGPrimitive3D {
  221. GDCLASS(CSGCylinder3D, CSGPrimitive3D);
  222. virtual CSGBrush *_build_brush() override;
  223. Ref<Material> material;
  224. float radius;
  225. float height;
  226. int sides;
  227. bool cone;
  228. bool smooth_faces;
  229. protected:
  230. static void _bind_methods();
  231. public:
  232. void set_radius(const float p_radius);
  233. float get_radius() const;
  234. void set_height(const float p_height);
  235. float get_height() const;
  236. void set_sides(const int p_sides);
  237. int get_sides() const;
  238. void set_cone(const bool p_cone);
  239. bool is_cone() const;
  240. void set_smooth_faces(bool p_smooth_faces);
  241. bool get_smooth_faces() const;
  242. void set_material(const Ref<Material> &p_material);
  243. Ref<Material> get_material() const;
  244. CSGCylinder3D();
  245. };
  246. class CSGTorus3D : public CSGPrimitive3D {
  247. GDCLASS(CSGTorus3D, CSGPrimitive3D);
  248. virtual CSGBrush *_build_brush() override;
  249. Ref<Material> material;
  250. float inner_radius;
  251. float outer_radius;
  252. int sides;
  253. int ring_sides;
  254. bool smooth_faces;
  255. protected:
  256. static void _bind_methods();
  257. public:
  258. void set_inner_radius(const float p_inner_radius);
  259. float get_inner_radius() const;
  260. void set_outer_radius(const float p_outer_radius);
  261. float get_outer_radius() const;
  262. void set_sides(const int p_sides);
  263. int get_sides() const;
  264. void set_ring_sides(const int p_ring_sides);
  265. int get_ring_sides() const;
  266. void set_smooth_faces(bool p_smooth_faces);
  267. bool get_smooth_faces() const;
  268. void set_material(const Ref<Material> &p_material);
  269. Ref<Material> get_material() const;
  270. CSGTorus3D();
  271. };
  272. class CSGPolygon3D : public CSGPrimitive3D {
  273. GDCLASS(CSGPolygon3D, CSGPrimitive3D);
  274. public:
  275. enum Mode {
  276. MODE_DEPTH,
  277. MODE_SPIN,
  278. MODE_PATH
  279. };
  280. enum PathIntervalType {
  281. PATH_INTERVAL_DISTANCE,
  282. PATH_INTERVAL_SUBDIVIDE
  283. };
  284. enum PathRotation {
  285. PATH_ROTATION_POLYGON,
  286. PATH_ROTATION_PATH,
  287. PATH_ROTATION_PATH_FOLLOW,
  288. };
  289. private:
  290. virtual CSGBrush *_build_brush() override;
  291. Vector<Vector2> polygon;
  292. Ref<Material> material;
  293. Mode mode;
  294. float depth;
  295. float spin_degrees;
  296. int spin_sides;
  297. NodePath path_node;
  298. PathIntervalType path_interval_type;
  299. float path_interval;
  300. float path_simplify_angle;
  301. PathRotation path_rotation;
  302. bool path_rotation_accurate;
  303. bool path_local;
  304. Path3D *path = nullptr;
  305. bool smooth_faces;
  306. bool path_continuous_u;
  307. real_t path_u_distance;
  308. bool path_joined;
  309. bool _is_editable_3d_polygon() const;
  310. bool _has_editable_3d_polygon_no_depth() const;
  311. void _path_changed();
  312. void _path_exited();
  313. protected:
  314. static void _bind_methods();
  315. void _validate_property(PropertyInfo &p_property) const;
  316. void _notification(int p_what);
  317. public:
  318. void set_polygon(const Vector<Vector2> &p_polygon);
  319. Vector<Vector2> get_polygon() const;
  320. void set_mode(Mode p_mode);
  321. Mode get_mode() const;
  322. void set_depth(float p_depth);
  323. float get_depth() const;
  324. void set_spin_degrees(float p_spin_degrees);
  325. float get_spin_degrees() const;
  326. void set_spin_sides(int p_spin_sides);
  327. int get_spin_sides() const;
  328. void set_path_node(const NodePath &p_path);
  329. NodePath get_path_node() const;
  330. void set_path_interval_type(PathIntervalType p_interval_type);
  331. PathIntervalType get_path_interval_type() const;
  332. void set_path_interval(float p_interval);
  333. float get_path_interval() const;
  334. void set_path_simplify_angle(float p_angle);
  335. float get_path_simplify_angle() const;
  336. void set_path_rotation(PathRotation p_rotation);
  337. PathRotation get_path_rotation() const;
  338. void set_path_rotation_accurate(bool p_enable);
  339. bool get_path_rotation_accurate() const;
  340. void set_path_local(bool p_enable);
  341. bool is_path_local() const;
  342. void set_path_continuous_u(bool p_enable);
  343. bool is_path_continuous_u() const;
  344. void set_path_u_distance(real_t p_path_u_distance);
  345. real_t get_path_u_distance() const;
  346. void set_path_joined(bool p_enable);
  347. bool is_path_joined() const;
  348. void set_smooth_faces(bool p_smooth_faces);
  349. bool get_smooth_faces() const;
  350. void set_material(const Ref<Material> &p_material);
  351. Ref<Material> get_material() const;
  352. CSGPolygon3D();
  353. };
  354. VARIANT_ENUM_CAST(CSGPolygon3D::Mode)
  355. VARIANT_ENUM_CAST(CSGPolygon3D::PathRotation)
  356. VARIANT_ENUM_CAST(CSGPolygon3D::PathIntervalType)
  357. #endif // CSG_SHAPE_H