Debian Codegraph @

editor/scene/3d/gizmos/marker_3d_gizmo_plugin.cpp

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