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