primitive_meshes.h 7.9 KB

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