Path: blob/master/editor/import/3d/resource_importer_obj.cpp
20806 views
/**************************************************************************/1/* resource_importer_obj.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 "resource_importer_obj.h"3132#include "core/io/file_access.h"33#include "core/io/resource_saver.h"34#include "scene/3d/importer_mesh_instance_3d.h"35#include "scene/3d/node_3d.h"36#include "scene/resources/3d/importer_mesh.h"37#include "scene/resources/mesh.h"38#include "scene/resources/surface_tool.h"3940static Error _parse_material_library(const String &p_path, HashMap<String, Ref<StandardMaterial3D>> &material_map, List<String> *r_missing_deps) {41Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);42ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Couldn't open MTL file '%s', it may not exist or not be readable.", p_path));4344Ref<StandardMaterial3D> current;45String current_name;46String base_path = p_path.get_base_dir();47while (true) {48String l = f->get_line().strip_edges();4950if (l.begins_with("newmtl ")) {51// Start of a new material.52current_name = l.replace("newmtl", "").strip_edges();53current.instantiate();54current->set_name(current_name);55material_map[current_name] = current;56} else if (l.begins_with("Ka ")) {57// Ambient color.58WARN_PRINT("OBJ: Ambient light for material '" + current_name + "' is ignored in PBR");5960} else if (l.begins_with("Kd ")) {61// Diffuse color.62ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);63Vector<String> v = l.split(" ", false);64ERR_FAIL_COND_V(v.size() < 4, ERR_INVALID_DATA);65Color c = current->get_albedo();66c.r = v[1].to_float();67c.g = v[2].to_float();68c.b = v[3].to_float();69current->set_albedo(c);70} else if (l.begins_with("Ks ")) {71// Specular color.72ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);73Vector<String> v = l.split(" ", false);74ERR_FAIL_COND_V(v.size() < 4, ERR_INVALID_DATA);75float r = v[1].to_float();76float g = v[2].to_float();77float b = v[3].to_float();78float metalness = MAX(r, MAX(g, b));79current->set_metallic(metalness);80} else if (l.begins_with("Ns ")) {81// Specular exponent.82ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);83Vector<String> v = l.split(" ", false);84ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);85float s = v[1].to_float();86current->set_metallic((1000.0 - s) / 1000.0);87} else if (l.begins_with("d ")) {88// Dissolve (1.0 is fully opaque, 0.0 is completely transparent).89ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);90Vector<String> v = l.split(" ", false);91ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);92float d = v[1].to_float();93Color c = current->get_albedo();94c.a = d;95current->set_albedo(c);96if (c.a < 0.99) {97current->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);98}99} else if (l.begins_with("Tr ")) {100// Transparency (1.0 is completely transparent, 0.0 is fully opaque).101ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);102Vector<String> v = l.split(" ", false);103ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);104float d = v[1].to_float();105Color c = current->get_albedo();106c.a = 1.0 - d;107current->set_albedo(c);108if (c.a < 0.99) {109current->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);110}111112} else if (l.begins_with("map_Ka ")) {113// Ambient texture map.114WARN_PRINT("OBJ: Ambient light texture for material '" + current_name + "' is ignored in PBR");115116} else if (l.begins_with("map_Kd ")) {117// Diffuse texture map.118ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);119120String p = l.replace("map_Kd", "").replace_char('\\', '/').strip_edges();121String path;122if (p.is_absolute_path()) {123path = p;124} else {125path = base_path.path_join(p);126}127128Ref<Texture2D> texture = ResourceLoader::load(path);129130if (texture.is_valid()) {131current->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, texture);132} else if (r_missing_deps) {133r_missing_deps->push_back(path);134}135136} else if (l.begins_with("map_Ks ")) {137// Specular color texture map.138ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);139140String p = l.replace("map_Ks", "").replace_char('\\', '/').strip_edges();141String path;142if (p.is_absolute_path()) {143path = p;144} else {145path = base_path.path_join(p);146}147148Ref<Texture2D> texture = ResourceLoader::load(path);149150if (texture.is_valid()) {151current->set_texture(StandardMaterial3D::TEXTURE_METALLIC, texture);152} else if (r_missing_deps) {153r_missing_deps->push_back(path);154}155156} else if (l.begins_with("map_Ns ")) {157// Specular exponent texture map.158ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);159160String p = l.replace("map_Ns", "").replace_char('\\', '/').strip_edges();161String path;162if (p.is_absolute_path()) {163path = p;164} else {165path = base_path.path_join(p);166}167168Ref<Texture2D> texture = ResourceLoader::load(path);169170if (texture.is_valid()) {171current->set_texture(StandardMaterial3D::TEXTURE_ROUGHNESS, texture);172} else if (r_missing_deps) {173r_missing_deps->push_back(path);174}175} else if (l.begins_with("map_bump ") || l.begins_with("map_Bump ")) {176// Bump texture map.177ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);178179l = l.begins_with("map_bump ") ? l.trim_prefix("map_bump ") : l.trim_prefix("map_Bump ");180l = l.strip_edges();181182// Read path and optional bump multiplier.183String p;184float bm = 1.0;185int bm_pos = l.find("-bm ");186if (bm_pos >= 0) {187int bm_start = bm_pos + 4;188int bm_end = l.find_char(' ', bm_start);189if (bm_end >= 0) {190bm = l.substr(bm_start, bm_end - bm_start).to_float();191p = l.substr(bm_end + 1);192} else { // Bump multiplier ends at end of line.193bm = l.substr(bm_start).to_float();194p = l.substr(0, bm_pos);195}196} else {197p = l;198}199200String path = base_path.path_join(p.replace_char('\\', '/').strip_edges());201202Ref<Texture2D> texture = ResourceLoader::load(path);203204if (texture.is_valid()) {205current->set_feature(StandardMaterial3D::FEATURE_NORMAL_MAPPING, true);206current->set_texture(StandardMaterial3D::TEXTURE_NORMAL, texture);207current->set_normal_scale(bm);208} else if (r_missing_deps) {209r_missing_deps->push_back(path);210}211} else if (f->eof_reached()) {212break;213}214}215216return OK;217}218219static Error _parse_obj(const String &p_path, List<Ref<ImporterMesh>> &r_meshes, bool p_single_mesh, bool p_generate_tangents, bool p_generate_lods, bool p_generate_shadow_mesh, bool p_generate_lightmap_uv2, float p_generate_lightmap_uv2_texel_size, const PackedByteArray &p_src_lightmap_cache, Vector3 p_scale_mesh, Vector3 p_offset_mesh, bool p_disable_compression, Vector<Vector<uint8_t>> &r_lightmap_caches, List<String> *r_missing_deps) {220Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);221ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Couldn't open OBJ file '%s', it may not exist or not be readable.", p_path));222223// Avoid trying to load/interpret potential build artifacts from Visual Studio (e.g. when compiling native plugins inside the project tree).224// This should only match if it's indeed a COFF file header.225// https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types226const int first_bytes = f->get_16();227static const Vector<int> coff_header_machines{2280x0, // IMAGE_FILE_MACHINE_UNKNOWN2290x8664, // IMAGE_FILE_MACHINE_AMD642300x1c0, // IMAGE_FILE_MACHINE_ARM2310x14c, // IMAGE_FILE_MACHINE_I3862320x200, // IMAGE_FILE_MACHINE_IA64233};234ERR_FAIL_COND_V_MSG(coff_header_machines.has(first_bytes), ERR_FILE_CORRUPT, vformat("Couldn't read OBJ file '%s', it seems to be binary, corrupted, or empty.", p_path));235f->seek(0);236237Ref<ImporterMesh> mesh;238mesh.instantiate();239240bool generate_tangents = p_generate_tangents;241Vector3 scale_mesh = p_scale_mesh;242Vector3 offset_mesh = p_offset_mesh;243244Vector<Vector3> vertices;245Vector<Vector3> normals;246Vector<Vector2> uvs;247Vector<Color> colors;248const String default_name = "Mesh";249String name = default_name;250251HashMap<String, HashMap<String, Ref<StandardMaterial3D>>> material_map;252253Ref<SurfaceTool> surf_tool = memnew(SurfaceTool);254surf_tool->begin(Mesh::PRIMITIVE_TRIANGLES);255256String current_material_library;257String current_material;258String current_group;259uint32_t smooth_group = 0;260bool smoothing = true;261const uint32_t no_smoothing_smooth_group = (uint32_t)-1;262263bool uses_uvs = false;264265while (true) {266String l = f->get_line().strip_edges();267while (l.length() && l[l.length() - 1] == '\\') {268String add = f->get_line().strip_edges();269l += add;270if (add.is_empty()) {271break;272}273}274275if (l.begins_with("v ")) {276//vertex277Vector<String> v = l.split(" ", false);278ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);279Vector3 vtx;280vtx.x = v[1].to_float() * scale_mesh.x + offset_mesh.x;281vtx.y = v[2].to_float() * scale_mesh.y + offset_mesh.y;282vtx.z = v[3].to_float() * scale_mesh.z + offset_mesh.z;283vertices.push_back(vtx);284//vertex color285if (v.size() >= 7) {286while (colors.size() < vertices.size() - 1) {287colors.push_back(Color(1.0, 1.0, 1.0));288}289Color c;290c.r = v[4].to_float();291c.g = v[5].to_float();292c.b = v[6].to_float();293colors.push_back(c);294} else if (!colors.is_empty()) {295colors.push_back(Color(1.0, 1.0, 1.0));296}297} else if (l.begins_with("vt ")) {298//uv299Vector<String> v = l.split(" ", false);300ERR_FAIL_COND_V(v.size() < 3, ERR_FILE_CORRUPT);301Vector2 uv;302uv.x = v[1].to_float();303uv.y = 1.0 - v[2].to_float();304uvs.push_back(uv);305} else if (l.begins_with("vn ")) {306//normal307Vector<String> v = l.split(" ", false);308ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);309Vector3 nrm;310nrm.x = v[1].to_float();311nrm.y = v[2].to_float();312nrm.z = v[3].to_float();313normals.push_back(nrm);314} else if (l.begins_with("f ")) {315//vertex316317Vector<String> v = l.split(" ", false);318ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);319320//not very fast, could be sped up321322Vector<String> face[3];323face[0] = v[1].split("/");324face[1] = v[2].split("/");325ERR_FAIL_COND_V(face[0].is_empty(), ERR_FILE_CORRUPT);326327ERR_FAIL_COND_V(face[0].size() != face[1].size(), ERR_FILE_CORRUPT);328for (int i = 2; i < v.size() - 1; i++) {329face[2] = v[i + 1].split("/");330331ERR_FAIL_COND_V(face[0].size() != face[2].size(), ERR_FILE_CORRUPT);332for (int j = 0; j < 3; j++) {333int idx = j;334335if (idx < 2) {336idx = 1 ^ idx;337}338339// Check UVs before faces as we may need to generate dummy tangents if there are no UVs.340if (face[idx].size() >= 2 && !face[idx][1].is_empty()) {341int uv = face[idx][1].to_int() - 1;342if (uv < 0) {343uv += uvs.size() + 1;344}345ERR_FAIL_INDEX_V(uv, uvs.size(), ERR_FILE_CORRUPT);346surf_tool->set_uv(uvs[uv]);347uses_uvs = true;348}349350if (face[idx].size() == 3) {351int norm = face[idx][2].to_int() - 1;352if (norm < 0) {353norm += normals.size() + 1;354}355ERR_FAIL_INDEX_V(norm, normals.size(), ERR_FILE_CORRUPT);356surf_tool->set_normal(normals[norm]);357if (generate_tangents && !uses_uvs) {358// We can't generate tangents without UVs, so create dummy tangents.359Vector3 tan = Vector3(normals[norm].z, -normals[norm].x, normals[norm].y).cross(normals[norm].normalized()).normalized();360surf_tool->set_tangent(Plane(tan.x, tan.y, tan.z, 1.0));361}362} else {363// No normals, use a dummy tangent since normals and tangents will be generated.364if (generate_tangents && !uses_uvs) {365// We can't generate tangents without UVs, so create dummy tangents.366surf_tool->set_tangent(Plane(1.0, 0.0, 0.0, 1.0));367}368}369370int vtx = face[idx][0].to_int() - 1;371if (vtx < 0) {372vtx += vertices.size() + 1;373}374ERR_FAIL_INDEX_V(vtx, vertices.size(), ERR_FILE_CORRUPT);375376Vector3 vertex = vertices[vtx];377if (!colors.is_empty()) {378surf_tool->set_color(colors[vtx]);379}380surf_tool->set_smooth_group(smoothing ? smooth_group : no_smoothing_smooth_group);381surf_tool->add_vertex(vertex);382}383384face[1] = face[2];385}386} else if (l.begins_with("s ")) { //smoothing387String what = l.substr(2).strip_edges();388bool do_smooth;389if (what == "off") {390do_smooth = false;391} else {392do_smooth = true;393}394if (do_smooth != smoothing) {395smoothing = do_smooth;396if (smoothing) {397smooth_group++;398}399}400} else if (/*l.begins_with("g ") ||*/ l.begins_with("usemtl ") || (l.begins_with("o ") || f->eof_reached())) { //commit group to mesh401uint64_t mesh_flags = RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES;402403if (p_disable_compression) {404mesh_flags = 0;405} else {406bool is_mesh_2d = true;407408// Disable compression if all z equals 0 (the mesh is 2D).409for (int i = 0; i < vertices.size(); i++) {410if (!Math::is_zero_approx(vertices[i].z)) {411is_mesh_2d = false;412break;413}414}415416if (is_mesh_2d) {417mesh_flags = 0;418}419}420421//groups are too annoying422if (surf_tool->get_vertex_array().size()) {423//another group going on, commit it424if (normals.is_empty()) {425surf_tool->generate_normals();426}427428if (generate_tangents && uses_uvs) {429surf_tool->generate_tangents();430}431432surf_tool->index();433434print_verbose("OBJ: Current material library " + current_material_library + " has " + itos(material_map.has(current_material_library)));435print_verbose("OBJ: Current material " + current_material + " has " + itos(material_map.has(current_material_library) && material_map[current_material_library].has(current_material)));436Ref<StandardMaterial3D> material;437if (material_map.has(current_material_library) && material_map[current_material_library].has(current_material)) {438material = material_map[current_material_library][current_material];439if (!colors.is_empty()) {440material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);441}442surf_tool->set_material(material);443}444445Array array = surf_tool->commit_to_arrays();446447if (mesh_flags & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES && generate_tangents && uses_uvs) {448// Compression is enabled, so let's validate that the normals and generated tangents are correct.449Vector<Vector3> norms = array[Mesh::ARRAY_NORMAL];450Vector<float> tangents = array[Mesh::ARRAY_TANGENT];451ERR_FAIL_COND_V(tangents.is_empty(), ERR_FILE_CORRUPT);452for (int vert = 0; vert < norms.size(); vert++) {453Vector3 tan = Vector3(tangents[vert * 4 + 0], tangents[vert * 4 + 1], tangents[vert * 4 + 2]);454if (std::abs(tan.dot(norms[vert])) > 0.0001) {455// Tangent is not perpendicular to the normal, so we can't use compression.456mesh_flags &= ~RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES;457}458}459}460461mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, array, TypedArray<Array>(), Dictionary(), material, name, mesh_flags);462463print_verbose("OBJ: Added surface :" + mesh->get_surface_name(mesh->get_surface_count() - 1));464465if (!current_material.is_empty()) {466if (mesh->get_surface_count() >= 1) {467mesh->set_surface_name(mesh->get_surface_count() - 1, current_material.get_basename());468}469} else if (!current_group.is_empty()) {470if (mesh->get_surface_count() >= 1) {471mesh->set_surface_name(mesh->get_surface_count() - 1, current_group);472}473}474475surf_tool->clear();476surf_tool->begin(Mesh::PRIMITIVE_TRIANGLES);477uses_uvs = false;478}479480if (l.begins_with("o ") || f->eof_reached()) {481if (!p_single_mesh) {482if (mesh->get_surface_count() > 0) {483mesh->set_name(name);484r_meshes.push_back(mesh);485mesh.instantiate();486}487name = default_name;488current_group = "";489current_material = "";490}491}492493if (f->eof_reached()) {494break;495}496497if (l.begins_with("o ")) {498name = l.substr(2).strip_edges();499}500501if (l.begins_with("usemtl ")) {502current_material = l.replace("usemtl", "").strip_edges();503}504505if (l.begins_with("g ")) {506current_group = l.substr(2).strip_edges();507}508509} else if (l.begins_with("mtllib ")) { //parse material510511current_material_library = l.replace("mtllib", "").strip_edges();512if (!material_map.has(current_material_library)) {513HashMap<String, Ref<StandardMaterial3D>> lib;514String lib_path = current_material_library;515if (lib_path.is_relative_path()) {516lib_path = p_path.get_base_dir().path_join(current_material_library);517}518Error err = _parse_material_library(lib_path, lib, r_missing_deps);519if (err == OK) {520material_map[current_material_library] = lib;521}522}523}524}525526if (p_generate_lightmap_uv2) {527Vector<uint8_t> lightmap_cache;528mesh->lightmap_unwrap_cached(Transform3D(), p_generate_lightmap_uv2_texel_size, p_src_lightmap_cache, lightmap_cache);529530if (!lightmap_cache.is_empty()) {531if (r_lightmap_caches.is_empty()) {532r_lightmap_caches.push_back(lightmap_cache);533} else {534// MD5 is stored at the beginning of the cache data.535const String new_md5 = String::md5(lightmap_cache.ptr());536537for (int i = 0; i < r_lightmap_caches.size(); i++) {538const String md5 = String::md5(r_lightmap_caches[i].ptr());539if (new_md5 < md5) {540r_lightmap_caches.insert(i, lightmap_cache);541break;542}543544if (new_md5 == md5) {545break;546}547}548}549}550}551552if (p_generate_lods) {553// Use normal merge/split angles that match the defaults used for 3D scene importing.554mesh->generate_lods(60.0f, {});555}556557if (p_generate_shadow_mesh) {558mesh->create_shadow_mesh();559}560561mesh->optimize_indices();562563if (p_single_mesh && mesh->get_surface_count() > 0) {564r_meshes.push_back(mesh);565}566567return OK;568}569570Node *EditorOBJImporter::import_scene(const String &p_path, uint32_t p_flags, const HashMap<StringName, Variant> &p_options, List<String> *r_missing_deps, Error *r_err) {571List<Ref<ImporterMesh>> meshes;572573// LOD, shadow mesh and lightmap UV2 generation are handled by ResourceImporterScene in this case,574// so disable it within the OBJ mesh import.575Vector<Vector<uint8_t>> mesh_lightmap_caches;576Error err = _parse_obj(p_path, meshes, false, p_flags & IMPORT_GENERATE_TANGENT_ARRAYS, false, false, false, 0.2, PackedByteArray(), Vector3(1, 1, 1), Vector3(0, 0, 0), p_flags & IMPORT_FORCE_DISABLE_MESH_COMPRESSION, mesh_lightmap_caches, r_missing_deps);577578if (err != OK) {579if (r_err) {580*r_err = err;581}582return nullptr;583}584585Node3D *scene = memnew(Node3D);586587for (Ref<ImporterMesh> m : meshes) {588ImporterMeshInstance3D *mi = memnew(ImporterMeshInstance3D);589mi->set_mesh(m);590mi->set_name(m->get_name());591scene->add_child(mi, true);592mi->set_owner(scene);593}594595if (r_err) {596*r_err = OK;597}598599return scene;600}601602void EditorOBJImporter::get_extensions(List<String> *r_extensions) const {603r_extensions->push_back("obj");604}605606////////////////////////////////////////////////////607608String ResourceImporterOBJ::get_importer_name() const {609return "wavefront_obj";610}611612String ResourceImporterOBJ::get_visible_name() const {613return "OBJ as Mesh";614}615616void ResourceImporterOBJ::get_recognized_extensions(List<String> *p_extensions) const {617p_extensions->push_back("obj");618}619620String ResourceImporterOBJ::get_save_extension() const {621return "mesh";622}623624String ResourceImporterOBJ::get_resource_type() const {625return "Mesh";626}627628int ResourceImporterOBJ::get_format_version() const {629return 1;630}631632int ResourceImporterOBJ::get_preset_count() const {633return 0;634}635636String ResourceImporterOBJ::get_preset_name(int p_idx) const {637return "";638}639640void ResourceImporterOBJ::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {641r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_tangents"), true));642r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_lods"), true));643r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_shadow_mesh"), true));644r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_lightmap_uv2", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));645r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "generate_lightmap_uv2_texel_size", PROPERTY_HINT_RANGE, "0.001,100,0.001"), 0.2));646r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "scale_mesh"), Vector3(1, 1, 1)));647r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "offset_mesh"), Vector3(0, 0, 0)));648r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "force_disable_mesh_compression"), false));649}650651bool ResourceImporterOBJ::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {652if (p_option == "generate_lightmap_uv2_texel_size" && !p_options["generate_lightmap_uv2"]) {653// Only display the lightmap texel size import option when lightmap UV2 generation is enabled.654return false;655}656657return true;658}659660Error ResourceImporterOBJ::import(ResourceUID::ID p_source_id, const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {661List<Ref<ImporterMesh>> meshes;662663Vector<uint8_t> src_lightmap_cache;664Vector<Vector<uint8_t>> mesh_lightmap_caches;665666Error err;667{668src_lightmap_cache = FileAccess::get_file_as_bytes(p_source_file + ".unwrap_cache", &err);669if (err != OK) {670src_lightmap_cache.clear();671}672}673674err = _parse_obj(p_source_file, meshes, true, p_options["generate_tangents"], p_options["generate_lods"], p_options["generate_shadow_mesh"], p_options["generate_lightmap_uv2"], p_options["generate_lightmap_uv2_texel_size"], src_lightmap_cache, p_options["scale_mesh"], p_options["offset_mesh"], p_options["force_disable_mesh_compression"], mesh_lightmap_caches, nullptr);675676if (mesh_lightmap_caches.size()) {677Ref<FileAccess> f = FileAccess::open(p_source_file + ".unwrap_cache", FileAccess::WRITE);678if (f.is_valid()) {679f->store_32(mesh_lightmap_caches.size());680for (int i = 0; i < mesh_lightmap_caches.size(); i++) {681String md5 = String::md5(mesh_lightmap_caches[i].ptr());682f->store_buffer(mesh_lightmap_caches[i].ptr(), mesh_lightmap_caches[i].size());683}684}685}686err = OK;687688ERR_FAIL_COND_V(err != OK, err);689ERR_FAIL_COND_V(meshes.size() != 1, ERR_BUG);690691String save_path = p_save_path + ".mesh";692693err = ResourceSaver::save(meshes.front()->get()->get_mesh(), save_path);694695ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save Mesh to file '" + save_path + "'.");696697r_gen_files->push_back(save_path);698699return OK;700}701702703