1/**************************************************************************/
2/* reflection_probe_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 "reflection_probe_gizmo_plugin.h"
32
33#include "editor/editor_node.h"
34#include "editor/editor_settings.h"
35#include "editor/editor_string_names.h"
36#include "editor/editor_undo_redo_manager.h"
37#include "editor/plugins/gizmos/gizmo_3d_helper.h"
38#include "editor/plugins/node_3d_editor_plugin.h"
39#include "scene/3d/reflection_probe.h"
40
41ReflectionProbeGizmoPlugin::ReflectionProbeGizmoPlugin() {
42 helper.instantiate();
43 Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/reflection_probe", Color(0.6, 1, 0.5));
44
45 create_material("reflection_probe_material", gizmo_color);
46
47 gizmo_color.a = 0.5;
48 create_material("reflection_internal_material", gizmo_color);
49
50 gizmo_color.a = 0.1;
51 create_material("reflection_probe_solid_material", gizmo_color);
52
53 create_icon_material("reflection_probe_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoReflectionProbe"), EditorStringName(EditorIcons)));
54 create_handle_material("handles");
55}
56
57ReflectionProbeGizmoPlugin::~ReflectionProbeGizmoPlugin() {
58}
59
60bool ReflectionProbeGizmoPlugin::has_gizmo(Node3D *p_spatial) {
61 return Object::cast_to<ReflectionProbe>(p_spatial) != nullptr;
62}
63
64String ReflectionProbeGizmoPlugin::get_gizmo_name() const {
65 return "ReflectionProbe";
66}
67
68int ReflectionProbeGizmoPlugin::get_priority() const {
69 return -1;
70}
71
72String ReflectionProbeGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
73 if (p_id < 6) {
74 return helper->box_get_handle_name(p_id);
75 }
76 switch (p_id) {
77 case 6:
78 return "Origin X";
79 case 7:
80 return "Origin Y";
81 case 8:
82 return "Origin Z";
83 }
84 return "";
85}
86
87Variant ReflectionProbeGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
88 ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());
89 return AABB(probe->get_origin_offset(), probe->get_size());
90}
91
92void ReflectionProbeGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {
93 // The initial value is only used for resizing the box, so we only need AABB size.
94 AABB aabb = get_handle_value(p_gizmo, p_id, p_secondary);
95 helper->initialize_handle_action(aabb.size, p_gizmo->get_node_3d()->get_global_transform());
96}
97
98void ReflectionProbeGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
99 ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());
100
101 Vector3 sg[2];
102 helper->get_segment(p_camera, p_point, sg);
103
104 if (p_id < 6) {
105 Vector3 size = probe->get_size();
106 Vector3 position;
107 helper->box_set_handle(sg, p_id, size, position);
108 probe->set_size(size);
109 probe->set_global_position(position);
110 } else {
111 p_id -= 6;
112
113 Vector3 origin = probe->get_origin_offset();
114 origin[p_id] = 0;
115
116 Vector3 axis;
117 axis[p_id] = 1.0;
118
119 Vector3 ra, rb;
120 Geometry3D::get_closest_points_between_segments(origin - axis * 16384, origin + axis * 16384, sg[0], sg[1], ra, rb);
121 // Adjust the actual position to account for the gizmo handle position
122 float d = ra[p_id] + 0.25;
123 if (Node3DEditor::get_singleton()->is_snap_enabled()) {
124 d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
125 }
126
127 origin[p_id] = d;
128 probe->set_origin_offset(origin);
129 }
130}
131
132void ReflectionProbeGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
133 ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());
134
135 if (p_id < 6) {
136 helper->box_commit_handle(TTR("Change Probe Size"), p_cancel, probe);
137 return;
138 }
139
140 AABB restore = p_restore;
141
142 if (p_cancel) {
143 probe->set_origin_offset(restore.position);
144 probe->set_size(restore.size);
145 return;
146 }
147
148 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
149 ur->create_action(TTR("Change Probe Origin Offset"));
150 ur->add_do_method(probe, "set_origin_offset", probe->get_origin_offset());
151 ur->add_undo_method(probe, "set_origin_offset", restore.position);
152 ur->commit_action();
153}
154
155void ReflectionProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
156 p_gizmo->clear();
157
158 if (p_gizmo->is_selected()) {
159 ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());
160 Vector<Vector3> lines;
161 Vector<Vector3> internal_lines;
162 Vector3 size = probe->get_size();
163
164 AABB aabb;
165 aabb.position = -size / 2;
166 aabb.size = size;
167
168 for (int i = 0; i < 12; i++) {
169 Vector3 a, b;
170 aabb.get_edge(i, a, b);
171 lines.push_back(a);
172 lines.push_back(b);
173 }
174
175 for (int i = 0; i < 8; i++) {
176 Vector3 ep = aabb.get_endpoint(i);
177 internal_lines.push_back(probe->get_origin_offset());
178 internal_lines.push_back(ep);
179 }
180
181 Vector<Vector3> handles = helper->box_get_handles(probe->get_size());
182
183 for (int i = 0; i < 3; i++) {
184 Vector3 orig_handle = probe->get_origin_offset();
185 orig_handle[i] -= 0.25;
186 lines.push_back(orig_handle);
187 handles.push_back(orig_handle);
188
189 orig_handle[i] += 0.5;
190 lines.push_back(orig_handle);
191 }
192
193 Ref<Material> material = get_material("reflection_probe_material", p_gizmo);
194 Ref<Material> material_internal = get_material("reflection_internal_material", p_gizmo);
195
196 p_gizmo->add_lines(lines, material);
197 p_gizmo->add_lines(internal_lines, material_internal);
198
199 if (p_gizmo->is_selected()) {
200 Ref<Material> solid_material = get_material("reflection_probe_solid_material", p_gizmo);
201 p_gizmo->add_solid_box(solid_material, probe->get_size());
202 }
203
204 p_gizmo->add_handles(handles, get_material("handles"));
205 }
206
207 Ref<Material> icon = get_material("reflection_probe_icon", p_gizmo);
208 p_gizmo->add_unscaled_billboard(icon, 0.05);
209}
210