mesh_instance.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**************************************************************************/
  2. /* mesh_instance.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_INSTANCE_H
  31. #define MESH_INSTANCE_H
  32. #include "scene/3d/skeleton.h"
  33. #include "scene/3d/visual_instance.h"
  34. #include "scene/resources/mesh.h"
  35. #include "scene/resources/skin.h"
  36. #include "core/local_vector.h"
  37. class MeshInstance : public GeometryInstance {
  38. GDCLASS(MeshInstance, GeometryInstance);
  39. protected:
  40. Ref<Mesh> mesh;
  41. Ref<Skin> skin;
  42. Ref<Skin> skin_internal;
  43. Ref<SkinReference> skin_ref;
  44. NodePath skeleton_path;
  45. struct SoftwareSkinning {
  46. enum Flags {
  47. // Data flags.
  48. FLAG_TRANSFORM_NORMALS = 1 << 0,
  49. // Runtime flags.
  50. FLAG_BONES_READY = 1 << 1,
  51. };
  52. struct SurfaceData {
  53. PoolByteArray source_buffer;
  54. uint32_t source_format;
  55. PoolByteArray buffer;
  56. PoolByteArray::Write buffer_write;
  57. bool transform_tangents;
  58. bool ensure_correct_normals;
  59. };
  60. Ref<Mesh> mesh_instance;
  61. LocalVector<SurfaceData> surface_data;
  62. };
  63. SoftwareSkinning *software_skinning;
  64. uint32_t software_skinning_flags;
  65. struct BlendShapeTrack {
  66. int idx;
  67. float value;
  68. BlendShapeTrack() {
  69. idx = 0;
  70. value = 0;
  71. }
  72. };
  73. Map<StringName, BlendShapeTrack> blend_shape_tracks;
  74. Vector<Ref<Material>> materials;
  75. void _mesh_changed();
  76. void _resolve_skeleton_path();
  77. bool _is_software_skinning_enabled() const;
  78. static bool _is_global_software_skinning_enabled();
  79. void _initialize_skinning(bool p_force_reset = false, bool p_call_attach_skeleton = true);
  80. void _update_skinning();
  81. private:
  82. // merging
  83. bool _merge_meshes(Vector<MeshInstance *> p_list, bool p_use_global_space, bool p_check_compatibility);
  84. bool _is_mergeable_with(const MeshInstance &p_other) const;
  85. void _merge_into_mesh_data(const MeshInstance &p_mi, const Transform &p_dest_tr_inv, int p_surface_id, LocalVector<Vector3> &r_verts, LocalVector<Vector3> &r_norms, LocalVector<real_t> &r_tangents, LocalVector<Color> &r_colors, LocalVector<Vector2> &r_uvs, LocalVector<Vector2> &r_uv2s, LocalVector<int> &r_inds);
  86. bool _ensure_indices_valid(LocalVector<int> &r_indices, const PoolVector<Vector3> &p_verts) const;
  87. bool _check_for_valid_indices(const LocalVector<int> &p_inds, const PoolVector<Vector3> &p_verts, LocalVector<int> *r_inds) const;
  88. bool _triangle_is_degenerate(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c, real_t p_epsilon) const;
  89. void _merge_log(String p_string) const;
  90. protected:
  91. bool _set(const StringName &p_name, const Variant &p_value);
  92. bool _get(const StringName &p_name, Variant &r_ret) const;
  93. void _get_property_list(List<PropertyInfo> *p_list) const;
  94. void _notification(int p_what);
  95. static void _bind_methods();
  96. public:
  97. void set_mesh(const Ref<Mesh> &p_mesh);
  98. Ref<Mesh> get_mesh() const;
  99. void set_skin(const Ref<Skin> &p_skin);
  100. Ref<Skin> get_skin() const;
  101. void set_skeleton_path(const NodePath &p_skeleton);
  102. NodePath get_skeleton_path();
  103. int get_surface_material_count() const;
  104. void set_surface_material(int p_surface, const Ref<Material> &p_material);
  105. Ref<Material> get_surface_material(int p_surface) const;
  106. Ref<Material> get_active_material(int p_surface) const;
  107. virtual void set_material_override(const Ref<Material> &p_material);
  108. virtual void set_material_overlay(const Ref<Material> &p_material);
  109. void set_software_skinning_transform_normals(bool p_enabled);
  110. bool is_software_skinning_transform_normals_enabled() const;
  111. Node *create_trimesh_collision_node();
  112. void create_trimesh_collision();
  113. Node *create_multiple_convex_collisions_node();
  114. void create_multiple_convex_collisions();
  115. Node *create_convex_collision_node(bool p_clean = true, bool p_simplify = false);
  116. void create_convex_collision(bool p_clean = true, bool p_simplify = false);
  117. void create_debug_tangents();
  118. // merging
  119. bool is_mergeable_with(Node *p_other) const;
  120. bool merge_meshes(Vector<Variant> p_list, bool p_use_global_space, bool p_check_compatibility);
  121. virtual AABB get_aabb() const;
  122. virtual PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  123. MeshInstance();
  124. ~MeshInstance();
  125. };
  126. #endif // MESH_INSTANCE_H