room_instance.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*************************************************************************/
  2. /* room_instance.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 "room_instance.h"
  31. #include "servers/visual_server.h"
  32. #include "geometry.h"
  33. #include "globals.h"
  34. #include "scene/resources/surface_tool.h"
  35. void Room::_notification(int p_what) {
  36. switch (p_what) {
  37. case NOTIFICATION_ENTER_WORLD: {
  38. // go find parent level
  39. Node *parent_room = get_parent();
  40. level = 0;
  41. while (parent_room) {
  42. Room *r = parent_room->cast_to<Room>();
  43. if (r) {
  44. level = r->level + 1;
  45. break;
  46. }
  47. parent_room = parent_room->get_parent();
  48. }
  49. if (sound_enabled)
  50. SpatialSoundServer::get_singleton()->room_set_space(sound_room, get_world()->get_sound_space());
  51. } break;
  52. case NOTIFICATION_TRANSFORM_CHANGED: {
  53. SpatialSoundServer::get_singleton()->room_set_transform(sound_room, get_global_transform());
  54. } break;
  55. case NOTIFICATION_EXIT_WORLD: {
  56. if (sound_enabled)
  57. SpatialSoundServer::get_singleton()->room_set_space(sound_room, RID());
  58. } break;
  59. }
  60. }
  61. RES Room::_get_gizmo_geometry() const {
  62. DVector<Face3> faces;
  63. if (!room.is_null())
  64. faces = room->get_geometry_hint();
  65. int count = faces.size();
  66. if (count == 0)
  67. return RES();
  68. DVector<Face3>::Read facesr = faces.read();
  69. const Face3 *facesptr = facesr.ptr();
  70. DVector<Vector3> points;
  71. Ref<SurfaceTool> surface_tool(memnew(SurfaceTool));
  72. Ref<FixedMaterial> mat(memnew(FixedMaterial));
  73. mat->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(0.2, 0.8, 0.9, 0.3));
  74. mat->set_line_width(4);
  75. mat->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  76. mat->set_flag(Material::FLAG_UNSHADED, true);
  77. // mat->set_hint(Material::HINT_NO_DEPTH_DRAW,true);
  78. surface_tool->begin(Mesh::PRIMITIVE_LINES);
  79. surface_tool->set_material(mat);
  80. for (int i = 0; i < count; i++) {
  81. surface_tool->add_vertex(facesptr[i].vertex[0]);
  82. surface_tool->add_vertex(facesptr[i].vertex[1]);
  83. surface_tool->add_vertex(facesptr[i].vertex[1]);
  84. surface_tool->add_vertex(facesptr[i].vertex[2]);
  85. surface_tool->add_vertex(facesptr[i].vertex[2]);
  86. surface_tool->add_vertex(facesptr[i].vertex[0]);
  87. }
  88. return surface_tool->commit();
  89. }
  90. AABB Room::get_aabb() const {
  91. if (room.is_null())
  92. return AABB();
  93. return room->get_bounds().get_aabb();
  94. }
  95. DVector<Face3> Room::get_faces(uint32_t p_usage_flags) const {
  96. return DVector<Face3>();
  97. }
  98. void Room::set_room(const Ref<RoomBounds> &p_room) {
  99. room = p_room;
  100. update_gizmo();
  101. if (room.is_valid()) {
  102. set_base(room->get_rid());
  103. } else {
  104. set_base(RID());
  105. }
  106. if (!is_inside_tree())
  107. return;
  108. propagate_notification(NOTIFICATION_AREA_CHANGED);
  109. update_gizmo();
  110. if (room.is_valid())
  111. SpatialSoundServer::get_singleton()->room_set_bounds(sound_room, room->get_bounds());
  112. }
  113. Ref<RoomBounds> Room::get_room() const {
  114. return room;
  115. }
  116. void Room::_parse_node_faces(DVector<Face3> &all_faces, const Node *p_node) const {
  117. const VisualInstance *vi = p_node->cast_to<VisualInstance>();
  118. if (vi) {
  119. DVector<Face3> faces = vi->get_faces(FACES_ENCLOSING);
  120. if (faces.size()) {
  121. int old_len = all_faces.size();
  122. all_faces.resize(all_faces.size() + faces.size());
  123. int new_len = all_faces.size();
  124. DVector<Face3>::Write all_facesw = all_faces.write();
  125. Face3 *all_facesptr = all_facesw.ptr();
  126. DVector<Face3>::Read facesr = faces.read();
  127. const Face3 *facesptr = facesr.ptr();
  128. Transform tr = vi->get_relative_transform(this);
  129. for (int i = old_len; i < new_len; i++) {
  130. Face3 f = facesptr[i - old_len];
  131. for (int j = 0; j < 3; j++)
  132. f.vertex[j] = tr.xform(f.vertex[j]);
  133. all_facesptr[i] = f;
  134. }
  135. }
  136. }
  137. for (int i = 0; i < p_node->get_child_count(); i++) {
  138. _parse_node_faces(all_faces, p_node->get_child(i));
  139. }
  140. }
  141. void Room::compute_room_from_subtree() {
  142. DVector<Face3> all_faces;
  143. _parse_node_faces(all_faces, this);
  144. if (all_faces.size() == 0)
  145. return;
  146. float error;
  147. DVector<Face3> wrapped_faces = Geometry::wrap_geometry(all_faces, &error);
  148. if (wrapped_faces.size() == 0)
  149. return;
  150. BSP_Tree tree(wrapped_faces, error);
  151. Ref<RoomBounds> room(memnew(RoomBounds));
  152. room->set_bounds(tree);
  153. room->set_geometry_hint(wrapped_faces);
  154. set_room(room);
  155. }
  156. void Room::set_simulate_acoustics(bool p_enable) {
  157. if (sound_enabled == p_enable)
  158. return;
  159. sound_enabled = p_enable;
  160. if (!is_inside_world())
  161. return; //nothing to do
  162. if (sound_enabled)
  163. SpatialSoundServer::get_singleton()->room_set_space(sound_room, get_world()->get_sound_space());
  164. else
  165. SpatialSoundServer::get_singleton()->room_set_space(sound_room, RID());
  166. }
  167. void Room::_bounds_changed() {
  168. update_gizmo();
  169. }
  170. bool Room::is_simulating_acoustics() const {
  171. return sound_enabled;
  172. }
  173. RID Room::get_sound_room() const {
  174. return RID();
  175. }
  176. void Room::_bind_methods() {
  177. ObjectTypeDB::bind_method(_MD("set_room", "room:Room"), &Room::set_room);
  178. ObjectTypeDB::bind_method(_MD("get_room:Room"), &Room::get_room);
  179. ObjectTypeDB::bind_method(_MD("compute_room_from_subtree"), &Room::compute_room_from_subtree);
  180. ObjectTypeDB::bind_method(_MD("set_simulate_acoustics", "enable"), &Room::set_simulate_acoustics);
  181. ObjectTypeDB::bind_method(_MD("is_simulating_acoustics"), &Room::is_simulating_acoustics);
  182. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "room/room", PROPERTY_HINT_RESOURCE_TYPE, "Area"), _SCS("set_room"), _SCS("get_room"));
  183. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "room/simulate_acoustics"), _SCS("set_simulate_acoustics"), _SCS("is_simulating_acoustics"));
  184. }
  185. Room::Room() {
  186. sound_enabled = false;
  187. sound_room = SpatialSoundServer::get_singleton()->room_create();
  188. level = 0;
  189. }
  190. Room::~Room() {
  191. SpatialSoundServer::get_singleton()->free(sound_room);
  192. }