csg_shape.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*************************************************************************/
  2. /* csg_shape.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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.h"
  35. #include "scene/resources/concave_polygon_shape.h"
  36. class CSGShape : public VisualInstance {
  37. GDCLASS(CSGShape, VisualInstance);
  38. public:
  39. enum Operation {
  40. OPERATION_UNION,
  41. OPERATION_INTERSECTION,
  42. OPERATION_SUBTRACTION,
  43. };
  44. private:
  45. Operation operation;
  46. CSGShape *parent;
  47. CSGBrush *brush;
  48. AABB node_aabb;
  49. bool dirty;
  50. float snap;
  51. bool use_collision;
  52. Ref<ConcavePolygonShape> root_collision_shape;
  53. RID root_collision_instance;
  54. Ref<ArrayMesh> root_mesh;
  55. struct Vector3Hasher {
  56. _ALWAYS_INLINE_ uint32_t hash(const Vector3 &p_vec3) const {
  57. uint32_t h = hash_djb2_one_float(p_vec3.x);
  58. h = hash_djb2_one_float(p_vec3.y, h);
  59. h = hash_djb2_one_float(p_vec3.z, h);
  60. return h;
  61. }
  62. };
  63. struct ShapeUpdateSurface {
  64. PoolVector<Vector3> vertices;
  65. PoolVector<Vector3> normals;
  66. PoolVector<Vector2> uvs;
  67. Ref<Material> material;
  68. int last_added;
  69. PoolVector<Vector3>::Write verticesw;
  70. PoolVector<Vector3>::Write normalsw;
  71. PoolVector<Vector2>::Write uvsw;
  72. };
  73. void _update_shape();
  74. protected:
  75. void _notification(int p_what);
  76. virtual CSGBrush *_build_brush() = 0;
  77. void _make_dirty();
  78. static void _bind_methods();
  79. friend class CSGCombiner;
  80. CSGBrush *_get_brush();
  81. virtual void _validate_property(PropertyInfo &property) const;
  82. public:
  83. void set_operation(Operation p_operation);
  84. Operation get_operation() const;
  85. virtual PoolVector<Vector3> get_brush_faces();
  86. virtual AABB get_aabb() const;
  87. virtual PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  88. void set_use_collision(bool p_enable);
  89. bool is_using_collision() const;
  90. void set_snap(float p_snap);
  91. float get_snap() const;
  92. bool is_root_shape() const;
  93. CSGShape();
  94. ~CSGShape();
  95. };
  96. VARIANT_ENUM_CAST(CSGShape::Operation)
  97. class CSGCombiner : public CSGShape {
  98. GDCLASS(CSGCombiner, CSGShape)
  99. private:
  100. virtual CSGBrush *_build_brush();
  101. public:
  102. CSGCombiner();
  103. };
  104. class CSGPrimitive : public CSGShape {
  105. GDCLASS(CSGPrimitive, CSGShape)
  106. private:
  107. bool invert_faces;
  108. protected:
  109. 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);
  110. static void _bind_methods();
  111. public:
  112. void set_invert_faces(bool p_invert);
  113. bool is_inverting_faces();
  114. CSGPrimitive();
  115. };
  116. class CSGMesh : public CSGPrimitive {
  117. GDCLASS(CSGMesh, CSGPrimitive)
  118. virtual CSGBrush *_build_brush();
  119. Ref<Mesh> mesh;
  120. void _mesh_changed();
  121. protected:
  122. static void _bind_methods();
  123. public:
  124. void set_mesh(const Ref<Mesh> &p_mesh);
  125. Ref<Mesh> get_mesh();
  126. };
  127. class CSGSphere : public CSGPrimitive {
  128. GDCLASS(CSGSphere, CSGPrimitive)
  129. virtual CSGBrush *_build_brush();
  130. Ref<Material> material;
  131. bool smooth_faces;
  132. float radius;
  133. int radial_segments;
  134. int rings;
  135. protected:
  136. static void _bind_methods();
  137. public:
  138. void set_radius(const float p_radius);
  139. float get_radius() const;
  140. void set_radial_segments(const int p_radial_segments);
  141. int get_radial_segments() const;
  142. void set_rings(const int p_rings);
  143. int get_rings() const;
  144. void set_material(const Ref<Material> &p_material);
  145. Ref<Material> get_material() const;
  146. void set_smooth_faces(bool p_smooth_faces);
  147. bool get_smooth_faces() const;
  148. CSGSphere();
  149. };
  150. class CSGBox : public CSGPrimitive {
  151. GDCLASS(CSGBox, CSGPrimitive)
  152. virtual CSGBrush *_build_brush();
  153. Ref<Material> material;
  154. float width;
  155. float height;
  156. float depth;
  157. protected:
  158. static void _bind_methods();
  159. public:
  160. void set_width(const float p_width);
  161. float get_width() const;
  162. void set_height(const float p_height);
  163. float get_height() const;
  164. void set_depth(const float p_depth);
  165. float get_depth() const;
  166. void set_material(const Ref<Material> &p_material);
  167. Ref<Material> get_material() const;
  168. CSGBox();
  169. };
  170. class CSGCylinder : public CSGPrimitive {
  171. GDCLASS(CSGCylinder, CSGPrimitive)
  172. virtual CSGBrush *_build_brush();
  173. Ref<Material> material;
  174. float radius;
  175. float height;
  176. int sides;
  177. bool cone;
  178. bool smooth_faces;
  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_height(const float p_height);
  185. float get_height() const;
  186. void set_sides(const int p_sides);
  187. int get_sides() const;
  188. void set_cone(const bool p_cone);
  189. bool is_cone() const;
  190. void set_smooth_faces(bool p_smooth_faces);
  191. bool get_smooth_faces() const;
  192. void set_material(const Ref<Material> &p_material);
  193. Ref<Material> get_material() const;
  194. CSGCylinder();
  195. };
  196. class CSGTorus : public CSGPrimitive {
  197. GDCLASS(CSGTorus, CSGPrimitive)
  198. virtual CSGBrush *_build_brush();
  199. Ref<Material> material;
  200. float inner_radius;
  201. float outer_radius;
  202. int sides;
  203. int ring_sides;
  204. bool smooth_faces;
  205. protected:
  206. static void _bind_methods();
  207. public:
  208. void set_inner_radius(const float p_inner_radius);
  209. float get_inner_radius() const;
  210. void set_outer_radius(const float p_outer_radius);
  211. float get_outer_radius() const;
  212. void set_sides(const int p_sides);
  213. int get_sides() const;
  214. void set_ring_sides(const int p_ring_sides);
  215. int get_ring_sides() const;
  216. void set_smooth_faces(bool p_smooth_faces);
  217. bool get_smooth_faces() const;
  218. void set_material(const Ref<Material> &p_material);
  219. Ref<Material> get_material() const;
  220. CSGTorus();
  221. };
  222. class CSGPolygon : public CSGPrimitive {
  223. GDCLASS(CSGPolygon, CSGPrimitive)
  224. public:
  225. enum Mode {
  226. MODE_DEPTH,
  227. MODE_SPIN,
  228. MODE_PATH
  229. };
  230. enum PathRotation {
  231. PATH_ROTATION_POLYGON,
  232. PATH_ROTATION_PATH,
  233. PATH_ROTATION_PATH_FOLLOW,
  234. };
  235. private:
  236. virtual CSGBrush *_build_brush();
  237. Vector<Vector2> polygon;
  238. Ref<Material> material;
  239. Mode mode;
  240. float depth;
  241. float spin_degrees;
  242. int spin_sides;
  243. NodePath path_node;
  244. float path_interval;
  245. PathRotation path_rotation;
  246. bool path_local;
  247. Node *path_cache;
  248. bool smooth_faces;
  249. bool path_continuous_u;
  250. bool path_joined;
  251. bool _is_editable_3d_polygon() const;
  252. bool _has_editable_3d_polygon_no_depth() const;
  253. void _path_changed();
  254. void _path_exited();
  255. protected:
  256. static void _bind_methods();
  257. virtual void _validate_property(PropertyInfo &property) const;
  258. void _notification(int p_what);
  259. public:
  260. void set_polygon(const Vector<Vector2> &p_polygon);
  261. Vector<Vector2> get_polygon() const;
  262. void set_mode(Mode p_mode);
  263. Mode get_mode() const;
  264. void set_depth(float p_depth);
  265. float get_depth() const;
  266. void set_spin_degrees(float p_spin_degrees);
  267. float get_spin_degrees() const;
  268. void set_spin_sides(int p_sides);
  269. int get_spin_sides() const;
  270. void set_path_node(const NodePath &p_path);
  271. NodePath get_path_node() const;
  272. void set_path_interval(float p_interval);
  273. float get_path_interval() const;
  274. void set_path_rotation(PathRotation p_rotation);
  275. PathRotation get_path_rotation() const;
  276. void set_path_local(bool p_enable);
  277. bool is_path_local() const;
  278. void set_path_continuous_u(bool p_enable);
  279. bool is_path_continuous_u() const;
  280. void set_path_joined(bool p_enable);
  281. bool is_path_joined() const;
  282. void set_smooth_faces(bool p_smooth_faces);
  283. bool get_smooth_faces() const;
  284. void set_material(const Ref<Material> &p_material);
  285. Ref<Material> get_material() const;
  286. CSGPolygon();
  287. };
  288. VARIANT_ENUM_CAST(CSGPolygon::Mode)
  289. VARIANT_ENUM_CAST(CSGPolygon::PathRotation)
  290. #endif // CSG_SHAPE_H