Path: blob/master/scene/resources/3d/concave_polygon_shape_3d.cpp
9898 views
/**************************************************************************/1/* concave_polygon_shape_3d.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 "concave_polygon_shape_3d.h"3132#include "scene/resources/mesh.h"33#include "servers/physics_server_3d.h"3435Vector<Vector3> ConcavePolygonShape3D::get_debug_mesh_lines() const {36HashSet<DrawEdge, DrawEdge> edges;3738int index_count = faces.size();39ERR_FAIL_COND_V((index_count % 3) != 0, Vector<Vector3>());4041const Vector3 *r = faces.ptr();4243for (int i = 0; i < index_count; i += 3) {44for (int j = 0; j < 3; j++) {45DrawEdge de(r[i + j], r[i + ((j + 1) % 3)]);46edges.insert(de);47}48}4950Vector<Vector3> points;51points.resize(edges.size() * 2);52int idx = 0;53for (const DrawEdge &E : edges) {54points.write[idx + 0] = E.a;55points.write[idx + 1] = E.b;56idx += 2;57}5859return points;60}6162Ref<ArrayMesh> ConcavePolygonShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {63Vector<Color> colors;6465for (int i = 0; i < faces.size(); i++) {66colors.push_back(p_modulate);67}6869Ref<ArrayMesh> mesh = memnew(ArrayMesh);70Array a;71a.resize(Mesh::ARRAY_MAX);72a[RS::ARRAY_VERTEX] = faces;73a[RS::ARRAY_COLOR] = colors;74mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);7576return mesh;77}7879real_t ConcavePolygonShape3D::get_enclosing_radius() const {80Vector<Vector3> data = get_faces();81const Vector3 *read = data.ptr();82real_t r = 0.0;83for (int i(0); i < data.size(); i++) {84r = MAX(read[i].length_squared(), r);85}86return Math::sqrt(r);87}8889void ConcavePolygonShape3D::_update_shape() {90Dictionary d;91d["faces"] = faces;92d["backface_collision"] = backface_collision;93PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), d);9495Shape3D::_update_shape();96}9798void ConcavePolygonShape3D::set_faces(const Vector<Vector3> &p_faces) {99faces = p_faces;100_update_shape();101emit_changed();102}103104Vector<Vector3> ConcavePolygonShape3D::get_faces() const {105return faces;106}107108void ConcavePolygonShape3D::set_backface_collision_enabled(bool p_enabled) {109backface_collision = p_enabled;110111if (!faces.is_empty()) {112_update_shape();113emit_changed();114}115}116117bool ConcavePolygonShape3D::is_backface_collision_enabled() const {118return backface_collision;119}120121void ConcavePolygonShape3D::_bind_methods() {122ClassDB::bind_method(D_METHOD("set_faces", "faces"), &ConcavePolygonShape3D::set_faces);123ClassDB::bind_method(D_METHOD("get_faces"), &ConcavePolygonShape3D::get_faces);124125ClassDB::bind_method(D_METHOD("set_backface_collision_enabled", "enabled"), &ConcavePolygonShape3D::set_backface_collision_enabled);126ClassDB::bind_method(D_METHOD("is_backface_collision_enabled"), &ConcavePolygonShape3D::is_backface_collision_enabled);127128ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_faces", "get_faces");129ADD_PROPERTY(PropertyInfo(Variant::BOOL, "backface_collision"), "set_backface_collision_enabled", "is_backface_collision_enabled");130}131132ConcavePolygonShape3D::ConcavePolygonShape3D() :133Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CONCAVE_POLYGON)) {134//set_planes(Vector3(1,1,1));135}136137138