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
31#include "resource_importer_obj.h"
32
33#include "core/io/file_access.h"
34#include "core/io/resource_saver.h"
35#include "scene/3d/importer_mesh_instance_3d.h"
36#include "scene/3d/mesh_instance_3d.h"
37#include "scene/3d/node_3d.h"
38#include "scene/resources/importer_mesh.h"
39#include "scene/resources/mesh.h"
40#include "scene/resources/surface_tool.h"
41
42uint32_t EditorOBJImporter::get_import_flags() const {
43 return IMPORT_SCENE;
44}
45
46static Error _parse_material_library(const String &p_path, HashMap<String, Ref<StandardMaterial3D>> &material_map, List<String> *r_missing_deps) {
47 Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
48 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));
49
50 Ref<StandardMaterial3D> current;
51 String current_name;
52 String base_path = p_path.get_base_dir();
53 while (true) {
54 String l = f->get_line().strip_edges();
55
56 if (l.begins_with("newmtl ")) {
57 //vertex
58
59 current_name = l.replace("newmtl", "").strip_edges();
60 current.instantiate();
61 current->set_name(current_name);
62 material_map[current_name] = current;
63 } else if (l.begins_with("Ka ")) {
64 //uv
65 WARN_PRINT("OBJ: Ambient light for material '" + current_name + "' is ignored in PBR");
66
67 } else if (l.begins_with("Kd ")) {
68 //normal
69 ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
70 Vector<String> v = l.split(" ", false);
71 ERR_FAIL_COND_V(v.size() < 4, ERR_INVALID_DATA);
72 Color c = current->get_albedo();
73 c.r = v[1].to_float();
74 c.g = v[2].to_float();
75 c.b = v[3].to_float();
76 current->set_albedo(c);
77 } else if (l.begins_with("Ks ")) {
78 //normal
79 ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
80 Vector<String> v = l.split(" ", false);
81 ERR_FAIL_COND_V(v.size() < 4, ERR_INVALID_DATA);
82 float r = v[1].to_float();
83 float g = v[2].to_float();
84 float b = v[3].to_float();
85 float metalness = MAX(r, MAX(g, b));
86 current->set_metallic(metalness);
87 } else if (l.begins_with("Ns ")) {
88 //normal
89 ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
90 Vector<String> v = l.split(" ", false);
91 ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);
92 float s = v[1].to_float();
93 current->set_metallic((1000.0 - s) / 1000.0);
94 } else if (l.begins_with("d ")) {
95 //normal
96 ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
97 Vector<String> v = l.split(" ", false);
98 ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);
99 float d = v[1].to_float();
100 Color c = current->get_albedo();
101 c.a = d;
102 current->set_albedo(c);
103 if (c.a < 0.99) {
104 current->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
105 }
106 } else if (l.begins_with("Tr ")) {
107 //normal
108 ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
109 Vector<String> v = l.split(" ", false);
110 ERR_FAIL_COND_V(v.size() != 2, ERR_INVALID_DATA);
111 float d = v[1].to_float();
112 Color c = current->get_albedo();
113 c.a = 1.0 - d;
114 current->set_albedo(c);
115 if (c.a < 0.99) {
116 current->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
117 }
118
119 } else if (l.begins_with("map_Ka ")) {
120 //uv
121 WARN_PRINT("OBJ: Ambient light texture for material '" + current_name + "' is ignored in PBR");
122
123 } else if (l.begins_with("map_Kd ")) {
124 //normal
125 ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
126
127 String p = l.replace("map_Kd", "").replace("\\", "/").strip_edges();
128 String path;
129 if (p.is_absolute_path()) {
130 path = p;
131 } else {
132 path = base_path.path_join(p);
133 }
134
135 Ref<Texture2D> texture = ResourceLoader::load(path);
136
137 if (texture.is_valid()) {
138 current->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, texture);
139 } else if (r_missing_deps) {
140 r_missing_deps->push_back(path);
141 }
142
143 } else if (l.begins_with("map_Ks ")) {
144 //normal
145 ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
146
147 String p = l.replace("map_Ks", "").replace("\\", "/").strip_edges();
148 String path;
149 if (p.is_absolute_path()) {
150 path = p;
151 } else {
152 path = base_path.path_join(p);
153 }
154
155 Ref<Texture2D> texture = ResourceLoader::load(path);
156
157 if (texture.is_valid()) {
158 current->set_texture(StandardMaterial3D::TEXTURE_METALLIC, texture);
159 } else if (r_missing_deps) {
160 r_missing_deps->push_back(path);
161 }
162
163 } else if (l.begins_with("map_Ns ")) {
164 //normal
165 ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
166
167 String p = l.replace("map_Ns", "").replace("\\", "/").strip_edges();
168 String path;
169 if (p.is_absolute_path()) {
170 path = p;
171 } else {
172 path = base_path.path_join(p);
173 }
174
175 Ref<Texture2D> texture = ResourceLoader::load(path);
176
177 if (texture.is_valid()) {
178 current->set_texture(StandardMaterial3D::TEXTURE_ROUGHNESS, texture);
179 } else if (r_missing_deps) {
180 r_missing_deps->push_back(path);
181 }
182 } else if (l.begins_with("map_bump ")) {
183 //normal
184 ERR_FAIL_COND_V(current.is_null(), ERR_FILE_CORRUPT);
185
186 String p = l.replace("map_bump", "").replace("\\", "/").strip_edges();
187 String path = base_path.path_join(p);
188
189 Ref<Texture2D> texture = ResourceLoader::load(path);
190
191 if (texture.is_valid()) {
192 current->set_feature(StandardMaterial3D::FEATURE_NORMAL_MAPPING, true);
193 current->set_texture(StandardMaterial3D::TEXTURE_NORMAL, texture);
194 } else if (r_missing_deps) {
195 r_missing_deps->push_back(path);
196 }
197 } else if (f->eof_reached()) {
198 break;
199 }
200 }
201
202 return OK;
203}
204
205static 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, List<String> *r_missing_deps) {
206 Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
207 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));
208
209 // Avoid trying to load/interpret potential build artifacts from Visual Studio (e.g. when compiling native plugins inside the project tree)
210 // This should only match, if it's indeed a COFF file header
211 // https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types
212 const int first_bytes = f->get_16();
213 static const Vector<int> coff_header_machines{
214 0x0, // IMAGE_FILE_MACHINE_UNKNOWN
215 0x8664, // IMAGE_FILE_MACHINE_AMD64
216 0x1c0, // IMAGE_FILE_MACHINE_ARM
217 0x14c, // IMAGE_FILE_MACHINE_I386
218 0x200, // IMAGE_FILE_MACHINE_IA64
219 };
220 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));
221 f->seek(0);
222
223 Ref<ArrayMesh> mesh;
224 mesh.instantiate();
225
226 bool generate_tangents = p_generate_tangents;
227 Vector3 scale_mesh = p_scale_mesh;
228 Vector3 offset_mesh = p_offset_mesh;
229 int mesh_flags = 0;
230
231 Vector<Vector3> vertices;
232 Vector<Vector3> normals;
233 Vector<Vector2> uvs;
234 Vector<Color> colors;
235 const String default_name = "Mesh";
236 String name = default_name;
237
238 HashMap<String, HashMap<String, Ref<StandardMaterial3D>>> material_map;
239
240 Ref<SurfaceTool> surf_tool = memnew(SurfaceTool);
241 surf_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
242
243 String current_material_library;
244 String current_material;
245 String current_group;
246 uint32_t smooth_group = 0;
247 bool smoothing = true;
248 const uint32_t no_smoothing_smooth_group = (uint32_t)-1;
249
250 while (true) {
251 String l = f->get_line().strip_edges();
252 while (l.length() && l[l.length() - 1] == '\\') {
253 String add = f->get_line().strip_edges();
254 l += add;
255 if (add.is_empty()) {
256 break;
257 }
258 }
259
260 if (l.begins_with("v ")) {
261 //vertex
262 Vector<String> v = l.split(" ", false);
263 ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);
264 Vector3 vtx;
265 vtx.x = v[1].to_float() * scale_mesh.x + offset_mesh.x;
266 vtx.y = v[2].to_float() * scale_mesh.y + offset_mesh.y;
267 vtx.z = v[3].to_float() * scale_mesh.z + offset_mesh.z;
268 vertices.push_back(vtx);
269 //vertex color
270 if (v.size() >= 7) {
271 while (colors.size() < vertices.size() - 1) {
272 colors.push_back(Color(1.0, 1.0, 1.0));
273 }
274 Color c;
275 c.r = v[4].to_float();
276 c.g = v[5].to_float();
277 c.b = v[6].to_float();
278 colors.push_back(c);
279 } else if (!colors.is_empty()) {
280 colors.push_back(Color(1.0, 1.0, 1.0));
281 }
282 } else if (l.begins_with("vt ")) {
283 //uv
284 Vector<String> v = l.split(" ", false);
285 ERR_FAIL_COND_V(v.size() < 3, ERR_FILE_CORRUPT);
286 Vector2 uv;
287 uv.x = v[1].to_float();
288 uv.y = 1.0 - v[2].to_float();
289 uvs.push_back(uv);
290
291 } else if (l.begins_with("vn ")) {
292 //normal
293 Vector<String> v = l.split(" ", false);
294 ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);
295 Vector3 nrm;
296 nrm.x = v[1].to_float();
297 nrm.y = v[2].to_float();
298 nrm.z = v[3].to_float();
299 normals.push_back(nrm);
300 } else if (l.begins_with("f ")) {
301 //vertex
302
303 Vector<String> v = l.split(" ", false);
304 ERR_FAIL_COND_V(v.size() < 4, ERR_FILE_CORRUPT);
305
306 //not very fast, could be sped up
307
308 Vector<String> face[3];
309 face[0] = v[1].split("/");
310 face[1] = v[2].split("/");
311 ERR_FAIL_COND_V(face[0].size() == 0, ERR_FILE_CORRUPT);
312
313 ERR_FAIL_COND_V(face[0].size() != face[1].size(), ERR_FILE_CORRUPT);
314 for (int i = 2; i < v.size() - 1; i++) {
315 face[2] = v[i + 1].split("/");
316
317 ERR_FAIL_COND_V(face[0].size() != face[2].size(), ERR_FILE_CORRUPT);
318 for (int j = 0; j < 3; j++) {
319 int idx = j;
320
321 if (idx < 2) {
322 idx = 1 ^ idx;
323 }
324
325 if (face[idx].size() == 3) {
326 int norm = face[idx][2].to_int() - 1;
327 if (norm < 0) {
328 norm += normals.size() + 1;
329 }
330 ERR_FAIL_INDEX_V(norm, normals.size(), ERR_FILE_CORRUPT);
331 surf_tool->set_normal(normals[norm]);
332 }
333
334 if (face[idx].size() >= 2 && !face[idx][1].is_empty()) {
335 int uv = face[idx][1].to_int() - 1;
336 if (uv < 0) {
337 uv += uvs.size() + 1;
338 }
339 ERR_FAIL_INDEX_V(uv, uvs.size(), ERR_FILE_CORRUPT);
340 surf_tool->set_uv(uvs[uv]);
341 }
342
343 int vtx = face[idx][0].to_int() - 1;
344 if (vtx < 0) {
345 vtx += vertices.size() + 1;
346 }
347 ERR_FAIL_INDEX_V(vtx, vertices.size(), ERR_FILE_CORRUPT);
348
349 Vector3 vertex = vertices[vtx];
350 if (!colors.is_empty()) {
351 surf_tool->set_color(colors[vtx]);
352 }
353 surf_tool->set_smooth_group(smoothing ? smooth_group : no_smoothing_smooth_group);
354 surf_tool->add_vertex(vertex);
355 }
356
357 face[1] = face[2];
358 }
359 } else if (l.begins_with("s ")) { //smoothing
360 String what = l.substr(2, l.length()).strip_edges();
361 bool do_smooth;
362 if (what == "off") {
363 do_smooth = false;
364 } else {
365 do_smooth = true;
366 }
367 if (do_smooth != smoothing) {
368 smoothing = do_smooth;
369 if (smoothing) {
370 smooth_group++;
371 }
372 }
373 } else if (/*l.begins_with("g ") ||*/ l.begins_with("usemtl ") || (l.begins_with("o ") || f->eof_reached())) { //commit group to mesh
374 //groups are too annoying
375 if (surf_tool->get_vertex_array().size()) {
376 //another group going on, commit it
377 if (normals.size() == 0) {
378 surf_tool->generate_normals();
379 }
380
381 if (generate_tangents && uvs.size()) {
382 surf_tool->generate_tangents();
383 }
384
385 surf_tool->index();
386
387 print_verbose("OBJ: Current material library " + current_material_library + " has " + itos(material_map.has(current_material_library)));
388 print_verbose("OBJ: Current material " + current_material + " has " + itos(material_map.has(current_material_library) && material_map[current_material_library].has(current_material)));
389
390 if (material_map.has(current_material_library) && material_map[current_material_library].has(current_material)) {
391 Ref<StandardMaterial3D> &material = material_map[current_material_library][current_material];
392 if (!colors.is_empty()) {
393 material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
394 }
395 surf_tool->set_material(material);
396 }
397
398 mesh = surf_tool->commit(mesh, mesh_flags);
399
400 if (!current_material.is_empty()) {
401 mesh->surface_set_name(mesh->get_surface_count() - 1, current_material.get_basename());
402 } else if (!current_group.is_empty()) {
403 mesh->surface_set_name(mesh->get_surface_count() - 1, current_group);
404 }
405
406 print_verbose("OBJ: Added surface :" + mesh->surface_get_name(mesh->get_surface_count() - 1));
407 surf_tool->clear();
408 surf_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
409 }
410
411 if (l.begins_with("o ") || f->eof_reached()) {
412 if (!p_single_mesh) {
413 if (mesh->get_surface_count() > 0) {
414 mesh->set_name(name);
415 r_meshes.push_back(mesh);
416 mesh.instantiate();
417 }
418 name = default_name;
419 current_group = "";
420 current_material = "";
421 }
422 }
423
424 if (f->eof_reached()) {
425 break;
426 }
427
428 if (l.begins_with("o ")) {
429 name = l.substr(2, l.length()).strip_edges();
430 }
431
432 if (l.begins_with("usemtl ")) {
433 current_material = l.replace("usemtl", "").strip_edges();
434 }
435
436 if (l.begins_with("g ")) {
437 current_group = l.substr(2, l.length()).strip_edges();
438 }
439
440 } else if (l.begins_with("mtllib ")) { //parse material
441
442 current_material_library = l.replace("mtllib", "").strip_edges();
443 if (!material_map.has(current_material_library)) {
444 HashMap<String, Ref<StandardMaterial3D>> lib;
445 String lib_path = current_material_library;
446 if (lib_path.is_relative_path()) {
447 lib_path = p_path.get_base_dir().path_join(current_material_library);
448 }
449 Error err = _parse_material_library(lib_path, lib, r_missing_deps);
450 if (err == OK) {
451 material_map[current_material_library] = lib;
452 }
453 }
454 }
455 }
456
457 if (p_single_mesh) {
458 r_meshes.push_back(mesh);
459 }
460
461 return OK;
462}
463
464Node *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) {
465 List<Ref<Mesh>> meshes;
466
467 Error err = _parse_obj(p_path, meshes, false, p_flags & IMPORT_GENERATE_TANGENT_ARRAYS, false, Vector3(1, 1, 1), Vector3(0, 0, 0), r_missing_deps);
468
469 if (err != OK) {
470 if (r_err) {
471 *r_err = err;
472 }
473 return nullptr;
474 }
475
476 Node3D *scene = memnew(Node3D);
477
478 for (const Ref<Mesh> &m : meshes) {
479 Ref<ImporterMesh> mesh;
480 mesh.instantiate();
481 mesh->set_name(m->get_name());
482 for (int i = 0; i < m->get_surface_count(); i++) {
483 mesh->add_surface(m->surface_get_primitive_type(i), m->surface_get_arrays(i), Array(), Dictionary(), m->surface_get_material(i));
484 }
485
486 ImporterMeshInstance3D *mi = memnew(ImporterMeshInstance3D);
487 mi->set_mesh(mesh);
488 mi->set_name(m->get_name());
489 scene->add_child(mi, true);
490 mi->set_owner(scene);
491 }
492
493 if (r_err) {
494 *r_err = OK;
495 }
496
497 return scene;
498}
499
500void EditorOBJImporter::get_extensions(List<String> *r_extensions) const {
501 r_extensions->push_back("obj");
502}
503
504EditorOBJImporter::EditorOBJImporter() {
505}
506
507////////////////////////////////////////////////////
508
509String ResourceImporterOBJ::get_importer_name() const {
510 return "wavefront_obj";
511}
512
513String ResourceImporterOBJ::get_visible_name() const {
514 return "OBJ As Mesh";
515}
516
517void ResourceImporterOBJ::get_recognized_extensions(List<String> *p_extensions) const {
518 p_extensions->push_back("obj");
519}
520
521String ResourceImporterOBJ::get_save_extension() const {
522 return "mesh";
523}
524
525String ResourceImporterOBJ::get_resource_type() const {
526 return "Mesh";
527}
528
529int ResourceImporterOBJ::get_format_version() const {
530 return 1;
531}
532
533int ResourceImporterOBJ::get_preset_count() const {
534 return 0;
535}
536
537String ResourceImporterOBJ::get_preset_name(int p_idx) const {
538 return "";
539}
540
541void ResourceImporterOBJ::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
542 r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate_tangents"), true));
543 r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "scale_mesh"), Vector3(1, 1, 1)));
544 r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "offset_mesh"), Vector3(0, 0, 0)));
545 r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "optimize_mesh"), true));
546}
547
548bool ResourceImporterOBJ::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
549 return true;
550}
551
552Error 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) {
553 List<Ref<Mesh>> meshes;
554
555 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"], nullptr);
556
557 ERR_FAIL_COND_V(err != OK, err);
558 ERR_FAIL_COND_V(meshes.size() != 1, ERR_BUG);
559
560 String save_path = p_save_path + ".mesh";
561
562 err = ResourceSaver::save(meshes.front()->get(), save_path);
563
564 ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save Mesh to file '" + save_path + "'.");
565
566 r_gen_files->push_back(save_path);
567
568 return OK;
569}
570
571ResourceImporterOBJ::ResourceImporterOBJ() {
572}
573