ray_cast_3d.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /**************************************************************************/
  2. /* ray_cast_3d.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. #include "ray_cast_3d.h"
  31. #include "scene/3d/physics/collision_object_3d.h"
  32. void RayCast3D::set_target_position(const Vector3 &p_point) {
  33. target_position = p_point;
  34. update_gizmos();
  35. if (Engine::get_singleton()->is_editor_hint()) {
  36. if (is_inside_tree()) {
  37. _update_debug_shape_vertices();
  38. }
  39. } else if (debug_instance.is_valid()) {
  40. _update_debug_shape();
  41. }
  42. }
  43. Vector3 RayCast3D::get_target_position() const {
  44. return target_position;
  45. }
  46. void RayCast3D::set_collision_mask(uint32_t p_mask) {
  47. collision_mask = p_mask;
  48. }
  49. uint32_t RayCast3D::get_collision_mask() const {
  50. return collision_mask;
  51. }
  52. void RayCast3D::set_collision_mask_value(int p_layer_number, bool p_value) {
  53. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  54. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  55. uint32_t mask = get_collision_mask();
  56. if (p_value) {
  57. mask |= 1 << (p_layer_number - 1);
  58. } else {
  59. mask &= ~(1 << (p_layer_number - 1));
  60. }
  61. set_collision_mask(mask);
  62. }
  63. bool RayCast3D::get_collision_mask_value(int p_layer_number) const {
  64. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  65. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  66. return get_collision_mask() & (1 << (p_layer_number - 1));
  67. }
  68. bool RayCast3D::is_colliding() const {
  69. return collided;
  70. }
  71. Object *RayCast3D::get_collider() const {
  72. if (against.is_null()) {
  73. return nullptr;
  74. }
  75. return ObjectDB::get_instance(against);
  76. }
  77. RID RayCast3D::get_collider_rid() const {
  78. return against_rid;
  79. }
  80. int RayCast3D::get_collider_shape() const {
  81. return against_shape;
  82. }
  83. Vector3 RayCast3D::get_collision_point() const {
  84. return collision_point;
  85. }
  86. Vector3 RayCast3D::get_collision_normal() const {
  87. return collision_normal;
  88. }
  89. int RayCast3D::get_collision_face_index() const {
  90. return collision_face_index;
  91. }
  92. void RayCast3D::set_enabled(bool p_enabled) {
  93. enabled = p_enabled;
  94. update_gizmos();
  95. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  96. set_physics_process_internal(p_enabled);
  97. }
  98. if (!p_enabled) {
  99. collided = false;
  100. }
  101. if (is_inside_tree() && get_tree()->is_debugging_collisions_hint()) {
  102. if (p_enabled) {
  103. _update_debug_shape();
  104. } else {
  105. _clear_debug_shape();
  106. }
  107. }
  108. }
  109. bool RayCast3D::is_enabled() const {
  110. return enabled;
  111. }
  112. void RayCast3D::set_exclude_parent_body(bool p_exclude_parent_body) {
  113. if (exclude_parent_body == p_exclude_parent_body) {
  114. return;
  115. }
  116. exclude_parent_body = p_exclude_parent_body;
  117. if (!is_inside_tree()) {
  118. return;
  119. }
  120. if (Object::cast_to<CollisionObject3D>(get_parent())) {
  121. if (exclude_parent_body) {
  122. exclude.insert(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
  123. } else {
  124. exclude.erase(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
  125. }
  126. }
  127. }
  128. bool RayCast3D::get_exclude_parent_body() const {
  129. return exclude_parent_body;
  130. }
  131. void RayCast3D::_notification(int p_what) {
  132. switch (p_what) {
  133. case NOTIFICATION_ENTER_TREE: {
  134. if (Engine::get_singleton()->is_editor_hint()) {
  135. _update_debug_shape_vertices();
  136. }
  137. if (enabled && !Engine::get_singleton()->is_editor_hint()) {
  138. set_physics_process_internal(true);
  139. } else {
  140. set_physics_process_internal(false);
  141. }
  142. if (get_tree()->is_debugging_collisions_hint()) {
  143. _update_debug_shape();
  144. }
  145. if (Object::cast_to<CollisionObject3D>(get_parent())) {
  146. if (exclude_parent_body) {
  147. exclude.insert(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
  148. } else {
  149. exclude.erase(Object::cast_to<CollisionObject3D>(get_parent())->get_rid());
  150. }
  151. }
  152. } break;
  153. case NOTIFICATION_EXIT_TREE: {
  154. if (enabled) {
  155. set_physics_process_internal(false);
  156. }
  157. if (debug_instance.is_valid()) {
  158. _clear_debug_shape();
  159. }
  160. } break;
  161. case NOTIFICATION_VISIBILITY_CHANGED: {
  162. if (is_inside_tree() && debug_instance.is_valid()) {
  163. RenderingServer::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
  164. }
  165. } break;
  166. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  167. if (!enabled) {
  168. break;
  169. }
  170. bool prev_collision_state = collided;
  171. _update_raycast_state();
  172. if (get_tree()->is_debugging_collisions_hint()) {
  173. if (prev_collision_state != collided) {
  174. _update_debug_shape_material(true);
  175. }
  176. if (is_inside_tree() && debug_instance.is_valid()) {
  177. RenderingServer::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
  178. }
  179. }
  180. } break;
  181. }
  182. }
  183. void RayCast3D::_update_raycast_state() {
  184. Ref<World3D> w3d = get_world_3d();
  185. ERR_FAIL_COND(w3d.is_null());
  186. PhysicsDirectSpaceState3D *dss = PhysicsServer3D::get_singleton()->space_get_direct_state(w3d->get_space());
  187. ERR_FAIL_NULL(dss);
  188. Transform3D gt = get_global_transform();
  189. Vector3 to = target_position;
  190. if (to == Vector3()) {
  191. to = Vector3(0, 0.01, 0);
  192. }
  193. PhysicsDirectSpaceState3D::RayParameters ray_params;
  194. ray_params.from = gt.get_origin();
  195. ray_params.to = gt.xform(to);
  196. ray_params.exclude = exclude;
  197. ray_params.collision_mask = collision_mask;
  198. ray_params.collide_with_bodies = collide_with_bodies;
  199. ray_params.collide_with_areas = collide_with_areas;
  200. ray_params.hit_from_inside = hit_from_inside;
  201. ray_params.hit_back_faces = hit_back_faces;
  202. PhysicsDirectSpaceState3D::RayResult rr;
  203. if (dss->intersect_ray(ray_params, rr)) {
  204. collided = true;
  205. against = rr.collider_id;
  206. against_rid = rr.rid;
  207. collision_point = rr.position;
  208. collision_normal = rr.normal;
  209. collision_face_index = rr.face_index;
  210. against_shape = rr.shape;
  211. } else {
  212. collided = false;
  213. against = ObjectID();
  214. against_rid = RID();
  215. against_shape = 0;
  216. }
  217. }
  218. void RayCast3D::force_raycast_update() {
  219. _update_raycast_state();
  220. }
  221. void RayCast3D::add_exception_rid(const RID &p_rid) {
  222. exclude.insert(p_rid);
  223. }
  224. void RayCast3D::add_exception(const CollisionObject3D *p_node) {
  225. ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject3D.");
  226. add_exception_rid(p_node->get_rid());
  227. }
  228. void RayCast3D::remove_exception_rid(const RID &p_rid) {
  229. exclude.erase(p_rid);
  230. }
  231. void RayCast3D::remove_exception(const CollisionObject3D *p_node) {
  232. ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject3D.");
  233. remove_exception_rid(p_node->get_rid());
  234. }
  235. void RayCast3D::clear_exceptions() {
  236. exclude.clear();
  237. if (exclude_parent_body && is_inside_tree()) {
  238. CollisionObject3D *parent = Object::cast_to<CollisionObject3D>(get_parent());
  239. if (parent) {
  240. exclude.insert(parent->get_rid());
  241. }
  242. }
  243. }
  244. void RayCast3D::set_collide_with_areas(bool p_enabled) {
  245. collide_with_areas = p_enabled;
  246. }
  247. bool RayCast3D::is_collide_with_areas_enabled() const {
  248. return collide_with_areas;
  249. }
  250. void RayCast3D::set_collide_with_bodies(bool p_enabled) {
  251. collide_with_bodies = p_enabled;
  252. }
  253. bool RayCast3D::is_collide_with_bodies_enabled() const {
  254. return collide_with_bodies;
  255. }
  256. void RayCast3D::set_hit_from_inside(bool p_enabled) {
  257. hit_from_inside = p_enabled;
  258. }
  259. bool RayCast3D::is_hit_from_inside_enabled() const {
  260. return hit_from_inside;
  261. }
  262. void RayCast3D::set_hit_back_faces(bool p_enabled) {
  263. hit_back_faces = p_enabled;
  264. }
  265. bool RayCast3D::is_hit_back_faces_enabled() const {
  266. return hit_back_faces;
  267. }
  268. void RayCast3D::_bind_methods() {
  269. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast3D::set_enabled);
  270. ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast3D::is_enabled);
  271. ClassDB::bind_method(D_METHOD("set_target_position", "local_point"), &RayCast3D::set_target_position);
  272. ClassDB::bind_method(D_METHOD("get_target_position"), &RayCast3D::get_target_position);
  273. ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast3D::is_colliding);
  274. ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast3D::force_raycast_update);
  275. ClassDB::bind_method(D_METHOD("get_collider"), &RayCast3D::get_collider);
  276. ClassDB::bind_method(D_METHOD("get_collider_rid"), &RayCast3D::get_collider_rid);
  277. ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast3D::get_collider_shape);
  278. ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast3D::get_collision_point);
  279. ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast3D::get_collision_normal);
  280. ClassDB::bind_method(D_METHOD("get_collision_face_index"), &RayCast3D::get_collision_face_index);
  281. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast3D::add_exception_rid);
  282. ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast3D::add_exception);
  283. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast3D::remove_exception_rid);
  284. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast3D::remove_exception);
  285. ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast3D::clear_exceptions);
  286. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast3D::set_collision_mask);
  287. ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast3D::get_collision_mask);
  288. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &RayCast3D::set_collision_mask_value);
  289. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &RayCast3D::get_collision_mask_value);
  290. ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast3D::set_exclude_parent_body);
  291. ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast3D::get_exclude_parent_body);
  292. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast3D::set_collide_with_areas);
  293. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast3D::is_collide_with_areas_enabled);
  294. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast3D::set_collide_with_bodies);
  295. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast3D::is_collide_with_bodies_enabled);
  296. ClassDB::bind_method(D_METHOD("set_hit_from_inside", "enable"), &RayCast3D::set_hit_from_inside);
  297. ClassDB::bind_method(D_METHOD("is_hit_from_inside_enabled"), &RayCast3D::is_hit_from_inside_enabled);
  298. ClassDB::bind_method(D_METHOD("set_hit_back_faces", "enable"), &RayCast3D::set_hit_back_faces);
  299. ClassDB::bind_method(D_METHOD("is_hit_back_faces_enabled"), &RayCast3D::is_hit_back_faces_enabled);
  300. ClassDB::bind_method(D_METHOD("set_debug_shape_custom_color", "debug_shape_custom_color"), &RayCast3D::set_debug_shape_custom_color);
  301. ClassDB::bind_method(D_METHOD("get_debug_shape_custom_color"), &RayCast3D::get_debug_shape_custom_color);
  302. ClassDB::bind_method(D_METHOD("set_debug_shape_thickness", "debug_shape_thickness"), &RayCast3D::set_debug_shape_thickness);
  303. ClassDB::bind_method(D_METHOD("get_debug_shape_thickness"), &RayCast3D::get_debug_shape_thickness);
  304. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  305. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
  306. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_position", PROPERTY_HINT_NONE, "suffix:m"), "set_target_position", "get_target_position");
  307. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  308. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_from_inside"), "set_hit_from_inside", "is_hit_from_inside_enabled");
  309. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_back_faces"), "set_hit_back_faces", "is_hit_back_faces_enabled");
  310. ADD_GROUP("Collide With", "collide_with");
  311. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
  312. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  313. ADD_GROUP("Debug Shape", "debug_shape");
  314. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_shape_custom_color"), "set_debug_shape_custom_color", "get_debug_shape_custom_color");
  315. ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_shape_thickness", PROPERTY_HINT_RANGE, "1,5"), "set_debug_shape_thickness", "get_debug_shape_thickness");
  316. }
  317. int RayCast3D::get_debug_shape_thickness() const {
  318. return debug_shape_thickness;
  319. }
  320. void RayCast3D::_update_debug_shape_vertices() {
  321. debug_shape_vertices.clear();
  322. debug_line_vertices.clear();
  323. if (target_position == Vector3()) {
  324. return;
  325. }
  326. debug_line_vertices.push_back(Vector3());
  327. debug_line_vertices.push_back(target_position);
  328. if (debug_shape_thickness > 1) {
  329. float scale_factor = 100.0;
  330. Vector3 dir = Vector3(target_position).normalized();
  331. // Draw truncated pyramid
  332. Vector3 normal = (std::abs(dir.x) + std::abs(dir.y) > CMP_EPSILON) ? Vector3(-dir.y, dir.x, 0).normalized() : Vector3(0, -dir.z, dir.y).normalized();
  333. normal *= debug_shape_thickness / scale_factor;
  334. int vertices_strip_order[14] = { 4, 5, 0, 1, 2, 5, 6, 4, 7, 0, 3, 2, 7, 6 };
  335. for (int v = 0; v < 14; v++) {
  336. Vector3 vertex = vertices_strip_order[v] < 4 ? normal : normal / 3.0 + target_position;
  337. debug_shape_vertices.push_back(vertex.rotated(dir, Math::PI * (0.5 * (vertices_strip_order[v] % 4) + 0.25)));
  338. }
  339. }
  340. }
  341. void RayCast3D::set_debug_shape_thickness(const int p_debug_shape_thickness) {
  342. debug_shape_thickness = p_debug_shape_thickness;
  343. update_gizmos();
  344. if (Engine::get_singleton()->is_editor_hint()) {
  345. if (is_inside_tree()) {
  346. _update_debug_shape_vertices();
  347. }
  348. } else if (debug_instance.is_valid()) {
  349. _update_debug_shape();
  350. }
  351. }
  352. const Vector<Vector3> &RayCast3D::get_debug_shape_vertices() const {
  353. return debug_shape_vertices;
  354. }
  355. const Vector<Vector3> &RayCast3D::get_debug_line_vertices() const {
  356. return debug_line_vertices;
  357. }
  358. void RayCast3D::set_debug_shape_custom_color(const Color &p_color) {
  359. debug_shape_custom_color = p_color;
  360. if (debug_material.is_valid()) {
  361. _update_debug_shape_material();
  362. }
  363. }
  364. Ref<StandardMaterial3D> RayCast3D::get_debug_material() {
  365. _update_debug_shape_material();
  366. return debug_material;
  367. }
  368. const Color &RayCast3D::get_debug_shape_custom_color() const {
  369. return debug_shape_custom_color;
  370. }
  371. void RayCast3D::_create_debug_shape() {
  372. _update_debug_shape_material();
  373. if (!debug_instance.is_valid()) {
  374. debug_instance = RenderingServer::get_singleton()->instance_create();
  375. }
  376. if (debug_mesh.is_null()) {
  377. debug_mesh.instantiate();
  378. }
  379. }
  380. void RayCast3D::_update_debug_shape_material(bool p_check_collision) {
  381. if (debug_material.is_null()) {
  382. Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);
  383. debug_material = material;
  384. material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  385. material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  386. // Use double-sided rendering so that the RayCast can be seen if the camera is inside.
  387. material->set_cull_mode(BaseMaterial3D::CULL_DISABLED);
  388. material->set_transparency(BaseMaterial3D::TRANSPARENCY_ALPHA);
  389. }
  390. Color color = debug_shape_custom_color;
  391. if (color == Color(0.0, 0.0, 0.0)) {
  392. // Use the default debug shape color defined in the Project Settings.
  393. color = get_tree()->get_debug_collisions_color();
  394. }
  395. if (p_check_collision && collided) {
  396. if ((color.get_h() < 0.055 || color.get_h() > 0.945) && color.get_s() > 0.5 && color.get_v() > 0.5) {
  397. // If base color is already quite reddish, highlight collision with green color
  398. color = Color(0.0, 1.0, 0.0, color.a);
  399. } else {
  400. // Else, highlight collision with red color
  401. color = Color(1.0, 0, 0, color.a);
  402. }
  403. }
  404. Ref<StandardMaterial3D> material = static_cast<Ref<StandardMaterial3D>>(debug_material);
  405. material->set_albedo(color);
  406. }
  407. void RayCast3D::_update_debug_shape() {
  408. if (!enabled) {
  409. return;
  410. }
  411. if (!debug_instance.is_valid()) {
  412. _create_debug_shape();
  413. }
  414. if (!debug_instance.is_valid() || debug_mesh.is_null()) {
  415. return;
  416. }
  417. _update_debug_shape_vertices();
  418. debug_mesh->clear_surfaces();
  419. Array a;
  420. a.resize(Mesh::ARRAY_MAX);
  421. uint32_t flags = 0;
  422. int surface_count = 0;
  423. if (!debug_line_vertices.is_empty()) {
  424. a[Mesh::ARRAY_VERTEX] = debug_line_vertices;
  425. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags);
  426. debug_mesh->surface_set_material(surface_count, debug_material);
  427. ++surface_count;
  428. }
  429. if (!debug_shape_vertices.is_empty()) {
  430. a[Mesh::ARRAY_VERTEX] = debug_shape_vertices;
  431. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_STRIP, a, Array(), Dictionary(), flags);
  432. debug_mesh->surface_set_material(surface_count, debug_material);
  433. ++surface_count;
  434. }
  435. RenderingServer::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
  436. if (is_inside_tree()) {
  437. RenderingServer::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
  438. RenderingServer::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
  439. RenderingServer::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
  440. }
  441. }
  442. void RayCast3D::_clear_debug_shape() {
  443. ERR_FAIL_NULL(RenderingServer::get_singleton());
  444. if (debug_instance.is_valid()) {
  445. RenderingServer::get_singleton()->free(debug_instance);
  446. debug_instance = RID();
  447. }
  448. if (debug_mesh.is_valid()) {
  449. RenderingServer::get_singleton()->free(debug_mesh->get_rid());
  450. debug_mesh = Ref<ArrayMesh>();
  451. }
  452. }
  453. RayCast3D::RayCast3D() {
  454. }