multimesh_instance_3d.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**************************************************************************/
  2. /* multimesh_instance_3d.cpp */
  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. #include "multimesh_instance_3d.h"
  31. #include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h"
  32. #include "scene/resources/navigation_mesh.h"
  33. #include "servers/navigation_server_3d.h"
  34. Callable MultiMeshInstance3D::_navmesh_source_geometry_parsing_callback;
  35. RID MultiMeshInstance3D::_navmesh_source_geometry_parser;
  36. void MultiMeshInstance3D::_refresh_interpolated() {
  37. if (is_inside_tree() && multimesh.is_valid()) {
  38. bool interpolated = is_physics_interpolated_and_enabled();
  39. multimesh->set_physics_interpolated(interpolated);
  40. }
  41. }
  42. void MultiMeshInstance3D::_physics_interpolated_changed() {
  43. VisualInstance3D::_physics_interpolated_changed();
  44. _refresh_interpolated();
  45. }
  46. void MultiMeshInstance3D::_bind_methods() {
  47. ClassDB::bind_method(D_METHOD("set_multimesh", "multimesh"), &MultiMeshInstance3D::set_multimesh);
  48. ClassDB::bind_method(D_METHOD("get_multimesh"), &MultiMeshInstance3D::get_multimesh);
  49. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multimesh", PROPERTY_HINT_RESOURCE_TYPE, "MultiMesh"), "set_multimesh", "get_multimesh");
  50. }
  51. void MultiMeshInstance3D::_notification(int p_what) {
  52. if (p_what == NOTIFICATION_ENTER_TREE) {
  53. _refresh_interpolated();
  54. }
  55. }
  56. void MultiMeshInstance3D::set_multimesh(const Ref<MultiMesh> &p_multimesh) {
  57. multimesh = p_multimesh;
  58. if (multimesh.is_valid()) {
  59. set_base(multimesh->get_rid());
  60. _refresh_interpolated();
  61. } else {
  62. set_base(RID());
  63. }
  64. }
  65. Ref<MultiMesh> MultiMeshInstance3D::get_multimesh() const {
  66. return multimesh;
  67. }
  68. Array MultiMeshInstance3D::get_meshes() const {
  69. if (multimesh.is_null() || multimesh->get_mesh().is_null() || multimesh->get_transform_format() != MultiMesh::TransformFormat::TRANSFORM_3D) {
  70. return Array();
  71. }
  72. int count = multimesh->get_visible_instance_count();
  73. if (count == -1) {
  74. count = multimesh->get_instance_count();
  75. }
  76. Ref<Mesh> mesh = multimesh->get_mesh();
  77. Array results;
  78. for (int i = 0; i < count; i++) {
  79. results.push_back(multimesh->get_instance_transform(i));
  80. results.push_back(mesh);
  81. }
  82. return results;
  83. }
  84. AABB MultiMeshInstance3D::get_aabb() const {
  85. if (multimesh.is_null()) {
  86. return AABB();
  87. } else {
  88. return multimesh->get_aabb();
  89. }
  90. }
  91. void MultiMeshInstance3D::navmesh_parse_init() {
  92. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  93. if (!_navmesh_source_geometry_parser.is_valid()) {
  94. _navmesh_source_geometry_parsing_callback = callable_mp_static(&MultiMeshInstance3D::navmesh_parse_source_geometry);
  95. _navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create();
  96. NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  97. }
  98. }
  99. void MultiMeshInstance3D::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) {
  100. MultiMeshInstance3D *multimesh_instance = Object::cast_to<MultiMeshInstance3D>(p_node);
  101. if (multimesh_instance == nullptr) {
  102. return;
  103. }
  104. NavigationMesh::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
  105. if (parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) {
  106. Ref<MultiMesh> multimesh = multimesh_instance->get_multimesh();
  107. if (multimesh.is_valid()) {
  108. Ref<Mesh> mesh = multimesh->get_mesh();
  109. if (mesh.is_valid()) {
  110. int n = multimesh->get_visible_instance_count();
  111. if (n == -1) {
  112. n = multimesh->get_instance_count();
  113. }
  114. for (int i = 0; i < n; i++) {
  115. p_source_geometry_data->add_mesh(mesh, multimesh_instance->get_global_transform() * multimesh->get_instance_transform(i));
  116. }
  117. }
  118. }
  119. }
  120. }
  121. MultiMeshInstance3D::MultiMeshInstance3D() {
  122. }
  123. MultiMeshInstance3D::~MultiMeshInstance3D() {
  124. }