1/**************************************************************************/
2/* physics_bone_3d_gizmo_plugin.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 "physics_bone_3d_gizmo_plugin.h"
32
33#include "editor/editor_settings.h"
34#include "editor/plugins/gizmos/joint_3d_gizmo_plugin.h"
35#include "editor/plugins/node_3d_editor_plugin.h"
36#include "scene/3d/physics_body_3d.h"
37
38PhysicalBone3DGizmoPlugin::PhysicalBone3DGizmoPlugin() {
39 create_material("joint_material", EDITOR_GET("editors/3d_gizmos/gizmo_colors/joint"));
40}
41
42bool PhysicalBone3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
43 return Object::cast_to<PhysicalBone3D>(p_spatial) != nullptr;
44}
45
46String PhysicalBone3DGizmoPlugin::get_gizmo_name() const {
47 return "PhysicalBone3D";
48}
49
50int PhysicalBone3DGizmoPlugin::get_priority() const {
51 return -1;
52}
53
54void PhysicalBone3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
55 p_gizmo->clear();
56
57 PhysicalBone3D *physical_bone = Object::cast_to<PhysicalBone3D>(p_gizmo->get_node_3d());
58
59 if (!physical_bone) {
60 return;
61 }
62
63 Skeleton3D *sk(physical_bone->find_skeleton_parent());
64 if (!sk) {
65 return;
66 }
67
68 PhysicalBone3D *pb(sk->get_physical_bone(physical_bone->get_bone_id()));
69 if (!pb) {
70 return;
71 }
72
73 PhysicalBone3D *pbp(sk->get_physical_bone_parent(physical_bone->get_bone_id()));
74 if (!pbp) {
75 return;
76 }
77
78 Vector<Vector3> points;
79
80 switch (physical_bone->get_joint_type()) {
81 case PhysicalBone3D::JOINT_TYPE_PIN: {
82 Joint3DGizmoPlugin::CreatePinJointGizmo(physical_bone->get_joint_offset(), points);
83 } break;
84 case PhysicalBone3D::JOINT_TYPE_CONE: {
85 const PhysicalBone3D::ConeJointData *cjd(static_cast<const PhysicalBone3D::ConeJointData *>(physical_bone->get_joint_data()));
86 Joint3DGizmoPlugin::CreateConeTwistJointGizmo(
87 physical_bone->get_joint_offset(),
88 physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
89 pb->get_global_transform(),
90 pbp->get_global_transform(),
91 cjd->swing_span,
92 cjd->twist_span,
93 &points,
94 &points);
95 } break;
96 case PhysicalBone3D::JOINT_TYPE_HINGE: {
97 const PhysicalBone3D::HingeJointData *hjd(static_cast<const PhysicalBone3D::HingeJointData *>(physical_bone->get_joint_data()));
98 Joint3DGizmoPlugin::CreateHingeJointGizmo(
99 physical_bone->get_joint_offset(),
100 physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
101 pb->get_global_transform(),
102 pbp->get_global_transform(),
103 hjd->angular_limit_lower,
104 hjd->angular_limit_upper,
105 hjd->angular_limit_enabled,
106 points,
107 &points,
108 &points);
109 } break;
110 case PhysicalBone3D::JOINT_TYPE_SLIDER: {
111 const PhysicalBone3D::SliderJointData *sjd(static_cast<const PhysicalBone3D::SliderJointData *>(physical_bone->get_joint_data()));
112 Joint3DGizmoPlugin::CreateSliderJointGizmo(
113 physical_bone->get_joint_offset(),
114 physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
115 pb->get_global_transform(),
116 pbp->get_global_transform(),
117 sjd->angular_limit_lower,
118 sjd->angular_limit_upper,
119 sjd->linear_limit_lower,
120 sjd->linear_limit_upper,
121 points,
122 &points,
123 &points);
124 } break;
125 case PhysicalBone3D::JOINT_TYPE_6DOF: {
126 const PhysicalBone3D::SixDOFJointData *sdofjd(static_cast<const PhysicalBone3D::SixDOFJointData *>(physical_bone->get_joint_data()));
127 Joint3DGizmoPlugin::CreateGeneric6DOFJointGizmo(
128 physical_bone->get_joint_offset(),
129
130 physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
131 pb->get_global_transform(),
132 pbp->get_global_transform(),
133
134 sdofjd->axis_data[0].angular_limit_lower,
135 sdofjd->axis_data[0].angular_limit_upper,
136 sdofjd->axis_data[0].linear_limit_lower,
137 sdofjd->axis_data[0].linear_limit_upper,
138 sdofjd->axis_data[0].angular_limit_enabled,
139 sdofjd->axis_data[0].linear_limit_enabled,
140
141 sdofjd->axis_data[1].angular_limit_lower,
142 sdofjd->axis_data[1].angular_limit_upper,
143 sdofjd->axis_data[1].linear_limit_lower,
144 sdofjd->axis_data[1].linear_limit_upper,
145 sdofjd->axis_data[1].angular_limit_enabled,
146 sdofjd->axis_data[1].linear_limit_enabled,
147
148 sdofjd->axis_data[2].angular_limit_lower,
149 sdofjd->axis_data[2].angular_limit_upper,
150 sdofjd->axis_data[2].linear_limit_lower,
151 sdofjd->axis_data[2].linear_limit_upper,
152 sdofjd->axis_data[2].angular_limit_enabled,
153 sdofjd->axis_data[2].linear_limit_enabled,
154
155 points,
156 &points,
157 &points);
158 } break;
159 default:
160 return;
161 }
162
163 Ref<Material> material = get_material("joint_material", p_gizmo);
164
165 p_gizmo->add_collision_segments(points);
166 p_gizmo->add_lines(points, material);
167}
168