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