mesh.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* mesh.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. OBJ_TYPE(Mesh, Resource);
  42. RES_BASE_EXTENSION("msh");
  43. public:
  44. enum {
  45. NO_INDEX_ARRAY = VisualServer::NO_INDEX_ARRAY,
  46. ARRAY_WEIGHTS_SIZE = VisualServer::ARRAY_WEIGHTS_SIZE
  47. };
  48. enum ArrayType {
  49. ARRAY_VERTEX = VisualServer::ARRAY_VERTEX,
  50. ARRAY_NORMAL = VisualServer::ARRAY_NORMAL,
  51. ARRAY_TANGENT = VisualServer::ARRAY_TANGENT,
  52. ARRAY_COLOR = VisualServer::ARRAY_COLOR,
  53. ARRAY_TEX_UV = VisualServer::ARRAY_TEX_UV,
  54. ARRAY_TEX_UV2 = VisualServer::ARRAY_TEX_UV2,
  55. ARRAY_BONES = VisualServer::ARRAY_BONES,
  56. ARRAY_WEIGHTS = VisualServer::ARRAY_WEIGHTS,
  57. ARRAY_INDEX = VisualServer::ARRAY_INDEX,
  58. ARRAY_MAX = VisualServer::ARRAY_MAX
  59. };
  60. enum ArrayFormat {
  61. /* ARRAY FORMAT FLAGS */
  62. ARRAY_FORMAT_VERTEX = 1 << ARRAY_VERTEX, // mandatory
  63. ARRAY_FORMAT_NORMAL = 1 << ARRAY_NORMAL,
  64. ARRAY_FORMAT_TANGENT = 1 << ARRAY_TANGENT,
  65. ARRAY_FORMAT_COLOR = 1 << ARRAY_COLOR,
  66. ARRAY_FORMAT_TEX_UV = 1 << ARRAY_TEX_UV,
  67. ARRAY_FORMAT_TEX_UV2 = 1 << ARRAY_TEX_UV2,
  68. ARRAY_FORMAT_BONES = 1 << ARRAY_BONES,
  69. ARRAY_FORMAT_WEIGHTS = 1 << ARRAY_WEIGHTS,
  70. ARRAY_FORMAT_INDEX = 1 << ARRAY_INDEX,
  71. };
  72. enum PrimitiveType {
  73. PRIMITIVE_POINTS = VisualServer::PRIMITIVE_POINTS,
  74. PRIMITIVE_LINES = VisualServer::PRIMITIVE_LINES,
  75. PRIMITIVE_LINE_STRIP = VisualServer::PRIMITIVE_LINE_STRIP,
  76. PRIMITIVE_LINE_LOOP = VisualServer::PRIMITIVE_LINE_LOOP,
  77. PRIMITIVE_TRIANGLES = VisualServer::PRIMITIVE_TRIANGLES,
  78. PRIMITIVE_TRIANGLE_STRIP = VisualServer::PRIMITIVE_TRIANGLE_STRIP,
  79. PRIMITIVE_TRIANGLE_FAN = VisualServer::PRIMITIVE_TRIANGLE_FAN,
  80. };
  81. enum MorphTargetMode {
  82. MORPH_MODE_NORMALIZED = VS::MORPH_MODE_NORMALIZED,
  83. MORPH_MODE_RELATIVE = VS::MORPH_MODE_RELATIVE,
  84. };
  85. private:
  86. struct Surface {
  87. String name;
  88. AABB aabb;
  89. bool alphasort;
  90. Ref<Material> material;
  91. };
  92. Vector<Surface> surfaces;
  93. RID mesh;
  94. AABB aabb;
  95. MorphTargetMode morph_target_mode;
  96. Vector<StringName> morph_targets;
  97. AABB custom_aabb;
  98. mutable Ref<TriangleMesh> triangle_mesh;
  99. void _recompute_aabb();
  100. protected:
  101. bool _set(const StringName &p_name, const Variant &p_value);
  102. bool _get(const StringName &p_name, Variant &r_ret) const;
  103. void _get_property_list(List<PropertyInfo> *p_list) const;
  104. static void _bind_methods();
  105. public:
  106. void add_surface(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes = Array(), bool p_alphasort = false);
  107. Array surface_get_arrays(int p_surface) const;
  108. virtual Array surface_get_morph_arrays(int p_surface) const;
  109. void add_custom_surface(const Variant &p_data); //only recognized by driver
  110. void add_morph_target(const StringName &p_name);
  111. int get_morph_target_count() const;
  112. StringName get_morph_target_name(int p_index) const;
  113. void clear_morph_targets();
  114. void set_morph_target_mode(MorphTargetMode p_mode);
  115. MorphTargetMode get_morph_target_mode() const;
  116. int get_surface_count() const;
  117. void surface_remove(int p_idx);
  118. void surface_set_custom_aabb(int p_surface, const AABB &p_aabb); //only recognized by driver
  119. int surface_get_array_len(int p_idx) const;
  120. int surface_get_array_index_len(int p_idx) const;
  121. uint32_t surface_get_format(int p_idx) const;
  122. PrimitiveType surface_get_primitive_type(int p_idx) const;
  123. bool surface_is_alpha_sorting_enabled(int p_idx) const;
  124. void surface_set_material(int p_idx, const Ref<Material> &p_material);
  125. Ref<Material> surface_get_material(int p_idx) const;
  126. void surface_set_name(int p_idx, const String &p_name);
  127. String surface_get_name(int p_idx) const;
  128. void add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data);
  129. void set_custom_aabb(const AABB &p_custom);
  130. AABB get_custom_aabb() const;
  131. AABB get_aabb() const;
  132. virtual RID get_rid() const;
  133. Ref<Shape> create_trimesh_shape() const;
  134. Ref<Shape> create_convex_shape() const;
  135. Ref<Mesh> create_outline(float p_margin) const;
  136. void center_geometry();
  137. void regen_normalmaps();
  138. DVector<Face3> get_faces() const;
  139. Ref<TriangleMesh> generate_triangle_mesh() const;
  140. Mesh();
  141. ~Mesh();
  142. };
  143. VARIANT_ENUM_CAST(Mesh::ArrayType);
  144. VARIANT_ENUM_CAST(Mesh::PrimitiveType);
  145. VARIANT_ENUM_CAST(Mesh::MorphTargetMode);
  146. #endif