marker_3d_gizmo_plugin.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**************************************************************************/
  2. /* marker_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 "marker_3d_gizmo_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_string_names.h"
  33. #include "scene/3d/marker_3d.h"
  34. Marker3DGizmoPlugin::Marker3DGizmoPlugin() {
  35. pos3d_mesh.instantiate();
  36. Vector<Vector3> cursor_points;
  37. Vector<Color> cursor_colors;
  38. const float cs = 1.0;
  39. // Add more points to create a "hard stop" in the color gradient.
  40. cursor_points.push_back(Vector3(+cs, 0, 0));
  41. cursor_points.push_back(Vector3());
  42. cursor_points.push_back(Vector3());
  43. cursor_points.push_back(Vector3(-cs, 0, 0));
  44. cursor_points.push_back(Vector3(0, +cs, 0));
  45. cursor_points.push_back(Vector3());
  46. cursor_points.push_back(Vector3());
  47. cursor_points.push_back(Vector3(0, -cs, 0));
  48. cursor_points.push_back(Vector3(0, 0, +cs));
  49. cursor_points.push_back(Vector3());
  50. cursor_points.push_back(Vector3());
  51. cursor_points.push_back(Vector3(0, 0, -cs));
  52. // Use the axis color which is brighter for the positive axis.
  53. // Use a darkened axis color for the negative axis.
  54. // This makes it possible to see in which direction the Marker3D node is rotated
  55. // (which can be important depending on how it's used).
  56. const Color color_x = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("axis_x_color"), EditorStringName(Editor));
  57. cursor_colors.push_back(color_x);
  58. cursor_colors.push_back(color_x);
  59. // FIXME: Use less strong darkening factor once GH-48573 is fixed.
  60. // The current darkening factor compensates for lines being too bright in the 3D editor.
  61. cursor_colors.push_back(color_x.lerp(Color(0, 0, 0), 0.75));
  62. cursor_colors.push_back(color_x.lerp(Color(0, 0, 0), 0.75));
  63. const Color color_y = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("axis_y_color"), EditorStringName(Editor));
  64. cursor_colors.push_back(color_y);
  65. cursor_colors.push_back(color_y);
  66. cursor_colors.push_back(color_y.lerp(Color(0, 0, 0), 0.75));
  67. cursor_colors.push_back(color_y.lerp(Color(0, 0, 0), 0.75));
  68. const Color color_z = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("axis_z_color"), EditorStringName(Editor));
  69. cursor_colors.push_back(color_z);
  70. cursor_colors.push_back(color_z);
  71. cursor_colors.push_back(color_z.lerp(Color(0, 0, 0), 0.75));
  72. cursor_colors.push_back(color_z.lerp(Color(0, 0, 0), 0.75));
  73. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  74. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  75. mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  76. mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  77. mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  78. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  79. Array d;
  80. d.resize(RS::ARRAY_MAX);
  81. d[Mesh::ARRAY_VERTEX] = cursor_points;
  82. d[Mesh::ARRAY_COLOR] = cursor_colors;
  83. pos3d_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, d);
  84. pos3d_mesh->surface_set_material(0, mat);
  85. }
  86. bool Marker3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  87. return Object::cast_to<Marker3D>(p_spatial) != nullptr;
  88. }
  89. String Marker3DGizmoPlugin::get_gizmo_name() const {
  90. return "Marker3D";
  91. }
  92. int Marker3DGizmoPlugin::get_priority() const {
  93. return -1;
  94. }
  95. void Marker3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  96. const Marker3D *marker = Object::cast_to<Marker3D>(p_gizmo->get_node_3d());
  97. const real_t extents = marker->get_gizmo_extents();
  98. const Transform3D xform(Basis::from_scale(Vector3(extents, extents, extents)));
  99. p_gizmo->clear();
  100. p_gizmo->add_mesh(pos3d_mesh, Ref<Material>(), xform);
  101. const Vector<Vector3> points = {
  102. Vector3(-extents, 0, 0),
  103. Vector3(+extents, 0, 0),
  104. Vector3(0, -extents, 0),
  105. Vector3(0, +extents, 0),
  106. Vector3(0, 0, -extents),
  107. Vector3(0, 0, +extents),
  108. };
  109. p_gizmo->add_collision_segments(points);
  110. }