joint_3d_gizmo_plugin.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /**************************************************************************/
  2. /* joint_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 "joint_3d_gizmo_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "scene/3d/physics/joints/cone_twist_joint_3d.h"
  34. #include "scene/3d/physics/joints/generic_6dof_joint_3d.h"
  35. #include "scene/3d/physics/joints/hinge_joint_3d.h"
  36. #include "scene/3d/physics/joints/pin_joint_3d.h"
  37. #include "scene/3d/physics/joints/slider_joint_3d.h"
  38. #define BODY_A_RADIUS 0.25
  39. #define BODY_B_RADIUS 0.27
  40. Basis JointGizmosDrawer::look_body(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) {
  41. const Vector3 &p_eye(p_joint_transform.origin);
  42. const Vector3 &p_target(p_body_transform.origin);
  43. Vector3 v_x, v_y, v_z;
  44. // Look the body with X
  45. v_x = p_target - p_eye;
  46. v_x.normalize();
  47. v_z = v_x.cross(Vector3(0, 1, 0));
  48. v_z.normalize();
  49. v_y = v_z.cross(v_x);
  50. v_y.normalize();
  51. Basis base;
  52. base.set_columns(v_x, v_y, v_z);
  53. // Absorb current joint transform
  54. base = p_joint_transform.basis.inverse() * base;
  55. return base;
  56. }
  57. Basis JointGizmosDrawer::look_body_toward(Vector3::Axis p_axis, const Transform3D &joint_transform, const Transform3D &body_transform) {
  58. switch (p_axis) {
  59. case Vector3::AXIS_X:
  60. return look_body_toward_x(joint_transform, body_transform);
  61. case Vector3::AXIS_Y:
  62. return look_body_toward_y(joint_transform, body_transform);
  63. case Vector3::AXIS_Z:
  64. return look_body_toward_z(joint_transform, body_transform);
  65. default:
  66. return Basis();
  67. }
  68. }
  69. Basis JointGizmosDrawer::look_body_toward_x(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) {
  70. const Vector3 &p_eye(p_joint_transform.origin);
  71. const Vector3 &p_target(p_body_transform.origin);
  72. const Vector3 p_front(p_joint_transform.basis.get_column(0));
  73. Vector3 v_x, v_y, v_z;
  74. // Look the body with X
  75. v_x = p_target - p_eye;
  76. v_x.normalize();
  77. v_y = p_front.cross(v_x);
  78. v_y.normalize();
  79. v_z = v_y.cross(p_front);
  80. v_z.normalize();
  81. // Clamp X to FRONT axis
  82. v_x = p_front;
  83. v_x.normalize();
  84. Basis base;
  85. base.set_columns(v_x, v_y, v_z);
  86. // Absorb current joint transform
  87. base = p_joint_transform.basis.inverse() * base;
  88. return base;
  89. }
  90. Basis JointGizmosDrawer::look_body_toward_y(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) {
  91. const Vector3 &p_eye(p_joint_transform.origin);
  92. const Vector3 &p_target(p_body_transform.origin);
  93. const Vector3 p_up(p_joint_transform.basis.get_column(1));
  94. Vector3 v_x, v_y, v_z;
  95. // Look the body with X
  96. v_x = p_target - p_eye;
  97. v_x.normalize();
  98. v_z = v_x.cross(p_up);
  99. v_z.normalize();
  100. v_x = p_up.cross(v_z);
  101. v_x.normalize();
  102. // Clamp Y to UP axis
  103. v_y = p_up;
  104. v_y.normalize();
  105. Basis base;
  106. base.set_columns(v_x, v_y, v_z);
  107. // Absorb current joint transform
  108. base = p_joint_transform.basis.inverse() * base;
  109. return base;
  110. }
  111. Basis JointGizmosDrawer::look_body_toward_z(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) {
  112. const Vector3 &p_eye(p_joint_transform.origin);
  113. const Vector3 &p_target(p_body_transform.origin);
  114. const Vector3 p_lateral(p_joint_transform.basis.get_column(2));
  115. Vector3 v_x, v_y, v_z;
  116. // Look the body with X
  117. v_x = p_target - p_eye;
  118. v_x.normalize();
  119. v_z = p_lateral;
  120. v_z.normalize();
  121. v_y = v_z.cross(v_x);
  122. v_y.normalize();
  123. // Clamp X to Z axis
  124. v_x = v_y.cross(v_z);
  125. v_x.normalize();
  126. Basis base;
  127. base.set_columns(v_x, v_y, v_z);
  128. // Absorb current joint transform
  129. base = p_joint_transform.basis.inverse() * base;
  130. return base;
  131. }
  132. void JointGizmosDrawer::draw_circle(Vector3::Axis p_axis, real_t p_radius, const Transform3D &p_offset, const Basis &p_base, real_t p_limit_lower, real_t p_limit_upper, Vector<Vector3> &r_points, bool p_inverse) {
  133. if (p_limit_lower == p_limit_upper) {
  134. r_points.push_back(p_offset.translated_local(Vector3()).origin);
  135. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(0.5, 0, 0))).origin);
  136. } else {
  137. if (p_limit_lower > p_limit_upper) {
  138. p_limit_lower = -Math::PI;
  139. p_limit_upper = Math::PI;
  140. }
  141. const int points = 32;
  142. for (int i = 0; i < points; i++) {
  143. real_t s = p_limit_lower + i * (p_limit_upper - p_limit_lower) / points;
  144. real_t n = p_limit_lower + (i + 1) * (p_limit_upper - p_limit_lower) / points;
  145. Vector3 from;
  146. Vector3 to;
  147. switch (p_axis) {
  148. case Vector3::AXIS_X:
  149. if (p_inverse) {
  150. from = p_base.xform(Vector3(0, Math::sin(s), Math::cos(s))) * p_radius;
  151. to = p_base.xform(Vector3(0, Math::sin(n), Math::cos(n))) * p_radius;
  152. } else {
  153. from = p_base.xform(Vector3(0, -Math::sin(s), Math::cos(s))) * p_radius;
  154. to = p_base.xform(Vector3(0, -Math::sin(n), Math::cos(n))) * p_radius;
  155. }
  156. break;
  157. case Vector3::AXIS_Y:
  158. if (p_inverse) {
  159. from = p_base.xform(Vector3(Math::cos(s), 0, -Math::sin(s))) * p_radius;
  160. to = p_base.xform(Vector3(Math::cos(n), 0, -Math::sin(n))) * p_radius;
  161. } else {
  162. from = p_base.xform(Vector3(Math::cos(s), 0, Math::sin(s))) * p_radius;
  163. to = p_base.xform(Vector3(Math::cos(n), 0, Math::sin(n))) * p_radius;
  164. }
  165. break;
  166. case Vector3::AXIS_Z:
  167. from = p_base.xform(Vector3(Math::cos(s), Math::sin(s), 0)) * p_radius;
  168. to = p_base.xform(Vector3(Math::cos(n), Math::sin(n), 0)) * p_radius;
  169. break;
  170. }
  171. if (i == points - 1) {
  172. r_points.push_back(p_offset.translated_local(to).origin);
  173. r_points.push_back(p_offset.translated_local(Vector3()).origin);
  174. }
  175. if (i == 0) {
  176. r_points.push_back(p_offset.translated_local(from).origin);
  177. r_points.push_back(p_offset.translated_local(Vector3()).origin);
  178. }
  179. r_points.push_back(p_offset.translated_local(from).origin);
  180. r_points.push_back(p_offset.translated_local(to).origin);
  181. }
  182. r_points.push_back(p_offset.translated_local(Vector3(0, p_radius * 1.5, 0)).origin);
  183. r_points.push_back(p_offset.translated_local(Vector3()).origin);
  184. }
  185. }
  186. void JointGizmosDrawer::draw_cone(const Transform3D &p_offset, const Basis &p_base, real_t p_swing, real_t p_twist, Vector<Vector3> &r_points) {
  187. float r = 1.0;
  188. float w = r * Math::sin(p_swing);
  189. float d = r * Math::cos(p_swing);
  190. //swing
  191. for (int i = 0; i < 360; i += 10) {
  192. float ra = Math::deg_to_rad((float)i);
  193. float rb = Math::deg_to_rad((float)i + 10);
  194. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;
  195. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;
  196. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(d, a.x, a.y))).origin);
  197. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(d, b.x, b.y))).origin);
  198. if (i % 90 == 0) {
  199. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(d, a.x, a.y))).origin);
  200. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3())).origin);
  201. }
  202. }
  203. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3())).origin);
  204. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(1, 0, 0))).origin);
  205. /// Twist
  206. float ts = Math::rad_to_deg(p_twist);
  207. ts = MIN(ts, 720);
  208. for (int i = 0; i < int(ts); i += 5) {
  209. float ra = Math::deg_to_rad((float)i);
  210. float rb = Math::deg_to_rad((float)i + 5);
  211. float c = i / 720.0;
  212. float cn = (i + 5) / 720.0;
  213. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w * c;
  214. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w * cn;
  215. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(c, a.x, a.y))).origin);
  216. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(cn, b.x, b.y))).origin);
  217. }
  218. }
  219. ////
  220. Joint3DGizmoPlugin::Joint3DGizmoPlugin() {
  221. create_material("joint_material", EDITOR_GET("editors/3d_gizmos/gizmo_colors/joint"));
  222. create_material("joint_body_a_material", EDITOR_GET("editors/3d_gizmos/gizmo_colors/joint_body_a"));
  223. create_material("joint_body_b_material", EDITOR_GET("editors/3d_gizmos/gizmo_colors/joint_body_b"));
  224. update_timer = memnew(Timer);
  225. update_timer->set_name("JointGizmoUpdateTimer");
  226. update_timer->set_wait_time(1.0 / 120.0);
  227. update_timer->connect("timeout", callable_mp(this, &Joint3DGizmoPlugin::incremental_update_gizmos));
  228. update_timer->set_autostart(true);
  229. callable_mp((Node *)EditorNode::get_singleton(), &Node::add_child).call_deferred(update_timer, false, Node::INTERNAL_MODE_DISABLED);
  230. }
  231. void Joint3DGizmoPlugin::incremental_update_gizmos() {
  232. if (!current_gizmos.is_empty()) {
  233. HashSet<EditorNode3DGizmo *>::Iterator E = current_gizmos.find(last_drawn);
  234. if (E) {
  235. ++E;
  236. }
  237. if (!E) {
  238. E = current_gizmos.begin();
  239. }
  240. redraw(*E);
  241. last_drawn = *E;
  242. }
  243. }
  244. bool Joint3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  245. return Object::cast_to<Joint3D>(p_spatial) != nullptr;
  246. }
  247. String Joint3DGizmoPlugin::get_gizmo_name() const {
  248. return "Joint3D";
  249. }
  250. int Joint3DGizmoPlugin::get_priority() const {
  251. return -1;
  252. }
  253. void Joint3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  254. Joint3D *joint = Object::cast_to<Joint3D>(p_gizmo->get_node_3d());
  255. p_gizmo->clear();
  256. Node3D *node_body_a = nullptr;
  257. if (!joint->get_node_a().is_empty()) {
  258. node_body_a = Object::cast_to<Node3D>(joint->get_node(joint->get_node_a()));
  259. }
  260. Node3D *node_body_b = nullptr;
  261. if (!joint->get_node_b().is_empty()) {
  262. node_body_b = Object::cast_to<Node3D>(joint->get_node(joint->get_node_b()));
  263. }
  264. if (!node_body_a && !node_body_b) {
  265. return;
  266. }
  267. Ref<Material> common_material = get_material("joint_material", p_gizmo);
  268. Ref<Material> body_a_material = get_material("joint_body_a_material", p_gizmo);
  269. Ref<Material> body_b_material = get_material("joint_body_b_material", p_gizmo);
  270. Vector<Vector3> points;
  271. Vector<Vector3> body_a_points;
  272. Vector<Vector3> body_b_points;
  273. if (Object::cast_to<PinJoint3D>(joint)) {
  274. CreatePinJointGizmo(Transform3D(), points);
  275. p_gizmo->add_collision_segments(points);
  276. p_gizmo->add_lines(points, common_material);
  277. }
  278. HingeJoint3D *hinge = Object::cast_to<HingeJoint3D>(joint);
  279. if (hinge) {
  280. CreateHingeJointGizmo(
  281. Transform3D(),
  282. hinge->get_global_transform(),
  283. node_body_a ? node_body_a->get_global_transform() : Transform3D(),
  284. node_body_b ? node_body_b->get_global_transform() : Transform3D(),
  285. hinge->get_param(HingeJoint3D::PARAM_LIMIT_LOWER),
  286. hinge->get_param(HingeJoint3D::PARAM_LIMIT_UPPER),
  287. hinge->get_flag(HingeJoint3D::FLAG_USE_LIMIT),
  288. points,
  289. node_body_a ? &body_a_points : nullptr,
  290. node_body_b ? &body_b_points : nullptr);
  291. p_gizmo->add_collision_segments(points);
  292. p_gizmo->add_collision_segments(body_a_points);
  293. p_gizmo->add_collision_segments(body_b_points);
  294. p_gizmo->add_lines(points, common_material);
  295. p_gizmo->add_lines(body_a_points, body_a_material);
  296. p_gizmo->add_lines(body_b_points, body_b_material);
  297. }
  298. SliderJoint3D *slider = Object::cast_to<SliderJoint3D>(joint);
  299. if (slider) {
  300. CreateSliderJointGizmo(
  301. Transform3D(),
  302. slider->get_global_transform(),
  303. node_body_a ? node_body_a->get_global_transform() : Transform3D(),
  304. node_body_b ? node_body_b->get_global_transform() : Transform3D(),
  305. slider->get_param(SliderJoint3D::PARAM_ANGULAR_LIMIT_LOWER),
  306. slider->get_param(SliderJoint3D::PARAM_ANGULAR_LIMIT_UPPER),
  307. slider->get_param(SliderJoint3D::PARAM_LINEAR_LIMIT_LOWER),
  308. slider->get_param(SliderJoint3D::PARAM_LINEAR_LIMIT_UPPER),
  309. points,
  310. node_body_a ? &body_a_points : nullptr,
  311. node_body_b ? &body_b_points : nullptr);
  312. p_gizmo->add_collision_segments(points);
  313. p_gizmo->add_collision_segments(body_a_points);
  314. p_gizmo->add_collision_segments(body_b_points);
  315. p_gizmo->add_lines(points, common_material);
  316. p_gizmo->add_lines(body_a_points, body_a_material);
  317. p_gizmo->add_lines(body_b_points, body_b_material);
  318. }
  319. ConeTwistJoint3D *cone = Object::cast_to<ConeTwistJoint3D>(joint);
  320. if (cone) {
  321. CreateConeTwistJointGizmo(
  322. Transform3D(),
  323. cone->get_global_transform(),
  324. node_body_a ? node_body_a->get_global_transform() : Transform3D(),
  325. node_body_b ? node_body_b->get_global_transform() : Transform3D(),
  326. cone->get_param(ConeTwistJoint3D::PARAM_SWING_SPAN),
  327. cone->get_param(ConeTwistJoint3D::PARAM_TWIST_SPAN),
  328. node_body_a ? &body_a_points : nullptr,
  329. node_body_b ? &body_b_points : nullptr);
  330. p_gizmo->add_collision_segments(body_a_points);
  331. p_gizmo->add_collision_segments(body_b_points);
  332. p_gizmo->add_lines(body_a_points, body_a_material);
  333. p_gizmo->add_lines(body_b_points, body_b_material);
  334. }
  335. Generic6DOFJoint3D *gen = Object::cast_to<Generic6DOFJoint3D>(joint);
  336. if (gen) {
  337. CreateGeneric6DOFJointGizmo(
  338. Transform3D(),
  339. gen->get_global_transform(),
  340. node_body_a ? node_body_a->get_global_transform() : Transform3D(),
  341. node_body_b ? node_body_b->get_global_transform() : Transform3D(),
  342. gen->get_param_x(Generic6DOFJoint3D::PARAM_ANGULAR_LOWER_LIMIT),
  343. gen->get_param_x(Generic6DOFJoint3D::PARAM_ANGULAR_UPPER_LIMIT),
  344. gen->get_param_x(Generic6DOFJoint3D::PARAM_LINEAR_LOWER_LIMIT),
  345. gen->get_param_x(Generic6DOFJoint3D::PARAM_LINEAR_UPPER_LIMIT),
  346. gen->get_flag_x(Generic6DOFJoint3D::FLAG_ENABLE_ANGULAR_LIMIT),
  347. gen->get_flag_x(Generic6DOFJoint3D::FLAG_ENABLE_LINEAR_LIMIT),
  348. gen->get_param_y(Generic6DOFJoint3D::PARAM_ANGULAR_LOWER_LIMIT),
  349. gen->get_param_y(Generic6DOFJoint3D::PARAM_ANGULAR_UPPER_LIMIT),
  350. gen->get_param_y(Generic6DOFJoint3D::PARAM_LINEAR_LOWER_LIMIT),
  351. gen->get_param_y(Generic6DOFJoint3D::PARAM_LINEAR_UPPER_LIMIT),
  352. gen->get_flag_y(Generic6DOFJoint3D::FLAG_ENABLE_ANGULAR_LIMIT),
  353. gen->get_flag_y(Generic6DOFJoint3D::FLAG_ENABLE_LINEAR_LIMIT),
  354. gen->get_param_z(Generic6DOFJoint3D::PARAM_ANGULAR_LOWER_LIMIT),
  355. gen->get_param_z(Generic6DOFJoint3D::PARAM_ANGULAR_UPPER_LIMIT),
  356. gen->get_param_z(Generic6DOFJoint3D::PARAM_LINEAR_LOWER_LIMIT),
  357. gen->get_param_z(Generic6DOFJoint3D::PARAM_LINEAR_UPPER_LIMIT),
  358. gen->get_flag_z(Generic6DOFJoint3D::FLAG_ENABLE_ANGULAR_LIMIT),
  359. gen->get_flag_z(Generic6DOFJoint3D::FLAG_ENABLE_LINEAR_LIMIT),
  360. points,
  361. node_body_a ? &body_a_points : nullptr,
  362. node_body_a ? &body_b_points : nullptr);
  363. p_gizmo->add_collision_segments(points);
  364. p_gizmo->add_collision_segments(body_a_points);
  365. p_gizmo->add_collision_segments(body_b_points);
  366. p_gizmo->add_lines(points, common_material);
  367. p_gizmo->add_lines(body_a_points, body_a_material);
  368. p_gizmo->add_lines(body_b_points, body_b_material);
  369. }
  370. }
  371. void Joint3DGizmoPlugin::CreatePinJointGizmo(const Transform3D &p_offset, Vector<Vector3> &r_cursor_points) {
  372. float cs = 0.25;
  373. r_cursor_points.push_back(p_offset.translated_local(Vector3(+cs, 0, 0)).origin);
  374. r_cursor_points.push_back(p_offset.translated_local(Vector3(-cs, 0, 0)).origin);
  375. r_cursor_points.push_back(p_offset.translated_local(Vector3(0, +cs, 0)).origin);
  376. r_cursor_points.push_back(p_offset.translated_local(Vector3(0, -cs, 0)).origin);
  377. r_cursor_points.push_back(p_offset.translated_local(Vector3(0, 0, +cs)).origin);
  378. r_cursor_points.push_back(p_offset.translated_local(Vector3(0, 0, -cs)).origin);
  379. }
  380. void Joint3DGizmoPlugin::CreateHingeJointGizmo(const Transform3D &p_offset, const Transform3D &p_trs_joint, const Transform3D &p_trs_body_a, const Transform3D &p_trs_body_b, real_t p_limit_lower, real_t p_limit_upper, bool p_use_limit, Vector<Vector3> &r_common_points, Vector<Vector3> *r_body_a_points, Vector<Vector3> *r_body_b_points) {
  381. r_common_points.push_back(p_offset.translated_local(Vector3(0, 0, 0.5)).origin);
  382. r_common_points.push_back(p_offset.translated_local(Vector3(0, 0, -0.5)).origin);
  383. if (!p_use_limit) {
  384. p_limit_upper = -1;
  385. p_limit_lower = 0;
  386. }
  387. if (r_body_a_points) {
  388. JointGizmosDrawer::draw_circle(Vector3::AXIS_Z,
  389. BODY_A_RADIUS,
  390. p_offset,
  391. JointGizmosDrawer::look_body_toward_z(p_trs_joint, p_trs_body_a),
  392. p_limit_lower,
  393. p_limit_upper,
  394. *r_body_a_points);
  395. }
  396. if (r_body_b_points) {
  397. JointGizmosDrawer::draw_circle(Vector3::AXIS_Z,
  398. BODY_B_RADIUS,
  399. p_offset,
  400. JointGizmosDrawer::look_body_toward_z(p_trs_joint, p_trs_body_b),
  401. p_limit_lower,
  402. p_limit_upper,
  403. *r_body_b_points);
  404. }
  405. }
  406. void Joint3DGizmoPlugin::CreateSliderJointGizmo(const Transform3D &p_offset, const Transform3D &p_trs_joint, const Transform3D &p_trs_body_a, const Transform3D &p_trs_body_b, real_t p_angular_limit_lower, real_t p_angular_limit_upper, real_t p_linear_limit_lower, real_t p_linear_limit_upper, Vector<Vector3> &r_points, Vector<Vector3> *r_body_a_points, Vector<Vector3> *r_body_b_points) {
  407. p_linear_limit_lower = -p_linear_limit_lower;
  408. p_linear_limit_upper = -p_linear_limit_upper;
  409. float cs = 0.25;
  410. r_points.push_back(p_offset.translated_local(Vector3(0, 0, 0.5)).origin);
  411. r_points.push_back(p_offset.translated_local(Vector3(0, 0, -0.5)).origin);
  412. if (p_linear_limit_lower >= p_linear_limit_upper) {
  413. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, 0, 0)).origin);
  414. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, 0, 0)).origin);
  415. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, -cs)).origin);
  416. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, cs)).origin);
  417. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, cs)).origin);
  418. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, cs)).origin);
  419. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, cs)).origin);
  420. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, -cs)).origin);
  421. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, -cs)).origin);
  422. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, -cs)).origin);
  423. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, -cs)).origin);
  424. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, cs)).origin);
  425. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, cs)).origin);
  426. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, cs)).origin);
  427. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, cs)).origin);
  428. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, -cs)).origin);
  429. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, -cs)).origin);
  430. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, -cs)).origin);
  431. } else {
  432. r_points.push_back(p_offset.translated_local(Vector3(+cs * 2, 0, 0)).origin);
  433. r_points.push_back(p_offset.translated_local(Vector3(-cs * 2, 0, 0)).origin);
  434. }
  435. if (r_body_a_points) {
  436. JointGizmosDrawer::draw_circle(
  437. Vector3::AXIS_X,
  438. BODY_A_RADIUS,
  439. p_offset,
  440. JointGizmosDrawer::look_body_toward(Vector3::AXIS_X, p_trs_joint, p_trs_body_a),
  441. p_angular_limit_lower,
  442. p_angular_limit_upper,
  443. *r_body_a_points);
  444. }
  445. if (r_body_b_points) {
  446. JointGizmosDrawer::draw_circle(
  447. Vector3::AXIS_X,
  448. BODY_B_RADIUS,
  449. p_offset,
  450. JointGizmosDrawer::look_body_toward(Vector3::AXIS_X, p_trs_joint, p_trs_body_b),
  451. p_angular_limit_lower,
  452. p_angular_limit_upper,
  453. *r_body_b_points,
  454. true);
  455. }
  456. }
  457. void Joint3DGizmoPlugin::CreateConeTwistJointGizmo(const Transform3D &p_offset, const Transform3D &p_trs_joint, const Transform3D &p_trs_body_a, const Transform3D &p_trs_body_b, real_t p_swing, real_t p_twist, Vector<Vector3> *r_body_a_points, Vector<Vector3> *r_body_b_points) {
  458. if (r_body_a_points) {
  459. JointGizmosDrawer::draw_cone(
  460. p_offset,
  461. JointGizmosDrawer::look_body(p_trs_joint, p_trs_body_a),
  462. p_swing,
  463. p_twist,
  464. *r_body_a_points);
  465. }
  466. if (r_body_b_points) {
  467. JointGizmosDrawer::draw_cone(
  468. p_offset,
  469. JointGizmosDrawer::look_body(p_trs_joint, p_trs_body_b),
  470. p_swing,
  471. p_twist,
  472. *r_body_b_points);
  473. }
  474. }
  475. void Joint3DGizmoPlugin::CreateGeneric6DOFJointGizmo(
  476. const Transform3D &p_offset,
  477. const Transform3D &p_trs_joint,
  478. const Transform3D &p_trs_body_a,
  479. const Transform3D &p_trs_body_b,
  480. real_t p_angular_limit_lower_x,
  481. real_t p_angular_limit_upper_x,
  482. real_t p_linear_limit_lower_x,
  483. real_t p_linear_limit_upper_x,
  484. bool p_enable_angular_limit_x,
  485. bool p_enable_linear_limit_x,
  486. real_t p_angular_limit_lower_y,
  487. real_t p_angular_limit_upper_y,
  488. real_t p_linear_limit_lower_y,
  489. real_t p_linear_limit_upper_y,
  490. bool p_enable_angular_limit_y,
  491. bool p_enable_linear_limit_y,
  492. real_t p_angular_limit_lower_z,
  493. real_t p_angular_limit_upper_z,
  494. real_t p_linear_limit_lower_z,
  495. real_t p_linear_limit_upper_z,
  496. bool p_enable_angular_limit_z,
  497. bool p_enable_linear_limit_z,
  498. Vector<Vector3> &r_points,
  499. Vector<Vector3> *r_body_a_points,
  500. Vector<Vector3> *r_body_b_points) {
  501. float cs = 0.25;
  502. for (int ax = 0; ax < 3; ax++) {
  503. float ll = 0;
  504. float ul = 0;
  505. float lll = 0;
  506. float lul = 0;
  507. int a1 = 0;
  508. int a2 = 0;
  509. int a3 = 0;
  510. bool enable_ang = false;
  511. bool enable_lin = false;
  512. switch (ax) {
  513. case 0:
  514. ll = p_angular_limit_lower_x;
  515. ul = p_angular_limit_upper_x;
  516. lll = -p_linear_limit_lower_x;
  517. lul = -p_linear_limit_upper_x;
  518. enable_ang = p_enable_angular_limit_x;
  519. enable_lin = p_enable_linear_limit_x;
  520. a1 = 0;
  521. a2 = 1;
  522. a3 = 2;
  523. break;
  524. case 1:
  525. ll = p_angular_limit_lower_y;
  526. ul = p_angular_limit_upper_y;
  527. lll = -p_linear_limit_lower_y;
  528. lul = -p_linear_limit_upper_y;
  529. enable_ang = p_enable_angular_limit_y;
  530. enable_lin = p_enable_linear_limit_y;
  531. a1 = 1;
  532. a2 = 2;
  533. a3 = 0;
  534. break;
  535. case 2:
  536. ll = p_angular_limit_lower_z;
  537. ul = p_angular_limit_upper_z;
  538. lll = -p_linear_limit_lower_z;
  539. lul = -p_linear_limit_upper_z;
  540. enable_ang = p_enable_angular_limit_z;
  541. enable_lin = p_enable_linear_limit_z;
  542. a1 = 2;
  543. a2 = 0;
  544. a3 = 1;
  545. break;
  546. }
  547. #define ADD_VTX(x, y, z) \
  548. { \
  549. Vector3 v; \
  550. v[a1] = (x); \
  551. v[a2] = (y); \
  552. v[a3] = (z); \
  553. r_points.push_back(p_offset.translated_local(v).origin); \
  554. }
  555. if (enable_lin && lll >= lul) {
  556. ADD_VTX(lul, 0, 0);
  557. ADD_VTX(lll, 0, 0);
  558. ADD_VTX(lul, -cs, -cs);
  559. ADD_VTX(lul, -cs, cs);
  560. ADD_VTX(lul, -cs, cs);
  561. ADD_VTX(lul, cs, cs);
  562. ADD_VTX(lul, cs, cs);
  563. ADD_VTX(lul, cs, -cs);
  564. ADD_VTX(lul, cs, -cs);
  565. ADD_VTX(lul, -cs, -cs);
  566. ADD_VTX(lll, -cs, -cs);
  567. ADD_VTX(lll, -cs, cs);
  568. ADD_VTX(lll, -cs, cs);
  569. ADD_VTX(lll, cs, cs);
  570. ADD_VTX(lll, cs, cs);
  571. ADD_VTX(lll, cs, -cs);
  572. ADD_VTX(lll, cs, -cs);
  573. ADD_VTX(lll, -cs, -cs);
  574. } else {
  575. ADD_VTX(+cs * 2, 0, 0);
  576. ADD_VTX(-cs * 2, 0, 0);
  577. }
  578. if (!enable_ang) {
  579. ll = 0;
  580. ul = -1;
  581. }
  582. if (r_body_a_points) {
  583. JointGizmosDrawer::draw_circle(
  584. static_cast<Vector3::Axis>(ax),
  585. BODY_A_RADIUS,
  586. p_offset,
  587. JointGizmosDrawer::look_body_toward(static_cast<Vector3::Axis>(ax), p_trs_joint, p_trs_body_a),
  588. ll,
  589. ul,
  590. *r_body_a_points,
  591. true);
  592. }
  593. if (r_body_b_points) {
  594. JointGizmosDrawer::draw_circle(
  595. static_cast<Vector3::Axis>(ax),
  596. BODY_B_RADIUS,
  597. p_offset,
  598. JointGizmosDrawer::look_body_toward(static_cast<Vector3::Axis>(ax), p_trs_joint, p_trs_body_b),
  599. ll,
  600. ul,
  601. *r_body_b_points);
  602. }
  603. }
  604. #undef ADD_VTX
  605. }