csg_shape.h 12 KB

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