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