| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  gpu_particles_collision_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_collision_3d_gizmo_plugin.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "editor/editor_settings.h" | 
|---|
| 34 | #include "editor/editor_undo_redo_manager.h" | 
|---|
| 35 | #include "editor/plugins/gizmos/gizmo_3d_helper.h" | 
|---|
| 36 | #include "editor/plugins/node_3d_editor_plugin.h" | 
|---|
| 37 | #include "scene/3d/gpu_particles_collision_3d.h" | 
|---|
| 38 |  | 
|---|
| 39 | GPUParticlesCollision3DGizmoPlugin::GPUParticlesCollision3DGizmoPlugin() { | 
|---|
| 40 | helper.instantiate(); | 
|---|
| 41 |  | 
|---|
| 42 | Color gizmo_color_attractor = EDITOR_DEF( "editors/3d_gizmos/gizmo_colors/particle_attractor", Color(1, 0.7, 0.5)); | 
|---|
| 43 | create_material( "shape_material_attractor", gizmo_color_attractor); | 
|---|
| 44 | gizmo_color_attractor.a = 0.15; | 
|---|
| 45 | create_material( "shape_material_attractor_internal", gizmo_color_attractor); | 
|---|
| 46 |  | 
|---|
| 47 | Color gizmo_color_collision = EDITOR_DEF( "editors/3d_gizmos/gizmo_colors/particle_collision", Color(0.5, 0.7, 1)); | 
|---|
| 48 | create_material( "shape_material_collision", gizmo_color_collision); | 
|---|
| 49 | gizmo_color_collision.a = 0.15; | 
|---|
| 50 | create_material( "shape_material_collision_internal", gizmo_color_collision); | 
|---|
| 51 |  | 
|---|
| 52 | create_handle_material( "handles"); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | GPUParticlesCollision3DGizmoPlugin::~GPUParticlesCollision3DGizmoPlugin() { | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | bool GPUParticlesCollision3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { | 
|---|
| 59 | return (Object::cast_to<GPUParticlesCollision3D>(p_spatial) != nullptr) || (Object::cast_to<GPUParticlesAttractor3D>(p_spatial) != nullptr); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | String GPUParticlesCollision3DGizmoPlugin::get_gizmo_name() const { | 
|---|
| 63 | return "GPUParticlesCollision3D"; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | int GPUParticlesCollision3DGizmoPlugin::get_priority() const { | 
|---|
| 67 | return -1; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | String GPUParticlesCollision3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { | 
|---|
| 71 | const Node3D *cs = p_gizmo->get_node_3d(); | 
|---|
| 72 |  | 
|---|
| 73 | if (Object::cast_to<GPUParticlesCollisionSphere3D>(cs) || Object::cast_to<GPUParticlesAttractorSphere3D>(cs)) { | 
|---|
| 74 | return "Radius"; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | if (Object::cast_to<GPUParticlesCollisionBox3D>(cs) || Object::cast_to<GPUParticlesAttractorBox3D>(cs) || Object::cast_to<GPUParticlesAttractorVectorField3D>(cs) || Object::cast_to<GPUParticlesCollisionSDF3D>(cs) || Object::cast_to<GPUParticlesCollisionHeightField3D>(cs)) { | 
|---|
| 78 | return helper->box_get_handle_name(p_id); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | return ""; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | Variant GPUParticlesCollision3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { | 
|---|
| 85 | const Node3D *cs = p_gizmo->get_node_3d(); | 
|---|
| 86 |  | 
|---|
| 87 | if (Object::cast_to<GPUParticlesCollisionSphere3D>(cs) || Object::cast_to<GPUParticlesAttractorSphere3D>(cs)) { | 
|---|
| 88 | return p_gizmo->get_node_3d()->call( "get_radius"); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | if (Object::cast_to<GPUParticlesCollisionBox3D>(cs) || Object::cast_to<GPUParticlesAttractorBox3D>(cs) || Object::cast_to<GPUParticlesAttractorVectorField3D>(cs) || Object::cast_to<GPUParticlesCollisionSDF3D>(cs) || Object::cast_to<GPUParticlesCollisionHeightField3D>(cs)) { | 
|---|
| 92 | return Vector3(p_gizmo->get_node_3d()->call( "get_size")); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | return Variant(); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void GPUParticlesCollision3DGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) { | 
|---|
| 99 | helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform()); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void GPUParticlesCollision3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) { | 
|---|
| 103 | Node3D *sn = p_gizmo->get_node_3d(); | 
|---|
| 104 |  | 
|---|
| 105 | Vector3 sg[2]; | 
|---|
| 106 | helper->get_segment(p_camera, p_point, sg); | 
|---|
| 107 |  | 
|---|
| 108 | if (Object::cast_to<GPUParticlesCollisionSphere3D>(sn) || Object::cast_to<GPUParticlesAttractorSphere3D>(sn)) { | 
|---|
| 109 | Vector3 ra, rb; | 
|---|
| 110 | Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb); | 
|---|
| 111 | float d = ra.x; | 
|---|
| 112 | if (Node3DEditor::get_singleton()->is_snap_enabled()) { | 
|---|
| 113 | d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | if (d < 0.001) { | 
|---|
| 117 | d = 0.001; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | sn->call( "set_radius", d); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | if (Object::cast_to<GPUParticlesCollisionBox3D>(sn) || Object::cast_to<GPUParticlesAttractorBox3D>(sn) || Object::cast_to<GPUParticlesAttractorVectorField3D>(sn) || Object::cast_to<GPUParticlesCollisionSDF3D>(sn) || Object::cast_to<GPUParticlesCollisionHeightField3D>(sn)) { | 
|---|
| 124 | Vector3 size = sn->call( "get_size"); | 
|---|
| 125 | Vector3 position; | 
|---|
| 126 | helper->box_set_handle(sg, p_id, size, position); | 
|---|
| 127 | sn->call( "set_size", size); | 
|---|
| 128 | sn->set_global_position(position); | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | void GPUParticlesCollision3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { | 
|---|
| 133 | Node3D *sn = p_gizmo->get_node_3d(); | 
|---|
| 134 |  | 
|---|
| 135 | if (Object::cast_to<GPUParticlesCollisionSphere3D>(sn) || Object::cast_to<GPUParticlesAttractorSphere3D>(sn)) { | 
|---|
| 136 | if (p_cancel) { | 
|---|
| 137 | sn->call( "set_radius", p_restore); | 
|---|
| 138 | return; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); | 
|---|
| 142 | ur->create_action(TTR( "Change Radius")); | 
|---|
| 143 | ur->add_do_method(sn, "set_radius", sn->call( "get_radius")); | 
|---|
| 144 | ur->add_undo_method(sn, "set_radius", p_restore); | 
|---|
| 145 | ur->commit_action(); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | if (Object::cast_to<GPUParticlesCollisionBox3D>(sn) || Object::cast_to<GPUParticlesAttractorBox3D>(sn) || Object::cast_to<GPUParticlesAttractorVectorField3D>(sn) || Object::cast_to<GPUParticlesCollisionSDF3D>(sn) || Object::cast_to<GPUParticlesCollisionHeightField3D>(sn)) { | 
|---|
| 149 | helper->box_commit_handle( "Change Box Shape Size", p_cancel, sn); | 
|---|
| 150 | } | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | void GPUParticlesCollision3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { | 
|---|
| 154 | Node3D *cs = p_gizmo->get_node_3d(); | 
|---|
| 155 |  | 
|---|
| 156 | p_gizmo->clear(); | 
|---|
| 157 |  | 
|---|
| 158 | Ref<Material> material; | 
|---|
| 159 | Ref<Material> material_internal; | 
|---|
| 160 | if (Object::cast_to<GPUParticlesAttractor3D>(cs)) { | 
|---|
| 161 | material = get_material( "shape_material_attractor", p_gizmo); | 
|---|
| 162 | material_internal = get_material( "shape_material_attractor_internal", p_gizmo); | 
|---|
| 163 | } else { | 
|---|
| 164 | material = get_material( "shape_material_collision", p_gizmo); | 
|---|
| 165 | material_internal = get_material( "shape_material_collision_internal", p_gizmo); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | const Ref<Material> handles_material = get_material( "handles"); | 
|---|
| 169 |  | 
|---|
| 170 | if (Object::cast_to<GPUParticlesCollisionSphere3D>(cs) || Object::cast_to<GPUParticlesAttractorSphere3D>(cs)) { | 
|---|
| 171 | float r = cs->call( "get_radius"); | 
|---|
| 172 |  | 
|---|
| 173 | Vector<Vector3> points; | 
|---|
| 174 |  | 
|---|
| 175 | for (int i = 0; i <= 360; i++) { | 
|---|
| 176 | float ra = Math::deg_to_rad((float)i); | 
|---|
| 177 | float rb = Math::deg_to_rad((float)i + 1); | 
|---|
| 178 | Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r; | 
|---|
| 179 | Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r; | 
|---|
| 180 |  | 
|---|
| 181 | points.push_back(Vector3(a.x, 0, a.y)); | 
|---|
| 182 | points.push_back(Vector3(b.x, 0, b.y)); | 
|---|
| 183 | points.push_back(Vector3(0, a.x, a.y)); | 
|---|
| 184 | points.push_back(Vector3(0, b.x, b.y)); | 
|---|
| 185 | points.push_back(Vector3(a.x, a.y, 0)); | 
|---|
| 186 | points.push_back(Vector3(b.x, b.y, 0)); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | Vector<Vector3> collision_segments; | 
|---|
| 190 |  | 
|---|
| 191 | for (int i = 0; i < 64; i++) { | 
|---|
| 192 | float ra = i * (Math_TAU / 64.0); | 
|---|
| 193 | float rb = (i + 1) * (Math_TAU / 64.0); | 
|---|
| 194 | Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r; | 
|---|
| 195 | Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r; | 
|---|
| 196 |  | 
|---|
| 197 | collision_segments.push_back(Vector3(a.x, 0, a.y)); | 
|---|
| 198 | collision_segments.push_back(Vector3(b.x, 0, b.y)); | 
|---|
| 199 | collision_segments.push_back(Vector3(0, a.x, a.y)); | 
|---|
| 200 | collision_segments.push_back(Vector3(0, b.x, b.y)); | 
|---|
| 201 | collision_segments.push_back(Vector3(a.x, a.y, 0)); | 
|---|
| 202 | collision_segments.push_back(Vector3(b.x, b.y, 0)); | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | p_gizmo->add_lines(points, material); | 
|---|
| 206 | p_gizmo->add_collision_segments(collision_segments); | 
|---|
| 207 | Vector<Vector3> handles; | 
|---|
| 208 | handles.push_back(Vector3(r, 0, 0)); | 
|---|
| 209 | p_gizmo->add_handles(handles, handles_material); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | if (Object::cast_to<GPUParticlesCollisionBox3D>(cs) || Object::cast_to<GPUParticlesAttractorBox3D>(cs) || Object::cast_to<GPUParticlesAttractorVectorField3D>(cs) || Object::cast_to<GPUParticlesCollisionSDF3D>(cs) || Object::cast_to<GPUParticlesCollisionHeightField3D>(cs)) { | 
|---|
| 213 | Vector<Vector3> lines; | 
|---|
| 214 | AABB aabb; | 
|---|
| 215 | aabb.size = cs->call( "get_size").operator Vector3(); | 
|---|
| 216 | aabb.position = aabb.size / -2; | 
|---|
| 217 |  | 
|---|
| 218 | for (int i = 0; i < 12; i++) { | 
|---|
| 219 | Vector3 a, b; | 
|---|
| 220 | aabb.get_edge(i, a, b); | 
|---|
| 221 | lines.push_back(a); | 
|---|
| 222 | lines.push_back(b); | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | Vector<Vector3> handles = helper->box_get_handles(aabb.size); | 
|---|
| 226 |  | 
|---|
| 227 | p_gizmo->add_lines(lines, material); | 
|---|
| 228 | p_gizmo->add_collision_segments(lines); | 
|---|
| 229 | p_gizmo->add_handles(handles, handles_material); | 
|---|
| 230 |  | 
|---|
| 231 | GPUParticlesCollisionSDF3D *col_sdf = Object::cast_to<GPUParticlesCollisionSDF3D>(cs); | 
|---|
| 232 | if (col_sdf) { | 
|---|
| 233 | static const int subdivs[GPUParticlesCollisionSDF3D::RESOLUTION_MAX] = { 16, 32, 64, 128, 256, 512 }; | 
|---|
| 234 | int subdiv = subdivs[col_sdf->get_resolution()]; | 
|---|
| 235 | float cell_size = aabb.get_longest_axis_size() / subdiv; | 
|---|
| 236 |  | 
|---|
| 237 | lines.clear(); | 
|---|
| 238 |  | 
|---|
| 239 | for (int i = 1; i < subdiv; i++) { | 
|---|
| 240 | for (int j = 0; j < 3; j++) { | 
|---|
| 241 | if (cell_size * i > aabb.size[j]) { | 
|---|
| 242 | continue; | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | int j_n1 = (j + 1) % 3; | 
|---|
| 246 | int j_n2 = (j + 2) % 3; | 
|---|
| 247 |  | 
|---|
| 248 | for (int k = 0; k < 4; k++) { | 
|---|
| 249 | Vector3 from = aabb.position, to = aabb.position; | 
|---|
| 250 | from[j] += cell_size * i; | 
|---|
| 251 | to[j] += cell_size * i; | 
|---|
| 252 |  | 
|---|
| 253 | if (k & 1) { | 
|---|
| 254 | to[j_n1] += aabb.size[j_n1]; | 
|---|
| 255 | } else { | 
|---|
| 256 | to[j_n2] += aabb.size[j_n2]; | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 | if (k & 2) { | 
|---|
| 260 | from[j_n1] += aabb.size[j_n1]; | 
|---|
| 261 | from[j_n2] += aabb.size[j_n2]; | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | lines.push_back(from); | 
|---|
| 265 | lines.push_back(to); | 
|---|
| 266 | } | 
|---|
| 267 | } | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | p_gizmo->add_lines(lines, material_internal); | 
|---|
| 271 | } | 
|---|
| 272 | } | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|