primitive_meshes.h 8.0 KB

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