mesh.h 9.2 KB

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