ray_cast_3d.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 "collision_object_3d.h"
  32. #include "mesh_instance_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_shape) {
  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_shape) {
  159. _clear_debug_shape();
  160. }
  161. } break;
  162. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  163. if (!enabled) {
  164. break;
  165. }
  166. bool prev_collision_state = collided;
  167. _update_raycast_state();
  168. if (prev_collision_state != collided && get_tree()->is_debugging_collisions_hint()) {
  169. _update_debug_shape_material(true);
  170. }
  171. } break;
  172. }
  173. }
  174. void RayCast3D::_update_raycast_state() {
  175. Ref<World3D> w3d = get_world_3d();
  176. ERR_FAIL_COND(w3d.is_null());
  177. PhysicsDirectSpaceState3D *dss = PhysicsServer3D::get_singleton()->space_get_direct_state(w3d->get_space());
  178. ERR_FAIL_NULL(dss);
  179. Transform3D gt = get_global_transform();
  180. Vector3 to = target_position;
  181. if (to == Vector3()) {
  182. to = Vector3(0, 0.01, 0);
  183. }
  184. PhysicsDirectSpaceState3D::RayParameters ray_params;
  185. ray_params.from = gt.get_origin();
  186. ray_params.to = gt.xform(to);
  187. ray_params.exclude = exclude;
  188. ray_params.collision_mask = collision_mask;
  189. ray_params.collide_with_bodies = collide_with_bodies;
  190. ray_params.collide_with_areas = collide_with_areas;
  191. ray_params.hit_from_inside = hit_from_inside;
  192. ray_params.hit_back_faces = hit_back_faces;
  193. PhysicsDirectSpaceState3D::RayResult rr;
  194. if (dss->intersect_ray(ray_params, rr)) {
  195. collided = true;
  196. against = rr.collider_id;
  197. against_rid = rr.rid;
  198. collision_point = rr.position;
  199. collision_normal = rr.normal;
  200. collision_face_index = rr.face_index;
  201. against_shape = rr.shape;
  202. } else {
  203. collided = false;
  204. against = ObjectID();
  205. against_rid = RID();
  206. against_shape = 0;
  207. }
  208. }
  209. void RayCast3D::force_raycast_update() {
  210. _update_raycast_state();
  211. }
  212. void RayCast3D::add_exception_rid(const RID &p_rid) {
  213. exclude.insert(p_rid);
  214. }
  215. void RayCast3D::add_exception(const CollisionObject3D *p_node) {
  216. ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject3D.");
  217. add_exception_rid(p_node->get_rid());
  218. }
  219. void RayCast3D::remove_exception_rid(const RID &p_rid) {
  220. exclude.erase(p_rid);
  221. }
  222. void RayCast3D::remove_exception(const CollisionObject3D *p_node) {
  223. ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject3D.");
  224. remove_exception_rid(p_node->get_rid());
  225. }
  226. void RayCast3D::clear_exceptions() {
  227. exclude.clear();
  228. if (exclude_parent_body && is_inside_tree()) {
  229. CollisionObject3D *parent = Object::cast_to<CollisionObject3D>(get_parent());
  230. if (parent) {
  231. exclude.insert(parent->get_rid());
  232. }
  233. }
  234. }
  235. void RayCast3D::set_collide_with_areas(bool p_enabled) {
  236. collide_with_areas = p_enabled;
  237. }
  238. bool RayCast3D::is_collide_with_areas_enabled() const {
  239. return collide_with_areas;
  240. }
  241. void RayCast3D::set_collide_with_bodies(bool p_enabled) {
  242. collide_with_bodies = p_enabled;
  243. }
  244. bool RayCast3D::is_collide_with_bodies_enabled() const {
  245. return collide_with_bodies;
  246. }
  247. void RayCast3D::set_hit_from_inside(bool p_enabled) {
  248. hit_from_inside = p_enabled;
  249. }
  250. bool RayCast3D::is_hit_from_inside_enabled() const {
  251. return hit_from_inside;
  252. }
  253. void RayCast3D::set_hit_back_faces(bool p_enabled) {
  254. hit_back_faces = p_enabled;
  255. }
  256. bool RayCast3D::is_hit_back_faces_enabled() const {
  257. return hit_back_faces;
  258. }
  259. void RayCast3D::_bind_methods() {
  260. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast3D::set_enabled);
  261. ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast3D::is_enabled);
  262. ClassDB::bind_method(D_METHOD("set_target_position", "local_point"), &RayCast3D::set_target_position);
  263. ClassDB::bind_method(D_METHOD("get_target_position"), &RayCast3D::get_target_position);
  264. ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast3D::is_colliding);
  265. ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast3D::force_raycast_update);
  266. ClassDB::bind_method(D_METHOD("get_collider"), &RayCast3D::get_collider);
  267. ClassDB::bind_method(D_METHOD("get_collider_rid"), &RayCast3D::get_collider_rid);
  268. ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast3D::get_collider_shape);
  269. ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast3D::get_collision_point);
  270. ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast3D::get_collision_normal);
  271. ClassDB::bind_method(D_METHOD("get_collision_face_index"), &RayCast3D::get_collision_face_index);
  272. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast3D::add_exception_rid);
  273. ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast3D::add_exception);
  274. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast3D::remove_exception_rid);
  275. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast3D::remove_exception);
  276. ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast3D::clear_exceptions);
  277. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast3D::set_collision_mask);
  278. ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast3D::get_collision_mask);
  279. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &RayCast3D::set_collision_mask_value);
  280. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &RayCast3D::get_collision_mask_value);
  281. ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast3D::set_exclude_parent_body);
  282. ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast3D::get_exclude_parent_body);
  283. ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast3D::set_collide_with_areas);
  284. ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast3D::is_collide_with_areas_enabled);
  285. ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast3D::set_collide_with_bodies);
  286. ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast3D::is_collide_with_bodies_enabled);
  287. ClassDB::bind_method(D_METHOD("set_hit_from_inside", "enable"), &RayCast3D::set_hit_from_inside);
  288. ClassDB::bind_method(D_METHOD("is_hit_from_inside_enabled"), &RayCast3D::is_hit_from_inside_enabled);
  289. ClassDB::bind_method(D_METHOD("set_hit_back_faces", "enable"), &RayCast3D::set_hit_back_faces);
  290. ClassDB::bind_method(D_METHOD("is_hit_back_faces_enabled"), &RayCast3D::is_hit_back_faces_enabled);
  291. ClassDB::bind_method(D_METHOD("set_debug_shape_custom_color", "debug_shape_custom_color"), &RayCast3D::set_debug_shape_custom_color);
  292. ClassDB::bind_method(D_METHOD("get_debug_shape_custom_color"), &RayCast3D::get_debug_shape_custom_color);
  293. ClassDB::bind_method(D_METHOD("set_debug_shape_thickness", "debug_shape_thickness"), &RayCast3D::set_debug_shape_thickness);
  294. ClassDB::bind_method(D_METHOD("get_debug_shape_thickness"), &RayCast3D::get_debug_shape_thickness);
  295. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  296. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
  297. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_position", PROPERTY_HINT_NONE, "suffix:m"), "set_target_position", "get_target_position");
  298. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  299. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_from_inside"), "set_hit_from_inside", "is_hit_from_inside_enabled");
  300. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_back_faces"), "set_hit_back_faces", "is_hit_back_faces_enabled");
  301. ADD_GROUP("Collide With", "collide_with");
  302. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_areas", "is_collide_with_areas_enabled");
  303. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
  304. ADD_GROUP("Debug Shape", "debug_shape");
  305. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_shape_custom_color"), "set_debug_shape_custom_color", "get_debug_shape_custom_color");
  306. ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_shape_thickness", PROPERTY_HINT_RANGE, "1,5"), "set_debug_shape_thickness", "get_debug_shape_thickness");
  307. }
  308. int RayCast3D::get_debug_shape_thickness() const {
  309. return debug_shape_thickness;
  310. }
  311. void RayCast3D::_update_debug_shape_vertices() {
  312. debug_shape_vertices.clear();
  313. debug_line_vertices.clear();
  314. if (target_position == Vector3()) {
  315. return;
  316. }
  317. debug_line_vertices.push_back(Vector3());
  318. debug_line_vertices.push_back(target_position);
  319. if (debug_shape_thickness > 1) {
  320. float scale_factor = 100.0;
  321. Vector3 dir = Vector3(target_position).normalized();
  322. // Draw truncated pyramid
  323. Vector3 normal = (fabs(dir.x) + fabs(dir.y) > CMP_EPSILON) ? Vector3(-dir.y, dir.x, 0).normalized() : Vector3(0, -dir.z, dir.y).normalized();
  324. normal *= debug_shape_thickness / scale_factor;
  325. int vertices_strip_order[14] = { 4, 5, 0, 1, 2, 5, 6, 4, 7, 0, 3, 2, 7, 6 };
  326. for (int v = 0; v < 14; v++) {
  327. Vector3 vertex = vertices_strip_order[v] < 4 ? normal : normal / 3.0 + target_position;
  328. debug_shape_vertices.push_back(vertex.rotated(dir, Math_PI * (0.5 * (vertices_strip_order[v] % 4) + 0.25)));
  329. }
  330. }
  331. }
  332. void RayCast3D::set_debug_shape_thickness(const int p_debug_shape_thickness) {
  333. debug_shape_thickness = p_debug_shape_thickness;
  334. update_gizmos();
  335. if (Engine::get_singleton()->is_editor_hint()) {
  336. if (is_inside_tree()) {
  337. _update_debug_shape_vertices();
  338. }
  339. } else if (debug_shape) {
  340. _update_debug_shape();
  341. }
  342. }
  343. const Vector<Vector3> &RayCast3D::get_debug_shape_vertices() const {
  344. return debug_shape_vertices;
  345. }
  346. const Vector<Vector3> &RayCast3D::get_debug_line_vertices() const {
  347. return debug_line_vertices;
  348. }
  349. void RayCast3D::set_debug_shape_custom_color(const Color &p_color) {
  350. debug_shape_custom_color = p_color;
  351. if (debug_material.is_valid()) {
  352. _update_debug_shape_material();
  353. }
  354. }
  355. Ref<StandardMaterial3D> RayCast3D::get_debug_material() {
  356. _update_debug_shape_material();
  357. return debug_material;
  358. }
  359. const Color &RayCast3D::get_debug_shape_custom_color() const {
  360. return debug_shape_custom_color;
  361. }
  362. void RayCast3D::_create_debug_shape() {
  363. _update_debug_shape_material();
  364. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  365. MeshInstance3D *mi = memnew(MeshInstance3D);
  366. mi->set_mesh(mesh);
  367. add_child(mi);
  368. debug_shape = mi;
  369. }
  370. void RayCast3D::_update_debug_shape_material(bool p_check_collision) {
  371. if (!debug_material.is_valid()) {
  372. Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);
  373. debug_material = material;
  374. material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  375. material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  376. // Use double-sided rendering so that the RayCast can be seen if the camera is inside.
  377. material->set_cull_mode(BaseMaterial3D::CULL_DISABLED);
  378. material->set_transparency(BaseMaterial3D::TRANSPARENCY_ALPHA);
  379. }
  380. Color color = debug_shape_custom_color;
  381. if (color == Color(0.0, 0.0, 0.0)) {
  382. // Use the default debug shape color defined in the Project Settings.
  383. color = get_tree()->get_debug_collisions_color();
  384. }
  385. if (p_check_collision && collided) {
  386. if ((color.get_h() < 0.055 || color.get_h() > 0.945) && color.get_s() > 0.5 && color.get_v() > 0.5) {
  387. // If base color is already quite reddish, highlight collision with green color
  388. color = Color(0.0, 1.0, 0.0, color.a);
  389. } else {
  390. // Else, highlight collision with red color
  391. color = Color(1.0, 0, 0, color.a);
  392. }
  393. }
  394. Ref<StandardMaterial3D> material = static_cast<Ref<StandardMaterial3D>>(debug_material);
  395. material->set_albedo(color);
  396. }
  397. void RayCast3D::_update_debug_shape() {
  398. if (!enabled) {
  399. return;
  400. }
  401. if (!debug_shape) {
  402. _create_debug_shape();
  403. }
  404. MeshInstance3D *mi = static_cast<MeshInstance3D *>(debug_shape);
  405. Ref<ArrayMesh> mesh = mi->get_mesh();
  406. if (!mesh.is_valid()) {
  407. return;
  408. }
  409. _update_debug_shape_vertices();
  410. mesh->clear_surfaces();
  411. Array a;
  412. a.resize(Mesh::ARRAY_MAX);
  413. uint32_t flags = 0;
  414. int surface_count = 0;
  415. if (!debug_line_vertices.is_empty()) {
  416. a[Mesh::ARRAY_VERTEX] = debug_line_vertices;
  417. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a, Array(), Dictionary(), flags);
  418. mesh->surface_set_material(surface_count, debug_material);
  419. ++surface_count;
  420. }
  421. if (!debug_shape_vertices.is_empty()) {
  422. a[Mesh::ARRAY_VERTEX] = debug_shape_vertices;
  423. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_STRIP, a, Array(), Dictionary(), flags);
  424. mesh->surface_set_material(surface_count, debug_material);
  425. ++surface_count;
  426. }
  427. }
  428. void RayCast3D::_clear_debug_shape() {
  429. if (!debug_shape) {
  430. return;
  431. }
  432. MeshInstance3D *mi = static_cast<MeshInstance3D *>(debug_shape);
  433. if (mi->is_inside_tree()) {
  434. mi->queue_free();
  435. } else {
  436. memdelete(mi);
  437. }
  438. debug_shape = nullptr;
  439. }
  440. RayCast3D::RayCast3D() {
  441. }