Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/3d/concave_polygon_shape_3d.cpp
9898 views
1
/**************************************************************************/
2
/* concave_polygon_shape_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 "concave_polygon_shape_3d.h"
32
33
#include "scene/resources/mesh.h"
34
#include "servers/physics_server_3d.h"
35
36
Vector<Vector3> ConcavePolygonShape3D::get_debug_mesh_lines() const {
37
HashSet<DrawEdge, DrawEdge> edges;
38
39
int index_count = faces.size();
40
ERR_FAIL_COND_V((index_count % 3) != 0, Vector<Vector3>());
41
42
const Vector3 *r = faces.ptr();
43
44
for (int i = 0; i < index_count; i += 3) {
45
for (int j = 0; j < 3; j++) {
46
DrawEdge de(r[i + j], r[i + ((j + 1) % 3)]);
47
edges.insert(de);
48
}
49
}
50
51
Vector<Vector3> points;
52
points.resize(edges.size() * 2);
53
int idx = 0;
54
for (const DrawEdge &E : edges) {
55
points.write[idx + 0] = E.a;
56
points.write[idx + 1] = E.b;
57
idx += 2;
58
}
59
60
return points;
61
}
62
63
Ref<ArrayMesh> ConcavePolygonShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
64
Vector<Color> colors;
65
66
for (int i = 0; i < faces.size(); i++) {
67
colors.push_back(p_modulate);
68
}
69
70
Ref<ArrayMesh> mesh = memnew(ArrayMesh);
71
Array a;
72
a.resize(Mesh::ARRAY_MAX);
73
a[RS::ARRAY_VERTEX] = faces;
74
a[RS::ARRAY_COLOR] = colors;
75
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);
76
77
return mesh;
78
}
79
80
real_t ConcavePolygonShape3D::get_enclosing_radius() const {
81
Vector<Vector3> data = get_faces();
82
const Vector3 *read = data.ptr();
83
real_t r = 0.0;
84
for (int i(0); i < data.size(); i++) {
85
r = MAX(read[i].length_squared(), r);
86
}
87
return Math::sqrt(r);
88
}
89
90
void ConcavePolygonShape3D::_update_shape() {
91
Dictionary d;
92
d["faces"] = faces;
93
d["backface_collision"] = backface_collision;
94
PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), d);
95
96
Shape3D::_update_shape();
97
}
98
99
void ConcavePolygonShape3D::set_faces(const Vector<Vector3> &p_faces) {
100
faces = p_faces;
101
_update_shape();
102
emit_changed();
103
}
104
105
Vector<Vector3> ConcavePolygonShape3D::get_faces() const {
106
return faces;
107
}
108
109
void ConcavePolygonShape3D::set_backface_collision_enabled(bool p_enabled) {
110
backface_collision = p_enabled;
111
112
if (!faces.is_empty()) {
113
_update_shape();
114
emit_changed();
115
}
116
}
117
118
bool ConcavePolygonShape3D::is_backface_collision_enabled() const {
119
return backface_collision;
120
}
121
122
void ConcavePolygonShape3D::_bind_methods() {
123
ClassDB::bind_method(D_METHOD("set_faces", "faces"), &ConcavePolygonShape3D::set_faces);
124
ClassDB::bind_method(D_METHOD("get_faces"), &ConcavePolygonShape3D::get_faces);
125
126
ClassDB::bind_method(D_METHOD("set_backface_collision_enabled", "enabled"), &ConcavePolygonShape3D::set_backface_collision_enabled);
127
ClassDB::bind_method(D_METHOD("is_backface_collision_enabled"), &ConcavePolygonShape3D::is_backface_collision_enabled);
128
129
ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_faces", "get_faces");
130
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "backface_collision"), "set_backface_collision_enabled", "is_backface_collision_enabled");
131
}
132
133
ConcavePolygonShape3D::ConcavePolygonShape3D() :
134
Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CONCAVE_POLYGON)) {
135
//set_planes(Vector3(1,1,1));
136
}
137
138