| 1 | /**************************************************************************/ |
| 2 | /* spring_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 "spring_bone_3d_gizmo_plugin.h" |
| 32 | |
| 33 | #include "editor/settings/editor_settings.h" |
| 34 | #include "scene/3d/spring_bone_collision_capsule_3d.h" |
| 35 | #include "scene/3d/spring_bone_collision_plane_3d.h" |
| 36 | #include "scene/3d/spring_bone_collision_sphere_3d.h" |
| 37 | |
| 38 | // SpringBoneSimulator3D |
| 39 | |
| 40 | SpringBoneSimulator3DGizmoPlugin::SelectionMaterials SpringBoneSimulator3DGizmoPlugin::selection_materials; |
| 41 | |
| 42 | SpringBoneSimulator3DGizmoPlugin::SpringBoneSimulator3DGizmoPlugin() { |
| 43 | selection_materials.unselected_mat.instantiate(); |
| 44 | selection_materials.unselected_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 45 | selection_materials.unselected_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
| 46 | selection_materials.unselected_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); |
| 47 | selection_materials.unselected_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); |
| 48 | selection_materials.unselected_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true); |
| 49 | |
| 50 | selection_materials.selected_mat.instantiate(); |
| 51 | Ref<Shader> sh; |
| 52 | sh.instantiate(); |
| 53 | sh->set_code(R"( |
| 54 | // Skeleton 3D gizmo bones shader. |
| 55 | |
| 56 | shader_type spatial; |
| 57 | render_mode unshaded, shadows_disabled; |
| 58 | |
| 59 | void vertex() { |
| 60 | if (!OUTPUT_IS_SRGB) { |
| 61 | COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb,vec3(0.04045))); |
| 62 | } |
| 63 | VERTEX = VERTEX; |
| 64 | POSITION = PROJECTION_MATRIX * VIEW_MATRIX * MODEL_MATRIX * vec4(VERTEX.xyz, 1.0); |
| 65 | POSITION.z = mix(POSITION.z, POSITION.w, 0.998); |
| 66 | } |
| 67 | |
| 68 | void fragment() { |
| 69 | ALBEDO = COLOR.rgb; |
| 70 | ALPHA = COLOR.a; |
| 71 | } |
| 72 | )"); |
| 73 | selection_materials.selected_mat->set_shader(sh); |
| 74 | } |
| 75 | |
| 76 | SpringBoneSimulator3DGizmoPlugin::~SpringBoneSimulator3DGizmoPlugin() { |
| 77 | selection_materials.unselected_mat.unref(); |
| 78 | selection_materials.selected_mat.unref(); |
| 79 | } |
| 80 | |
| 81 | bool SpringBoneSimulator3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 82 | return Object::cast_to<SpringBoneSimulator3D>(p_spatial) != nullptr; |
| 83 | } |
| 84 | |
| 85 | String SpringBoneSimulator3DGizmoPlugin::get_gizmo_name() const { |
| 86 | return "SpringBoneSimulator3D"; |
| 87 | } |
| 88 | |
| 89 | int SpringBoneSimulator3DGizmoPlugin::get_priority() const { |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | void SpringBoneSimulator3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 94 | SpringBoneSimulator3D *simulator = Object::cast_to<SpringBoneSimulator3D>(p_gizmo->get_node_3d()); |
| 95 | p_gizmo->clear(); |
| 96 | |
| 97 | if (!simulator->get_setting_count()) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | Skeleton3D *skeleton = simulator->get_skeleton(); |
| 102 | if (!skeleton) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | Ref<ArrayMesh> mesh = get_joints_mesh(skeleton, simulator, p_gizmo->is_selected()); |
| 107 | Transform3D skel_tr = simulator->get_global_transform().inverse() * skeleton->get_global_transform(); |
| 108 | p_gizmo->add_mesh(mesh, Ref<Material>(), skel_tr, skeleton->register_skin(skeleton->create_skin_from_rest_transforms())); |
| 109 | } |
| 110 | |
| 111 | Ref<ArrayMesh> SpringBoneSimulator3DGizmoPlugin::get_joints_mesh(Skeleton3D *p_skeleton, SpringBoneSimulator3D *p_simulator, bool p_is_selected) { |
| 112 | Color bone_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/spring_bone_joint"); |
| 113 | |
| 114 | Ref<SurfaceTool> surface_tool; |
| 115 | surface_tool.instantiate(); |
| 116 | surface_tool->begin(Mesh::PRIMITIVE_LINES); |
| 117 | |
| 118 | if (p_is_selected) { |
| 119 | surface_tool->set_material(selection_materials.selected_mat); |
| 120 | } else { |
| 121 | selection_materials.unselected_mat->set_albedo(bone_color); |
| 122 | surface_tool->set_material(selection_materials.unselected_mat); |
| 123 | } |
| 124 | |
| 125 | LocalVector<int> bones; |
| 126 | LocalVector<float> weights; |
| 127 | bones.resize(4); |
| 128 | weights.resize(4); |
| 129 | for (int i = 0; i < 4; i++) { |
| 130 | bones[i] = 0; |
| 131 | weights[i] = 0; |
| 132 | } |
| 133 | weights[0] = 1; |
| 134 | |
| 135 | for (int i = 0; i < p_simulator->get_setting_count(); i++) { |
| 136 | int current_bone = -1; |
| 137 | int prev_bone = -1; |
| 138 | int joint_end = p_simulator->get_joint_count(i) - 1; |
| 139 | bool is_extended = p_simulator->is_end_bone_extended(i) && p_simulator->get_end_bone_length(i) > 0; |
| 140 | for (int j = 0; j <= joint_end; j++) { |
| 141 | current_bone = p_simulator->get_joint_bone(i, j); |
| 142 | Transform3D global_pose = p_skeleton->get_bone_global_rest(current_bone); |
| 143 | if (j > 0) { |
| 144 | Transform3D parent_global_pose = p_skeleton->get_bone_global_rest(prev_bone); |
| 145 | Vector3 bone_vector = p_simulator->get_bone_vector(i, j - 1); |
| 146 | Vector3 center = parent_global_pose.translated_local(bone_vector).origin; |
| 147 | draw_line(surface_tool, parent_global_pose.origin, center, bone_color); |
| 148 | draw_sphere(surface_tool, global_pose.basis, center, p_simulator->get_joint_radius(i, j - 1), bone_color); |
| 149 | |
| 150 | // Draw rotation axis vector if not ROTATION_AXIS_ALL. |
| 151 | if (j != joint_end || (j == joint_end && is_extended)) { |
| 152 | SkeletonModifier3D::RotationAxis rotation_axis = p_simulator->get_joint_rotation_axis(i, j); |
| 153 | if (rotation_axis != SkeletonModifier3D::ROTATION_AXIS_ALL) { |
| 154 | Vector3 axis_vector = p_simulator->get_joint_rotation_axis_vector(i, j); |
| 155 | if (!axis_vector.is_zero_approx()) { |
| 156 | float line_length = p_simulator->get_joint_radius(i, j - 1) * 2.0; |
| 157 | Vector3 axis = global_pose.basis.xform(axis_vector.normalized()) * line_length; |
| 158 | draw_line(surface_tool, center - axis, center + axis, bone_color); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | if (j == joint_end && is_extended) { |
| 164 | Vector3 bone_vector = p_simulator->get_bone_vector(i, j); |
| 165 | if (bone_vector.is_zero_approx()) { |
| 166 | continue; |
| 167 | } |
| 168 | bones[0] = current_bone; |
| 169 | surface_tool->set_bones(Vector<int>(bones)); |
| 170 | surface_tool->set_weights(Vector<float>(weights)); |
| 171 | Vector3 center = global_pose.translated_local(bone_vector).origin; |
| 172 | draw_line(surface_tool, global_pose.origin, center, bone_color); |
| 173 | draw_sphere(surface_tool, global_pose.basis, center, p_simulator->get_joint_radius(i, j), bone_color); |
| 174 | } else { |
| 175 | bones[0] = current_bone; |
| 176 | surface_tool->set_bones(Vector<int>(bones)); |
| 177 | surface_tool->set_weights(Vector<float>(weights)); |
| 178 | if (j == 0) { |
| 179 | // Draw rotation axis vector if not ROTATION_AXIS_ALL. |
| 180 | SkeletonModifier3D::RotationAxis rotation_axis = p_simulator->get_joint_rotation_axis(i, j); |
| 181 | if (rotation_axis != SkeletonModifier3D::ROTATION_AXIS_ALL) { |
| 182 | Vector3 axis_vector = p_simulator->get_joint_rotation_axis_vector(i, j); |
| 183 | if (!axis_vector.is_zero_approx()) { |
| 184 | float line_length = p_simulator->get_joint_radius(i, j) * 2.0; |
| 185 | Vector3 axis = global_pose.basis.xform(axis_vector.normalized()) * line_length; |
| 186 | draw_line(surface_tool, global_pose.origin - axis, global_pose.origin + axis, bone_color); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | prev_bone = current_bone; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return surface_tool->commit(); |
| 196 | } |
| 197 | |
| 198 | void SpringBoneSimulator3DGizmoPlugin::draw_sphere(Ref<SurfaceTool> &p_surface_tool, const Basis &p_basis, const Vector3 &p_center, float p_radius, const Color &p_color) { |
| 199 | static constexpr int STEP = 16; |
| 200 | static constexpr float SPPI = Math::TAU / (float)STEP; |
| 201 | |
| 202 | for (int i = 1; i <= STEP; i++) { |
| 203 | p_surface_tool->set_color(p_color); |
| 204 | p_surface_tool->add_vertex(p_center + ((p_basis.xform(Vector3::UP * p_radius)).rotated(p_basis.xform(Vector3::RIGHT), SPPI * ((i - 1) % STEP)))); |
| 205 | p_surface_tool->set_color(p_color); |
| 206 | p_surface_tool->add_vertex(p_center + ((p_basis.xform(Vector3::UP * p_radius)).rotated(p_basis.xform(Vector3::RIGHT), SPPI * (i % STEP)))); |
| 207 | } |
| 208 | for (int i = 1; i <= STEP; i++) { |
| 209 | p_surface_tool->set_color(p_color); |
| 210 | p_surface_tool->add_vertex(p_center + ((p_basis.xform(Vector3::RIGHT * p_radius)).rotated(p_basis.xform(Vector3::FORWARD), SPPI * ((i - 1) % STEP)))); |
| 211 | p_surface_tool->set_color(p_color); |
| 212 | p_surface_tool->add_vertex(p_center + ((p_basis.xform(Vector3::RIGHT * p_radius)).rotated(p_basis.xform(Vector3::FORWARD), SPPI * (i % STEP)))); |
| 213 | } |
| 214 | for (int i = 1; i <= STEP; i++) { |
| 215 | p_surface_tool->set_color(p_color); |
| 216 | p_surface_tool->add_vertex(p_center + ((p_basis.xform(Vector3::FORWARD * p_radius)).rotated(p_basis.xform(Vector3::UP), SPPI * ((i - 1) % STEP)))); |
| 217 | p_surface_tool->set_color(p_color); |
| 218 | p_surface_tool->add_vertex(p_center + ((p_basis.xform(Vector3::FORWARD * p_radius)).rotated(p_basis.xform(Vector3::UP), SPPI * (i % STEP)))); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void SpringBoneSimulator3DGizmoPlugin::draw_line(Ref<SurfaceTool> &p_surface_tool, const Vector3 &p_begin_pos, const Vector3 &p_end_pos, const Color &p_color) { |
| 223 | p_surface_tool->set_color(p_color); |
| 224 | p_surface_tool->add_vertex(p_begin_pos); |
| 225 | p_surface_tool->set_color(p_color); |
| 226 | p_surface_tool->add_vertex(p_end_pos); |
| 227 | } |
| 228 | |
| 229 | // SpringBoneCollision3D |
| 230 | |
| 231 | SpringBoneCollision3DGizmoPlugin::SelectionMaterials SpringBoneCollision3DGizmoPlugin::selection_materials; |
| 232 | |
| 233 | SpringBoneCollision3DGizmoPlugin::SpringBoneCollision3DGizmoPlugin() { |
| 234 | selection_materials.unselected_mat.instantiate(); |
| 235 | selection_materials.unselected_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 236 | selection_materials.unselected_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
| 237 | selection_materials.unselected_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); |
| 238 | selection_materials.unselected_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); |
| 239 | selection_materials.unselected_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true); |
| 240 | |
| 241 | selection_materials.selected_mat.instantiate(); |
| 242 | Ref<Shader> sh; |
| 243 | sh.instantiate(); |
| 244 | sh->set_code(R"( |
| 245 | // Skeleton 3D gizmo bones shader. |
| 246 | |
| 247 | shader_type spatial; |
| 248 | render_mode unshaded, shadows_disabled; |
| 249 | |
| 250 | void vertex() { |
| 251 | if (!OUTPUT_IS_SRGB) { |
| 252 | COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb,vec3(0.04045))); |
| 253 | } |
| 254 | VERTEX = VERTEX; |
| 255 | POSITION = PROJECTION_MATRIX * VIEW_MATRIX * MODEL_MATRIX * vec4(VERTEX.xyz, 1.0); |
| 256 | POSITION.z = mix(POSITION.z, POSITION.w, 0.998); |
| 257 | } |
| 258 | |
| 259 | void fragment() { |
| 260 | ALBEDO = COLOR.rgb; |
| 261 | ALPHA = COLOR.a; |
| 262 | } |
| 263 | )"); |
| 264 | selection_materials.selected_mat->set_shader(sh); |
| 265 | } |
| 266 | |
| 267 | SpringBoneCollision3DGizmoPlugin::~SpringBoneCollision3DGizmoPlugin() { |
| 268 | selection_materials.unselected_mat.unref(); |
| 269 | selection_materials.selected_mat.unref(); |
| 270 | } |
| 271 | |
| 272 | bool SpringBoneCollision3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 273 | return Object::cast_to<SpringBoneCollision3D>(p_spatial) != nullptr; |
| 274 | } |
| 275 | |
| 276 | String SpringBoneCollision3DGizmoPlugin::get_gizmo_name() const { |
| 277 | return "SpringBoneCollision3D"; |
| 278 | } |
| 279 | |
| 280 | int SpringBoneCollision3DGizmoPlugin::get_priority() const { |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | void SpringBoneCollision3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 285 | SpringBoneCollision3D *collision = Object::cast_to<SpringBoneCollision3D>(p_gizmo->get_node_3d()); |
| 286 | p_gizmo->clear(); |
| 287 | |
| 288 | Ref<ArrayMesh> mesh = get_collision_mesh(collision, p_gizmo->is_selected()); |
| 289 | p_gizmo->add_mesh(mesh); |
| 290 | } |
| 291 | |
| 292 | Ref<ArrayMesh> SpringBoneCollision3DGizmoPlugin::get_collision_mesh(SpringBoneCollision3D *p_collision, bool p_is_selected) { |
| 293 | Color collision_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/spring_bone_collision"); |
| 294 | Color inside_collision_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/spring_bone_inside_collision"); |
| 295 | |
| 296 | Ref<SurfaceTool> surface_tool; |
| 297 | surface_tool.instantiate(); |
| 298 | surface_tool->begin(Mesh::PRIMITIVE_LINES); |
| 299 | |
| 300 | if (p_is_selected) { |
| 301 | surface_tool->set_material(selection_materials.selected_mat); |
| 302 | } else { |
| 303 | selection_materials.unselected_mat->set_albedo(collision_color); |
| 304 | surface_tool->set_material(selection_materials.unselected_mat); |
| 305 | } |
| 306 | |
| 307 | SpringBoneCollisionSphere3D *sphere = Object::cast_to<SpringBoneCollisionSphere3D>(p_collision); |
| 308 | if (sphere) { |
| 309 | draw_sphere(surface_tool, sphere->get_radius(), sphere->is_inside() ? inside_collision_color : collision_color); |
| 310 | return surface_tool->commit(); |
| 311 | } |
| 312 | |
| 313 | SpringBoneCollisionCapsule3D *capsule = Object::cast_to<SpringBoneCollisionCapsule3D>(p_collision); |
| 314 | if (capsule) { |
| 315 | draw_capsule(surface_tool, capsule->get_radius(), capsule->get_height(), capsule->is_inside() ? inside_collision_color : collision_color); |
| 316 | return surface_tool->commit(); |
| 317 | } |
| 318 | |
| 319 | SpringBoneCollisionPlane3D *plane = Object::cast_to<SpringBoneCollisionPlane3D>(p_collision); |
| 320 | if (plane) { |
| 321 | draw_plane(surface_tool, collision_color); |
| 322 | return surface_tool->commit(); |
| 323 | } |
| 324 | |
| 325 | return surface_tool->commit(); |
| 326 | } |
| 327 | |
| 328 | void SpringBoneCollision3DGizmoPlugin::draw_sphere(Ref<SurfaceTool> &p_surface_tool, float p_radius, const Color &p_color) { |
| 329 | static constexpr int STEP = 16; |
| 330 | static constexpr float SPPI = Math::TAU / (float)STEP; |
| 331 | |
| 332 | for (int i = 1; i <= STEP; i++) { |
| 333 | p_surface_tool->set_color(p_color); |
| 334 | p_surface_tool->add_vertex((Vector3::UP * p_radius).rotated(Vector3::RIGHT, SPPI * ((i - 1) % STEP))); |
| 335 | p_surface_tool->set_color(p_color); |
| 336 | p_surface_tool->add_vertex((Vector3::UP * p_radius).rotated(Vector3::RIGHT, SPPI * (i % STEP))); |
| 337 | } |
| 338 | for (int i = 1; i <= STEP; i++) { |
| 339 | p_surface_tool->set_color(p_color); |
| 340 | p_surface_tool->add_vertex((Vector3::RIGHT * p_radius).rotated(Vector3::FORWARD, SPPI * ((i - 1) % STEP))); |
| 341 | p_surface_tool->set_color(p_color); |
| 342 | p_surface_tool->add_vertex((Vector3::RIGHT * p_radius).rotated(Vector3::FORWARD, SPPI * (i % STEP))); |
| 343 | } |
| 344 | for (int i = 1; i <= STEP; i++) { |
| 345 | p_surface_tool->set_color(p_color); |
| 346 | p_surface_tool->add_vertex((Vector3::FORWARD * p_radius).rotated(Vector3::UP, SPPI * ((i - 1) % STEP))); |
| 347 | p_surface_tool->set_color(p_color); |
| 348 | p_surface_tool->add_vertex((Vector3::FORWARD * p_radius).rotated(Vector3::UP, SPPI * (i % STEP))); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void SpringBoneCollision3DGizmoPlugin::draw_capsule(Ref<SurfaceTool> &p_surface_tool, float p_radius, float p_height, const Color &p_color) { |
| 353 | static constexpr int STEP = 16; |
| 354 | static constexpr int HALF_STEP = 8; |
| 355 | static constexpr float SPPI = (float)Math::TAU / STEP; |
| 356 | static constexpr float HALF_PI = (float)Math::PI * 0.5f; |
| 357 | |
| 358 | Vector3 top = Vector3::UP * (p_height * 0.5 - p_radius); |
| 359 | Vector3 bottom = -top; |
| 360 | |
| 361 | for (int i = 1; i <= STEP; i++) { |
| 362 | p_surface_tool->set_color(p_color); |
| 363 | p_surface_tool->add_vertex((i - 1 < HALF_STEP ? top : bottom) + (Vector3::UP * p_radius).rotated(Vector3::RIGHT, -HALF_PI + SPPI * ((i - 1) % STEP))); |
| 364 | p_surface_tool->set_color(p_color); |
| 365 | p_surface_tool->add_vertex((i - 1 < HALF_STEP ? top : bottom) + (Vector3::UP * p_radius).rotated(Vector3::RIGHT, -HALF_PI + SPPI * (i % STEP))); |
| 366 | } |
| 367 | for (int i = 1; i <= STEP; i++) { |
| 368 | p_surface_tool->set_color(p_color); |
| 369 | p_surface_tool->add_vertex((i - 1 < HALF_STEP ? top : bottom) + (Vector3::RIGHT * p_radius).rotated(Vector3::FORWARD, SPPI * ((i - 1) % STEP))); |
| 370 | p_surface_tool->set_color(p_color); |
| 371 | p_surface_tool->add_vertex((i - 1 < HALF_STEP ? top : bottom) + (Vector3::RIGHT * p_radius).rotated(Vector3::FORWARD, SPPI * (i % STEP))); |
| 372 | } |
| 373 | for (int i = 1; i <= STEP; i++) { |
| 374 | p_surface_tool->set_color(p_color); |
| 375 | p_surface_tool->add_vertex(top + (Vector3::FORWARD * p_radius).rotated(Vector3::UP, SPPI * ((i - 1) % STEP))); |
| 376 | p_surface_tool->set_color(p_color); |
| 377 | p_surface_tool->add_vertex(top + (Vector3::FORWARD * p_radius).rotated(Vector3::UP, SPPI * (i % STEP))); |
| 378 | } |
| 379 | for (int i = 1; i <= STEP; i++) { |
| 380 | p_surface_tool->set_color(p_color); |
| 381 | p_surface_tool->add_vertex(bottom + (Vector3::FORWARD * p_radius).rotated(Vector3::UP, SPPI * ((i - 1) % STEP))); |
| 382 | p_surface_tool->set_color(p_color); |
| 383 | p_surface_tool->add_vertex(bottom + (Vector3::FORWARD * p_radius).rotated(Vector3::UP, SPPI * (i % STEP))); |
| 384 | } |
| 385 | LocalVector<Vector3> directions; |
| 386 | directions.resize(4); |
| 387 | directions[0] = Vector3::RIGHT; |
| 388 | directions[1] = Vector3::LEFT; |
| 389 | directions[2] = Vector3::FORWARD; |
| 390 | directions[3] = Vector3::BACK; |
| 391 | for (int i = 0; i < 4; i++) { |
| 392 | Vector3 dir = directions[i] * p_radius; |
| 393 | p_surface_tool->set_color(p_color); |
| 394 | p_surface_tool->add_vertex(top + dir); |
| 395 | p_surface_tool->add_vertex(bottom + dir); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | void SpringBoneCollision3DGizmoPlugin::draw_plane(Ref<SurfaceTool> &p_surface_tool, const Color &p_color) { |
| 400 | static constexpr float HALF_PI = (float)Math::PI * 0.5f; |
| 401 | static constexpr float ARROW_LENGTH = 0.3f; |
| 402 | static constexpr float ARROW_HALF_WIDTH = 0.05f; |
| 403 | static constexpr float ARROW_TOP_HALF_WIDTH = 0.1f; |
| 404 | static constexpr float ARROW_TOP = 0.5f; |
| 405 | static constexpr float RECT_SIZE = 1.0f; |
| 406 | static constexpr int RECT_STEP_COUNT = 9; |
| 407 | static constexpr float RECT_HALF_SIZE = RECT_SIZE * 0.5f; |
| 408 | static constexpr float RECT_STEP = RECT_SIZE / RECT_STEP_COUNT; |
| 409 | |
| 410 | p_surface_tool->set_color(p_color); |
| 411 | |
| 412 | // Draw arrow of the normal. |
| 413 | LocalVector<Vector3> arrow; |
| 414 | arrow.resize(7); |
| 415 | arrow[0] = Vector3(0, ARROW_TOP, 0); |
| 416 | arrow[1] = Vector3(-ARROW_TOP_HALF_WIDTH, ARROW_LENGTH, 0); |
| 417 | arrow[2] = Vector3(-ARROW_HALF_WIDTH, ARROW_LENGTH, 0); |
| 418 | arrow[3] = Vector3(-ARROW_HALF_WIDTH, 0, 0); |
| 419 | arrow[4] = Vector3(ARROW_HALF_WIDTH, 0, 0); |
| 420 | arrow[5] = Vector3(ARROW_HALF_WIDTH, ARROW_LENGTH, 0); |
| 421 | arrow[6] = Vector3(ARROW_TOP_HALF_WIDTH, ARROW_LENGTH, 0); |
| 422 | for (int i = 0; i < 2; i++) { |
| 423 | Basis ma(Vector3::UP, HALF_PI * i); |
| 424 | for (uint32_t j = 0; j < arrow.size(); j++) { |
| 425 | Vector3 v1 = arrow[j]; |
| 426 | Vector3 v2 = arrow[(j + 1) % arrow.size()]; |
| 427 | p_surface_tool->add_vertex(ma.xform(v1)); |
| 428 | p_surface_tool->add_vertex(ma.xform(v2)); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // Draw dashed line of the rect. |
| 433 | for (int i = 0; i < 4; i++) { |
| 434 | Basis ma(Vector3::UP, HALF_PI * i); |
| 435 | for (int j = 0; j < RECT_STEP_COUNT; j++) { |
| 436 | if (j % 2 == 1) { |
| 437 | continue; |
| 438 | } |
| 439 | Vector3 v1 = Vector3(RECT_HALF_SIZE, 0, RECT_HALF_SIZE - RECT_STEP * j); |
| 440 | Vector3 v2 = Vector3(RECT_HALF_SIZE, 0, RECT_HALF_SIZE - RECT_STEP * (j + 1)); |
| 441 | p_surface_tool->add_vertex(ma.xform(v1)); |
| 442 | p_surface_tool->add_vertex(ma.xform(v2)); |
| 443 | } |
| 444 | } |
| 445 | } |