| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | /*  mesh_instance_3d.cpp                                                 */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-01-01 11:16:22 +01:00
										 |  |  | /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* 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 "mesh_instance_3d.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | #include "collision_shape_3d.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/core_string_names.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | #include "physics_body_3d.h"
 | 
					
						
							| 
									
										
										
										
											2017-07-03 10:44:45 -03:00
										 |  |  | #include "scene/resources/material.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | #include "skeleton_3d.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-21 01:36:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | bool MeshInstance3D::_set(const StringName &p_name, const Variant &p_value) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//this is not _too_ bad performance wise, really. it only arrives here if the property was not set anywhere else.
 | 
					
						
							|  |  |  | 	//add to it that it's probably found on first call to _set anyway.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!get_instance().is_valid()) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Map<StringName, BlendShapeTrack>::Element *E = blend_shape_tracks.find(p_name); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 	if (E) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		E->get().value = p_value; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		RenderingServer::get_singleton()->instance_set_blend_shape_weight(get_instance(), E->get().idx, E->get().value); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 	if (p_name.operator String().begins_with("material/")) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		int idx = p_name.operator String().get_slicec('/', 1).to_int(); | 
					
						
							|  |  |  | 		if (idx >= materials.size() || idx < 0) | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		set_surface_material(idx, p_value); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | bool MeshInstance3D::_get(const StringName &p_name, Variant &r_ret) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!get_instance().is_valid()) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	const Map<StringName, BlendShapeTrack>::Element *E = blend_shape_tracks.find(p_name); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 	if (E) { | 
					
						
							|  |  |  | 		r_ret = E->get().value; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 	if (p_name.operator String().begins_with("material/")) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		int idx = p_name.operator String().get_slicec('/', 1).to_int(); | 
					
						
							|  |  |  | 		if (idx >= materials.size() || idx < 0) | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		r_ret = materials[idx]; | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::_get_property_list(List<PropertyInfo> *p_list) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	List<String> ls; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (const Map<StringName, BlendShapeTrack>::Element *E = blend_shape_tracks.front(); E; E = E->next()) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		ls.push_back(E->key()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 18:03:38 +01:00
										 |  |  | 	ls.sort(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (List<String>::Element *E = ls.front(); E; E = E->next()) { | 
					
						
							| 
									
										
										
											
												Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
											
										 
											2020-02-24 15:20:53 -03:00
										 |  |  | 		p_list->push_back(PropertyInfo(Variant::FLOAT, E->get(), PROPERTY_HINT_RANGE, "0,1,0.00001")); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (mesh.is_valid()) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		for (int i = 0; i < mesh->get_surface_count(); i++) { | 
					
						
							| 
									
										
										
										
											2020-02-28 08:27:04 -03:00
										 |  |  | 			p_list->push_back(PropertyInfo(Variant::OBJECT, "material/" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,StandardMaterial3D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DEFERRED_SET_RESOURCE)); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::set_mesh(const Ref<Mesh> &p_mesh) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (mesh == p_mesh) | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (mesh.is_valid()) { | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 		mesh->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &MeshInstance3D::_mesh_changed)); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 		materials.clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	mesh = p_mesh; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 08:34:00 -03:00
										 |  |  | 	blend_shape_tracks.clear(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	if (mesh.is_valid()) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		for (int i = 0; i < mesh->get_blend_shape_count(); i++) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 08:34:00 -03:00
										 |  |  | 			BlendShapeTrack mt; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			mt.idx = i; | 
					
						
							|  |  |  | 			mt.value = 0; | 
					
						
							|  |  |  | 			blend_shape_tracks["blend_shapes/" + String(mesh->get_blend_shape_name(i))] = mt; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 		mesh->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &MeshInstance3D::_mesh_changed)); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 		materials.resize(mesh->get_surface_count()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		set_base(mesh->get_rid()); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		set_base(RID()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-29 11:30:28 +01:00
										 |  |  | 	update_gizmo(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 	_change_notify(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Ref<Mesh> MeshInstance3D::get_mesh() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return mesh; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::_resolve_skeleton_path() { | 
					
						
							| 
									
										
										
										
											2014-05-13 13:24:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 19:46:32 -03:00
										 |  |  | 	Ref<SkinReference> new_skin_reference; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!skeleton_path.is_empty()) { | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 		Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(get_node(skeleton_path)); | 
					
						
							| 
									
										
										
										
											2019-09-18 19:46:32 -03:00
										 |  |  | 		if (skeleton) { | 
					
						
							| 
									
										
										
										
											2019-12-16 13:00:30 -03:00
										 |  |  | 			new_skin_reference = skeleton->register_skin(skin_internal); | 
					
						
							|  |  |  | 			if (skin_internal.is_null()) { | 
					
						
							| 
									
										
										
										
											2019-09-18 19:46:32 -03:00
										 |  |  | 				//a skin was created for us
 | 
					
						
							| 
									
										
										
										
											2019-12-16 13:00:30 -03:00
										 |  |  | 				skin_internal = new_skin_reference->get_skin(); | 
					
						
							| 
									
										
										
										
											2019-09-18 19:46:32 -03:00
										 |  |  | 				_change_notify(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	skin_ref = new_skin_reference; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (skin_ref.is_valid()) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		RenderingServer::get_singleton()->instance_attach_skeleton(get_instance(), skin_ref->get_skeleton()); | 
					
						
							| 
									
										
										
										
											2019-09-18 19:46:32 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		RenderingServer::get_singleton()->instance_attach_skeleton(get_instance(), RID()); | 
					
						
							| 
									
										
										
										
											2019-09-18 19:46:32 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::set_skin(const Ref<Skin> &p_skin) { | 
					
						
							| 
									
										
										
										
											2019-12-16 13:00:30 -03:00
										 |  |  | 	skin_internal = p_skin; | 
					
						
							| 
									
										
										
										
											2019-09-18 19:46:32 -03:00
										 |  |  | 	skin = p_skin; | 
					
						
							|  |  |  | 	if (!is_inside_tree()) | 
					
						
							| 
									
										
										
										
											2014-05-13 13:24:26 +08:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2019-09-18 19:46:32 -03:00
										 |  |  | 	_resolve_skeleton_path(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-13 13:24:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Ref<Skin> MeshInstance3D::get_skin() const { | 
					
						
							| 
									
										
										
										
											2019-09-18 19:46:32 -03:00
										 |  |  | 	return skin; | 
					
						
							| 
									
										
										
										
											2014-05-13 13:24:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::set_skeleton_path(const NodePath &p_skeleton) { | 
					
						
							| 
									
										
										
										
											2014-05-13 13:24:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	skeleton_path = p_skeleton; | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	if (!is_inside_tree()) | 
					
						
							| 
									
										
										
										
											2014-05-13 13:24:26 +08:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	_resolve_skeleton_path(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | NodePath MeshInstance3D::get_skeleton_path() { | 
					
						
							| 
									
										
										
										
											2014-05-13 13:24:26 +08:00
										 |  |  | 	return skeleton_path; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | AABB MeshInstance3D::get_aabb() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!mesh.is_null()) | 
					
						
							|  |  |  | 		return mesh->get_aabb(); | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | 	return AABB(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Vector<Face3> MeshInstance3D::get_faces(uint32_t p_usage_flags) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (!(p_usage_flags & (FACES_SOLID | FACES_ENCLOSING))) | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 		return Vector<Face3>(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (mesh.is_null()) | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 		return Vector<Face3>(); | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return mesh->get_faces(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Node *MeshInstance3D::create_trimesh_collision_node() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (mesh.is_null()) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	Ref<Shape3D> shape = mesh->create_trimesh_shape(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	if (shape.is_null()) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	StaticBody3D *static_body = memnew(StaticBody3D); | 
					
						
							|  |  |  | 	CollisionShape3D *cshape = memnew(CollisionShape3D); | 
					
						
							| 
									
										
										
										
											2017-07-15 01:23:10 -03:00
										 |  |  | 	cshape->set_shape(shape); | 
					
						
							|  |  |  | 	static_body->add_child(cshape); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return static_body; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::create_trimesh_collision() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_trimesh_collision_node()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	ERR_FAIL_COND(!static_body); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	static_body->set_name(String(get_name()) + "_col"); | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	add_child(static_body); | 
					
						
							| 
									
										
										
										
											2017-07-15 01:23:10 -03:00
										 |  |  | 	if (get_owner()) { | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 		CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(0)); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		static_body->set_owner(get_owner()); | 
					
						
							|  |  |  | 		cshape->set_owner(get_owner()); | 
					
						
							| 
									
										
										
										
											2017-07-15 01:23:10 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Node *MeshInstance3D::create_convex_collision_node() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (mesh.is_null()) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	Ref<Shape3D> shape = mesh->create_convex_shape(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	if (shape.is_null()) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	StaticBody3D *static_body = memnew(StaticBody3D); | 
					
						
							|  |  |  | 	CollisionShape3D *cshape = memnew(CollisionShape3D); | 
					
						
							| 
									
										
										
										
											2017-07-15 01:23:10 -03:00
										 |  |  | 	cshape->set_shape(shape); | 
					
						
							|  |  |  | 	static_body->add_child(cshape); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return static_body; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::create_convex_collision() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	StaticBody3D *static_body = Object::cast_to<StaticBody3D>(create_convex_collision_node()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	ERR_FAIL_COND(!static_body); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	static_body->set_name(String(get_name()) + "_col"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	add_child(static_body); | 
					
						
							| 
									
										
										
										
											2017-07-15 01:23:10 -03:00
										 |  |  | 	if (get_owner()) { | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 		CollisionShape3D *cshape = Object::cast_to<CollisionShape3D>(static_body->get_child(0)); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		static_body->set_owner(get_owner()); | 
					
						
							|  |  |  | 		cshape->set_owner(get_owner()); | 
					
						
							| 
									
										
										
										
											2017-07-15 01:23:10 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::_notification(int p_what) { | 
					
						
							| 
									
										
										
										
											2014-05-13 13:24:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p_what == NOTIFICATION_ENTER_TREE) { | 
					
						
							| 
									
										
										
										
											2014-05-13 13:24:26 +08:00
										 |  |  | 		_resolve_skeleton_path(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | int MeshInstance3D::get_surface_material_count() const { | 
					
						
							| 
									
										
										
										
											2018-10-23 15:25:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return materials.size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::set_surface_material(int p_surface, const Ref<Material> &p_material) { | 
					
						
							| 
									
										
										
										
											2014-05-13 13:24:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_INDEX(p_surface, materials.size()); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	materials.write[p_surface] = p_material; | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (materials[p_surface].is_valid()) | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		RS::get_singleton()->instance_set_surface_material(get_instance(), p_surface, materials[p_surface]->get_rid()); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		RS::get_singleton()->instance_set_surface_material(get_instance(), p_surface, RID()); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | Ref<Material> MeshInstance3D::get_surface_material(int p_surface) const { | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_INDEX_V(p_surface, materials.size(), Ref<Material>()); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return materials[p_surface]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::_mesh_changed() { | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	materials.resize(mesh->get_surface_count()); | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::create_debug_tangents() { | 
					
						
							| 
									
										
										
										
											2017-07-03 10:44:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Vector<Vector3> lines; | 
					
						
							|  |  |  | 	Vector<Color> colors; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Ref<Mesh> mesh = get_mesh(); | 
					
						
							|  |  |  | 	if (!mesh.is_valid()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < mesh->get_surface_count(); i++) { | 
					
						
							|  |  |  | 		Array arrays = mesh->surface_get_arrays(i); | 
					
						
							|  |  |  | 		Vector<Vector3> verts = arrays[Mesh::ARRAY_VERTEX]; | 
					
						
							|  |  |  | 		Vector<Vector3> norms = arrays[Mesh::ARRAY_NORMAL]; | 
					
						
							|  |  |  | 		if (norms.size() == 0) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		Vector<float> tangents = arrays[Mesh::ARRAY_TANGENT]; | 
					
						
							|  |  |  | 		if (tangents.size() == 0) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (int j = 0; j < verts.size(); j++) { | 
					
						
							|  |  |  | 			Vector3 v = verts[j]; | 
					
						
							|  |  |  | 			Vector3 n = norms[j]; | 
					
						
							|  |  |  | 			Vector3 t = Vector3(tangents[j * 4 + 0], tangents[j * 4 + 1], tangents[j * 4 + 2]); | 
					
						
							|  |  |  | 			Vector3 b = (n.cross(t)).normalized() * tangents[j * 4 + 3]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			lines.push_back(v); //normal
 | 
					
						
							|  |  |  | 			colors.push_back(Color(0, 0, 1)); //color
 | 
					
						
							|  |  |  | 			lines.push_back(v + n * 0.04); //normal
 | 
					
						
							|  |  |  | 			colors.push_back(Color(0, 0, 1)); //color
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			lines.push_back(v); //tangent
 | 
					
						
							|  |  |  | 			colors.push_back(Color(1, 0, 0)); //color
 | 
					
						
							|  |  |  | 			lines.push_back(v + t * 0.04); //tangent
 | 
					
						
							|  |  |  | 			colors.push_back(Color(1, 0, 0)); //color
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			lines.push_back(v); //binormal
 | 
					
						
							|  |  |  | 			colors.push_back(Color(0, 1, 0)); //color
 | 
					
						
							|  |  |  | 			lines.push_back(v + b * 0.04); //binormal
 | 
					
						
							|  |  |  | 			colors.push_back(Color(0, 1, 0)); //color
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (lines.size()) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-15 01:01:52 -03:00
										 |  |  | 		Ref<StandardMaterial3D> sm; | 
					
						
							| 
									
										
										
										
											2017-07-03 10:44:45 -03:00
										 |  |  | 		sm.instance(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-15 01:01:52 -03:00
										 |  |  | 		sm->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); | 
					
						
							|  |  |  | 		sm->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); | 
					
						
							|  |  |  | 		sm->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); | 
					
						
							| 
									
										
										
										
											2017-07-03 10:44:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Ref<ArrayMesh> am; | 
					
						
							|  |  |  | 		am.instance(); | 
					
						
							|  |  |  | 		Array a; | 
					
						
							|  |  |  | 		a.resize(Mesh::ARRAY_MAX); | 
					
						
							|  |  |  | 		a[Mesh::ARRAY_VERTEX] = lines; | 
					
						
							|  |  |  | 		a[Mesh::ARRAY_COLOR] = colors; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		am->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a); | 
					
						
							|  |  |  | 		am->surface_set_material(0, sm); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 		MeshInstance3D *mi = memnew(MeshInstance3D); | 
					
						
							| 
									
										
										
										
											2017-07-03 10:44:45 -03:00
										 |  |  | 		mi->set_mesh(am); | 
					
						
							|  |  |  | 		mi->set_name("DebugTangents"); | 
					
						
							|  |  |  | 		add_child(mi); | 
					
						
							| 
									
										
										
										
											2017-07-04 23:52:23 -03:00
										 |  |  | #ifdef TOOLS_ENABLED
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (this == get_tree()->get_edited_scene_root()) | 
					
						
							|  |  |  | 			mi->set_owner(this); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			mi->set_owner(get_owner()); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-07-03 10:44:45 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | void MeshInstance3D::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshInstance3D::set_mesh); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_mesh"), &MeshInstance3D::get_mesh); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_skeleton_path", "skeleton_path"), &MeshInstance3D::set_skeleton_path); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_skeleton_path"), &MeshInstance3D::get_skeleton_path); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_skin", "skin"), &MeshInstance3D::set_skin); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_skin"), &MeshInstance3D::get_skin); | 
					
						
							| 
									
										
										
										
											2017-01-14 11:07:57 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_surface_material_count"), &MeshInstance3D::get_surface_material_count); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_surface_material", "surface", "material"), &MeshInstance3D::set_surface_material); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_surface_material", "surface"), &MeshInstance3D::get_surface_material); | 
					
						
							| 
									
										
										
										
											2017-06-30 21:30:17 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("create_trimesh_collision"), &MeshInstance3D::create_trimesh_collision); | 
					
						
							|  |  |  | 	ClassDB::set_method_flags("MeshInstance3D", "create_trimesh_collision", METHOD_FLAGS_DEFAULT); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("create_convex_collision"), &MeshInstance3D::create_convex_collision); | 
					
						
							|  |  |  | 	ClassDB::set_method_flags("MeshInstance3D", "create_convex_collision", METHOD_FLAGS_DEFAULT); | 
					
						
							| 
									
										
										
										
											2017-01-04 01:16:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("create_debug_tangents"), &MeshInstance3D::create_debug_tangents); | 
					
						
							|  |  |  | 	ClassDB::set_method_flags("MeshInstance3D", "create_debug_tangents", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR); | 
					
						
							| 
									
										
										
										
											2017-07-03 10:44:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh"); | 
					
						
							| 
									
										
										
										
											2019-10-31 19:54:21 -03:00
										 |  |  | 	ADD_GROUP("Skeleton", ""); | 
					
						
							| 
									
										
										
										
											2019-09-18 19:46:32 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_skin", "get_skin"); | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_skeleton_path", "get_skeleton_path"); | 
					
						
							| 
									
										
										
										
											2019-11-01 00:35:26 -03:00
										 |  |  | 	ADD_GROUP("", ""); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | MeshInstance3D::MeshInstance3D() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	skeleton_path = NodePath(".."); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | MeshInstance3D::~MeshInstance3D() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } |