Path: blob/master/scene/resources/3d/importer_mesh.cpp
9903 views
/**************************************************************************/1/* importer_mesh.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "importer_mesh.h"3132#include "core/io/marshalls.h"33#include "core/math/random_pcg.h"34#include "scene/resources/surface_tool.h"3536#ifndef PHYSICS_3D_DISABLED37#include "core/math/convex_hull.h"38#endif // PHYSICS_3D_DISABLED3940String ImporterMesh::validate_blend_shape_name(const String &p_name) {41return p_name.replace_char(':', '_');42}4344void ImporterMesh::add_blend_shape(const String &p_name) {45ERR_FAIL_COND(surfaces.size() > 0);46blend_shapes.push_back(validate_blend_shape_name(p_name));47}4849int ImporterMesh::get_blend_shape_count() const {50return blend_shapes.size();51}5253String ImporterMesh::get_blend_shape_name(int p_blend_shape) const {54ERR_FAIL_INDEX_V(p_blend_shape, blend_shapes.size(), String());55return blend_shapes[p_blend_shape];56}5758void ImporterMesh::set_blend_shape_mode(Mesh::BlendShapeMode p_blend_shape_mode) {59blend_shape_mode = p_blend_shape_mode;60}6162Mesh::BlendShapeMode ImporterMesh::get_blend_shape_mode() const {63return blend_shape_mode;64}6566void ImporterMesh::add_surface(Mesh::PrimitiveType p_primitive, const Array &p_arrays, const TypedArray<Array> &p_blend_shapes, const Dictionary &p_lods, const Ref<Material> &p_material, const String &p_name, const uint64_t p_flags) {67ERR_FAIL_COND(p_blend_shapes.size() != blend_shapes.size());68ERR_FAIL_COND(p_arrays.size() != Mesh::ARRAY_MAX);69Surface s;70s.primitive = p_primitive;71s.arrays = p_arrays;72s.name = p_name;73s.flags = p_flags;7475Vector<Vector3> vertex_array = p_arrays[Mesh::ARRAY_VERTEX];76int vertex_count = vertex_array.size();77ERR_FAIL_COND(vertex_count == 0);7879for (int i = 0; i < blend_shapes.size(); i++) {80Array bsdata = p_blend_shapes[i];81ERR_FAIL_COND(bsdata.size() != Mesh::ARRAY_MAX);82Vector<Vector3> vertex_data = bsdata[Mesh::ARRAY_VERTEX];83ERR_FAIL_COND(vertex_data.size() != vertex_count);84Surface::BlendShape bs;85bs.arrays = bsdata;86s.blend_shape_data.push_back(bs);87}8889for (const KeyValue<Variant, Variant> &kv : p_lods) {90ERR_CONTINUE(!kv.key.is_num());91Surface::LOD lod;92lod.distance = kv.key;93lod.indices = kv.value;94ERR_CONTINUE(lod.indices.is_empty());95s.lods.push_back(lod);96}9798s.material = p_material;99100surfaces.push_back(s);101mesh.unref();102}103104int ImporterMesh::get_surface_count() const {105return surfaces.size();106}107108Mesh::PrimitiveType ImporterMesh::get_surface_primitive_type(int p_surface) {109ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Mesh::PRIMITIVE_MAX);110return surfaces[p_surface].primitive;111}112Array ImporterMesh::get_surface_arrays(int p_surface) const {113ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());114return surfaces[p_surface].arrays;115}116String ImporterMesh::get_surface_name(int p_surface) const {117ERR_FAIL_INDEX_V(p_surface, surfaces.size(), String());118return surfaces[p_surface].name;119}120void ImporterMesh::set_surface_name(int p_surface, const String &p_name) {121ERR_FAIL_INDEX(p_surface, surfaces.size());122surfaces.write[p_surface].name = p_name;123mesh.unref();124}125126Array ImporterMesh::get_surface_blend_shape_arrays(int p_surface, int p_blend_shape) const {127ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());128ERR_FAIL_INDEX_V(p_blend_shape, surfaces[p_surface].blend_shape_data.size(), Array());129return surfaces[p_surface].blend_shape_data[p_blend_shape].arrays;130}131int ImporterMesh::get_surface_lod_count(int p_surface) const {132ERR_FAIL_INDEX_V(p_surface, surfaces.size(), 0);133return surfaces[p_surface].lods.size();134}135Vector<int> ImporterMesh::get_surface_lod_indices(int p_surface, int p_lod) const {136ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Vector<int>());137ERR_FAIL_INDEX_V(p_lod, surfaces[p_surface].lods.size(), Vector<int>());138139return surfaces[p_surface].lods[p_lod].indices;140}141142float ImporterMesh::get_surface_lod_size(int p_surface, int p_lod) const {143ERR_FAIL_INDEX_V(p_surface, surfaces.size(), 0);144ERR_FAIL_INDEX_V(p_lod, surfaces[p_surface].lods.size(), 0);145return surfaces[p_surface].lods[p_lod].distance;146}147148uint64_t ImporterMesh::get_surface_format(int p_surface) const {149ERR_FAIL_INDEX_V(p_surface, surfaces.size(), 0);150return surfaces[p_surface].flags;151}152153Ref<Material> ImporterMesh::get_surface_material(int p_surface) const {154ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Ref<Material>());155return surfaces[p_surface].material;156}157158void ImporterMesh::set_surface_material(int p_surface, const Ref<Material> &p_material) {159ERR_FAIL_INDEX(p_surface, surfaces.size());160surfaces.write[p_surface].material = p_material;161mesh.unref();162}163164template <typename T>165static Vector<T> _remap_array(Vector<T> p_array, const Vector<uint32_t> &p_remap, uint32_t p_vertex_count) {166ERR_FAIL_COND_V(p_array.size() % p_remap.size() != 0, p_array);167int num_elements = p_array.size() / p_remap.size();168T *data = p_array.ptrw();169SurfaceTool::remap_vertex_func(data, data, p_remap.size(), sizeof(T) * num_elements, p_remap.ptr());170p_array.resize(p_vertex_count * num_elements);171return p_array;172}173174static void _remap_arrays(Array &r_arrays, const Vector<uint32_t> &p_remap, uint32_t p_vertex_count) {175for (int i = 0; i < r_arrays.size(); i++) {176if (i == RS::ARRAY_INDEX) {177continue;178}179180switch (r_arrays[i].get_type()) {181case Variant::NIL:182break;183case Variant::PACKED_VECTOR3_ARRAY:184r_arrays[i] = _remap_array<Vector3>(r_arrays[i], p_remap, p_vertex_count);185break;186case Variant::PACKED_VECTOR2_ARRAY:187r_arrays[i] = _remap_array<Vector2>(r_arrays[i], p_remap, p_vertex_count);188break;189case Variant::PACKED_FLOAT32_ARRAY:190r_arrays[i] = _remap_array<float>(r_arrays[i], p_remap, p_vertex_count);191break;192case Variant::PACKED_INT32_ARRAY:193r_arrays[i] = _remap_array<int32_t>(r_arrays[i], p_remap, p_vertex_count);194break;195case Variant::PACKED_BYTE_ARRAY:196r_arrays[i] = _remap_array<uint8_t>(r_arrays[i], p_remap, p_vertex_count);197break;198case Variant::PACKED_COLOR_ARRAY:199r_arrays[i] = _remap_array<Color>(r_arrays[i], p_remap, p_vertex_count);200break;201default:202ERR_FAIL_MSG("Unhandled array type.");203}204}205}206207void ImporterMesh::optimize_indices() {208if (!SurfaceTool::optimize_vertex_cache_func) {209return;210}211if (!SurfaceTool::optimize_vertex_fetch_remap_func || !SurfaceTool::remap_vertex_func || !SurfaceTool::remap_index_func) {212return;213}214215for (int i = 0; i < surfaces.size(); i++) {216if (surfaces[i].primitive != Mesh::PRIMITIVE_TRIANGLES) {217continue;218}219220Vector<Vector3> vertices = surfaces[i].arrays[RS::ARRAY_VERTEX];221PackedInt32Array indices = surfaces[i].arrays[RS::ARRAY_INDEX];222223unsigned int index_count = indices.size();224unsigned int vertex_count = vertices.size();225226if (index_count == 0) {227continue;228}229230// Optimize indices for vertex cache to establish final triangle order.231int *indices_ptr = indices.ptrw();232SurfaceTool::optimize_vertex_cache_func((unsigned int *)indices_ptr, (const unsigned int *)indices_ptr, index_count, vertex_count);233surfaces.write[i].arrays[RS::ARRAY_INDEX] = indices;234235for (int j = 0; j < surfaces[i].lods.size(); ++j) {236Surface::LOD &lod = surfaces.write[i].lods.write[j];237int *lod_indices_ptr = lod.indices.ptrw();238SurfaceTool::optimize_vertex_cache_func((unsigned int *)lod_indices_ptr, (const unsigned int *)lod_indices_ptr, lod.indices.size(), vertex_count);239}240241// Concatenate indices for all LODs in the order of coarse->fine; this establishes the effective order of vertices,242// and is important to optimize for vertex fetch (all GPUs) and shading (Mali GPUs)243PackedInt32Array merged_indices;244for (int j = surfaces[i].lods.size() - 1; j >= 0; --j) {245merged_indices.append_array(surfaces[i].lods[j].indices);246}247merged_indices.append_array(indices);248249// Generate remap array that establishes optimal vertex order according to the order of indices above.250Vector<uint32_t> remap;251remap.resize(vertex_count);252unsigned int new_vertex_count = SurfaceTool::optimize_vertex_fetch_remap_func(remap.ptrw(), (const unsigned int *)merged_indices.ptr(), merged_indices.size(), vertex_count);253254// We need to remap all vertex and index arrays in lockstep according to the remap.255SurfaceTool::remap_index_func((unsigned int *)indices_ptr, (const unsigned int *)indices_ptr, index_count, remap.ptr());256surfaces.write[i].arrays[RS::ARRAY_INDEX] = indices;257258for (int j = 0; j < surfaces[i].lods.size(); ++j) {259Surface::LOD &lod = surfaces.write[i].lods.write[j];260int *lod_indices_ptr = lod.indices.ptrw();261SurfaceTool::remap_index_func((unsigned int *)lod_indices_ptr, (const unsigned int *)lod_indices_ptr, lod.indices.size(), remap.ptr());262}263264_remap_arrays(surfaces.write[i].arrays, remap, new_vertex_count);265for (int j = 0; j < surfaces[i].blend_shape_data.size(); j++) {266_remap_arrays(surfaces.write[i].blend_shape_data.write[j].arrays, remap, new_vertex_count);267}268}269270if (shadow_mesh.is_valid()) {271shadow_mesh->optimize_indices();272}273}274275#define VERTEX_SKIN_FUNC(bone_count, vert_idx, read_array, write_array, transform_array, bone_array, weight_array) \276Vector3 transformed_vert; \277for (unsigned int weight_idx = 0; weight_idx < bone_count; weight_idx++) { \278int bone_idx = bone_array[vert_idx * bone_count + weight_idx]; \279float w = weight_array[vert_idx * bone_count + weight_idx]; \280if (w < FLT_EPSILON) { \281continue; \282} \283ERR_FAIL_INDEX(bone_idx, static_cast<int>(transform_array.size())); \284transformed_vert += transform_array[bone_idx].xform(read_array[vert_idx]) * w; \285} \286write_array[vert_idx] = transformed_vert;287288void ImporterMesh::generate_lods(float p_normal_merge_angle, Array p_bone_transform_array) {289if (!SurfaceTool::simplify_scale_func) {290return;291}292if (!SurfaceTool::simplify_with_attrib_func) {293return;294}295296LocalVector<Transform3D> bone_transform_vector;297for (int i = 0; i < p_bone_transform_array.size(); i++) {298ERR_FAIL_COND(p_bone_transform_array[i].get_type() != Variant::TRANSFORM3D);299bone_transform_vector.push_back(p_bone_transform_array[i]);300}301302for (int i = 0; i < surfaces.size(); i++) {303if (surfaces[i].primitive != Mesh::PRIMITIVE_TRIANGLES) {304continue;305}306307surfaces.write[i].lods.clear();308Vector<Vector3> vertices = surfaces[i].arrays[RS::ARRAY_VERTEX];309PackedInt32Array indices = surfaces[i].arrays[RS::ARRAY_INDEX];310Vector<Vector3> normals = surfaces[i].arrays[RS::ARRAY_NORMAL];311Vector<float> tangents = surfaces[i].arrays[RS::ARRAY_TANGENT];312Vector<Vector2> uvs = surfaces[i].arrays[RS::ARRAY_TEX_UV];313Vector<Vector2> uv2s = surfaces[i].arrays[RS::ARRAY_TEX_UV2];314Vector<int> bones = surfaces[i].arrays[RS::ARRAY_BONES];315Vector<float> weights = surfaces[i].arrays[RS::ARRAY_WEIGHTS];316Vector<Color> colors = surfaces[i].arrays[RS::ARRAY_COLOR];317318unsigned int index_count = indices.size();319unsigned int vertex_count = vertices.size();320321if (index_count == 0) {322continue; //no lods if no indices323}324ERR_FAIL_COND_MSG(index_count % 3 != 0, "ImporterMesh::generate_lods: Indexed triangle meshes MUST have an index array with a size that is a multiple of 3, but got " + itos(index_count) + " indices. Cannot generate LODs for this invalid mesh.");325326const Vector3 *vertices_ptr = vertices.ptr();327const int *indices_ptr = indices.ptr();328329if (normals.is_empty()) {330normals.resize(index_count);331Vector3 *n_ptr = normals.ptrw();332for (unsigned int j = 0; j < index_count; j += 3) {333const Vector3 &v0 = vertices_ptr[indices_ptr[j + 0]];334const Vector3 &v1 = vertices_ptr[indices_ptr[j + 1]];335const Vector3 &v2 = vertices_ptr[indices_ptr[j + 2]];336Vector3 n = vec3_cross(v0 - v2, v0 - v1).normalized();337n_ptr[j + 0] = n;338n_ptr[j + 1] = n;339n_ptr[j + 2] = n;340}341}342343if (bones.size() > 0 && weights.size() && bone_transform_vector.size() > 0) {344Vector3 *vertices_ptrw = vertices.ptrw();345346// Apply bone transforms to regular surface.347unsigned int bone_weight_length = surfaces[i].flags & Mesh::ARRAY_FLAG_USE_8_BONE_WEIGHTS ? 8 : 4;348349const int *bo = bones.ptr();350const float *we = weights.ptr();351352for (unsigned int j = 0; j < vertex_count; j++) {353VERTEX_SKIN_FUNC(bone_weight_length, j, vertices_ptr, vertices_ptrw, bone_transform_vector, bo, we)354}355356vertices_ptr = vertices.ptr();357}358359float normal_merge_threshold = Math::cos(Math::deg_to_rad(p_normal_merge_angle));360const Vector3 *normals_ptr = normals.ptr();361362HashMap<Vector3, LocalVector<Pair<int, int>>> unique_vertices;363364LocalVector<int> vertex_remap;365LocalVector<int> vertex_inverse_remap;366LocalVector<Vector3> merged_vertices;367LocalVector<Vector3> merged_normals;368LocalVector<int> merged_normals_counts;369const Vector2 *uvs_ptr = uvs.ptr();370const Vector2 *uv2s_ptr = uv2s.ptr();371const float *tangents_ptr = tangents.ptr();372const Color *colors_ptr = colors.ptr();373374for (unsigned int j = 0; j < vertex_count; j++) {375const Vector3 &v = vertices_ptr[j];376const Vector3 &n = normals_ptr[j];377378HashMap<Vector3, LocalVector<Pair<int, int>>>::Iterator E = unique_vertices.find(v);379380if (E) {381const LocalVector<Pair<int, int>> &close_verts = E->value;382383bool found = false;384for (const Pair<int, int> &idx : close_verts) {385bool is_uvs_close = (!uvs_ptr || uvs_ptr[j].distance_squared_to(uvs_ptr[idx.second]) < CMP_EPSILON2);386bool is_uv2s_close = (!uv2s_ptr || uv2s_ptr[j].distance_squared_to(uv2s_ptr[idx.second]) < CMP_EPSILON2);387bool is_tang_aligned = !tangents_ptr || (tangents_ptr[j * 4 + 3] < 0) == (tangents_ptr[idx.second * 4 + 3] < 0);388ERR_FAIL_INDEX(idx.second, normals.size());389bool is_normals_close = normals[idx.second].dot(n) > normal_merge_threshold;390bool is_col_close = (!colors_ptr || colors_ptr[j].is_equal_approx(colors_ptr[idx.second]));391if (is_uvs_close && is_uv2s_close && is_normals_close && is_tang_aligned && is_col_close) {392vertex_remap.push_back(idx.first);393merged_normals[idx.first] += normals[idx.second];394merged_normals_counts[idx.first]++;395found = true;396break;397}398}399400if (!found) {401int vcount = merged_vertices.size();402unique_vertices[v].push_back(Pair<int, int>(vcount, j));403vertex_inverse_remap.push_back(j);404merged_vertices.push_back(v);405vertex_remap.push_back(vcount);406merged_normals.push_back(normals_ptr[j]);407merged_normals_counts.push_back(1);408}409} else {410int vcount = merged_vertices.size();411unique_vertices[v] = LocalVector<Pair<int, int>>();412unique_vertices[v].push_back(Pair<int, int>(vcount, j));413vertex_inverse_remap.push_back(j);414merged_vertices.push_back(v);415vertex_remap.push_back(vcount);416merged_normals.push_back(normals_ptr[j]);417merged_normals_counts.push_back(1);418}419}420421LocalVector<int> merged_indices;422merged_indices.resize(index_count);423for (unsigned int j = 0; j < index_count; j++) {424merged_indices[j] = vertex_remap[indices[j]];425}426427unsigned int merged_vertex_count = merged_vertices.size();428const Vector3 *merged_vertices_ptr = merged_vertices.ptr();429const int32_t *merged_indices_ptr = merged_indices.ptr();430Vector3 *merged_normals_ptr = merged_normals.ptr();431432{433const int *counts_ptr = merged_normals_counts.ptr();434for (unsigned int j = 0; j < merged_vertex_count; j++) {435merged_normals_ptr[j] /= counts_ptr[j];436}437}438439Vector<float> merged_vertices_f32 = vector3_to_float32_array(merged_vertices_ptr, merged_vertex_count);440float scale = SurfaceTool::simplify_scale_func(merged_vertices_f32.ptr(), merged_vertex_count, sizeof(float) * 3);441442const size_t attrib_count = 6; // 3 for normal + 3 for color (if present)443444float attrib_weights[attrib_count] = {};445446// Give some weight to normal preservation447attrib_weights[0] = attrib_weights[1] = attrib_weights[2] = 1.0f;448449// Give some weight to colors but only if present to avoid redundant computations during simplification450if (colors_ptr) {451attrib_weights[3] = attrib_weights[4] = attrib_weights[5] = 1.0f;452}453454LocalVector<float> merged_attribs;455merged_attribs.resize(merged_vertex_count * attrib_count);456float *merged_attribs_ptr = merged_attribs.ptr();457458memset(merged_attribs_ptr, 0, merged_attribs.size() * sizeof(float));459460for (unsigned int j = 0; j < merged_vertex_count; ++j) {461merged_attribs_ptr[j * attrib_count + 0] = merged_normals_ptr[j].x;462merged_attribs_ptr[j * attrib_count + 1] = merged_normals_ptr[j].y;463merged_attribs_ptr[j * attrib_count + 2] = merged_normals_ptr[j].z;464465if (colors_ptr) {466unsigned int rj = vertex_inverse_remap[j];467468merged_attribs_ptr[j * attrib_count + 3] = colors_ptr[rj].r;469merged_attribs_ptr[j * attrib_count + 4] = colors_ptr[rj].g;470merged_attribs_ptr[j * attrib_count + 5] = colors_ptr[rj].b;471}472}473474unsigned int index_target = 12; // Start with the smallest target, 4 triangles475unsigned int last_index_count = 0;476477const float max_mesh_error = 1.0f; // we only need LODs that can be selected by error threshold478float mesh_error = 0.0f;479480while (index_target < index_count) {481PackedInt32Array new_indices;482new_indices.resize(index_count);483484const int simplify_options = SurfaceTool::SIMPLIFY_LOCK_BORDER;485486size_t new_index_count = SurfaceTool::simplify_with_attrib_func(487(unsigned int *)new_indices.ptrw(),488(const uint32_t *)merged_indices_ptr, index_count,489merged_vertices_f32.ptr(), merged_vertex_count,490sizeof(float) * 3, // Vertex stride491merged_attribs_ptr,492sizeof(float) * attrib_count, // Attribute stride493attrib_weights, attrib_count,494nullptr, // Vertex lock495index_target,496max_mesh_error,497simplify_options,498&mesh_error);499500if (new_index_count < last_index_count * 1.5f) {501index_target = index_target * 1.5f;502continue;503}504505if (new_index_count == 0 || (new_index_count >= (index_count * 0.75f))) {506break;507}508if (new_index_count > 5000000) {509// This limit theoretically shouldn't be needed, but it's here510// as an ad-hoc fix to prevent a crash with complex meshes.511// The crash still happens with limit of 6000000, but 5000000 works.512// In the future, identify what's causing that crash and fix it.513WARN_PRINT("Mesh LOD generation failed for mesh " + get_name() + " surface " + itos(i) + ", mesh is too complex. Some automatic LODs were not generated.");514break;515}516517new_indices.resize(new_index_count);518{519int *ptrw = new_indices.ptrw();520for (unsigned int j = 0; j < new_index_count; j++) {521ptrw[j] = vertex_inverse_remap[ptrw[j]];522}523}524525Surface::LOD lod;526lod.distance = MAX(mesh_error * scale, CMP_EPSILON2);527lod.indices = new_indices;528surfaces.write[i].lods.push_back(lod);529index_target = MAX(new_index_count, index_target) * 2;530last_index_count = new_index_count;531532if (mesh_error == 0.0f) {533break;534}535}536537surfaces.write[i].lods.sort_custom<Surface::LODComparator>();538}539}540541void ImporterMesh::_generate_lods_bind(float p_normal_merge_angle, float p_normal_split_angle, Array p_skin_pose_transform_array) {542// p_normal_split_angle is unused, but kept for compatibility543generate_lods(p_normal_merge_angle, p_skin_pose_transform_array);544}545546bool ImporterMesh::has_mesh() const {547return mesh.is_valid();548}549550Ref<ArrayMesh> ImporterMesh::get_mesh(const Ref<ArrayMesh> &p_base) {551ERR_FAIL_COND_V(surfaces.is_empty(), Ref<ArrayMesh>());552553if (mesh.is_null()) {554if (p_base.is_valid()) {555mesh = p_base;556}557if (mesh.is_null()) {558mesh.instantiate();559}560mesh->set_name(get_name());561if (has_meta("import_id")) {562mesh->set_meta("import_id", get_meta("import_id"));563}564for (int i = 0; i < blend_shapes.size(); i++) {565mesh->add_blend_shape(blend_shapes[i]);566}567mesh->set_blend_shape_mode(blend_shape_mode);568for (int i = 0; i < surfaces.size(); i++) {569Array bs_data;570if (surfaces[i].blend_shape_data.size()) {571for (int j = 0; j < surfaces[i].blend_shape_data.size(); j++) {572bs_data.push_back(surfaces[i].blend_shape_data[j].arrays);573}574}575Dictionary lods;576if (surfaces[i].lods.size()) {577for (int j = 0; j < surfaces[i].lods.size(); j++) {578lods[surfaces[i].lods[j].distance] = surfaces[i].lods[j].indices;579}580}581582mesh->add_surface_from_arrays(surfaces[i].primitive, surfaces[i].arrays, bs_data, lods, surfaces[i].flags);583if (surfaces[i].material.is_valid()) {584mesh->surface_set_material(mesh->get_surface_count() - 1, surfaces[i].material);585}586if (!surfaces[i].name.is_empty()) {587mesh->surface_set_name(mesh->get_surface_count() - 1, surfaces[i].name);588}589}590591mesh->set_lightmap_size_hint(lightmap_size_hint);592593if (shadow_mesh.is_valid()) {594Ref<ArrayMesh> shadow = shadow_mesh->get_mesh();595mesh->set_shadow_mesh(shadow);596}597}598599return mesh;600}601602void ImporterMesh::clear() {603surfaces.clear();604blend_shapes.clear();605mesh.unref();606}607608void ImporterMesh::create_shadow_mesh() {609if (shadow_mesh.is_valid()) {610shadow_mesh.unref();611}612613//no shadow mesh for blendshapes614if (blend_shapes.size() > 0) {615return;616}617//no shadow mesh for skeletons618for (int i = 0; i < surfaces.size(); i++) {619if (surfaces[i].arrays[RS::ARRAY_BONES].get_type() != Variant::NIL) {620return;621}622if (surfaces[i].arrays[RS::ARRAY_WEIGHTS].get_type() != Variant::NIL) {623return;624}625}626627shadow_mesh.instantiate();628629for (int i = 0; i < surfaces.size(); i++) {630LocalVector<int> vertex_remap;631Vector<Vector3> new_vertices;632Vector<Vector3> vertices = surfaces[i].arrays[RS::ARRAY_VERTEX];633int vertex_count = vertices.size();634{635HashMap<Vector3, int> unique_vertices;636const Vector3 *vptr = vertices.ptr();637for (int j = 0; j < vertex_count; j++) {638const Vector3 &v = vptr[j];639640HashMap<Vector3, int>::Iterator E = unique_vertices.find(v);641642if (E) {643vertex_remap.push_back(E->value);644} else {645int vcount = unique_vertices.size();646unique_vertices[v] = vcount;647vertex_remap.push_back(vcount);648new_vertices.push_back(v);649}650}651}652653Array new_surface;654new_surface.resize(RS::ARRAY_MAX);655Dictionary lods;656657// print_line("original vertex count: " + itos(vertices.size()) + " new vertex count: " + itos(new_vertices.size()));658659new_surface[RS::ARRAY_VERTEX] = new_vertices;660661Vector<int> indices = surfaces[i].arrays[RS::ARRAY_INDEX];662if (indices.size()) {663int index_count = indices.size();664const int *index_rptr = indices.ptr();665Vector<int> new_indices;666new_indices.resize(indices.size());667int *index_wptr = new_indices.ptrw();668669for (int j = 0; j < index_count; j++) {670int index = index_rptr[j];671ERR_FAIL_INDEX(index, vertex_count);672index_wptr[j] = vertex_remap[index];673}674675new_surface[RS::ARRAY_INDEX] = new_indices;676677// Make sure the same LODs as the full version are used.678// This makes it more coherent between rendered model and its shadows.679for (int j = 0; j < surfaces[i].lods.size(); j++) {680indices = surfaces[i].lods[j].indices;681682index_count = indices.size();683index_rptr = indices.ptr();684new_indices.resize(indices.size());685index_wptr = new_indices.ptrw();686687for (int k = 0; k < index_count; k++) {688int index = index_rptr[k];689ERR_FAIL_INDEX(index, vertex_count);690index_wptr[k] = vertex_remap[index];691}692693lods[surfaces[i].lods[j].distance] = new_indices;694}695}696697shadow_mesh->add_surface(surfaces[i].primitive, new_surface, Array(), lods, Ref<Material>(), surfaces[i].name, surfaces[i].flags);698}699}700701Ref<ImporterMesh> ImporterMesh::get_shadow_mesh() const {702return shadow_mesh;703}704705void ImporterMesh::_set_data(const Dictionary &p_data) {706clear();707if (p_data.has("blend_shape_names")) {708blend_shapes = p_data["blend_shape_names"];709}710if (p_data.has("surfaces")) {711Array surface_arr = p_data["surfaces"];712for (int i = 0; i < surface_arr.size(); i++) {713Dictionary s = surface_arr[i];714ERR_CONTINUE(!s.has("primitive"));715ERR_CONTINUE(!s.has("arrays"));716Mesh::PrimitiveType prim = Mesh::PrimitiveType(int(s["primitive"]));717ERR_CONTINUE(prim >= Mesh::PRIMITIVE_MAX);718Array arr = s["arrays"];719Dictionary lods;720String surf_name;721if (s.has("name")) {722surf_name = s["name"];723}724if (s.has("lods")) {725lods = s["lods"];726}727Array b_shapes;728if (s.has("b_shapes")) {729b_shapes = s["b_shapes"];730}731Ref<Material> material;732if (s.has("material")) {733material = s["material"];734}735uint64_t flags = 0;736if (s.has("flags")) {737flags = s["flags"];738}739add_surface(prim, arr, b_shapes, lods, material, surf_name, flags);740}741}742}743Dictionary ImporterMesh::_get_data() const {744Dictionary data;745if (blend_shapes.size()) {746data["blend_shape_names"] = blend_shapes;747}748Array surface_arr;749for (int i = 0; i < surfaces.size(); i++) {750Dictionary d;751d["primitive"] = surfaces[i].primitive;752d["arrays"] = surfaces[i].arrays;753if (surfaces[i].blend_shape_data.size()) {754Array bs_data;755for (int j = 0; j < surfaces[i].blend_shape_data.size(); j++) {756bs_data.push_back(surfaces[i].blend_shape_data[j].arrays);757}758d["blend_shapes"] = bs_data;759}760if (surfaces[i].lods.size()) {761Dictionary lods;762for (int j = 0; j < surfaces[i].lods.size(); j++) {763lods[surfaces[i].lods[j].distance] = surfaces[i].lods[j].indices;764}765d["lods"] = lods;766}767768if (surfaces[i].material.is_valid()) {769d["material"] = surfaces[i].material;770}771772if (!surfaces[i].name.is_empty()) {773d["name"] = surfaces[i].name;774}775776d["flags"] = surfaces[i].flags;777778surface_arr.push_back(d);779}780data["surfaces"] = surface_arr;781return data;782}783784Vector<Face3> ImporterMesh::get_faces() const {785Vector<Face3> faces;786for (int i = 0; i < surfaces.size(); i++) {787if (surfaces[i].primitive == Mesh::PRIMITIVE_TRIANGLES) {788Vector<Vector3> vertices = surfaces[i].arrays[Mesh::ARRAY_VERTEX];789Vector<int> indices = surfaces[i].arrays[Mesh::ARRAY_INDEX];790if (indices.size()) {791for (int j = 0; j < indices.size(); j += 3) {792Face3 f;793f.vertex[0] = vertices[indices[j + 0]];794f.vertex[1] = vertices[indices[j + 1]];795f.vertex[2] = vertices[indices[j + 2]];796faces.push_back(f);797}798} else {799for (int j = 0; j < vertices.size(); j += 3) {800Face3 f;801f.vertex[0] = vertices[j + 0];802f.vertex[1] = vertices[j + 1];803f.vertex[2] = vertices[j + 2];804faces.push_back(f);805}806}807}808}809810return faces;811}812813#ifndef PHYSICS_3D_DISABLED814Vector<Ref<Shape3D>> ImporterMesh::convex_decompose(const Ref<MeshConvexDecompositionSettings> &p_settings) const {815ERR_FAIL_NULL_V(Mesh::convex_decomposition_function, Vector<Ref<Shape3D>>());816817const Vector<Face3> faces = get_faces();818int face_count = faces.size();819820Vector<Vector3> vertices;821uint32_t vertex_count = 0;822vertices.resize(face_count * 3);823Vector<uint32_t> indices;824indices.resize(face_count * 3);825{826HashMap<Vector3, uint32_t> vertex_map;827Vector3 *vertex_w = vertices.ptrw();828uint32_t *index_w = indices.ptrw();829for (int i = 0; i < face_count; i++) {830for (int j = 0; j < 3; j++) {831const Vector3 &vertex = faces[i].vertex[j];832HashMap<Vector3, uint32_t>::Iterator found_vertex = vertex_map.find(vertex);833uint32_t index;834if (found_vertex) {835index = found_vertex->value;836} else {837index = vertex_count++;838vertex_map[vertex] = index;839vertex_w[index] = vertex;840}841index_w[i * 3 + j] = index;842}843}844}845vertices.resize(vertex_count);846847Vector<Vector<Vector3>> decomposed = Mesh::convex_decomposition_function((real_t *)vertices.ptr(), vertex_count, indices.ptr(), face_count, p_settings, nullptr);848849Vector<Ref<Shape3D>> ret;850851for (int i = 0; i < decomposed.size(); i++) {852Ref<ConvexPolygonShape3D> shape;853shape.instantiate();854shape->set_points(decomposed[i]);855ret.push_back(shape);856}857858return ret;859}860861Ref<ConvexPolygonShape3D> ImporterMesh::create_convex_shape(bool p_clean, bool p_simplify) const {862if (p_simplify) {863Ref<MeshConvexDecompositionSettings> settings;864settings.instantiate();865settings->set_max_convex_hulls(1);866Vector<Ref<Shape3D>> decomposed = convex_decompose(settings);867if (decomposed.size() == 1) {868return decomposed[0];869} else {870ERR_PRINT("Convex shape simplification failed, falling back to simpler process.");871}872}873874Vector<Vector3> vertices;875for (int i = 0; i < get_surface_count(); i++) {876Array a = get_surface_arrays(i);877ERR_FAIL_COND_V(a.is_empty(), Ref<ConvexPolygonShape3D>());878Vector<Vector3> v = a[Mesh::ARRAY_VERTEX];879vertices.append_array(v);880}881882Ref<ConvexPolygonShape3D> shape = memnew(ConvexPolygonShape3D);883884if (p_clean) {885Geometry3D::MeshData md;886Error err = ConvexHullComputer::convex_hull(vertices, md);887if (err == OK) {888shape->set_points(md.vertices);889return shape;890} else {891ERR_PRINT("Convex shape cleaning failed, falling back to simpler process.");892}893}894895shape->set_points(vertices);896return shape;897}898899Ref<ConcavePolygonShape3D> ImporterMesh::create_trimesh_shape() const {900Vector<Face3> faces = get_faces();901if (faces.is_empty()) {902return Ref<ConcavePolygonShape3D>();903}904905Vector<Vector3> face_points;906face_points.resize(faces.size() * 3);907908for (int i = 0; i < face_points.size(); i += 3) {909Face3 f = faces.get(i / 3);910face_points.set(i, f.vertex[0]);911face_points.set(i + 1, f.vertex[1]);912face_points.set(i + 2, f.vertex[2]);913}914915Ref<ConcavePolygonShape3D> shape = memnew(ConcavePolygonShape3D);916shape->set_faces(face_points);917return shape;918}919#endif // PHYSICS_3D_DISABLED920921Ref<NavigationMesh> ImporterMesh::create_navigation_mesh() {922Vector<Face3> faces = get_faces();923if (faces.is_empty()) {924return Ref<NavigationMesh>();925}926927HashMap<Vector3, int> unique_vertices;928Vector<Vector<int>> face_polygons;929face_polygons.resize(faces.size());930931for (int i = 0; i < faces.size(); i++) {932Vector<int> face_indices;933face_indices.resize(3);934for (int j = 0; j < 3; j++) {935Vector3 v = faces[i].vertex[j];936int idx;937if (unique_vertices.has(v)) {938idx = unique_vertices[v];939} else {940idx = unique_vertices.size();941unique_vertices[v] = idx;942}943face_indices.write[j] = idx;944}945face_polygons.write[i] = face_indices;946}947948Vector<Vector3> vertices;949vertices.resize(unique_vertices.size());950for (const KeyValue<Vector3, int> &E : unique_vertices) {951vertices.write[E.value] = E.key;952}953954Ref<NavigationMesh> nm;955nm.instantiate();956nm->set_data(vertices, face_polygons);957958return nm;959}960961extern bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y);962963struct EditorSceneFormatImporterMeshLightmapSurface {964Ref<Material> material;965LocalVector<SurfaceTool::Vertex> vertices;966Mesh::PrimitiveType primitive = Mesh::PrimitiveType::PRIMITIVE_MAX;967uint64_t format = 0;968String name;969};970971static const uint32_t custom_shift[RS::ARRAY_CUSTOM_COUNT] = { Mesh::ARRAY_FORMAT_CUSTOM0_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM1_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM2_SHIFT, Mesh::ARRAY_FORMAT_CUSTOM3_SHIFT };972973Error ImporterMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform, float p_texel_size, const Vector<uint8_t> &p_src_cache, Vector<uint8_t> &r_dst_cache) {974ERR_FAIL_NULL_V(array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED);975ERR_FAIL_COND_V_MSG(blend_shapes.size() != 0, ERR_UNAVAILABLE, "Can't unwrap mesh with blend shapes.");976977LocalVector<float> vertices;978LocalVector<float> normals;979LocalVector<int> indices;980LocalVector<float> uv;981LocalVector<Pair<int, int>> uv_indices;982983Vector<EditorSceneFormatImporterMeshLightmapSurface> lightmap_surfaces;984985// Keep only the scale986Basis basis = p_base_transform.get_basis();987Vector3 scale = Vector3(basis.get_column(0).length(), basis.get_column(1).length(), basis.get_column(2).length());988989Transform3D transform;990transform.scale(scale);991992Basis normal_basis = transform.basis.inverse().transposed();993994for (int i = 0; i < get_surface_count(); i++) {995EditorSceneFormatImporterMeshLightmapSurface s;996s.primitive = get_surface_primitive_type(i);997998ERR_FAIL_COND_V_MSG(s.primitive != Mesh::PRIMITIVE_TRIANGLES, ERR_UNAVAILABLE, "Only triangles are supported for lightmap unwrap.");999Array arrays = get_surface_arrays(i);1000s.material = get_surface_material(i);1001s.name = get_surface_name(i);10021003SurfaceTool::create_vertex_array_from_arrays(arrays, s.vertices, &s.format);10041005PackedVector3Array rvertices = arrays[Mesh::ARRAY_VERTEX];1006int vc = rvertices.size();10071008PackedVector3Array rnormals = arrays[Mesh::ARRAY_NORMAL];10091010if (!rnormals.size()) {1011continue;1012}10131014int vertex_ofs = vertices.size() / 3;10151016vertices.resize((vertex_ofs + vc) * 3);1017normals.resize((vertex_ofs + vc) * 3);1018uv_indices.resize(vertex_ofs + vc);10191020for (int j = 0; j < vc; j++) {1021Vector3 v = transform.xform(rvertices[j]);1022Vector3 n = normal_basis.xform(rnormals[j]).normalized();10231024vertices[(j + vertex_ofs) * 3 + 0] = v.x;1025vertices[(j + vertex_ofs) * 3 + 1] = v.y;1026vertices[(j + vertex_ofs) * 3 + 2] = v.z;1027normals[(j + vertex_ofs) * 3 + 0] = n.x;1028normals[(j + vertex_ofs) * 3 + 1] = n.y;1029normals[(j + vertex_ofs) * 3 + 2] = n.z;1030uv_indices[j + vertex_ofs] = Pair<int, int>(i, j);1031}10321033PackedInt32Array rindices = arrays[Mesh::ARRAY_INDEX];1034int ic = rindices.size();10351036float eps = 1.19209290e-7F; // Taken from xatlas.h1037if (ic == 0) {1038for (int j = 0; j < vc / 3; j++) {1039Vector3 p0 = transform.xform(rvertices[j * 3 + 0]);1040Vector3 p1 = transform.xform(rvertices[j * 3 + 1]);1041Vector3 p2 = transform.xform(rvertices[j * 3 + 2]);10421043if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) {1044continue;1045}10461047indices.push_back(vertex_ofs + j * 3 + 0);1048indices.push_back(vertex_ofs + j * 3 + 1);1049indices.push_back(vertex_ofs + j * 3 + 2);1050}10511052} else {1053for (int j = 0; j < ic / 3; j++) {1054ERR_FAIL_INDEX_V(rindices[j * 3 + 0], rvertices.size(), ERR_INVALID_DATA);1055ERR_FAIL_INDEX_V(rindices[j * 3 + 1], rvertices.size(), ERR_INVALID_DATA);1056ERR_FAIL_INDEX_V(rindices[j * 3 + 2], rvertices.size(), ERR_INVALID_DATA);1057Vector3 p0 = transform.xform(rvertices[rindices[j * 3 + 0]]);1058Vector3 p1 = transform.xform(rvertices[rindices[j * 3 + 1]]);1059Vector3 p2 = transform.xform(rvertices[rindices[j * 3 + 2]]);10601061if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) {1062continue;1063}10641065indices.push_back(vertex_ofs + rindices[j * 3 + 0]);1066indices.push_back(vertex_ofs + rindices[j * 3 + 1]);1067indices.push_back(vertex_ofs + rindices[j * 3 + 2]);1068}1069}10701071lightmap_surfaces.push_back(s);1072}10731074//unwrap10751076bool use_cache = true; // Used to request cache generation and to know if cache was used1077uint8_t *gen_cache;1078int gen_cache_size;1079float *gen_uvs;1080int *gen_vertices;1081int *gen_indices;1082int gen_vertex_count;1083int gen_index_count;1084int size_x;1085int size_y;10861087bool ok = array_mesh_lightmap_unwrap_callback(p_texel_size, vertices.ptr(), normals.ptr(), vertices.size() / 3, indices.ptr(), indices.size(), p_src_cache.ptr(), &use_cache, &gen_cache, &gen_cache_size, &gen_uvs, &gen_vertices, &gen_vertex_count, &gen_indices, &gen_index_count, &size_x, &size_y);10881089if (!ok) {1090return ERR_CANT_CREATE;1091}10921093//create surfacetools for each surface..1094LocalVector<Ref<SurfaceTool>> surfaces_tools;10951096for (int i = 0; i < lightmap_surfaces.size(); i++) {1097Ref<SurfaceTool> st;1098st.instantiate();1099st->set_skin_weight_count((lightmap_surfaces[i].format & Mesh::ARRAY_FLAG_USE_8_BONE_WEIGHTS) ? SurfaceTool::SKIN_8_WEIGHTS : SurfaceTool::SKIN_4_WEIGHTS);1100st->begin(Mesh::PRIMITIVE_TRIANGLES);1101st->set_material(lightmap_surfaces[i].material);1102st->set_meta("name", lightmap_surfaces[i].name);11031104for (int custom_i = 0; custom_i < RS::ARRAY_CUSTOM_COUNT; custom_i++) {1105st->set_custom_format(custom_i, (SurfaceTool::CustomFormat)((lightmap_surfaces[i].format >> custom_shift[custom_i]) & RS::ARRAY_FORMAT_CUSTOM_MASK));1106}1107surfaces_tools.push_back(st); //stay there1108}11091110//remove surfaces1111clear();11121113print_verbose("Mesh: Gen indices: " + itos(gen_index_count));11141115//go through all indices1116for (int i = 0; i < gen_index_count; i += 3) {1117ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 0]], (int)uv_indices.size(), ERR_BUG);1118ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 1]], (int)uv_indices.size(), ERR_BUG);1119ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 2]], (int)uv_indices.size(), ERR_BUG);11201121ERR_FAIL_COND_V(uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 1]]].first || uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 2]]].first, ERR_BUG);11221123int surface = uv_indices[gen_vertices[gen_indices[i + 0]]].first;11241125for (int j = 0; j < 3; j++) {1126SurfaceTool::Vertex v = lightmap_surfaces[surface].vertices[uv_indices[gen_vertices[gen_indices[i + j]]].second];11271128if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_COLOR) {1129surfaces_tools[surface]->set_color(v.color);1130}1131if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_TEX_UV) {1132surfaces_tools[surface]->set_uv(v.uv);1133}1134if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_NORMAL) {1135surfaces_tools[surface]->set_normal(v.normal);1136}1137if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_TANGENT) {1138Plane t;1139t.normal = v.tangent;1140t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1;1141surfaces_tools[surface]->set_tangent(t);1142}1143if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_BONES) {1144surfaces_tools[surface]->set_bones(v.bones);1145}1146if (lightmap_surfaces[surface].format & Mesh::ARRAY_FORMAT_WEIGHTS) {1147surfaces_tools[surface]->set_weights(v.weights);1148}1149for (int custom_i = 0; custom_i < RS::ARRAY_CUSTOM_COUNT; custom_i++) {1150if ((lightmap_surfaces[surface].format >> custom_shift[custom_i]) & RS::ARRAY_FORMAT_CUSTOM_MASK) {1151surfaces_tools[surface]->set_custom(custom_i, v.custom[custom_i]);1152}1153}11541155Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);1156surfaces_tools[surface]->set_uv2(uv2);11571158surfaces_tools[surface]->add_vertex(v.vertex);1159}1160}11611162//generate surfaces1163for (int i = 0; i < lightmap_surfaces.size(); i++) {1164Ref<SurfaceTool> &tool = surfaces_tools[i];1165tool->index();1166Array arrays = tool->commit_to_arrays();11671168uint64_t format = lightmap_surfaces[i].format;1169if (tool->get_skin_weight_count() == SurfaceTool::SKIN_8_WEIGHTS) {1170format |= RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS;1171} else {1172format &= ~RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS;1173}11741175add_surface(tool->get_primitive_type(), arrays, Array(), Dictionary(), tool->get_material(), tool->get_meta("name"), format);1176}11771178set_lightmap_size_hint(Size2(size_x, size_y));11791180if (gen_cache_size > 0) {1181r_dst_cache.resize(gen_cache_size);1182memcpy(r_dst_cache.ptrw(), gen_cache, gen_cache_size);1183memfree(gen_cache);1184}11851186if (!use_cache) {1187// Cache was not used, free the buffers1188memfree(gen_vertices);1189memfree(gen_indices);1190memfree(gen_uvs);1191}11921193return OK;1194}11951196void ImporterMesh::set_lightmap_size_hint(const Size2i &p_size) {1197lightmap_size_hint = p_size;1198}11991200Size2i ImporterMesh::get_lightmap_size_hint() const {1201return lightmap_size_hint;1202}12031204void ImporterMesh::_bind_methods() {1205ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &ImporterMesh::add_blend_shape);1206ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &ImporterMesh::get_blend_shape_count);1207ClassDB::bind_method(D_METHOD("get_blend_shape_name", "blend_shape_idx"), &ImporterMesh::get_blend_shape_name);12081209ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &ImporterMesh::set_blend_shape_mode);1210ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &ImporterMesh::get_blend_shape_mode);12111212ClassDB::bind_method(D_METHOD("add_surface", "primitive", "arrays", "blend_shapes", "lods", "material", "name", "flags"), &ImporterMesh::add_surface, DEFVAL(TypedArray<Array>()), DEFVAL(Dictionary()), DEFVAL(Ref<Material>()), DEFVAL(String()), DEFVAL(0));12131214ClassDB::bind_method(D_METHOD("get_surface_count"), &ImporterMesh::get_surface_count);1215ClassDB::bind_method(D_METHOD("get_surface_primitive_type", "surface_idx"), &ImporterMesh::get_surface_primitive_type);1216ClassDB::bind_method(D_METHOD("get_surface_name", "surface_idx"), &ImporterMesh::get_surface_name);1217ClassDB::bind_method(D_METHOD("get_surface_arrays", "surface_idx"), &ImporterMesh::get_surface_arrays);1218ClassDB::bind_method(D_METHOD("get_surface_blend_shape_arrays", "surface_idx", "blend_shape_idx"), &ImporterMesh::get_surface_blend_shape_arrays);1219ClassDB::bind_method(D_METHOD("get_surface_lod_count", "surface_idx"), &ImporterMesh::get_surface_lod_count);1220ClassDB::bind_method(D_METHOD("get_surface_lod_size", "surface_idx", "lod_idx"), &ImporterMesh::get_surface_lod_size);1221ClassDB::bind_method(D_METHOD("get_surface_lod_indices", "surface_idx", "lod_idx"), &ImporterMesh::get_surface_lod_indices);1222ClassDB::bind_method(D_METHOD("get_surface_material", "surface_idx"), &ImporterMesh::get_surface_material);1223ClassDB::bind_method(D_METHOD("get_surface_format", "surface_idx"), &ImporterMesh::get_surface_format);12241225ClassDB::bind_method(D_METHOD("set_surface_name", "surface_idx", "name"), &ImporterMesh::set_surface_name);1226ClassDB::bind_method(D_METHOD("set_surface_material", "surface_idx", "material"), &ImporterMesh::set_surface_material);12271228ClassDB::bind_method(D_METHOD("generate_lods", "normal_merge_angle", "normal_split_angle", "bone_transform_array"), &ImporterMesh::_generate_lods_bind);1229ClassDB::bind_method(D_METHOD("get_mesh", "base_mesh"), &ImporterMesh::get_mesh, DEFVAL(Ref<ArrayMesh>()));1230ClassDB::bind_method(D_METHOD("clear"), &ImporterMesh::clear);12311232ClassDB::bind_method(D_METHOD("_set_data", "data"), &ImporterMesh::_set_data);1233ClassDB::bind_method(D_METHOD("_get_data"), &ImporterMesh::_get_data);12341235ClassDB::bind_method(D_METHOD("set_lightmap_size_hint", "size"), &ImporterMesh::set_lightmap_size_hint);1236ClassDB::bind_method(D_METHOD("get_lightmap_size_hint"), &ImporterMesh::get_lightmap_size_hint);12371238ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");1239}124012411242