Path: blob/master/scene/2d/multimesh_instance_2d.cpp
10277 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_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::_notification(int p_what) {45switch (p_what) {46case NOTIFICATION_DRAW: {47if (multimesh.is_valid()) {48draw_multimesh(multimesh, texture);49}50} break;51}52}5354void MultiMeshInstance2D::_bind_methods() {55ClassDB::bind_method(D_METHOD("set_multimesh", "multimesh"), &MultiMeshInstance2D::set_multimesh);56ClassDB::bind_method(D_METHOD("get_multimesh"), &MultiMeshInstance2D::get_multimesh);5758ClassDB::bind_method(D_METHOD("set_texture", "texture"), &MultiMeshInstance2D::set_texture);59ClassDB::bind_method(D_METHOD("get_texture"), &MultiMeshInstance2D::get_texture);6061ADD_SIGNAL(MethodInfo("texture_changed"));6263ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multimesh", PROPERTY_HINT_RESOURCE_TYPE, "MultiMesh"), "set_multimesh", "get_multimesh");64ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");65}6667void MultiMeshInstance2D::set_multimesh(const Ref<MultiMesh> &p_multimesh) {68// Cleanup previous connection if any.69if (multimesh.is_valid()) {70multimesh->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));71}72multimesh = p_multimesh;7374// Connect to the multimesh so the AABB can update when instance transforms are changed.75if (multimesh.is_valid()) {76multimesh->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));77}78queue_redraw();79}8081Ref<MultiMesh> MultiMeshInstance2D::get_multimesh() const {82return multimesh;83}8485void MultiMeshInstance2D::set_texture(const Ref<Texture2D> &p_texture) {86if (p_texture == texture) {87return;88}89texture = p_texture;90queue_redraw();91emit_signal(SceneStringName(texture_changed));92}9394Ref<Texture2D> MultiMeshInstance2D::get_texture() const {95return texture;96}9798#ifdef DEBUG_ENABLED99Rect2 MultiMeshInstance2D::_edit_get_rect() const {100if (multimesh.is_valid()) {101AABB aabb = multimesh->get_aabb();102return Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);103}104105return Node2D::_edit_get_rect();106}107#endif // DEBUG_ENABLED108109#ifndef NAVIGATION_2D_DISABLED110void MultiMeshInstance2D::navmesh_parse_init() {111ERR_FAIL_NULL(NavigationServer2D::get_singleton());112if (!_navmesh_source_geometry_parser.is_valid()) {113_navmesh_source_geometry_parsing_callback = callable_mp_static(&MultiMeshInstance2D::navmesh_parse_source_geometry);114_navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();115NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);116}117}118119void MultiMeshInstance2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {120MultiMeshInstance2D *multimesh_instance = Object::cast_to<MultiMeshInstance2D>(p_node);121122if (multimesh_instance == nullptr) {123return;124}125126NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();127if (!(parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_BOTH)) {128return;129}130131Ref<MultiMesh> multimesh = multimesh_instance->get_multimesh();132if (!(multimesh.is_valid() && multimesh->get_transform_format() == MultiMesh::TRANSFORM_2D)) {133return;134}135136Ref<Mesh> mesh = multimesh->get_mesh();137if (mesh.is_null()) {138return;139}140141using namespace Clipper2Lib;142143PathsD mesh_subject_paths, dummy_clip_paths;144145for (int i = 0; i < mesh->get_surface_count(); i++) {146if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {147continue;148}149150if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FLAG_USE_2D_VERTICES)) {151continue;152}153154PathD subject_path;155156int index_count = 0;157if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {158index_count = mesh->surface_get_array_index_len(i);159} else {160index_count = mesh->surface_get_array_len(i);161}162163ERR_CONTINUE((index_count == 0 || (index_count % 3) != 0));164165Array a = mesh->surface_get_arrays(i);166167Vector<Vector2> mesh_vertices = a[Mesh::ARRAY_VERTEX];168169if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {170Vector<int> mesh_indices = a[Mesh::ARRAY_INDEX];171for (int vertex_index : mesh_indices) {172const Vector2 &vertex = mesh_vertices[vertex_index];173const PointD &point = PointD(vertex.x, vertex.y);174subject_path.push_back(point);175}176} else {177for (const Vector2 &vertex : mesh_vertices) {178const PointD &point = PointD(vertex.x, vertex.y);179subject_path.push_back(point);180}181}182mesh_subject_paths.push_back(subject_path);183}184185PathsD mesh_path_solution = Union(mesh_subject_paths, dummy_clip_paths, FillRule::NonZero);186187//path_solution = RamerDouglasPeucker(path_solution, 0.025);188189int multimesh_instance_count = multimesh->get_visible_instance_count();190if (multimesh_instance_count == -1) {191multimesh_instance_count = multimesh->get_instance_count();192}193194const Transform2D multimesh_instance_xform = p_source_geometry_data->root_node_transform * multimesh_instance->get_global_transform();195196for (int i = 0; i < multimesh_instance_count; i++) {197const Transform2D multimesh_instance_mesh_instance_xform = multimesh_instance_xform * multimesh->get_instance_transform_2d(i);198199for (const PathD &mesh_path : mesh_path_solution) {200Vector<Vector2> shape_outline;201202for (const PointD &mesh_path_point : mesh_path) {203shape_outline.push_back(Point2(static_cast<real_t>(mesh_path_point.x), static_cast<real_t>(mesh_path_point.y)));204}205206for (int j = 0; j < shape_outline.size(); j++) {207shape_outline.write[j] = multimesh_instance_mesh_instance_xform.xform(shape_outline[j]);208}209p_source_geometry_data->add_obstruction_outline(shape_outline);210}211}212}213#endif // NAVIGATION_2D_DISABLED214215MultiMeshInstance2D::MultiMeshInstance2D() {216}217218MultiMeshInstance2D::~MultiMeshInstance2D() {219}220221222