feat: updated engine version to 4.4-rc1

This commit is contained in:
Sara 2025-02-23 14:38:14 +01:00
parent ee00efde1f
commit 21ba8e33af
5459 changed files with 1128836 additions and 198305 deletions

View file

@ -36,6 +36,13 @@
#include "scene/resources/3d/concave_polygon_shape_3d.h"
#include "scene/resources/3d/convex_polygon_shape_3d.h"
#include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h"
#include "scene/resources/navigation_mesh.h"
#include "servers/navigation_server_3d.h"
Callable MeshInstance3D::_navmesh_source_geometry_parsing_callback;
RID MeshInstance3D::_navmesh_source_geometry_parser;
bool MeshInstance3D::_set(const StringName &p_name, const Variant &p_value) {
//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.
@ -87,17 +94,9 @@ bool MeshInstance3D::_get(const StringName &p_name, Variant &r_ret) const {
}
void MeshInstance3D::_get_property_list(List<PropertyInfo> *p_list) const {
List<String> ls;
for (const KeyValue<StringName, int> &E : blend_shape_properties) {
ls.push_back(E.key);
for (uint32_t i = 0; i < blend_shape_tracks.size(); i++) {
p_list->push_back(PropertyInfo(Variant::FLOAT, vformat("blend_shapes/%s", String(mesh->get_blend_shape_name(i))), PROPERTY_HINT_RANGE, "-1,1,0.00001"));
}
ls.sort();
for (const String &E : ls) {
p_list->push_back(PropertyInfo(Variant::FLOAT, E, PROPERTY_HINT_RANGE, "-1,1,0.00001"));
}
if (mesh.is_valid()) {
for (int i = 0; i < mesh->get_surface_count(); i++) {
p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("%s/%d", PNAME("surface_material_override"), i), PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_DEFAULT));
@ -142,6 +141,7 @@ int MeshInstance3D::get_blend_shape_count() const {
}
return mesh->get_blend_shape_count();
}
int MeshInstance3D::find_blend_shape_by_name(const StringName &p_name) {
if (mesh.is_null()) {
return -1;
@ -153,11 +153,13 @@ int MeshInstance3D::find_blend_shape_by_name(const StringName &p_name) {
}
return -1;
}
float MeshInstance3D::get_blend_shape_value(int p_blend_shape) const {
ERR_FAIL_COND_V(mesh.is_null(), 0.0);
ERR_FAIL_INDEX_V(p_blend_shape, (int)blend_shape_tracks.size(), 0);
return blend_shape_tracks[p_blend_shape];
}
void MeshInstance3D::set_blend_shape_value(int p_blend_shape, float p_value) {
ERR_FAIL_COND(mesh.is_null());
ERR_FAIL_INDEX(p_blend_shape, (int)blend_shape_tracks.size());
@ -221,7 +223,7 @@ NodePath MeshInstance3D::get_skeleton_path() {
}
AABB MeshInstance3D::get_aabb() const {
if (!mesh.is_null()) {
if (mesh.is_valid()) {
return mesh->get_aabb();
}
@ -386,21 +388,24 @@ Ref<Material> MeshInstance3D::get_active_material(int p_surface) const {
void MeshInstance3D::_mesh_changed() {
ERR_FAIL_COND(mesh.is_null());
surface_override_materials.resize(mesh->get_surface_count());
const int surface_count = mesh->get_surface_count();
surface_override_materials.resize(surface_count);
uint32_t initialize_bs_from = blend_shape_tracks.size();
blend_shape_tracks.resize(mesh->get_blend_shape_count());
for (uint32_t i = 0; i < blend_shape_tracks.size(); i++) {
blend_shape_properties["blend_shapes/" + String(mesh->get_blend_shape_name(i))] = i;
if (i < initialize_bs_from) {
set_blend_shape_value(i, blend_shape_tracks[i]);
} else {
set_blend_shape_value(i, 0);
if (surface_count > 0) {
for (uint32_t i = 0; i < blend_shape_tracks.size(); i++) {
blend_shape_properties["blend_shapes/" + String(mesh->get_blend_shape_name(i))] = i;
if (i < initialize_bs_from) {
set_blend_shape_value(i, blend_shape_tracks[i]);
} else {
set_blend_shape_value(i, 0);
}
}
}
int surface_count = mesh->get_surface_count();
for (int surface_index = 0; surface_index < surface_count; ++surface_index) {
if (surface_override_materials[surface_index].is_valid()) {
RS::get_singleton()->instance_set_surface_override_material(get_instance(), surface_index, surface_override_materials[surface_index]->get_rid());
@ -415,7 +420,7 @@ MeshInstance3D *MeshInstance3D::create_debug_tangents_node() {
Vector<Color> colors;
Ref<Mesh> m = get_mesh();
if (!m.is_valid()) {
if (m.is_null()) {
return nullptr;
}
@ -517,12 +522,12 @@ bool MeshInstance3D::_property_get_revert(const StringName &p_name, Variant &r_p
Ref<ArrayMesh> MeshInstance3D::bake_mesh_from_current_blend_shape_mix(Ref<ArrayMesh> p_existing) {
Ref<ArrayMesh> source_mesh = get_mesh();
ERR_FAIL_NULL_V_MSG(source_mesh, Ref<ArrayMesh>(), "The source mesh must be a valid ArrayMesh.");
ERR_FAIL_COND_V_MSG(source_mesh.is_null(), Ref<ArrayMesh>(), "The source mesh must be a valid ArrayMesh.");
Ref<ArrayMesh> bake_mesh;
if (p_existing.is_valid()) {
ERR_FAIL_NULL_V_MSG(p_existing, Ref<ArrayMesh>(), "The existing mesh must be a valid ArrayMesh.");
ERR_FAIL_COND_V_MSG(p_existing.is_null(), Ref<ArrayMesh>(), "The existing mesh must be a valid ArrayMesh.");
ERR_FAIL_COND_V_MSG(source_mesh == p_existing, Ref<ArrayMesh>(), "The source mesh can not be the same mesh as the existing mesh.");
bake_mesh = p_existing;
@ -671,6 +676,205 @@ Ref<ArrayMesh> MeshInstance3D::bake_mesh_from_current_blend_shape_mix(Ref<ArrayM
return bake_mesh;
}
Ref<ArrayMesh> MeshInstance3D::bake_mesh_from_current_skeleton_pose(Ref<ArrayMesh> p_existing) {
Ref<ArrayMesh> source_mesh = get_mesh();
ERR_FAIL_COND_V_MSG(source_mesh.is_null(), Ref<ArrayMesh>(), "The source mesh must be a valid ArrayMesh.");
Ref<ArrayMesh> bake_mesh;
if (p_existing.is_valid()) {
ERR_FAIL_COND_V_MSG(source_mesh == p_existing, Ref<ArrayMesh>(), "The source mesh can not be the same mesh as the existing mesh.");
bake_mesh = p_existing;
} else {
bake_mesh.instantiate();
}
ERR_FAIL_COND_V_MSG(skin_ref.is_null(), Ref<ArrayMesh>(), "The source mesh must have a valid skin.");
ERR_FAIL_COND_V_MSG(skin_internal.is_null(), Ref<ArrayMesh>(), "The source mesh must have a valid skin.");
RID skeleton = skin_ref->get_skeleton();
ERR_FAIL_COND_V_MSG(!skeleton.is_valid(), Ref<ArrayMesh>(), "The source mesh must have its skin registered with a valid skeleton.");
const int bone_count = RenderingServer::get_singleton()->skeleton_get_bone_count(skeleton);
ERR_FAIL_COND_V(bone_count <= 0, Ref<ArrayMesh>());
ERR_FAIL_COND_V(bone_count < skin_internal->get_bind_count(), Ref<ArrayMesh>());
LocalVector<Transform3D> bone_transforms;
bone_transforms.resize(bone_count);
for (int bone_index = 0; bone_index < bone_count; bone_index++) {
bone_transforms[bone_index] = RenderingServer::get_singleton()->skeleton_bone_get_transform(skeleton, bone_index);
}
bake_mesh->clear_surfaces();
int mesh_surface_count = source_mesh->get_surface_count();
for (int surface_index = 0; surface_index < mesh_surface_count; surface_index++) {
ERR_CONTINUE(source_mesh->surface_get_primitive_type(surface_index) != Mesh::PRIMITIVE_TRIANGLES);
uint32_t surface_format = source_mesh->surface_get_format(surface_index);
ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_VERTEX));
ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_BONES));
ERR_CONTINUE(0 == (surface_format & Mesh::ARRAY_FORMAT_WEIGHTS));
unsigned int bones_per_vertex = surface_format & Mesh::ARRAY_FLAG_USE_8_BONE_WEIGHTS ? 8 : 4;
surface_format &= ~Mesh::ARRAY_FORMAT_BONES;
surface_format &= ~Mesh::ARRAY_FORMAT_WEIGHTS;
const Array &source_mesh_arrays = source_mesh->surface_get_arrays(surface_index);
ERR_FAIL_COND_V(source_mesh_arrays.size() != RS::ARRAY_MAX, Ref<ArrayMesh>());
const Vector<Vector3> &source_mesh_vertex_array = source_mesh_arrays[Mesh::ARRAY_VERTEX];
const Vector<Vector3> &source_mesh_normal_array = source_mesh_arrays[Mesh::ARRAY_NORMAL];
const Vector<float> &source_mesh_tangent_array = source_mesh_arrays[Mesh::ARRAY_TANGENT];
const Vector<int> &source_mesh_bones_array = source_mesh_arrays[Mesh::ARRAY_BONES];
const Vector<float> &source_mesh_weights_array = source_mesh_arrays[Mesh::ARRAY_WEIGHTS];
unsigned int vertex_count = source_mesh_vertex_array.size();
int expected_bone_array_size = vertex_count * bones_per_vertex;
ERR_CONTINUE(source_mesh_bones_array.size() != expected_bone_array_size);
ERR_CONTINUE(source_mesh_weights_array.size() != expected_bone_array_size);
Array new_mesh_arrays;
new_mesh_arrays.resize(Mesh::ARRAY_MAX);
for (int i = 0; i < source_mesh_arrays.size(); i++) {
if (i == Mesh::ARRAY_VERTEX || i == Mesh::ARRAY_NORMAL || i == Mesh::ARRAY_TANGENT || i == Mesh::ARRAY_BONES || i == Mesh::ARRAY_WEIGHTS) {
continue;
}
new_mesh_arrays[i] = source_mesh_arrays[i];
}
bool use_normal_array = source_mesh_normal_array.size() == source_mesh_vertex_array.size();
bool use_tangent_array = source_mesh_tangent_array.size() / 4 == source_mesh_vertex_array.size();
Vector<Vector3> lerped_vertex_array = source_mesh_vertex_array;
Vector<Vector3> lerped_normal_array = source_mesh_normal_array;
Vector<float> lerped_tangent_array = source_mesh_tangent_array;
const Vector3 *source_vertices_ptr = source_mesh_vertex_array.ptr();
const Vector3 *source_normals_ptr = source_mesh_normal_array.ptr();
const float *source_tangents_ptr = source_mesh_tangent_array.ptr();
const int *source_bones_ptr = source_mesh_bones_array.ptr();
const float *source_weights_ptr = source_mesh_weights_array.ptr();
Vector3 *lerped_vertices_ptrw = lerped_vertex_array.ptrw();
Vector3 *lerped_normals_ptrw = lerped_normal_array.ptrw();
float *lerped_tangents_ptrw = lerped_tangent_array.ptrw();
for (unsigned int vertex_index = 0; vertex_index < vertex_count; vertex_index++) {
Vector3 lerped_vertex;
Vector3 lerped_normal;
Vector3 lerped_tangent;
const Vector3 &source_vertex = source_vertices_ptr[vertex_index];
Vector3 source_normal;
if (use_normal_array) {
source_normal = source_normals_ptr[vertex_index];
}
int tangent_index = vertex_index * 4;
Vector4 source_tangent;
Vector3 source_tangent_vec3;
if (use_tangent_array) {
source_tangent = Vector4(
source_tangents_ptr[tangent_index],
source_tangents_ptr[tangent_index + 1],
source_tangents_ptr[tangent_index + 2],
source_tangents_ptr[tangent_index + 3]);
DEV_ASSERT(source_tangent.w == 1.0 || source_tangent.w == -1.0);
source_tangent_vec3 = Vector3(source_tangent.x, source_tangent.y, source_tangent.z);
}
for (unsigned int weight_index = 0; weight_index < bones_per_vertex; weight_index++) {
float bone_weight = source_weights_ptr[vertex_index * bones_per_vertex + weight_index];
if (bone_weight < FLT_EPSILON) {
continue;
}
int vertex_bone_index = source_bones_ptr[vertex_index * bones_per_vertex + weight_index];
const Transform3D &bone_transform = bone_transforms[vertex_bone_index];
const Basis bone_basis = bone_transform.basis.orthonormalized();
ERR_FAIL_INDEX_V(vertex_bone_index, static_cast<int>(bone_transforms.size()), Ref<ArrayMesh>());
lerped_vertex += source_vertex.lerp(bone_transform.xform(source_vertex), bone_weight) - source_vertex;
;
if (use_normal_array) {
lerped_normal += source_normal.lerp(bone_basis.xform(source_normal), bone_weight) - source_normal;
}
if (use_tangent_array) {
lerped_tangent += source_tangent_vec3.lerp(bone_basis.xform(source_tangent_vec3), bone_weight) - source_tangent_vec3;
}
}
lerped_vertices_ptrw[vertex_index] += lerped_vertex;
if (use_normal_array) {
lerped_normals_ptrw[vertex_index] = (source_normal + lerped_normal).normalized();
}
if (use_tangent_array) {
lerped_tangent = (source_tangent_vec3 + lerped_tangent).normalized();
lerped_tangents_ptrw[tangent_index] = lerped_tangent.x;
lerped_tangents_ptrw[tangent_index + 1] = lerped_tangent.y;
lerped_tangents_ptrw[tangent_index + 2] = lerped_tangent.z;
}
}
new_mesh_arrays[Mesh::ARRAY_VERTEX] = lerped_vertex_array;
if (use_normal_array) {
new_mesh_arrays[Mesh::ARRAY_NORMAL] = lerped_normal_array;
}
if (use_tangent_array) {
new_mesh_arrays[Mesh::ARRAY_TANGENT] = lerped_tangent_array;
}
bake_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, new_mesh_arrays, Array(), Dictionary(), surface_format);
}
return bake_mesh;
}
Ref<TriangleMesh> MeshInstance3D::generate_triangle_mesh() const {
if (mesh.is_valid()) {
return mesh->generate_triangle_mesh();
}
return Ref<TriangleMesh>();
}
void MeshInstance3D::navmesh_parse_init() {
ERR_FAIL_NULL(NavigationServer3D::get_singleton());
if (!_navmesh_source_geometry_parser.is_valid()) {
_navmesh_source_geometry_parsing_callback = callable_mp_static(&MeshInstance3D::navmesh_parse_source_geometry);
_navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create();
NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
}
}
void MeshInstance3D::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) {
MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(p_node);
if (mesh_instance == nullptr) {
return;
}
NavigationMesh::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
if (parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) {
Ref<Mesh> mesh = mesh_instance->get_mesh();
if (mesh.is_valid()) {
p_source_geometry_data->add_mesh(mesh, mesh_instance->get_global_transform());
}
}
}
void MeshInstance3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshInstance3D::set_mesh);
ClassDB::bind_method(D_METHOD("get_mesh"), &MeshInstance3D::get_mesh);
@ -700,6 +904,7 @@ void MeshInstance3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_debug_tangents"), &MeshInstance3D::create_debug_tangents);
ClassDB::bind_method(D_METHOD("bake_mesh_from_current_blend_shape_mix", "existing"), &MeshInstance3D::bake_mesh_from_current_blend_shape_mix, DEFVAL(Ref<ArrayMesh>()));
ClassDB::bind_method(D_METHOD("bake_mesh_from_current_skeleton_pose", "existing"), &MeshInstance3D::bake_mesh_from_current_skeleton_pose, DEFVAL(Ref<ArrayMesh>()));
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
ADD_GROUP("Skeleton", "");