mesh.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*************************************************************************/
  2. /* mesh.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 MESH_H
  31. #define MESH_H
  32. #include "resource.h"
  33. #include "scene/resources/material.h"
  34. #include "scene/resources/shape.h"
  35. #include "servers/visual_server.h"
  36. #include "triangle_mesh.h"
  37. /**
  38. @author Juan Linietsky <reduzio@gmail.com>
  39. */
  40. class Mesh : public Resource {
  41. GDCLASS(Mesh, Resource);
  42. mutable Ref<TriangleMesh> triangle_mesh; //cached
  43. protected:
  44. void _clear_triangle_mesh() const;
  45. static void _bind_methods();
  46. public:
  47. enum {
  48. NO_INDEX_ARRAY = VisualServer::NO_INDEX_ARRAY,
  49. ARRAY_WEIGHTS_SIZE = VisualServer::ARRAY_WEIGHTS_SIZE
  50. };
  51. enum ArrayType {
  52. ARRAY_VERTEX = VisualServer::ARRAY_VERTEX,
  53. ARRAY_NORMAL = VisualServer::ARRAY_NORMAL,
  54. ARRAY_TANGENT = VisualServer::ARRAY_TANGENT,
  55. ARRAY_COLOR = VisualServer::ARRAY_COLOR,
  56. ARRAY_TEX_UV = VisualServer::ARRAY_TEX_UV,
  57. ARRAY_TEX_UV2 = VisualServer::ARRAY_TEX_UV2,
  58. ARRAY_BONES = VisualServer::ARRAY_BONES,
  59. ARRAY_WEIGHTS = VisualServer::ARRAY_WEIGHTS,
  60. ARRAY_INDEX = VisualServer::ARRAY_INDEX,
  61. ARRAY_MAX = VisualServer::ARRAY_MAX
  62. };
  63. enum ArrayFormat {
  64. /* ARRAY FORMAT FLAGS */
  65. ARRAY_FORMAT_VERTEX = 1 << ARRAY_VERTEX, // mandatory
  66. ARRAY_FORMAT_NORMAL = 1 << ARRAY_NORMAL,
  67. ARRAY_FORMAT_TANGENT = 1 << ARRAY_TANGENT,
  68. ARRAY_FORMAT_COLOR = 1 << ARRAY_COLOR,
  69. ARRAY_FORMAT_TEX_UV = 1 << ARRAY_TEX_UV,
  70. ARRAY_FORMAT_TEX_UV2 = 1 << ARRAY_TEX_UV2,
  71. ARRAY_FORMAT_BONES = 1 << ARRAY_BONES,
  72. ARRAY_FORMAT_WEIGHTS = 1 << ARRAY_WEIGHTS,
  73. ARRAY_FORMAT_INDEX = 1 << ARRAY_INDEX,
  74. ARRAY_COMPRESS_BASE = (ARRAY_INDEX + 1),
  75. ARRAY_COMPRESS_VERTEX = 1 << (ARRAY_VERTEX + ARRAY_COMPRESS_BASE), // mandatory
  76. ARRAY_COMPRESS_NORMAL = 1 << (ARRAY_NORMAL + ARRAY_COMPRESS_BASE),
  77. ARRAY_COMPRESS_TANGENT = 1 << (ARRAY_TANGENT + ARRAY_COMPRESS_BASE),
  78. ARRAY_COMPRESS_COLOR = 1 << (ARRAY_COLOR + ARRAY_COMPRESS_BASE),
  79. ARRAY_COMPRESS_TEX_UV = 1 << (ARRAY_TEX_UV + ARRAY_COMPRESS_BASE),
  80. ARRAY_COMPRESS_TEX_UV2 = 1 << (ARRAY_TEX_UV2 + ARRAY_COMPRESS_BASE),
  81. ARRAY_COMPRESS_BONES = 1 << (ARRAY_BONES + ARRAY_COMPRESS_BASE),
  82. ARRAY_COMPRESS_WEIGHTS = 1 << (ARRAY_WEIGHTS + ARRAY_COMPRESS_BASE),
  83. ARRAY_COMPRESS_INDEX = 1 << (ARRAY_INDEX + ARRAY_COMPRESS_BASE),
  84. ARRAY_FLAG_USE_2D_VERTICES = ARRAY_COMPRESS_INDEX << 1,
  85. ARRAY_FLAG_USE_16_BIT_BONES = ARRAY_COMPRESS_INDEX << 2,
  86. ARRAY_COMPRESS_DEFAULT = ARRAY_COMPRESS_VERTEX | ARRAY_COMPRESS_NORMAL | ARRAY_COMPRESS_TANGENT | ARRAY_COMPRESS_COLOR | ARRAY_COMPRESS_TEX_UV | ARRAY_COMPRESS_TEX_UV2 | ARRAY_COMPRESS_WEIGHTS
  87. };
  88. enum PrimitiveType {
  89. PRIMITIVE_POINTS = VisualServer::PRIMITIVE_POINTS,
  90. PRIMITIVE_LINES = VisualServer::PRIMITIVE_LINES,
  91. PRIMITIVE_LINE_STRIP = VisualServer::PRIMITIVE_LINE_STRIP,
  92. PRIMITIVE_LINE_LOOP = VisualServer::PRIMITIVE_LINE_LOOP,
  93. PRIMITIVE_TRIANGLES = VisualServer::PRIMITIVE_TRIANGLES,
  94. PRIMITIVE_TRIANGLE_STRIP = VisualServer::PRIMITIVE_TRIANGLE_STRIP,
  95. PRIMITIVE_TRIANGLE_FAN = VisualServer::PRIMITIVE_TRIANGLE_FAN,
  96. };
  97. enum BlendShapeMode {
  98. BLEND_SHAPE_MODE_NORMALIZED = VS::BLEND_SHAPE_MODE_NORMALIZED,
  99. BLEND_SHAPE_MODE_RELATIVE = VS::BLEND_SHAPE_MODE_RELATIVE,
  100. };
  101. virtual int get_surface_count() const = 0;
  102. virtual int surface_get_array_len(int p_idx) const = 0;
  103. virtual int surface_get_array_index_len(int p_idx) const = 0;
  104. virtual Array surface_get_arrays(int p_surface) const = 0;
  105. virtual Array surface_get_blend_shape_arrays(int p_surface) const = 0;
  106. virtual uint32_t surface_get_format(int p_idx) const = 0;
  107. virtual PrimitiveType surface_get_primitive_type(int p_idx) const = 0;
  108. virtual Ref<Material> surface_get_material(int p_idx) const = 0;
  109. virtual int get_blend_shape_count() const = 0;
  110. virtual StringName get_blend_shape_name(int p_index) const = 0;
  111. PoolVector<Face3> get_faces() const;
  112. Ref<TriangleMesh> generate_triangle_mesh() const;
  113. Ref<Shape> create_trimesh_shape() const;
  114. Ref<Shape> create_convex_shape() const;
  115. Ref<Mesh> create_outline(float p_margin) const;
  116. virtual Rect3 get_aabb() const = 0;
  117. Mesh();
  118. };
  119. class ArrayMesh : public Mesh {
  120. GDCLASS(ArrayMesh, Mesh);
  121. RES_BASE_EXTENSION("mesh");
  122. private:
  123. struct Surface {
  124. String name;
  125. Rect3 aabb;
  126. Ref<Material> material;
  127. bool is_2d;
  128. };
  129. Vector<Surface> surfaces;
  130. RID mesh;
  131. Rect3 aabb;
  132. BlendShapeMode blend_shape_mode;
  133. Vector<StringName> blend_shapes;
  134. Rect3 custom_aabb;
  135. void _recompute_aabb();
  136. protected:
  137. virtual bool _is_generated() const { return false; }
  138. bool _set(const StringName &p_name, const Variant &p_value);
  139. bool _get(const StringName &p_name, Variant &r_ret) const;
  140. void _get_property_list(List<PropertyInfo> *p_list) const;
  141. static void _bind_methods();
  142. public:
  143. void add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes = Array(), uint32_t p_flags = ARRAY_COMPRESS_DEFAULT);
  144. void add_surface(uint32_t p_format, PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const Rect3 &p_aabb, const Vector<PoolVector<uint8_t> > &p_blend_shapes = Vector<PoolVector<uint8_t> >(), const Vector<Rect3> &p_bone_aabbs = Vector<Rect3>());
  145. Array surface_get_arrays(int p_surface) const;
  146. Array surface_get_blend_shape_arrays(int p_surface) const;
  147. void add_blend_shape(const StringName &p_name);
  148. int get_blend_shape_count() const;
  149. StringName get_blend_shape_name(int p_index) const;
  150. void clear_blend_shapes();
  151. void set_blend_shape_mode(BlendShapeMode p_mode);
  152. BlendShapeMode get_blend_shape_mode() const;
  153. int get_surface_count() const;
  154. void surface_remove(int p_idx);
  155. void surface_set_custom_aabb(int p_idx, const Rect3 &p_aabb); //only recognized by driver
  156. int surface_get_array_len(int p_idx) const;
  157. int surface_get_array_index_len(int p_idx) const;
  158. uint32_t surface_get_format(int p_idx) const;
  159. PrimitiveType surface_get_primitive_type(int p_idx) const;
  160. bool surface_is_alpha_sorting_enabled(int p_idx) const;
  161. void surface_set_material(int p_idx, const Ref<Material> &p_material);
  162. Ref<Material> surface_get_material(int p_idx) const;
  163. void surface_set_name(int p_idx, const String &p_name);
  164. String surface_get_name(int p_idx) const;
  165. void add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data);
  166. void set_custom_aabb(const Rect3 &p_custom);
  167. Rect3 get_custom_aabb() const;
  168. Rect3 get_aabb() const;
  169. virtual RID get_rid() const;
  170. void center_geometry();
  171. void regen_normalmaps();
  172. ArrayMesh();
  173. ~ArrayMesh();
  174. };
  175. VARIANT_ENUM_CAST(Mesh::ArrayType);
  176. VARIANT_ENUM_CAST(Mesh::ArrayFormat);
  177. VARIANT_ENUM_CAST(Mesh::PrimitiveType);
  178. VARIANT_ENUM_CAST(Mesh::BlendShapeMode);
  179. #endif