primitive_meshes.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*************************************************************************/
  2. /* primitive_meshes.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 PRIMITIVE_MESHES_H
  31. #define PRIMITIVE_MESHES_H
  32. #include "scene/resources/mesh.h"
  33. ///@TODO probably should change a few integers to unsigned integers...
  34. /**
  35. @author Bastiaan Olij <[email protected]>
  36. Base class for all the classes in this file, handles a number of code functions that are shared among all meshes.
  37. This class is set apart that it assumes a single surface is always generated for our mesh.
  38. */
  39. class PrimitiveMesh : public Mesh {
  40. GDCLASS(PrimitiveMesh, Mesh);
  41. private:
  42. RID mesh;
  43. mutable AABB aabb;
  44. AABB custom_aabb;
  45. Ref<Material> material;
  46. bool flip_faces;
  47. mutable bool pending_request;
  48. void _update() const;
  49. protected:
  50. Mesh::PrimitiveType primitive_type;
  51. static void _bind_methods();
  52. virtual void _create_mesh_array(Array &p_arr) const = 0;
  53. void _request_update();
  54. public:
  55. virtual int get_surface_count() const;
  56. virtual int surface_get_array_len(int p_idx) const;
  57. virtual int surface_get_array_index_len(int p_idx) const;
  58. virtual Array surface_get_arrays(int p_surface) const;
  59. virtual Array surface_get_blend_shape_arrays(int p_surface) const;
  60. virtual uint32_t surface_get_format(int p_idx) const;
  61. virtual Mesh::PrimitiveType surface_get_primitive_type(int p_idx) const;
  62. virtual void surface_set_material(int p_idx, const Ref<Material> &p_material);
  63. virtual Ref<Material> surface_get_material(int p_idx) const;
  64. virtual int get_blend_shape_count() const;
  65. virtual StringName get_blend_shape_name(int p_index) const;
  66. virtual AABB get_aabb() const;
  67. virtual RID get_rid() const;
  68. void set_material(const Ref<Material> &p_material);
  69. Ref<Material> get_material() const;
  70. Array get_mesh_arrays() const;
  71. void set_custom_aabb(const AABB &p_custom);
  72. AABB get_custom_aabb() const;
  73. void set_flip_faces(bool p_enable);
  74. bool get_flip_faces() const;
  75. PrimitiveMesh();
  76. ~PrimitiveMesh();
  77. };
  78. /**
  79. Mesh for a simple capsule
  80. */
  81. class CapsuleMesh : public PrimitiveMesh {
  82. GDCLASS(CapsuleMesh, PrimitiveMesh);
  83. private:
  84. float radius;
  85. float mid_height;
  86. int radial_segments;
  87. int rings;
  88. protected:
  89. static void _bind_methods();
  90. virtual void _create_mesh_array(Array &p_arr) const;
  91. public:
  92. void set_radius(const float p_radius);
  93. float get_radius() const;
  94. void set_mid_height(const float p_mid_height);
  95. float get_mid_height() const;
  96. void set_radial_segments(const int p_segments);
  97. int get_radial_segments() const;
  98. void set_rings(const int p_rings);
  99. int get_rings() const;
  100. CapsuleMesh();
  101. };
  102. /**
  103. Similar to test cube but with subdivision support and different texture coordinates
  104. */
  105. class CubeMesh : public PrimitiveMesh {
  106. GDCLASS(CubeMesh, PrimitiveMesh);
  107. private:
  108. Vector3 size;
  109. int subdivide_w;
  110. int subdivide_h;
  111. int subdivide_d;
  112. protected:
  113. static void _bind_methods();
  114. virtual void _create_mesh_array(Array &p_arr) const;
  115. public:
  116. void set_size(const Vector3 &p_size);
  117. Vector3 get_size() const;
  118. void set_subdivide_width(const int p_divisions);
  119. int get_subdivide_width() const;
  120. void set_subdivide_height(const int p_divisions);
  121. int get_subdivide_height() const;
  122. void set_subdivide_depth(const int p_divisions);
  123. int get_subdivide_depth() const;
  124. CubeMesh();
  125. };
  126. /**
  127. A cylinder
  128. */
  129. class CylinderMesh : public PrimitiveMesh {
  130. GDCLASS(CylinderMesh, PrimitiveMesh);
  131. private:
  132. float top_radius;
  133. float bottom_radius;
  134. float height;
  135. int radial_segments;
  136. int rings;
  137. protected:
  138. static void _bind_methods();
  139. virtual void _create_mesh_array(Array &p_arr) const;
  140. public:
  141. void set_top_radius(const float p_radius);
  142. float get_top_radius() const;
  143. void set_bottom_radius(const float p_radius);
  144. float get_bottom_radius() const;
  145. void set_height(const float p_height);
  146. float get_height() const;
  147. void set_radial_segments(const int p_segments);
  148. int get_radial_segments() const;
  149. void set_rings(const int p_rings);
  150. int get_rings() const;
  151. CylinderMesh();
  152. };
  153. /**
  154. Similar to quadmesh but with tessellation support
  155. */
  156. class PlaneMesh : public PrimitiveMesh {
  157. GDCLASS(PlaneMesh, PrimitiveMesh);
  158. private:
  159. Size2 size;
  160. int subdivide_w;
  161. int subdivide_d;
  162. protected:
  163. static void _bind_methods();
  164. virtual void _create_mesh_array(Array &p_arr) const;
  165. public:
  166. void set_size(const Size2 &p_size);
  167. Size2 get_size() const;
  168. void set_subdivide_width(const int p_divisions);
  169. int get_subdivide_width() const;
  170. void set_subdivide_depth(const int p_divisions);
  171. int get_subdivide_depth() const;
  172. PlaneMesh();
  173. };
  174. /**
  175. A prism shapen, handy for ramps, triangles, etc.
  176. */
  177. class PrismMesh : public PrimitiveMesh {
  178. GDCLASS(PrismMesh, PrimitiveMesh);
  179. private:
  180. float left_to_right;
  181. Vector3 size;
  182. int subdivide_w;
  183. int subdivide_h;
  184. int subdivide_d;
  185. protected:
  186. static void _bind_methods();
  187. virtual void _create_mesh_array(Array &p_arr) const;
  188. public:
  189. void set_left_to_right(const float p_left_to_right);
  190. float get_left_to_right() const;
  191. void set_size(const Vector3 &p_size);
  192. Vector3 get_size() const;
  193. void set_subdivide_width(const int p_divisions);
  194. int get_subdivide_width() const;
  195. void set_subdivide_height(const int p_divisions);
  196. int get_subdivide_height() const;
  197. void set_subdivide_depth(const int p_divisions);
  198. int get_subdivide_depth() const;
  199. PrismMesh();
  200. };
  201. /**
  202. Our original quadmesh...
  203. */
  204. class QuadMesh : public PrimitiveMesh {
  205. GDCLASS(QuadMesh, PrimitiveMesh);
  206. private:
  207. Size2 size;
  208. protected:
  209. static void _bind_methods();
  210. virtual void _create_mesh_array(Array &p_arr) const;
  211. public:
  212. QuadMesh();
  213. void set_size(const Size2 &p_size);
  214. Size2 get_size() const;
  215. };
  216. /**
  217. A sphere..
  218. */
  219. class SphereMesh : public PrimitiveMesh {
  220. GDCLASS(SphereMesh, PrimitiveMesh);
  221. private:
  222. float radius;
  223. float height;
  224. int radial_segments;
  225. int rings;
  226. bool is_hemisphere;
  227. protected:
  228. static void _bind_methods();
  229. virtual void _create_mesh_array(Array &p_arr) const;
  230. public:
  231. void set_radius(const float p_radius);
  232. float get_radius() const;
  233. void set_height(const float p_height);
  234. float get_height() const;
  235. void set_radial_segments(const int p_radial_segments);
  236. int get_radial_segments() const;
  237. void set_rings(const int p_rings);
  238. int get_rings() const;
  239. void set_is_hemisphere(const bool p_is_hemisphere);
  240. bool get_is_hemisphere() const;
  241. SphereMesh();
  242. };
  243. /**
  244. A single point for use in particle systems
  245. */
  246. class PointMesh : public PrimitiveMesh {
  247. GDCLASS(PointMesh, PrimitiveMesh)
  248. protected:
  249. virtual void _create_mesh_array(Array &p_arr) const;
  250. public:
  251. PointMesh();
  252. };
  253. #endif