Path: blob/master/scene/2d/multimesh_instance_2d.cpp
20959 views
/**************************************************************************/1/* multimesh_instance_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "multimesh_instance_2d.h"3132#ifndef NAVIGATION_2D_DISABLED33#include "scene/resources/2d/navigation_mesh_source_geometry_data_2d.h"34#include "scene/resources/2d/navigation_polygon.h"35#include "servers/navigation_2d/navigation_server_2d.h"3637#include "thirdparty/clipper2/include/clipper2/clipper.h"38#include "thirdparty/misc/polypartition.h"39#endif // NAVIGATION_2D_DISABLED4041Callable MultiMeshInstance2D::_navmesh_source_geometry_parsing_callback;42RID MultiMeshInstance2D::_navmesh_source_geometry_parser;4344void MultiMeshInstance2D::_refresh_interpolated() {45if (is_inside_tree() && multimesh.is_valid()) {46bool interpolated = is_physics_interpolated_and_enabled();47multimesh->set_physics_interpolated(interpolated);48}49}5051void MultiMeshInstance2D::_physics_interpolated_changed() {52CanvasItem::_physics_interpolated_changed();53_refresh_interpolated();54}5556void MultiMeshInstance2D::_notification(int p_what) {57switch (p_what) {58case NOTIFICATION_ENTER_TREE: {59_refresh_interpolated();60break;61}62case NOTIFICATION_DRAW: {63if (multimesh.is_valid()) {64draw_multimesh(multimesh, texture);65}66} break;67}68}6970void MultiMeshInstance2D::_bind_methods() {71ClassDB::bind_method(D_METHOD("set_multimesh", "multimesh"), &MultiMeshInstance2D::set_multimesh);72ClassDB::bind_method(D_METHOD("get_multimesh"), &MultiMeshInstance2D::get_multimesh);7374ClassDB::bind_method(D_METHOD("set_texture", "texture"), &MultiMeshInstance2D::set_texture);75ClassDB::bind_method(D_METHOD("get_texture"), &MultiMeshInstance2D::get_texture);7677ADD_SIGNAL(MethodInfo("texture_changed"));7879ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multimesh", PROPERTY_HINT_RESOURCE_TYPE, MultiMesh::get_class_static()), "set_multimesh", "get_multimesh");80ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static()), "set_texture", "get_texture");81}8283void MultiMeshInstance2D::set_multimesh(const Ref<MultiMesh> &p_multimesh) {84// Cleanup previous connection if any.85if (multimesh.is_valid()) {86multimesh->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));87}88multimesh = p_multimesh;8990// Connect to the multimesh so the AABB can update when instance transforms are changed.91if (multimesh.is_valid()) {92multimesh->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));93_refresh_interpolated();94}95queue_redraw();96}9798Ref<MultiMesh> MultiMeshInstance2D::get_multimesh() const {99return multimesh;100}101102void MultiMeshInstance2D::set_texture(const Ref<Texture2D> &p_texture) {103if (p_texture == texture) {104return;105}106texture = p_texture;107queue_redraw();108emit_signal(SceneStringName(texture_changed));109}110111Ref<Texture2D> MultiMeshInstance2D::get_texture() const {112return texture;113}114115#ifdef DEBUG_ENABLED116Rect2 MultiMeshInstance2D::_edit_get_rect() const {117if (multimesh.is_valid()) {118AABB aabb = multimesh->get_aabb();119return Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);120}121122return Node2D::_edit_get_rect();123}124#endif // DEBUG_ENABLED125126#ifndef NAVIGATION_2D_DISABLED127void MultiMeshInstance2D::navmesh_parse_init() {128ERR_FAIL_NULL(NavigationServer2D::get_singleton());129if (!_navmesh_source_geometry_parser.is_valid()) {130_navmesh_source_geometry_parsing_callback = callable_mp_static(&MultiMeshInstance2D::navmesh_parse_source_geometry);131_navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();132NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);133}134}135136void MultiMeshInstance2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {137MultiMeshInstance2D *multimesh_instance = Object::cast_to<MultiMeshInstance2D>(p_node);138139if (multimesh_instance == nullptr) {140return;141}142143NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();144if (!(parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_BOTH)) {145return;146}147148Ref<MultiMesh> multimesh = multimesh_instance->get_multimesh();149if (!(multimesh.is_valid() && multimesh->get_transform_format() == MultiMesh::TRANSFORM_2D)) {150return;151}152153Ref<Mesh> mesh = multimesh->get_mesh();154if (mesh.is_null()) {155return;156}157158using namespace Clipper2Lib;159160PathsD mesh_subject_paths, dummy_clip_paths;161162for (int i = 0; i < mesh->get_surface_count(); i++) {163if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {164continue;165}166167if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FLAG_USE_2D_VERTICES)) {168continue;169}170171PathD subject_path;172173int index_count = 0;174if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {175index_count = mesh->surface_get_array_index_len(i);176} else {177index_count = mesh->surface_get_array_len(i);178}179180ERR_CONTINUE((index_count == 0 || (index_count % 3) != 0));181182Array a = mesh->surface_get_arrays(i);183184Vector<Vector2> mesh_vertices = a[Mesh::ARRAY_VERTEX];185186if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {187Vector<int> mesh_indices = a[Mesh::ARRAY_INDEX];188for (int vertex_index : mesh_indices) {189const Vector2 &vertex = mesh_vertices[vertex_index];190const PointD &point = PointD(vertex.x, vertex.y);191subject_path.push_back(point);192}193} else {194for (const Vector2 &vertex : mesh_vertices) {195const PointD &point = PointD(vertex.x, vertex.y);196subject_path.push_back(point);197}198}199mesh_subject_paths.push_back(subject_path);200}201202PathsD mesh_path_solution = Union(mesh_subject_paths, dummy_clip_paths, FillRule::NonZero);203204//path_solution = RamerDouglasPeucker(path_solution, 0.025);205206int multimesh_instance_count = multimesh->get_visible_instance_count();207if (multimesh_instance_count == -1) {208multimesh_instance_count = multimesh->get_instance_count();209}210211const Transform2D multimesh_instance_xform = p_source_geometry_data->root_node_transform * multimesh_instance->get_global_transform();212213for (int i = 0; i < multimesh_instance_count; i++) {214const Transform2D multimesh_instance_mesh_instance_xform = multimesh_instance_xform * multimesh->get_instance_transform_2d(i);215216for (const PathD &mesh_path : mesh_path_solution) {217Vector<Vector2> shape_outline;218219for (const PointD &mesh_path_point : mesh_path) {220shape_outline.push_back(Point2(static_cast<real_t>(mesh_path_point.x), static_cast<real_t>(mesh_path_point.y)));221}222223for (int j = 0; j < shape_outline.size(); j++) {224shape_outline.write[j] = multimesh_instance_mesh_instance_xform.xform(shape_outline[j]);225}226p_source_geometry_data->add_obstruction_outline(shape_outline);227}228}229}230#endif // NAVIGATION_2D_DISABLED231232MultiMeshInstance2D::MultiMeshInstance2D() {233}234235MultiMeshInstance2D::~MultiMeshInstance2D() {236}237238239