ray_cast_3d.cpp 19 KB

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