Path: blob/master/scene/resources/3d/convex_polygon_shape_3d.cpp
9903 views
/**************************************************************************/1/* convex_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 "convex_polygon_shape_3d.h"31#include "core/math/convex_hull.h"32#include "scene/resources/mesh.h"33#include "servers/physics_server_3d.h"3435Vector<Vector3> ConvexPolygonShape3D::get_debug_mesh_lines() const {36Vector<Vector3> poly_points = get_points();3738if (poly_points.size() > 1) { // Need at least 2 points for a line.39Vector<Vector3> varr = Variant(poly_points);40Geometry3D::MeshData md;41Error err = ConvexHullComputer::convex_hull(varr, md);42if (err == OK) {43Vector<Vector3> lines;44lines.resize(md.edges.size() * 2);45for (uint32_t i = 0; i < md.edges.size(); i++) {46lines.write[i * 2 + 0] = md.vertices[md.edges[i].vertex_a];47lines.write[i * 2 + 1] = md.vertices[md.edges[i].vertex_b];48}49return lines;50}51}5253return Vector<Vector3>();54}5556Ref<ArrayMesh> ConvexPolygonShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {57const Vector<Vector3> hull_points = get_points();5859Vector<Vector3> verts;60Vector<Color> colors;61Vector<int> indices;6263if (hull_points.size() >= 3) {64Geometry3D::MeshData md;65Error err = ConvexHullComputer::convex_hull(hull_points, md);66if (err == OK) {67verts = md.vertices;68for (int i = 0; i < verts.size(); i++) {69colors.push_back(p_modulate);70}71for (const Geometry3D::MeshData::Face &face : md.faces) {72const int first_point = face.indices[0];73const int indices_count = face.indices.size();74for (int i = 1; i < indices_count - 1; i++) {75indices.push_back(first_point);76indices.push_back(face.indices[i]);77indices.push_back(face.indices[i + 1]);78}79}80}81}8283Ref<ArrayMesh> mesh = memnew(ArrayMesh);84Array a;85a.resize(Mesh::ARRAY_MAX);86a[RS::ARRAY_VERTEX] = verts;87a[RS::ARRAY_COLOR] = colors;88a[RS::ARRAY_INDEX] = indices;89mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);9091return mesh;92}9394real_t ConvexPolygonShape3D::get_enclosing_radius() const {95Vector<Vector3> data = get_points();96const Vector3 *read = data.ptr();97real_t r = 0.0;98for (int i(0); i < data.size(); i++) {99r = MAX(read[i].length_squared(), r);100}101return Math::sqrt(r);102}103104void ConvexPolygonShape3D::_update_shape() {105PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), points);106Shape3D::_update_shape();107}108109void ConvexPolygonShape3D::set_points(const Vector<Vector3> &p_points) {110points = p_points;111_update_shape();112emit_changed();113}114115Vector<Vector3> ConvexPolygonShape3D::get_points() const {116return points;117}118119void ConvexPolygonShape3D::_bind_methods() {120ClassDB::bind_method(D_METHOD("set_points", "points"), &ConvexPolygonShape3D::set_points);121ClassDB::bind_method(D_METHOD("get_points"), &ConvexPolygonShape3D::get_points);122123ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "points"), "set_points", "get_points");124}125126ConvexPolygonShape3D::ConvexPolygonShape3D() :127Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CONVEX_POLYGON)) {128}129130131