csg_shape.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*************************************************************************/
  2. /* csg_shape.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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.h"
  35. #include "scene/3d/visual_instance.h"
  36. #include "scene/resources/concave_polygon_shape.h"
  37. #include "thirdparty/misc/mikktspace.h"
  38. class CSGShape : public GeometryInstance {
  39. GDCLASS(CSGShape, GeometryInstance);
  40. public:
  41. enum Operation {
  42. OPERATION_UNION,
  43. OPERATION_INTERSECTION,
  44. OPERATION_SUBTRACTION,
  45. };
  46. private:
  47. Operation operation;
  48. CSGShape *parent;
  49. CSGBrush *brush;
  50. AABB node_aabb;
  51. bool dirty;
  52. float snap;
  53. bool use_collision;
  54. uint32_t collision_layer;
  55. uint32_t collision_mask;
  56. Ref<ConcavePolygonShape> root_collision_shape;
  57. RID root_collision_instance;
  58. bool calculate_tangents;
  59. Ref<ArrayMesh> root_mesh;
  60. struct Vector3Hasher {
  61. _ALWAYS_INLINE_ uint32_t hash(const Vector3 &p_vec3) const {
  62. uint32_t h = hash_djb2_one_float(p_vec3.x);
  63. h = hash_djb2_one_float(p_vec3.y, h);
  64. h = hash_djb2_one_float(p_vec3.z, h);
  65. return h;
  66. }
  67. };
  68. struct ShapeUpdateSurface {
  69. PoolVector<Vector3> vertices;
  70. PoolVector<Vector3> normals;
  71. PoolVector<Vector2> uvs;
  72. PoolVector<float> tans;
  73. Ref<Material> material;
  74. int last_added;
  75. PoolVector<Vector3>::Write verticesw;
  76. PoolVector<Vector3>::Write normalsw;
  77. PoolVector<Vector2>::Write uvsw;
  78. PoolVector<float>::Write tansw;
  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. void _update_shape();
  89. protected:
  90. void _notification(int p_what);
  91. virtual CSGBrush *_build_brush() = 0;
  92. void _make_dirty();
  93. static void _bind_methods();
  94. friend class CSGCombiner;
  95. CSGBrush *_get_brush();
  96. virtual void _validate_property(PropertyInfo &property) const;
  97. public:
  98. Array get_meshes() const;
  99. void force_update_shape();
  100. void set_operation(Operation p_operation);
  101. Operation get_operation() const;
  102. virtual PoolVector<Vector3> get_brush_faces();
  103. virtual AABB get_aabb() const;
  104. virtual PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  105. void set_use_collision(bool p_enable);
  106. bool is_using_collision() const;
  107. void set_collision_layer(uint32_t p_layer);
  108. uint32_t get_collision_layer() const;
  109. void set_collision_mask(uint32_t p_mask);
  110. uint32_t get_collision_mask() const;
  111. void set_collision_layer_bit(int p_bit, bool p_value);
  112. bool get_collision_layer_bit(int p_bit) const;
  113. void set_collision_mask_bit(int p_bit, bool p_value);
  114. bool get_collision_mask_bit(int p_bit) const;
  115. void set_snap(float p_snap);
  116. float get_snap() const;
  117. void set_calculate_tangents(bool p_calculate_tangents);
  118. bool is_calculating_tangents() const;
  119. bool is_root_shape() const;
  120. CSGShape();
  121. ~CSGShape();
  122. };
  123. VARIANT_ENUM_CAST(CSGShape::Operation)
  124. class CSGCombiner : public CSGShape {
  125. GDCLASS(CSGCombiner, CSGShape);
  126. private:
  127. virtual CSGBrush *_build_brush();
  128. public:
  129. CSGCombiner();
  130. };
  131. class CSGPrimitive : public CSGShape {
  132. GDCLASS(CSGPrimitive, CSGShape);
  133. protected:
  134. bool invert_faces;
  135. CSGBrush *_create_brush_from_arrays(const PoolVector<Vector3> &p_vertices, const PoolVector<Vector2> &p_uv, const PoolVector<bool> &p_smooth, const PoolVector<Ref<Material>> &p_materials);
  136. static void _bind_methods();
  137. public:
  138. void set_invert_faces(bool p_invert);
  139. bool is_inverting_faces();
  140. CSGPrimitive();
  141. };
  142. class CSGMesh : public CSGPrimitive {
  143. GDCLASS(CSGMesh, CSGPrimitive);
  144. virtual CSGBrush *_build_brush();
  145. Ref<Mesh> mesh;
  146. Ref<Material> material;
  147. void _mesh_changed();
  148. protected:
  149. static void _bind_methods();
  150. public:
  151. void set_mesh(const Ref<Mesh> &p_mesh);
  152. Ref<Mesh> get_mesh();
  153. void set_material(const Ref<Material> &p_material);
  154. Ref<Material> get_material() const;
  155. };
  156. class CSGSphere : public CSGPrimitive {
  157. GDCLASS(CSGSphere, CSGPrimitive);
  158. virtual CSGBrush *_build_brush();
  159. Ref<Material> material;
  160. bool smooth_faces;
  161. float radius;
  162. int radial_segments;
  163. int rings;
  164. protected:
  165. static void _bind_methods();
  166. public:
  167. void set_radius(const float p_radius);
  168. float get_radius() const;
  169. void set_radial_segments(const int p_radial_segments);
  170. int get_radial_segments() const;
  171. void set_rings(const int p_rings);
  172. int get_rings() const;
  173. void set_material(const Ref<Material> &p_material);
  174. Ref<Material> get_material() const;
  175. void set_smooth_faces(bool p_smooth_faces);
  176. bool get_smooth_faces() const;
  177. CSGSphere();
  178. };
  179. class CSGBox : public CSGPrimitive {
  180. GDCLASS(CSGBox, CSGPrimitive);
  181. virtual CSGBrush *_build_brush();
  182. Ref<Material> material;
  183. float width;
  184. float height;
  185. float depth;
  186. protected:
  187. static void _bind_methods();
  188. public:
  189. void set_width(const float p_width);
  190. float get_width() const;
  191. void set_height(const float p_height);
  192. float get_height() const;
  193. void set_depth(const float p_depth);
  194. float get_depth() const;
  195. void set_material(const Ref<Material> &p_material);
  196. Ref<Material> get_material() const;
  197. CSGBox();
  198. };
  199. class CSGCylinder : public CSGPrimitive {
  200. GDCLASS(CSGCylinder, CSGPrimitive);
  201. virtual CSGBrush *_build_brush();
  202. Ref<Material> material;
  203. float radius;
  204. float height;
  205. int sides;
  206. bool cone;
  207. bool smooth_faces;
  208. protected:
  209. static void _bind_methods();
  210. public:
  211. void set_radius(const float p_radius);
  212. float get_radius() const;
  213. void set_height(const float p_height);
  214. float get_height() const;
  215. void set_sides(const int p_sides);
  216. int get_sides() const;
  217. void set_cone(const bool p_cone);
  218. bool is_cone() const;
  219. void set_smooth_faces(bool p_smooth_faces);
  220. bool get_smooth_faces() const;
  221. void set_material(const Ref<Material> &p_material);
  222. Ref<Material> get_material() const;
  223. CSGCylinder();
  224. };
  225. class CSGTorus : public CSGPrimitive {
  226. GDCLASS(CSGTorus, CSGPrimitive);
  227. virtual CSGBrush *_build_brush();
  228. Ref<Material> material;
  229. float inner_radius;
  230. float outer_radius;
  231. int sides;
  232. int ring_sides;
  233. bool smooth_faces;
  234. protected:
  235. static void _bind_methods();
  236. public:
  237. void set_inner_radius(const float p_inner_radius);
  238. float get_inner_radius() const;
  239. void set_outer_radius(const float p_outer_radius);
  240. float get_outer_radius() const;
  241. void set_sides(const int p_sides);
  242. int get_sides() const;
  243. void set_ring_sides(const int p_ring_sides);
  244. int get_ring_sides() const;
  245. void set_smooth_faces(bool p_smooth_faces);
  246. bool get_smooth_faces() const;
  247. void set_material(const Ref<Material> &p_material);
  248. Ref<Material> get_material() const;
  249. CSGTorus();
  250. };
  251. class CSGPolygon : public CSGPrimitive {
  252. GDCLASS(CSGPolygon, CSGPrimitive);
  253. public:
  254. enum Mode {
  255. MODE_DEPTH,
  256. MODE_SPIN,
  257. MODE_PATH
  258. };
  259. enum PathIntervalType {
  260. PATH_INTERVAL_DISTANCE,
  261. PATH_INTERVAL_SUBDIVIDE
  262. };
  263. enum PathRotation {
  264. PATH_ROTATION_POLYGON,
  265. PATH_ROTATION_PATH,
  266. PATH_ROTATION_PATH_FOLLOW,
  267. };
  268. private:
  269. virtual CSGBrush *_build_brush();
  270. Vector<Vector2> polygon;
  271. Ref<Material> material;
  272. Mode mode;
  273. float depth;
  274. float spin_degrees;
  275. int spin_sides;
  276. NodePath path_node;
  277. PathIntervalType path_interval_type;
  278. float path_interval;
  279. float path_simplify_angle;
  280. PathRotation path_rotation;
  281. bool path_local;
  282. Path *path;
  283. bool smooth_faces;
  284. bool path_continuous_u;
  285. real_t path_u_distance;
  286. bool path_joined;
  287. bool _is_editable_3d_polygon() const;
  288. bool _has_editable_3d_polygon_no_depth() const;
  289. void _path_changed();
  290. void _path_exited();
  291. protected:
  292. static void _bind_methods();
  293. virtual void _validate_property(PropertyInfo &property) const;
  294. void _notification(int p_what);
  295. public:
  296. void set_polygon(const Vector<Vector2> &p_polygon);
  297. Vector<Vector2> get_polygon() const;
  298. void set_mode(Mode p_mode);
  299. Mode get_mode() const;
  300. void set_depth(float p_depth);
  301. float get_depth() const;
  302. void set_spin_degrees(float p_spin_degrees);
  303. float get_spin_degrees() const;
  304. void set_spin_sides(int p_spin_sides);
  305. int get_spin_sides() const;
  306. void set_path_node(const NodePath &p_path);
  307. NodePath get_path_node() const;
  308. void set_path_interval_type(PathIntervalType p_interval_type);
  309. PathIntervalType get_path_interval_type() const;
  310. void set_path_interval(float p_interval);
  311. float get_path_interval() const;
  312. void set_path_simplify_angle(float p_angle);
  313. float get_path_simplify_angle() const;
  314. void set_path_rotation(PathRotation p_rotation);
  315. PathRotation get_path_rotation() const;
  316. void set_path_local(bool p_enable);
  317. bool is_path_local() const;
  318. void set_path_continuous_u(bool p_enable);
  319. bool is_path_continuous_u() const;
  320. void set_path_u_distance(real_t p_path_u_distance);
  321. real_t get_path_u_distance() const;
  322. void set_path_joined(bool p_enable);
  323. bool is_path_joined() const;
  324. void set_smooth_faces(bool p_smooth_faces);
  325. bool get_smooth_faces() const;
  326. void set_material(const Ref<Material> &p_material);
  327. Ref<Material> get_material() const;
  328. CSGPolygon();
  329. };
  330. VARIANT_ENUM_CAST(CSGPolygon::Mode)
  331. VARIANT_ENUM_CAST(CSGPolygon::PathRotation)
  332. VARIANT_ENUM_CAST(CSGPolygon::PathIntervalType)
  333. #endif // CSG_SHAPE_H