1 | /**************************************************************************/ |
2 | /* gpu_particles_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 "gpu_particles_3d_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/node_3d_editor_plugin.h" |
38 | #include "scene/3d/gpu_particles_3d.h" |
39 | |
40 | GPUParticles3DGizmoPlugin::GPUParticles3DGizmoPlugin() { |
41 | Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/particles" , Color(0.8, 0.7, 0.4)); |
42 | create_material("particles_material" , gizmo_color); |
43 | gizmo_color.a = MAX((gizmo_color.a - 0.2) * 0.02, 0.0); |
44 | create_material("particles_solid_material" , gizmo_color); |
45 | create_icon_material("particles_icon" , EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoGPUParticles3D" ), EditorStringName(EditorIcons))); |
46 | create_handle_material("handles" ); |
47 | } |
48 | |
49 | bool GPUParticles3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
50 | return Object::cast_to<GPUParticles3D>(p_spatial) != nullptr; |
51 | } |
52 | |
53 | String GPUParticles3DGizmoPlugin::get_gizmo_name() const { |
54 | return "GPUParticles3D" ; |
55 | } |
56 | |
57 | int GPUParticles3DGizmoPlugin::get_priority() const { |
58 | return -1; |
59 | } |
60 | |
61 | bool GPUParticles3DGizmoPlugin::is_selectable_when_hidden() const { |
62 | return true; |
63 | } |
64 | |
65 | String GPUParticles3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
66 | switch (p_id) { |
67 | case 0: |
68 | return "Size X" ; |
69 | case 1: |
70 | return "Size Y" ; |
71 | case 2: |
72 | return "Size Z" ; |
73 | case 3: |
74 | return "Pos X" ; |
75 | case 4: |
76 | return "Pos Y" ; |
77 | case 5: |
78 | return "Pos Z" ; |
79 | } |
80 | |
81 | return "" ; |
82 | } |
83 | |
84 | Variant GPUParticles3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
85 | GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(p_gizmo->get_node_3d()); |
86 | return particles->get_visibility_aabb(); |
87 | } |
88 | |
89 | void GPUParticles3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) { |
90 | GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(p_gizmo->get_node_3d()); |
91 | |
92 | Transform3D gt = particles->get_global_transform(); |
93 | Transform3D gi = gt.affine_inverse(); |
94 | |
95 | bool move = p_id >= 3; |
96 | p_id = p_id % 3; |
97 | |
98 | AABB aabb = particles->get_visibility_aabb(); |
99 | Vector3 ray_from = p_camera->project_ray_origin(p_point); |
100 | Vector3 ray_dir = p_camera->project_ray_normal(p_point); |
101 | |
102 | Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) }; |
103 | |
104 | Vector3 ofs = aabb.get_center(); |
105 | |
106 | Vector3 axis; |
107 | axis[p_id] = 1.0; |
108 | |
109 | if (move) { |
110 | Vector3 ra, rb; |
111 | Geometry3D::get_closest_points_between_segments(ofs - axis * 4096, ofs + axis * 4096, sg[0], sg[1], ra, rb); |
112 | |
113 | float d = ra[p_id]; |
114 | if (Node3DEditor::get_singleton()->is_snap_enabled()) { |
115 | d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); |
116 | } |
117 | |
118 | aabb.position[p_id] = d - 1.0 - aabb.size[p_id] * 0.5; |
119 | particles->set_visibility_aabb(aabb); |
120 | |
121 | } else { |
122 | Vector3 ra, rb; |
123 | Geometry3D::get_closest_points_between_segments(ofs, ofs + axis * 4096, sg[0], sg[1], ra, rb); |
124 | |
125 | float d = ra[p_id] - ofs[p_id]; |
126 | if (Node3DEditor::get_singleton()->is_snap_enabled()) { |
127 | d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); |
128 | } |
129 | |
130 | if (d < 0.001) { |
131 | d = 0.001; |
132 | } |
133 | //resize |
134 | aabb.position[p_id] = (aabb.position[p_id] + aabb.size[p_id] * 0.5) - d; |
135 | aabb.size[p_id] = d * 2; |
136 | particles->set_visibility_aabb(aabb); |
137 | } |
138 | } |
139 | |
140 | void GPUParticles3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { |
141 | GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(p_gizmo->get_node_3d()); |
142 | |
143 | if (p_cancel) { |
144 | particles->set_visibility_aabb(p_restore); |
145 | return; |
146 | } |
147 | |
148 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
149 | ur->create_action(TTR("Change Particles AABB" )); |
150 | ur->add_do_method(particles, "set_visibility_aabb" , particles->get_visibility_aabb()); |
151 | ur->add_undo_method(particles, "set_visibility_aabb" , p_restore); |
152 | ur->commit_action(); |
153 | } |
154 | |
155 | void GPUParticles3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
156 | p_gizmo->clear(); |
157 | |
158 | if (p_gizmo->is_selected()) { |
159 | GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(p_gizmo->get_node_3d()); |
160 | |
161 | Vector<Vector3> lines; |
162 | AABB aabb = particles->get_visibility_aabb(); |
163 | |
164 | for (int i = 0; i < 12; i++) { |
165 | Vector3 a, b; |
166 | aabb.get_edge(i, a, b); |
167 | lines.push_back(a); |
168 | lines.push_back(b); |
169 | } |
170 | |
171 | Vector<Vector3> handles; |
172 | |
173 | for (int i = 0; i < 3; i++) { |
174 | Vector3 ax; |
175 | ax[i] = aabb.position[i] + aabb.size[i]; |
176 | ax[(i + 1) % 3] = aabb.position[(i + 1) % 3] + aabb.size[(i + 1) % 3] * 0.5; |
177 | ax[(i + 2) % 3] = aabb.position[(i + 2) % 3] + aabb.size[(i + 2) % 3] * 0.5; |
178 | handles.push_back(ax); |
179 | } |
180 | |
181 | Vector3 center = aabb.get_center(); |
182 | for (int i = 0; i < 3; i++) { |
183 | Vector3 ax; |
184 | ax[i] = 1.0; |
185 | handles.push_back(center + ax); |
186 | lines.push_back(center); |
187 | lines.push_back(center + ax); |
188 | } |
189 | |
190 | Ref<Material> material = get_material("particles_material" , p_gizmo); |
191 | |
192 | p_gizmo->add_lines(lines, material); |
193 | |
194 | Ref<Material> solid_material = get_material("particles_solid_material" , p_gizmo); |
195 | p_gizmo->add_solid_box(solid_material, aabb.get_size(), aabb.get_center()); |
196 | |
197 | p_gizmo->add_handles(handles, get_material("handles" )); |
198 | } |
199 | |
200 | Ref<Material> icon = get_material("particles_icon" , p_gizmo); |
201 | p_gizmo->add_unscaled_billboard(icon, 0.05); |
202 | } |
203 | |