csg_shape.h 12 KB

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