Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/3d/world_boundary_shape_3d.cpp
9898 views
1
/**************************************************************************/
2
/* world_boundary_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 "world_boundary_shape_3d.h"
32
33
#include "scene/resources/mesh.h"
34
#include "servers/physics_server_3d.h"
35
36
Vector<Vector3> WorldBoundaryShape3D::get_debug_mesh_lines() const {
37
Plane p = get_plane();
38
39
Vector3 n1 = p.get_any_perpendicular_normal();
40
Vector3 n2 = p.normal.cross(n1).normalized();
41
42
Vector3 pface[4] = {
43
p.normal * p.d + n1 * 10.0 + n2 * 10.0,
44
p.normal * p.d + n1 * 10.0 + n2 * -10.0,
45
p.normal * p.d + n1 * -10.0 + n2 * -10.0,
46
p.normal * p.d + n1 * -10.0 + n2 * 10.0,
47
};
48
49
Vector<Vector3> points = {
50
pface[0],
51
pface[1],
52
pface[1],
53
pface[2],
54
pface[2],
55
pface[3],
56
pface[3],
57
pface[0],
58
p.normal * p.d,
59
p.normal * p.d + p.normal * 3
60
};
61
62
return points;
63
}
64
65
Ref<ArrayMesh> WorldBoundaryShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
66
Plane p = get_plane();
67
68
Vector3 n1 = p.get_any_perpendicular_normal();
69
Vector3 n2 = p.normal.cross(n1).normalized();
70
71
Vector3 pface[4] = {
72
p.normal * p.d + n1 * 10.0 + n2 * 10.0,
73
p.normal * p.d + n1 * 10.0 + n2 * -10.0,
74
p.normal * p.d + n1 * -10.0 + n2 * -10.0,
75
p.normal * p.d + n1 * -10.0 + n2 * 10.0,
76
};
77
78
Vector<Vector3> points = {
79
pface[0],
80
pface[1],
81
pface[2],
82
pface[3],
83
};
84
85
Vector<Color> colors = {
86
p_modulate,
87
p_modulate,
88
p_modulate,
89
p_modulate,
90
};
91
92
Vector<int> indices = {
93
0,
94
1,
95
2,
96
0,
97
2,
98
3,
99
};
100
101
Ref<ArrayMesh> mesh = memnew(ArrayMesh);
102
Array a;
103
a.resize(Mesh::ARRAY_MAX);
104
a[RS::ARRAY_VERTEX] = points;
105
a[RS::ARRAY_COLOR] = colors;
106
a[RS::ARRAY_INDEX] = indices;
107
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);
108
109
return mesh;
110
}
111
112
void WorldBoundaryShape3D::_update_shape() {
113
PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), plane);
114
Shape3D::_update_shape();
115
}
116
117
void WorldBoundaryShape3D::set_plane(const Plane &p_plane) {
118
plane = p_plane;
119
_update_shape();
120
emit_changed();
121
}
122
123
const Plane &WorldBoundaryShape3D::get_plane() const {
124
return plane;
125
}
126
127
void WorldBoundaryShape3D::_bind_methods() {
128
ClassDB::bind_method(D_METHOD("set_plane", "plane"), &WorldBoundaryShape3D::set_plane);
129
ClassDB::bind_method(D_METHOD("get_plane"), &WorldBoundaryShape3D::get_plane);
130
131
ADD_PROPERTY(PropertyInfo(Variant::PLANE, "plane", PROPERTY_HINT_NONE, "suffix:m"), "set_plane", "get_plane");
132
}
133
134
WorldBoundaryShape3D::WorldBoundaryShape3D() :
135
Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_WORLD_BOUNDARY)) {
136
set_plane(Plane(0, 1, 0, 0));
137
}
138
139