spring_bone_3d_gizmo_plugin.cpp 19 KB

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