primitive_meshes.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*************************************************************************/
  2. /* primitive_meshes.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 <mux213@gmail.com>
  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 Ref<Material> surface_get_material(int p_idx) const;
  63. virtual int get_blend_shape_count() const;
  64. virtual StringName get_blend_shape_name(int p_index) const;
  65. virtual AABB get_aabb() const;
  66. virtual RID get_rid() const;
  67. void set_material(const Ref<Material> &p_material);
  68. Ref<Material> get_material() const;
  69. Array get_mesh_arrays() const;
  70. void set_custom_aabb(const AABB &p_custom);
  71. AABB get_custom_aabb() const;
  72. void set_flip_faces(bool p_enable);
  73. bool get_flip_faces() const;
  74. PrimitiveMesh();
  75. ~PrimitiveMesh();
  76. };
  77. /**
  78. Mesh for a simple capsule
  79. */
  80. class CapsuleMesh : public PrimitiveMesh {
  81. GDCLASS(CapsuleMesh, PrimitiveMesh);
  82. private:
  83. float radius;
  84. float mid_height;
  85. int radial_segments;
  86. int rings;
  87. protected:
  88. static void _bind_methods();
  89. virtual void _create_mesh_array(Array &p_arr) const;
  90. public:
  91. void set_radius(const float p_radius);
  92. float get_radius() const;
  93. void set_mid_height(const float p_mid_height);
  94. float get_mid_height() const;
  95. void set_radial_segments(const int p_segments);
  96. int get_radial_segments() const;
  97. void set_rings(const int p_rings);
  98. int get_rings() const;
  99. CapsuleMesh();
  100. };
  101. /**
  102. Similar to test cube but with subdivision support and different texture coordinates
  103. */
  104. class CubeMesh : public PrimitiveMesh {
  105. GDCLASS(CubeMesh, PrimitiveMesh);
  106. private:
  107. Vector3 size;
  108. int subdivide_w;
  109. int subdivide_h;
  110. int subdivide_d;
  111. protected:
  112. static void _bind_methods();
  113. virtual void _create_mesh_array(Array &p_arr) const;
  114. public:
  115. void set_size(const Vector3 &p_size);
  116. Vector3 get_size() const;
  117. void set_subdivide_width(const int p_divisions);
  118. int get_subdivide_width() const;
  119. void set_subdivide_height(const int p_divisions);
  120. int get_subdivide_height() const;
  121. void set_subdivide_depth(const int p_divisions);
  122. int get_subdivide_depth() const;
  123. CubeMesh();
  124. };
  125. /**
  126. A cylinder
  127. */
  128. class CylinderMesh : public PrimitiveMesh {
  129. GDCLASS(CylinderMesh, PrimitiveMesh);
  130. private:
  131. float top_radius;
  132. float bottom_radius;
  133. float height;
  134. int radial_segments;
  135. int rings;
  136. protected:
  137. static void _bind_methods();
  138. virtual void _create_mesh_array(Array &p_arr) const;
  139. public:
  140. void set_top_radius(const float p_radius);
  141. float get_top_radius() const;
  142. void set_bottom_radius(const float p_radius);
  143. float get_bottom_radius() const;
  144. void set_height(const float p_height);
  145. float get_height() const;
  146. void set_radial_segments(const int p_segments);
  147. int get_radial_segments() const;
  148. void set_rings(const int p_rings);
  149. int get_rings() const;
  150. CylinderMesh();
  151. };
  152. /**
  153. Similar to quadmesh but with tessellation support
  154. */
  155. class PlaneMesh : public PrimitiveMesh {
  156. GDCLASS(PlaneMesh, PrimitiveMesh);
  157. private:
  158. Size2 size;
  159. int subdivide_w;
  160. int subdivide_d;
  161. protected:
  162. static void _bind_methods();
  163. virtual void _create_mesh_array(Array &p_arr) const;
  164. public:
  165. void set_size(const Size2 &p_size);
  166. Size2 get_size() const;
  167. void set_subdivide_width(const int p_divisions);
  168. int get_subdivide_width() const;
  169. void set_subdivide_depth(const int p_divisions);
  170. int get_subdivide_depth() const;
  171. PlaneMesh();
  172. };
  173. /**
  174. A prism shapen, handy for ramps, triangles, etc.
  175. */
  176. class PrismMesh : public PrimitiveMesh {
  177. GDCLASS(PrismMesh, PrimitiveMesh);
  178. private:
  179. float left_to_right;
  180. Vector3 size;
  181. int subdivide_w;
  182. int subdivide_h;
  183. int subdivide_d;
  184. protected:
  185. static void _bind_methods();
  186. virtual void _create_mesh_array(Array &p_arr) const;
  187. public:
  188. void set_left_to_right(const float p_left_to_right);
  189. float get_left_to_right() const;
  190. void set_size(const Vector3 &p_size);
  191. Vector3 get_size() const;
  192. void set_subdivide_width(const int p_divisions);
  193. int get_subdivide_width() const;
  194. void set_subdivide_height(const int p_divisions);
  195. int get_subdivide_height() const;
  196. void set_subdivide_depth(const int p_divisions);
  197. int get_subdivide_depth() const;
  198. PrismMesh();
  199. };
  200. /**
  201. Our original quadmesh...
  202. */
  203. class QuadMesh : public PrimitiveMesh {
  204. GDCLASS(QuadMesh, PrimitiveMesh)
  205. private:
  206. Size2 size;
  207. protected:
  208. static void _bind_methods();
  209. virtual void _create_mesh_array(Array &p_arr) const;
  210. public:
  211. QuadMesh();
  212. void set_size(const Size2 &p_size);
  213. Size2 get_size() const;
  214. };
  215. /**
  216. A sphere..
  217. */
  218. class SphereMesh : public PrimitiveMesh {
  219. GDCLASS(SphereMesh, PrimitiveMesh);
  220. private:
  221. float radius;
  222. float height;
  223. int radial_segments;
  224. int rings;
  225. bool is_hemisphere;
  226. protected:
  227. static void _bind_methods();
  228. virtual void _create_mesh_array(Array &p_arr) const;
  229. public:
  230. void set_radius(const float p_radius);
  231. float get_radius() const;
  232. void set_height(const float p_height);
  233. float get_height() const;
  234. void set_radial_segments(const int p_radial_segments);
  235. int get_radial_segments() const;
  236. void set_rings(const int p_rings);
  237. int get_rings() const;
  238. void set_is_hemisphere(const bool p_is_hemisphere);
  239. bool get_is_hemisphere() const;
  240. SphereMesh();
  241. };
  242. #endif