resource_importer_obj.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /**************************************************************************/
  2. /* resource_importer_obj.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "resource_importer_obj.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/resource_saver.h"
  33. #include "scene/3d/importer_mesh_instance_3d.h"
  34. #include "scene/3d/mesh_instance_3d.h"
  35. #include "scene/3d/node_3d.h"
  36. #include "scene/resources/importer_mesh.h"
  37. #include "scene/resources/mesh.h"
  38. #include "scene/resources/surface_tool.h"
  39. uint32_t EditorOBJImporter::get_import_flags() const {
  40. return IMPORT_SCENE;
  41. }
  42. static Error _parse_material_library(const String &p_path, HashMap<String, Ref<StandardMaterial3D>> &material_map, List<String> *r_missing_deps) {
  43. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  44. ERR_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));
  45. Ref<StandardMaterial3D> current;
  46. String current_name;
  47. String base_path = p_path.get_base_dir();
  48. while (true) {
  49. String l = f->get_line().strip_edges();
  50. if (l.begins_with("newmtl ")) {
  51. //vertex
  52. current_name = l.replace("newmtl", "").strip_edges();
  53. current.instantiate();
  54. current->set_name(current_name);
  55. material_map[current_name] = current;
  56. } else if (l.begins_with("Ka ")) {
  57. //uv
  58. WARN_PRINT("OBJ: Ambient light for material '" + current_name + "' is ignored in PBR");
  59. } else if (l.begins_with("Kd ")) {
  60. //normal
  61. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  62. Vector<String> v = l.split(" ", false);
  63. ERR_FAIL_COND_V(v.size() < 4, ERR_INVALID_DATA);
  64. Color c = current->get_albedo();
  65. c.r = v[1].to_float();
  66. c.g = v[2].to_float();
  67. c.b = v[3].to_float();
  68. current->set_albedo(c);
  69. } else if (l.begins_with("Ks ")) {
  70. //normal
  71. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  72. Vector<String> v = l.split(" ", false);
  73. ERR_FAIL_COND_V(v.size() < 4, ERR_INVALID_DATA);
  74. float r = v[1].to_float();
  75. float g = v[2].to_float();
  76. float b = v[3].to_float();
  77. float metalness = MAX(r, MAX(g, b));
  78. current->set_metallic(metalness);
  79. } else if (l.begins_with("Ns ")) {
  80. //normal
  81. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  82. Vector<String> v = l.split(" ", false);
  83. ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);
  84. float s = v[1].to_float();
  85. current->set_metallic((1000.0 - s) / 1000.0);
  86. } else if (l.begins_with("d ")) {
  87. //normal
  88. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  89. Vector<String> v = l.split(" ", false);
  90. ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);
  91. float d = v[1].to_float();
  92. Color c = current->get_albedo();
  93. c.a = d;
  94. current->set_albedo(c);
  95. if (c.a < 0.99) {
  96. current->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  97. }
  98. } else if (l.begins_with("Tr ")) {
  99. //normal
  100. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  101. Vector<String> v = l.split(" ", false);
  102. ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);
  103. float d = v[1].to_float();
  104. Color c = current->get_albedo();
  105. c.a = 1.0 - d;
  106. current->set_albedo(c);
  107. if (c.a < 0.99) {
  108. current->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  109. }
  110. } else if (l.begins_with("map_Ka ")) {
  111. //uv
  112. WARN_PRINT("OBJ: Ambient light texture for material '" + current_name + "' is ignored in PBR");
  113. } else if (l.begins_with("map_Kd ")) {
  114. //normal
  115. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  116. String p = l.replace("map_Kd", "").replace("\\", "/").strip_edges();
  117. String path;
  118. if (p.is_absolute_path()) {
  119. path = p;
  120. } else {
  121. path = base_path.path_join(p);
  122. }
  123. Ref<Texture2D> texture = ResourceLoader::load(path);
  124. if (texture.is_valid()) {
  125. current->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, texture);
  126. } else if (r_missing_deps) {
  127. r_missing_deps->push_back(path);
  128. }
  129. } else if (l.begins_with("map_Ks ")) {
  130. //normal
  131. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  132. String p = l.replace("map_Ks", "").replace("\\", "/").strip_edges();
  133. String path;
  134. if (p.is_absolute_path()) {
  135. path = p;
  136. } else {
  137. path = base_path.path_join(p);
  138. }
  139. Ref<Texture2D> texture = ResourceLoader::load(path);
  140. if (texture.is_valid()) {
  141. current->set_texture(StandardMaterial3D::TEXTURE_METALLIC, texture);
  142. } else if (r_missing_deps) {
  143. r_missing_deps->push_back(path);
  144. }
  145. } else if (l.begins_with("map_Ns ")) {
  146. //normal
  147. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  148. String p = l.replace("map_Ns", "").replace("\\", "/").strip_edges();
  149. String path;
  150. if (p.is_absolute_path()) {
  151. path = p;
  152. } else {
  153. path = base_path.path_join(p);
  154. }
  155. Ref<Texture2D> texture = ResourceLoader::load(path);
  156. if (texture.is_valid()) {
  157. current->set_texture(StandardMaterial3D::TEXTURE_ROUGHNESS, texture);
  158. } else if (r_missing_deps) {
  159. r_missing_deps->push_back(path);
  160. }
  161. } else if (l.begins_with("map_bump ")) {
  162. //normal
  163. ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
  164. String p = l.replace("map_bump", "").replace("\\", "/").strip_edges();
  165. String path = base_path.path_join(p);
  166. Ref<Texture2D> texture = ResourceLoader::load(path);
  167. if (texture.is_valid()) {
  168. current->set_feature(StandardMaterial3D::FEATURE_NORMAL_MAPPING, true);
  169. current->set_texture(StandardMaterial3D::TEXTURE_NORMAL, texture);
  170. } else if (r_missing_deps) {
  171. r_missing_deps->push_back(path);
  172. }
  173. } else if (f->eof_reached()) {
  174. break;
  175. }
  176. }
  177. return OK;
  178. }
  179. static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_single_mesh, bool p_generate_tangents, bool p_optimize, Vector3 p_scale_mesh, Vector3 p_offset_mesh, bool p_disable_compression, List<String> *r_missing_deps) {
  180. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  181. ERR_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));
  182. // Avoid trying to load/interpret potential build artifacts from Visual Studio (e.g. when compiling native plugins inside the project tree)
  183. // This should only match, if it's indeed a COFF file header
  184. // https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types
  185. const int first_bytes = f->get_16();
  186. static const Vector<int> coff_header_machines{
  187. 0x0, // IMAGE_FILE_MACHINE_UNKNOWN
  188. 0x8664, // IMAGE_FILE_MACHINE_AMD64
  189. 0x1c0, // IMAGE_FILE_MACHINE_ARM
  190. 0x14c, // IMAGE_FILE_MACHINE_I386
  191. 0x200, // IMAGE_FILE_MACHINE_IA64
  192. };
  193. ERR_FAIL_COND_V_MSG(coff_header_machines.find(first_bytes) != -1, ERR_FILE_CORRUPT, vformat("Couldn't read OBJ file '%s', it seems to be binary, corrupted, or empty.", p_path));
  194. f->seek(0);
  195. Ref<ArrayMesh> mesh;
  196. mesh.instantiate();
  197. bool generate_tangents = p_generate_tangents;
  198. Vector3 scale_mesh = p_scale_mesh;
  199. Vector3 offset_mesh = p_offset_mesh;
  200. uint64_t mesh_flags = RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES;
  201. if (p_disable_compression) {
  202. mesh_flags = 0;
  203. }
  204. Vector<Vector3> vertices;
  205. Vector<Vector3> normals;
  206. Vector<Vector2> uvs;
  207. Vector<Color> colors;
  208. const String default_name = "Mesh";
  209. String name = default_name;
  210. HashMap<String, HashMap<String, Ref<StandardMaterial3D>>> material_map;
  211. Ref<SurfaceTool> surf_tool = memnew(SurfaceTool);
  212. surf_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
  213. String current_material_library;
  214. String current_material;
  215. String current_group;
  216. uint32_t smooth_group = 0;
  217. bool smoothing = true;
  218. const uint32_t no_smoothing_smooth_group = (uint32_t)-1;
  219. while (true) {
  220. String l = f->get_line().strip_edges();
  221. while (l.length() && l[l.length() - 1] == '\\') {
  222. String add = f->get_line().strip_edges();
  223. l += add;
  224. if (add.is_empty()) {
  225. break;
  226. }
  227. }
  228. if (l.begins_with("v ")) {
  229. //vertex
  230. Vector<String> v = l.split(" ", false);
  231. ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);
  232. Vector3 vtx;
  233. vtx.x = v[1].to_float() * scale_mesh.x + offset_mesh.x;
  234. vtx.y = v[2].to_float() * scale_mesh.y + offset_mesh.y;
  235. vtx.z = v[3].to_float() * scale_mesh.z + offset_mesh.z;
  236. vertices.push_back(vtx);
  237. //vertex color
  238. if (v.size() >= 7) {
  239. while (colors.size() < vertices.size() - 1) {
  240. colors.push_back(Color(1.0, 1.0, 1.0));
  241. }
  242. Color c;
  243. c.r = v[4].to_float();
  244. c.g = v[5].to_float();
  245. c.b = v[6].to_float();
  246. colors.push_back(c);
  247. } else if (!colors.is_empty()) {
  248. colors.push_back(Color(1.0, 1.0, 1.0));
  249. }
  250. } else if (l.begins_with("vt ")) {
  251. //uv
  252. Vector<String> v = l.split(" ", false);
  253. ERR_FAIL_COND_V(v.size() < 3, ERR_FILE_CORRUPT);
  254. Vector2 uv;
  255. uv.x = v[1].to_float();
  256. uv.y = 1.0 - v[2].to_float();
  257. uvs.push_back(uv);
  258. } else if (l.begins_with("vn ")) {
  259. //normal
  260. Vector<String> v = l.split(" ", false);
  261. ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);
  262. Vector3 nrm;
  263. nrm.x = v[1].to_float();
  264. nrm.y = v[2].to_float();
  265. nrm.z = v[3].to_float();
  266. normals.push_back(nrm);
  267. } else if (l.begins_with("f ")) {
  268. //vertex
  269. Vector<String> v = l.split(" ", false);
  270. ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);
  271. //not very fast, could be sped up
  272. Vector<String> face[3];
  273. face[0] = v[1].split("/");
  274. face[1] = v[2].split("/");
  275. ERR_FAIL_COND_V(face[0].size() == 0, ERR_FILE_CORRUPT);
  276. ERR_FAIL_COND_V(face[0].size() != face[1].size(), ERR_FILE_CORRUPT);
  277. for (int i = 2; i < v.size() - 1; i++) {
  278. face[2] = v[i + 1].split("/");
  279. ERR_FAIL_COND_V(face[0].size() != face[2].size(), ERR_FILE_CORRUPT);
  280. for (int j = 0; j < 3; j++) {
  281. int idx = j;
  282. if (idx < 2) {
  283. idx = 1 ^ idx;
  284. }
  285. if (face[idx].size() == 3) {
  286. int norm = face[idx][2].to_int() - 1;
  287. if (norm < 0) {
  288. norm += normals.size() + 1;
  289. }
  290. ERR_FAIL_INDEX_V(norm, normals.size(), ERR_FILE_CORRUPT);
  291. surf_tool->set_normal(normals[norm]);
  292. }
  293. if (face[idx].size() >= 2 && !face[idx][1].is_empty()) {
  294. int uv = face[idx][1].to_int() - 1;
  295. if (uv < 0) {
  296. uv += uvs.size() + 1;
  297. }
  298. ERR_FAIL_INDEX_V(uv, uvs.size(), ERR_FILE_CORRUPT);
  299. surf_tool->set_uv(uvs[uv]);
  300. }
  301. int vtx = face[idx][0].to_int() - 1;
  302. if (vtx < 0) {
  303. vtx += vertices.size() + 1;
  304. }
  305. ERR_FAIL_INDEX_V(vtx, vertices.size(), ERR_FILE_CORRUPT);
  306. Vector3 vertex = vertices[vtx];
  307. if (!colors.is_empty()) {
  308. surf_tool->set_color(colors[vtx]);
  309. }
  310. surf_tool->set_smooth_group(smoothing ? smooth_group : no_smoothing_smooth_group);
  311. surf_tool->add_vertex(vertex);
  312. }
  313. face[1] = face[2];
  314. }
  315. } else if (l.begins_with("s ")) { //smoothing
  316. String what = l.substr(2, l.length()).strip_edges();
  317. bool do_smooth;
  318. if (what == "off") {
  319. do_smooth = false;
  320. } else {
  321. do_smooth = true;
  322. }
  323. if (do_smooth != smoothing) {
  324. smoothing = do_smooth;
  325. if (smoothing) {
  326. smooth_group++;
  327. }
  328. }
  329. } else if (/*l.begins_with("g ") ||*/ l.begins_with("usemtl ") || (l.begins_with("o ") || f->eof_reached())) { //commit group to mesh
  330. //groups are too annoying
  331. if (surf_tool->get_vertex_array().size()) {
  332. //another group going on, commit it
  333. if (normals.size() == 0) {
  334. surf_tool->generate_normals();
  335. }
  336. if (generate_tangents && uvs.size()) {
  337. surf_tool->generate_tangents();
  338. } else {
  339. // We need tangents in order to compress vertex data. So disable if tangents aren't generated.
  340. mesh_flags &= ~RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES;
  341. }
  342. surf_tool->index();
  343. print_verbose("OBJ: Current material library " + current_material_library + " has " + itos(material_map.has(current_material_library)));
  344. print_verbose("OBJ: Current material " + current_material + " has " + itos(material_map.has(current_material_library) && material_map[current_material_library].has(current_material)));
  345. if (material_map.has(current_material_library) && material_map[current_material_library].has(current_material)) {
  346. Ref<StandardMaterial3D> &material = material_map[current_material_library][current_material];
  347. if (!colors.is_empty()) {
  348. material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  349. }
  350. surf_tool->set_material(material);
  351. }
  352. mesh = surf_tool->commit(mesh, mesh_flags);
  353. if (!current_material.is_empty()) {
  354. mesh->surface_set_name(mesh->get_surface_count() - 1, current_material.get_basename());
  355. } else if (!current_group.is_empty()) {
  356. mesh->surface_set_name(mesh->get_surface_count() - 1, current_group);
  357. }
  358. print_verbose("OBJ: Added surface :" + mesh->surface_get_name(mesh->get_surface_count() - 1));
  359. surf_tool->clear();
  360. surf_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
  361. }
  362. if (l.begins_with("o ") || f->eof_reached()) {
  363. if (!p_single_mesh) {
  364. if (mesh->get_surface_count() > 0) {
  365. mesh->set_name(name);
  366. r_meshes.push_back(mesh);
  367. mesh.instantiate();
  368. }
  369. name = default_name;
  370. current_group = "";
  371. current_material = "";
  372. }
  373. }
  374. if (f->eof_reached()) {
  375. break;
  376. }
  377. if (l.begins_with("o ")) {
  378. name = l.substr(2, l.length()).strip_edges();
  379. }
  380. if (l.begins_with("usemtl ")) {
  381. current_material = l.replace("usemtl", "").strip_edges();
  382. }
  383. if (l.begins_with("g ")) {
  384. current_group = l.substr(2, l.length()).strip_edges();
  385. }
  386. } else if (l.begins_with("mtllib ")) { //parse material
  387. current_material_library = l.replace("mtllib", "").strip_edges();
  388. if (!material_map.has(current_material_library)) {
  389. HashMap<String, Ref<StandardMaterial3D>> lib;
  390. String lib_path = current_material_library;
  391. if (lib_path.is_relative_path()) {
  392. lib_path = p_path.get_base_dir().path_join(current_material_library);
  393. }
  394. Error err = _parse_material_library(lib_path, lib, r_missing_deps);
  395. if (err == OK) {
  396. material_map[current_material_library] = lib;
  397. }
  398. }
  399. }
  400. }
  401. if (p_single_mesh) {
  402. r_meshes.push_back(mesh);
  403. }
  404. return OK;
  405. }
  406. Node *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) {
  407. List<Ref<Mesh>> meshes;
  408. Error err = _parse_obj(p_path, meshes, false, p_flags & IMPORT_GENERATE_TANGENT_ARRAYS, false, Vector3(1, 1, 1), Vector3(0, 0, 0), p_flags & IMPORT_FORCE_DISABLE_MESH_COMPRESSION, r_missing_deps);
  409. if (err != OK) {
  410. if (r_err) {
  411. *r_err = err;
  412. }
  413. return nullptr;
  414. }
  415. Node3D *scene = memnew(Node3D);
  416. for (const Ref<Mesh> &m : meshes) {
  417. Ref<ImporterMesh> mesh;
  418. mesh.instantiate();
  419. mesh->set_name(m->get_name());
  420. for (int i = 0; i < m->get_surface_count(); i++) {
  421. mesh->add_surface(m->surface_get_primitive_type(i), m->surface_get_arrays(i), Array(), Dictionary(), m->surface_get_material(i));
  422. }
  423. ImporterMeshInstance3D *mi = memnew(ImporterMeshInstance3D);
  424. mi->set_mesh(mesh);
  425. mi->set_name(m->get_name());
  426. scene->add_child(mi, true);
  427. mi->set_owner(scene);
  428. }
  429. if (r_err) {
  430. *r_err = OK;
  431. }
  432. return scene;
  433. }
  434. void EditorOBJImporter::get_extensions(List<String> *r_extensions) const {
  435. r_extensions->push_back("obj");
  436. }
  437. EditorOBJImporter::EditorOBJImporter() {
  438. }
  439. ////////////////////////////////////////////////////
  440. String ResourceImporterOBJ::get_importer_name() const {
  441. return "wavefront_obj";
  442. }
  443. String ResourceImporterOBJ::get_visible_name() const {
  444. return "OBJ As Mesh";
  445. }
  446. void ResourceImporterOBJ::get_recognized_extensions(List<String> *p_extensions) const {
  447. p_extensions->push_back("obj");
  448. }
  449. String ResourceImporterOBJ::get_save_extension() const {
  450. return "mesh";
  451. }
  452. String ResourceImporterOBJ::get_resource_type() const {
  453. return "Mesh";
  454. }
  455. int ResourceImporterOBJ::get_format_version() const {
  456. return 1;
  457. }
  458. int ResourceImporterOBJ::get_preset_count() const {
  459. return 0;
  460. }
  461. String ResourceImporterOBJ::get_preset_name(int p_idx) const {
  462. return "";
  463. }
  464. void ResourceImporterOBJ::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  465. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_tangents"), true));
  466. r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "scale_mesh"), Vector3(1, 1, 1)));
  467. r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "offset_mesh"), Vector3(0, 0, 0)));
  468. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "optimize_mesh"), true));
  469. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "force_disable_mesh_compression"), false));
  470. }
  471. bool ResourceImporterOBJ::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  472. return true;
  473. }
  474. Error ResourceImporterOBJ::import(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) {
  475. List<Ref<Mesh>> meshes;
  476. Error err = _parse_obj(p_source_file, meshes, true, p_options["generate_tangents"], p_options["optimize_mesh"], p_options["scale_mesh"], p_options["offset_mesh"], p_options["force_disable_mesh_compression"], nullptr);
  477. ERR_FAIL_COND_V(err != OK, err);
  478. ERR_FAIL_COND_V(meshes.size() != 1, ERR_BUG);
  479. String save_path = p_save_path + ".mesh";
  480. err = ResourceSaver::save(meshes.front()->get(), save_path);
  481. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save Mesh to file '" + save_path + "'.");
  482. r_gen_files->push_back(save_path);
  483. return OK;
  484. }
  485. ResourceImporterOBJ::ResourceImporterOBJ() {
  486. }