Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/3d/shape_3d.cpp
9903 views
1
/**************************************************************************/
2
/* 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 "shape_3d.h"
32
33
#include "scene/main/scene_tree.h"
34
#include "scene/resources/mesh.h"
35
#include "servers/physics_server_3d.h"
36
37
void Shape3D::add_vertices_to_array(Vector<Vector3> &array, const Transform3D &p_xform) {
38
Vector<Vector3> toadd = get_debug_mesh_lines();
39
40
if (toadd.size()) {
41
int base = array.size();
42
array.resize(base + toadd.size());
43
Vector3 *w = array.ptrw();
44
for (int i = 0; i < toadd.size(); i++) {
45
w[i + base] = p_xform.xform(toadd[i]);
46
}
47
}
48
}
49
50
void Shape3D::set_custom_solver_bias(real_t p_bias) {
51
custom_bias = p_bias;
52
PhysicsServer3D::get_singleton()->shape_set_custom_solver_bias(shape, custom_bias);
53
}
54
55
real_t Shape3D::get_custom_solver_bias() const {
56
return custom_bias;
57
}
58
59
real_t Shape3D::get_margin() const {
60
return margin;
61
}
62
63
void Shape3D::set_margin(real_t p_margin) {
64
margin = p_margin;
65
PhysicsServer3D::get_singleton()->shape_set_margin(shape, margin);
66
}
67
68
void Shape3D::set_debug_color(const Color &p_color) {
69
if (p_color == debug_color) {
70
return;
71
}
72
73
debug_color = p_color;
74
#ifdef DEBUG_ENABLED
75
debug_properties_edited = true;
76
#endif // DEBUG_ENABLED
77
_update_shape();
78
}
79
80
Color Shape3D::get_debug_color() const {
81
return debug_color;
82
}
83
84
void Shape3D::set_debug_fill(bool p_fill) {
85
if (p_fill == debug_fill) {
86
return;
87
}
88
89
debug_fill = p_fill;
90
#ifdef DEBUG_ENABLED
91
debug_properties_edited = true;
92
#endif // DEBUG_ENABLED
93
_update_shape();
94
}
95
96
bool Shape3D::get_debug_fill() const {
97
return debug_fill;
98
}
99
100
Ref<ArrayMesh> Shape3D::get_debug_mesh() {
101
if (debug_mesh_cache.is_valid()) {
102
return debug_mesh_cache;
103
}
104
105
Vector<Vector3> lines = get_debug_mesh_lines();
106
107
debug_mesh_cache.instantiate();
108
109
if (!lines.is_empty()) {
110
Vector<Color> colors;
111
colors.resize(lines.size());
112
colors.fill(debug_color);
113
114
Array lines_array;
115
lines_array.resize(Mesh::ARRAY_MAX);
116
lines_array[Mesh::ARRAY_VERTEX] = lines;
117
lines_array[Mesh::ARRAY_COLOR] = colors;
118
119
debug_mesh_cache->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, lines_array);
120
121
SceneTree *scene_tree = SceneTree::get_singleton();
122
if (scene_tree) {
123
debug_mesh_cache->surface_set_material(0, scene_tree->get_debug_collision_material());
124
}
125
126
if (debug_fill) {
127
Ref<ArrayMesh> array_mesh = get_debug_arraymesh_faces(debug_color * Color(1.0, 1.0, 1.0, 0.0625));
128
if (array_mesh.is_valid() && array_mesh->get_surface_count() > 0) {
129
Array solid_array = array_mesh->surface_get_arrays(0);
130
debug_mesh_cache->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, solid_array);
131
if (scene_tree) {
132
debug_mesh_cache->surface_set_material(1, scene_tree->get_debug_collision_material());
133
}
134
}
135
}
136
}
137
138
return debug_mesh_cache;
139
}
140
141
void Shape3D::_update_shape() {
142
emit_changed();
143
debug_mesh_cache.unref();
144
}
145
146
void Shape3D::_bind_methods() {
147
ClassDB::bind_method(D_METHOD("set_custom_solver_bias", "bias"), &Shape3D::set_custom_solver_bias);
148
ClassDB::bind_method(D_METHOD("get_custom_solver_bias"), &Shape3D::get_custom_solver_bias);
149
150
ClassDB::bind_method(D_METHOD("set_margin", "margin"), &Shape3D::set_margin);
151
ClassDB::bind_method(D_METHOD("get_margin"), &Shape3D::get_margin);
152
153
ClassDB::bind_method(D_METHOD("get_debug_mesh"), &Shape3D::get_debug_mesh);
154
155
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_solver_bias", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_custom_solver_bias", "get_custom_solver_bias");
156
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,10,0.001,or_greater,suffix:m"), "set_margin", "get_margin");
157
}
158
159
Shape3D::Shape3D() {
160
ERR_PRINT("Default constructor must not be called!");
161
}
162
163
Shape3D::Shape3D(RID p_shape) :
164
shape(p_shape) {}
165
166
Shape3D::~Shape3D() {
167
ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
168
PhysicsServer3D::get_singleton()->free(shape);
169
}
170
171