| 1 | /**************************************************************************/ |
| 2 | /* audio_stream_player_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 "audio_stream_player_3d_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/settings/editor_settings.h" |
| 37 | #include "scene/3d/audio_stream_player_3d.h" |
| 38 | |
| 39 | AudioStreamPlayer3DGizmoPlugin::AudioStreamPlayer3DGizmoPlugin() { |
| 40 | Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/stream_player_3d"); |
| 41 | |
| 42 | create_icon_material("stream_player_3d_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Gizmo3DSamplePlayer"), EditorStringName(EditorIcons))); |
| 43 | create_material("stream_player_3d_material_primary", gizmo_color); |
| 44 | create_material("stream_player_3d_material_secondary", gizmo_color * Color(1, 1, 1, 0.35)); |
| 45 | // Enable vertex colors for the billboard material as the gizmo color depends on the |
| 46 | // AudioStreamPlayer3D attenuation type and source (Unit Size or Max Distance). |
| 47 | create_material("stream_player_3d_material_billboard", Color(1, 1, 1), true, false, true); |
| 48 | create_handle_material("handles"); |
| 49 | } |
| 50 | |
| 51 | bool AudioStreamPlayer3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 52 | return Object::cast_to<AudioStreamPlayer3D>(p_spatial) != nullptr; |
| 53 | } |
| 54 | |
| 55 | String AudioStreamPlayer3DGizmoPlugin::get_gizmo_name() const { |
| 56 | return "AudioStreamPlayer3D"; |
| 57 | } |
| 58 | |
| 59 | int AudioStreamPlayer3DGizmoPlugin::get_priority() const { |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | String AudioStreamPlayer3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 64 | return "Emission Radius"; |
| 65 | } |
| 66 | |
| 67 | Variant AudioStreamPlayer3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 68 | AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_node_3d()); |
| 69 | return player->get_emission_angle(); |
| 70 | } |
| 71 | |
| 72 | void AudioStreamPlayer3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) { |
| 73 | AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_node_3d()); |
| 74 | |
| 75 | Transform3D gt = player->get_global_transform(); |
| 76 | Transform3D gi = gt.affine_inverse(); |
| 77 | |
| 78 | Vector3 ray_from = p_camera->project_ray_origin(p_point); |
| 79 | Vector3 ray_dir = p_camera->project_ray_normal(p_point); |
| 80 | Vector3 ray_to = ray_from + ray_dir * 4096; |
| 81 | |
| 82 | ray_from = gi.xform(ray_from); |
| 83 | ray_to = gi.xform(ray_to); |
| 84 | |
| 85 | float closest_dist = 1e20; |
| 86 | float closest_angle = 1e20; |
| 87 | |
| 88 | for (int i = 0; i < 180; i++) { |
| 89 | float a = Math::deg_to_rad((float)i); |
| 90 | float an = Math::deg_to_rad((float)(i + 1)); |
| 91 | |
| 92 | Vector3 from(Math::sin(a), 0, -Math::cos(a)); |
| 93 | Vector3 to(Math::sin(an), 0, -Math::cos(an)); |
| 94 | |
| 95 | Vector3 r1, r2; |
| 96 | Geometry3D::get_closest_points_between_segments(from, to, ray_from, ray_to, r1, r2); |
| 97 | float d = r1.distance_to(r2); |
| 98 | if (d < closest_dist) { |
| 99 | closest_dist = d; |
| 100 | closest_angle = i; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (closest_angle < 91) { |
| 105 | player->set_emission_angle(closest_angle); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void AudioStreamPlayer3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { |
| 110 | AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_node_3d()); |
| 111 | |
| 112 | if (p_cancel) { |
| 113 | player->set_emission_angle(p_restore); |
| 114 | |
| 115 | } else { |
| 116 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 117 | ur->create_action(TTR("Change AudioStreamPlayer3D Emission Angle")); |
| 118 | ur->add_do_method(player, "set_emission_angle", player->get_emission_angle()); |
| 119 | ur->add_undo_method(player, "set_emission_angle", p_restore); |
| 120 | ur->commit_action(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void AudioStreamPlayer3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 125 | p_gizmo->clear(); |
| 126 | |
| 127 | if (p_gizmo->is_selected()) { |
| 128 | const AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_node_3d()); |
| 129 | |
| 130 | if (player->get_attenuation_model() != AudioStreamPlayer3D::ATTENUATION_DISABLED || player->get_max_distance() > CMP_EPSILON) { |
| 131 | // Draw a circle to represent sound volume attenuation. |
| 132 | // Use only a billboard circle to represent radius. |
| 133 | // This helps distinguish AudioStreamPlayer3D gizmos from OmniLight3D gizmos. |
| 134 | const Ref<Material> lines_billboard_material = get_material("stream_player_3d_material_billboard", p_gizmo); |
| 135 | |
| 136 | // Soft distance cap varies depending on attenuation model, as some will fade out more aggressively than others. |
| 137 | // Multipliers were empirically determined through testing. |
| 138 | float soft_multiplier; |
| 139 | switch (player->get_attenuation_model()) { |
| 140 | case AudioStreamPlayer3D::ATTENUATION_INVERSE_DISTANCE: |
| 141 | soft_multiplier = 12.0; |
| 142 | break; |
| 143 | case AudioStreamPlayer3D::ATTENUATION_INVERSE_SQUARE_DISTANCE: |
| 144 | soft_multiplier = 4.0; |
| 145 | break; |
| 146 | case AudioStreamPlayer3D::ATTENUATION_LOGARITHMIC: |
| 147 | soft_multiplier = 3.25; |
| 148 | break; |
| 149 | default: |
| 150 | // Ensures Max Distance's radius visualization is not capped by Unit Size |
| 151 | // (when the attenuation mode is Disabled). |
| 152 | soft_multiplier = 10000.0; |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | // Draw the distance at which the sound can be reasonably heard. |
| 157 | // This can be either a hard distance cap with the Max Distance property (if set above 0.0), |
| 158 | // or a soft distance cap with the Unit Size property (sound never reaches true zero). |
| 159 | // When Max Distance is 0.0, `r` represents the distance above which the |
| 160 | // sound can't be heard in *most* (but not all) scenarios. |
| 161 | float radius = player->get_unit_size() * soft_multiplier; |
| 162 | if (player->get_max_distance() > CMP_EPSILON) { |
| 163 | radius = MIN(radius, player->get_max_distance()); |
| 164 | } |
| 165 | |
| 166 | #define PUSH_QUARTER_XY(m_from_x, m_from_y, m_to_x, m_to_y, m_y) \ |
| 167 | points_ptrw[index++] = Vector3(m_from_x, -m_from_y - m_y, 0); \ |
| 168 | points_ptrw[index++] = Vector3(m_to_x, -m_to_y - m_y, 0); \ |
| 169 | points_ptrw[index++] = Vector3(m_from_x, m_from_y + m_y, 0); \ |
| 170 | points_ptrw[index++] = Vector3(m_to_x, m_to_y + m_y, 0); \ |
| 171 | points_ptrw[index++] = Vector3(-m_from_x, -m_from_y - m_y, 0); \ |
| 172 | points_ptrw[index++] = Vector3(-m_to_x, -m_to_y - m_y, 0); \ |
| 173 | points_ptrw[index++] = Vector3(-m_from_x, m_from_y + m_y, 0); \ |
| 174 | points_ptrw[index++] = Vector3(-m_to_x, m_to_y + m_y, 0); |
| 175 | |
| 176 | // Number of points in an octant. So there will be 8 * points_in_octant points in total. |
| 177 | // This corresponds to the smoothness of the circle. |
| 178 | const uint32_t points_in_octant = 15; |
| 179 | const real_t octant_angle = Math::PI / 4; |
| 180 | const real_t inc = (Math::PI / (4 * points_in_octant)); |
| 181 | const real_t radius_squared = radius * radius; |
| 182 | real_t r = 0; |
| 183 | |
| 184 | Vector<Vector3> points_billboard; |
| 185 | points_billboard.resize(8 * points_in_octant * 2); |
| 186 | Vector3 *points_ptrw = points_billboard.ptrw(); |
| 187 | |
| 188 | uint32_t index = 0; |
| 189 | float previous_x = radius; |
| 190 | float previous_y = 0.f; |
| 191 | |
| 192 | for (uint32_t i = 0; i < points_in_octant; i++) { |
| 193 | r += inc; |
| 194 | real_t x = Math::cos((i == points_in_octant - 1) ? octant_angle : r) * radius; |
| 195 | real_t y = Math::sqrt(radius_squared - (x * x)); |
| 196 | |
| 197 | PUSH_QUARTER_XY(previous_x, previous_y, x, y, 0); |
| 198 | PUSH_QUARTER_XY(previous_y, previous_x, y, x, 0); |
| 199 | previous_x = x; |
| 200 | previous_y = y; |
| 201 | } |
| 202 | |
| 203 | #undef PUSH_QUARTER_XY |
| 204 | |
| 205 | Color color; |
| 206 | switch (player->get_attenuation_model()) { |
| 207 | // Pick cold colors for all attenuation models (except Disabled), |
| 208 | // so that soft caps can be easily distinguished from hard caps |
| 209 | // (which use warm colors). |
| 210 | case AudioStreamPlayer3D::ATTENUATION_INVERSE_DISTANCE: |
| 211 | color = Color(0.4, 0.8, 1); |
| 212 | break; |
| 213 | case AudioStreamPlayer3D::ATTENUATION_INVERSE_SQUARE_DISTANCE: |
| 214 | color = Color(0.4, 0.5, 1); |
| 215 | break; |
| 216 | case AudioStreamPlayer3D::ATTENUATION_LOGARITHMIC: |
| 217 | color = Color(0.4, 0.2, 1); |
| 218 | break; |
| 219 | default: |
| 220 | // Disabled attenuation mode. |
| 221 | // This is never reached when Max Distance is 0, but the |
| 222 | // hue-inverted form of this color will be used if Max Distance is greater than 0. |
| 223 | color = Color(1, 1, 1); |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | if (player->get_max_distance() > CMP_EPSILON) { |
| 228 | // Sound is hard-capped by max distance. The attenuation model still matters, |
| 229 | // so invert the hue of the color that was chosen above. |
| 230 | color.set_h(color.get_h() + 0.5); |
| 231 | } |
| 232 | |
| 233 | p_gizmo->add_lines(points_billboard, lines_billboard_material, true, color); |
| 234 | } |
| 235 | |
| 236 | if (player->is_emission_angle_enabled()) { |
| 237 | const float ha = Math::deg_to_rad(player->get_emission_angle()); |
| 238 | const float ofs = -Math::cos(ha); |
| 239 | const float radius = Math::sin(ha); |
| 240 | |
| 241 | const uint32_t points_in_octant = 7; |
| 242 | const real_t octant_angle = Math::PI / 4; |
| 243 | const real_t inc = (Math::PI / (4 * points_in_octant)); |
| 244 | const real_t radius_squared = radius * radius; |
| 245 | real_t r = 0; |
| 246 | |
| 247 | Vector<Vector3> points_primary; |
| 248 | points_primary.resize(8 * points_in_octant * 2); |
| 249 | Vector3 *points_ptrw = points_primary.ptrw(); |
| 250 | |
| 251 | uint32_t index = 0; |
| 252 | float previous_x = radius; |
| 253 | float previous_y = 0.f; |
| 254 | #define PUSH_QUARTER(m_from_x, m_from_y, m_to_x, m_to_y, m_y) \ |
| 255 | points_ptrw[index++] = Vector3(m_from_x, -m_from_y, m_y); \ |
| 256 | points_ptrw[index++] = Vector3(m_to_x, -m_to_y, m_y); \ |
| 257 | points_ptrw[index++] = Vector3(m_from_x, m_from_y, m_y); \ |
| 258 | points_ptrw[index++] = Vector3(m_to_x, m_to_y, m_y); \ |
| 259 | points_ptrw[index++] = Vector3(-m_from_x, -m_from_y, m_y); \ |
| 260 | points_ptrw[index++] = Vector3(-m_to_x, -m_to_y, m_y); \ |
| 261 | points_ptrw[index++] = Vector3(-m_from_x, m_from_y, m_y); \ |
| 262 | points_ptrw[index++] = Vector3(-m_to_x, m_to_y, m_y); |
| 263 | |
| 264 | for (uint32_t i = 0; i < points_in_octant; i++) { |
| 265 | r += inc; |
| 266 | real_t x = Math::cos((i == points_in_octant - 1) ? octant_angle : r) * radius; |
| 267 | real_t y = Math::sqrt(radius_squared - (x * x)); |
| 268 | |
| 269 | PUSH_QUARTER(previous_x, previous_y, x, y, ofs); |
| 270 | PUSH_QUARTER(previous_y, previous_x, y, x, ofs); |
| 271 | |
| 272 | previous_x = x; |
| 273 | previous_y = y; |
| 274 | } |
| 275 | #undef PUSH_QUARTER |
| 276 | |
| 277 | const Ref<Material> material_primary = get_material("stream_player_3d_material_primary", p_gizmo); |
| 278 | p_gizmo->add_lines(points_primary, material_primary); |
| 279 | |
| 280 | Vector<Vector3> points_secondary; |
| 281 | points_secondary.resize(16); |
| 282 | Vector3 *points_second_ptrw = points_secondary.ptrw(); |
| 283 | uint32_t index2 = 0; |
| 284 | // Lines to the circle. |
| 285 | points_second_ptrw[index2++] = Vector3(); |
| 286 | points_second_ptrw[index2++] = Vector3(radius, 0, ofs); |
| 287 | points_second_ptrw[index2++] = Vector3(); |
| 288 | points_second_ptrw[index2++] = Vector3(-radius, 0, ofs); |
| 289 | points_second_ptrw[index2++] = Vector3(); |
| 290 | points_second_ptrw[index2++] = Vector3(0, radius, ofs); |
| 291 | points_second_ptrw[index2++] = Vector3(); |
| 292 | points_second_ptrw[index2++] = Vector3(0, -radius, ofs); |
| 293 | real_t octant_value = Math::cos(octant_angle) * radius; |
| 294 | points_second_ptrw[index2++] = Vector3(); |
| 295 | points_second_ptrw[index2++] = Vector3(octant_value, octant_value, ofs); |
| 296 | points_second_ptrw[index2++] = Vector3(); |
| 297 | points_second_ptrw[index2++] = Vector3(-octant_value, octant_value, ofs); |
| 298 | points_second_ptrw[index2++] = Vector3(); |
| 299 | points_second_ptrw[index2++] = Vector3(-octant_value, -octant_value, ofs); |
| 300 | points_second_ptrw[index2++] = Vector3(); |
| 301 | points_second_ptrw[index2++] = Vector3(octant_value, -octant_value, ofs); |
| 302 | |
| 303 | const Ref<Material> material_secondary = get_material("stream_player_3d_material_secondary", p_gizmo); |
| 304 | p_gizmo->add_lines(points_secondary, material_secondary); |
| 305 | |
| 306 | Vector<Vector3> handles; |
| 307 | handles.push_back(Vector3(Math::sin(ha), 0, -Math::cos(ha))); |
| 308 | p_gizmo->add_handles(handles, get_material("handles")); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | const Ref<Material> icon = get_material("stream_player_3d_icon", p_gizmo); |
| 313 | p_gizmo->add_unscaled_billboard(icon, 0.05); |
| 314 | } |