csg_shape.h 12 KB

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