| 
									
										
										
										
											2023-01-05 13:25:55 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  shape_3d.cpp                                                          */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | #include "shape_3d.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-26 22:58:03 -03:00
										 |  |  | #include "scene/main/scene_tree.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "scene/resources/mesh.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | #include "servers/physics_server_3d.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-17 01:08:21 -04:00
										 |  |  | void Shape3D::add_vertices_to_array(Vector<Vector3> &array, const Transform3D &p_xform) { | 
					
						
							| 
									
										
										
										
											2019-10-10 13:35:00 -07:00
										 |  |  | 	Vector<Vector3> toadd = get_debug_mesh_lines(); | 
					
						
							| 
									
										
										
										
											2015-09-20 13:03:46 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (toadd.size()) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		int base = array.size(); | 
					
						
							|  |  |  | 		array.resize(base + toadd.size()); | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 		Vector3 *w = array.ptrw(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		for (int i = 0; i < toadd.size(); i++) { | 
					
						
							|  |  |  | 			w[i + base] = p_xform.xform(toadd[i]); | 
					
						
							| 
									
										
										
										
											2015-09-20 13:03:46 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:10:13 -07:00
										 |  |  | void Shape3D::set_custom_solver_bias(real_t p_bias) { | 
					
						
							|  |  |  | 	custom_bias = p_bias; | 
					
						
							|  |  |  | 	PhysicsServer3D::get_singleton()->shape_set_custom_solver_bias(shape, custom_bias); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | real_t Shape3D::get_custom_solver_bias() const { | 
					
						
							|  |  |  | 	return custom_bias; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | real_t Shape3D::get_margin() const { | 
					
						
							| 
									
										
										
										
											2018-07-10 12:50:14 +00:00
										 |  |  | 	return margin; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void Shape3D::set_margin(real_t p_margin) { | 
					
						
							| 
									
										
										
										
											2018-07-10 12:50:14 +00:00
										 |  |  | 	margin = p_margin; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 	PhysicsServer3D::get_singleton()->shape_set_margin(shape, margin); | 
					
						
							| 
									
										
										
										
											2018-07-10 12:50:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 03:26:10 -04:00
										 |  |  | void Shape3D::set_debug_color(const Color &p_color) { | 
					
						
							|  |  |  | 	if (p_color == debug_color) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	debug_color = p_color; | 
					
						
							| 
									
										
										
										
											2024-12-23 15:02:54 -05:00
										 |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							| 
									
										
										
										
											2024-12-06 12:36:37 +03:00
										 |  |  | 	debug_properties_edited = true; | 
					
						
							| 
									
										
										
										
											2024-12-23 15:02:54 -05:00
										 |  |  | #endif // DEBUG_ENABLED
 | 
					
						
							| 
									
										
										
										
											2024-04-02 03:26:10 -04:00
										 |  |  | 	_update_shape(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Color Shape3D::get_debug_color() const { | 
					
						
							|  |  |  | 	return debug_color; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Shape3D::set_debug_fill(bool p_fill) { | 
					
						
							|  |  |  | 	if (p_fill == debug_fill) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	debug_fill = p_fill; | 
					
						
							| 
									
										
										
										
											2024-12-23 15:02:54 -05:00
										 |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							| 
									
										
										
										
											2024-12-06 12:36:37 +03:00
										 |  |  | 	debug_properties_edited = true; | 
					
						
							| 
									
										
										
										
											2024-12-23 15:02:54 -05:00
										 |  |  | #endif // DEBUG_ENABLED
 | 
					
						
							| 
									
										
										
										
											2024-04-02 03:26:10 -04:00
										 |  |  | 	_update_shape(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Shape3D::get_debug_fill() const { | 
					
						
							|  |  |  | 	return debug_fill; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-12-06 12:36:37 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Ref<ArrayMesh> Shape3D::get_debug_mesh() { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (debug_mesh_cache.is_valid()) { | 
					
						
							| 
									
										
										
										
											2015-09-20 13:03:46 -03:00
										 |  |  | 		return debug_mesh_cache; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-20 13:03:46 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-10 13:35:00 -07:00
										 |  |  | 	Vector<Vector3> lines = get_debug_mesh_lines(); | 
					
						
							| 
									
										
										
										
											2015-09-20 13:03:46 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-09 15:21:41 -05:00
										 |  |  | 	debug_mesh_cache.instantiate(); | 
					
						
							| 
									
										
										
										
											2015-09-20 13:03:46 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 12:04:21 +00:00
										 |  |  | 	if (!lines.is_empty()) { | 
					
						
							| 
									
										
										
										
											2025-07-11 19:41:17 +02:00
										 |  |  | 		Vector<Color> colors; | 
					
						
							|  |  |  | 		colors.resize(lines.size()); | 
					
						
							|  |  |  | 		colors.fill(debug_color); | 
					
						
							| 
									
										
										
										
											2015-09-20 13:03:46 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 03:26:10 -04:00
										 |  |  | 		Array lines_array; | 
					
						
							|  |  |  | 		lines_array.resize(Mesh::ARRAY_MAX); | 
					
						
							| 
									
										
										
										
											2025-07-11 19:41:17 +02:00
										 |  |  | 		lines_array[Mesh::ARRAY_VERTEX] = lines; | 
					
						
							|  |  |  | 		lines_array[Mesh::ARRAY_COLOR] = colors; | 
					
						
							| 
									
										
										
										
											2015-09-20 13:03:46 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 03:26:10 -04:00
										 |  |  | 		debug_mesh_cache->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, lines_array); | 
					
						
							| 
									
										
										
										
											2025-07-11 19:41:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		SceneTree *scene_tree = SceneTree::get_singleton(); | 
					
						
							|  |  |  | 		if (scene_tree) { | 
					
						
							|  |  |  | 			debug_mesh_cache->surface_set_material(0, scene_tree->get_debug_collision_material()); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-20 13:03:46 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 03:26:10 -04:00
										 |  |  | 		if (debug_fill) { | 
					
						
							| 
									
										
										
										
											2025-01-02 02:33:50 -05:00
										 |  |  | 			Ref<ArrayMesh> array_mesh = get_debug_arraymesh_faces(debug_color * Color(1.0, 1.0, 1.0, 0.0625)); | 
					
						
							|  |  |  | 			if (array_mesh.is_valid() && array_mesh->get_surface_count() > 0) { | 
					
						
							|  |  |  | 				Array solid_array = array_mesh->surface_get_arrays(0); | 
					
						
							|  |  |  | 				debug_mesh_cache->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, solid_array); | 
					
						
							| 
									
										
										
										
											2025-07-11 19:41:17 +02:00
										 |  |  | 				if (scene_tree) { | 
					
						
							|  |  |  | 					debug_mesh_cache->surface_set_material(1, scene_tree->get_debug_collision_material()); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2025-01-02 02:33:50 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-09-20 13:03:46 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return debug_mesh_cache; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void Shape3D::_update_shape() { | 
					
						
							| 
									
										
										
										
											2019-03-19 11:53:37 +08:00
										 |  |  | 	emit_changed(); | 
					
						
							|  |  |  | 	debug_mesh_cache.unref(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void Shape3D::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2021-12-06 18:10:13 -07:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_custom_solver_bias", "bias"), &Shape3D::set_custom_solver_bias); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_custom_solver_bias"), &Shape3D::get_custom_solver_bias); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_margin", "margin"), &Shape3D::set_margin); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_margin"), &Shape3D::get_margin); | 
					
						
							| 
									
										
										
										
											2018-07-10 12:50:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 18:18:39 -07:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_debug_mesh"), &Shape3D::get_debug_mesh); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:10:13 -07:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_solver_bias", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_custom_solver_bias", "get_custom_solver_bias"); | 
					
						
							| 
									
										
										
										
											2023-03-18 17:19:21 +01:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,10,0.001,or_greater,suffix:m"), "set_margin", "get_margin"); | 
					
						
							| 
									
										
										
										
											2018-07-10 12:50:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 17:01:17 +02:00
										 |  |  | Shape3D::Shape3D() { | 
					
						
							|  |  |  | 	ERR_PRINT("Default constructor must not be called!"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Shape3D::Shape3D(RID p_shape) : | 
					
						
							| 
									
										
										
										
											2020-05-12 17:01:17 +02:00
										 |  |  | 		shape(p_shape) {} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Shape3D::~Shape3D() { | 
					
						
							| 
									
										
										
										
											2022-12-02 13:27:26 -05:00
										 |  |  | 	ERR_FAIL_NULL(PhysicsServer3D::get_singleton()); | 
					
						
							|  |  |  | 	PhysicsServer3D::get_singleton()->free(shape); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } |