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