spatial_player.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*************************************************************************/
  2. /* spatial_player.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "spatial_player.h"
  31. #include "camera.h"
  32. #include "scene/resources/surface_tool.h"
  33. #include "servers/audio_server.h"
  34. #include "servers/spatial_sound_server.h"
  35. void SpatialPlayer::_notification(int p_what) {
  36. switch (p_what) {
  37. case NOTIFICATION_ENTER_WORLD: {
  38. //find the sound space
  39. source_rid = SpatialSoundServer::get_singleton()->source_create(get_world()->get_sound_space());
  40. for (int i = 0; i < PARAM_MAX; i++)
  41. set_param(Param(i), params[i]);
  42. } break;
  43. case NOTIFICATION_TRANSFORM_CHANGED: {
  44. SpatialSoundServer::get_singleton()->source_set_transform(source_rid, get_global_transform());
  45. } break;
  46. case NOTIFICATION_EXIT_WORLD: {
  47. if (source_rid.is_valid())
  48. SpatialSoundServer::get_singleton()->free(source_rid);
  49. } break;
  50. }
  51. }
  52. void SpatialPlayer::set_param(Param p_param, float p_value) {
  53. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  54. params[p_param] = p_value;
  55. if (p_param == PARAM_EMISSION_CONE_DEGREES) {
  56. update_gizmo();
  57. }
  58. if (source_rid.is_valid())
  59. SpatialSoundServer::get_singleton()->source_set_param(source_rid, (SpatialSoundServer::SourceParam)p_param, p_value);
  60. }
  61. float SpatialPlayer::get_param(Param p_param) const {
  62. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  63. return params[p_param];
  64. }
  65. bool SpatialPlayer::_can_gizmo_scale() const {
  66. return false;
  67. }
  68. RES SpatialPlayer::_get_gizmo_geometry() const {
  69. Ref<SurfaceTool> surface_tool(memnew(SurfaceTool));
  70. Ref<FixedMaterial> mat(memnew(FixedMaterial));
  71. mat->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(0.0, 0.6, 0.7, 0.05));
  72. mat->set_parameter(FixedMaterial::PARAM_EMISSION, Color(0.5, 0.7, 0.8));
  73. mat->set_blend_mode(Material::BLEND_MODE_ADD);
  74. mat->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  75. // mat->set_hint(Material::HINT_NO_DEPTH_DRAW,true);
  76. surface_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
  77. surface_tool->set_material(mat);
  78. int sides = 16;
  79. int sections = 24;
  80. // float len=1;
  81. float deg = Math::deg2rad(params[PARAM_EMISSION_CONE_DEGREES]);
  82. if (deg == 180)
  83. deg = 179.5;
  84. Vector3 to = Vector3(0, 0, -1);
  85. for (int j = 0; j < sections; j++) {
  86. Vector3 p1 = Matrix3(Vector3(1, 0, 0), deg * j / sections).xform(to);
  87. Vector3 p2 = Matrix3(Vector3(1, 0, 0), deg * (j + 1) / sections).xform(to);
  88. for (int i = 0; i < sides; i++) {
  89. Vector3 p1r = Matrix3(Vector3(0, 0, 1), Math_PI * 2 * float(i) / sides).xform(p1);
  90. Vector3 p1s = Matrix3(Vector3(0, 0, 1), Math_PI * 2 * float(i + 1) / sides).xform(p1);
  91. Vector3 p2s = Matrix3(Vector3(0, 0, 1), Math_PI * 2 * float(i + 1) / sides).xform(p2);
  92. Vector3 p2r = Matrix3(Vector3(0, 0, 1), Math_PI * 2 * float(i) / sides).xform(p2);
  93. surface_tool->add_normal(p1r.normalized());
  94. surface_tool->add_vertex(p1r);
  95. surface_tool->add_normal(p1s.normalized());
  96. surface_tool->add_vertex(p1s);
  97. surface_tool->add_normal(p2s.normalized());
  98. surface_tool->add_vertex(p2s);
  99. surface_tool->add_normal(p1r.normalized());
  100. surface_tool->add_vertex(p1r);
  101. surface_tool->add_normal(p2s.normalized());
  102. surface_tool->add_vertex(p2s);
  103. surface_tool->add_normal(p2r.normalized());
  104. surface_tool->add_vertex(p2r);
  105. if (j == sections - 1) {
  106. surface_tool->add_normal(p2r.normalized());
  107. surface_tool->add_vertex(p2r);
  108. surface_tool->add_normal(p2s.normalized());
  109. surface_tool->add_vertex(p2s);
  110. surface_tool->add_normal(Vector3(0, 0, 1));
  111. surface_tool->add_vertex(Vector3());
  112. }
  113. }
  114. }
  115. Ref<Mesh> mesh = surface_tool->commit();
  116. Ref<FixedMaterial> mat_speaker(memnew(FixedMaterial));
  117. mat_speaker->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(0.3, 0.3, 0.6));
  118. mat_speaker->set_parameter(FixedMaterial::PARAM_SPECULAR, Color(0.5, 0.5, 0.6));
  119. //mat_speaker->set_blend_mode( Material::BLEND_MODE_MIX);
  120. //mat_speaker->set_flag(Material::FLAG_DOUBLE_SIDED,false);
  121. //mat_speaker->set_flag(Material::FLAG_UNSHADED,true);
  122. surface_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
  123. surface_tool->set_material(mat_speaker);
  124. // float radius=1;
  125. const int speaker_points = 8;
  126. Vector3 speaker[speaker_points] = {
  127. Vector3(0, 0, 1) * 0.15,
  128. Vector3(1, 1, 1) * 0.15,
  129. Vector3(1, 1, 0) * 0.15,
  130. Vector3(2, 2, -1) * 0.15,
  131. Vector3(1, 1, -1) * 0.15,
  132. Vector3(0.8, 0.8, -1.2) * 0.15,
  133. Vector3(0.5, 0.5, -1.4) * 0.15,
  134. Vector3(0.0, 0.0, -1.6) * 0.15
  135. };
  136. int speaker_sides = 10;
  137. for (int i = 0; i < speaker_sides; i++) {
  138. Matrix3 ma(Vector3(0, 0, 1), Math_PI * 2 * float(i) / speaker_sides);
  139. Matrix3 mb(Vector3(0, 0, 1), Math_PI * 2 * float(i + 1) / speaker_sides);
  140. for (int j = 0; j < speaker_points - 1; j++) {
  141. Vector3 points[4] = {
  142. ma.xform(speaker[j]),
  143. mb.xform(speaker[j]),
  144. mb.xform(speaker[j + 1]),
  145. ma.xform(speaker[j + 1]),
  146. };
  147. Vector3 n = -Plane(points[0], points[1], points[2]).normal;
  148. surface_tool->add_normal(n);
  149. surface_tool->add_vertex(points[0]);
  150. surface_tool->add_normal(n);
  151. surface_tool->add_vertex(points[2]);
  152. surface_tool->add_normal(n);
  153. surface_tool->add_vertex(points[1]);
  154. surface_tool->add_normal(n);
  155. surface_tool->add_vertex(points[0]);
  156. surface_tool->add_normal(n);
  157. surface_tool->add_vertex(points[3]);
  158. surface_tool->add_normal(n);
  159. surface_tool->add_vertex(points[2]);
  160. }
  161. }
  162. return surface_tool->commit(mesh);
  163. }
  164. void SpatialPlayer::_bind_methods() {
  165. ObjectTypeDB::bind_method(_MD("set_param", "param", "value"), &SpatialPlayer::set_param);
  166. ObjectTypeDB::bind_method(_MD("get_param", "param"), &SpatialPlayer::get_param);
  167. BIND_CONSTANT(PARAM_VOLUME_DB);
  168. BIND_CONSTANT(PARAM_PITCH_SCALE);
  169. BIND_CONSTANT(PARAM_ATTENUATION_MIN_DISTANCE);
  170. BIND_CONSTANT(PARAM_ATTENUATION_MAX_DISTANCE);
  171. BIND_CONSTANT(PARAM_ATTENUATION_DISTANCE_EXP);
  172. BIND_CONSTANT(PARAM_EMISSION_CONE_DEGREES);
  173. BIND_CONSTANT(PARAM_EMISSION_CONE_ATTENUATION_DB);
  174. BIND_CONSTANT(PARAM_MAX);
  175. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_VOLUME_DB);
  176. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/pitch_scale", PROPERTY_HINT_RANGE, "0.001,32,0.001"), _SCS("set_param"), _SCS("get_param"), PARAM_PITCH_SCALE);
  177. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/attenuation/min_distance", PROPERTY_HINT_RANGE, "0.01,4096,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_MIN_DISTANCE);
  178. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/attenuation/max_distance", PROPERTY_HINT_RANGE, "0.01,4096,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_MAX_DISTANCE);
  179. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/attenuation/distance_exp", PROPERTY_HINT_EXP_EASING, "attenuation"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_DISTANCE_EXP);
  180. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/emission_cone/degrees", PROPERTY_HINT_RANGE, "0,180,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_EMISSION_CONE_DEGREES);
  181. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "params/emission_cone/attenuation_db", PROPERTY_HINT_RANGE, "-80,24,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_EMISSION_CONE_ATTENUATION_DB);
  182. }
  183. SpatialPlayer::SpatialPlayer() {
  184. params[PARAM_VOLUME_DB] = 0.0;
  185. params[PARAM_PITCH_SCALE] = 1.0;
  186. params[PARAM_ATTENUATION_MIN_DISTANCE] = 1;
  187. params[PARAM_ATTENUATION_MAX_DISTANCE] = 100;
  188. params[PARAM_ATTENUATION_DISTANCE_EXP] = 1.0; //linear (and not really good)
  189. params[PARAM_EMISSION_CONE_DEGREES] = 180.0; //cone disabled
  190. params[PARAM_EMISSION_CONE_ATTENUATION_DB] = -6.0; //minus 6 db attenuation
  191. }
  192. SpatialPlayer::~SpatialPlayer() {
  193. }