csg_shape.h 11 KB

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