godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.1k stars 21.16k forks source link

.obj files imported as Mesh are unable to reliably reference textures #81555

Open lyuma opened 1 year ago

lyuma commented 1 year ago

Godot version

4.1.1

System information

Godot v4.1.stable - Windows 10.0.19045 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3090 (NVIDIA; 31.0.15.3713) - Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz (16 Threads)

Issue description

If the .godot/imported directory is cleared, the .obj files (with materials) in a project appear to be getting imported before the .png files that their materials reference. This leads to huge amounts of error spam, as well as missing texture references during the first import.

The issue here is we are importing .obj files too early, when in he import as Mesh workflow. Most other models are imported as Scene which happens afterwards.

ERROR: Unable to open file: res://.godot/imported/road_2_tile.png-8c3542a850c37db56509a795f0efbdc7.s3tc.ctex.
   at: (scene/resources/texture.cpp:847)
ERROR: Failed loading resource: res://.godot/imported/road_2_tile.png-8c3542a850c37db56509a795f0efbdc7.s3tc.ctex. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:273)
ERROR: Failed loading resource: res://assets/terrain/grasslands/road_2_tile.png. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:273)
ERROR: Cannot create file 'res://.godot/imported/road_2_tile.obj-95c273f81f3df720a4e0dee5911e1b43.mesh'.
   at: (core/io/resource_format_binary.cpp:2070)
ERROR: Cannot save Mesh to file 'res://.godot/imported/road_2_tile.obj-95c273f81f3df720a4e0dee5911e1b43.mesh'.
   at: (editor/import/resource_importer_obj.cpp:564)
ERROR: Error importing 'res://assets/terrain/grasslands/road_2_tile.obj'.
   at: (editor/editor_file_system.cpp:2043)
WARNING: OBJ: Ambient light for material 'palette' is ignored in PBR
     at: _parse_material_library (editor/import/resource_importer_obj.cpp:65)

A workaround is to delete the obj files from .godot/imported such that they may be reimported in the correct order.

Steps to reproduce

  1. open the attached project (make sure .godot is deleted)
  2. notice that .obj files are imported first
  3. many errors will be printed

Minimal reproduction project

clone https://github.com/P1X-in/Tanks-of-Freedom-3-D

lyuma commented 1 year ago

From rocket chat: We could make it: images/sounds meshes scenes Or make .obj meshes be treated like scenes for import purposes.

akien-mga commented 1 year ago

I tried this simple approach:

diff --git a/editor/import/resource_importer_obj.cpp b/editor/import/resource_importer_obj.cpp
index adc21aaa7b..145db2f642 100644
--- a/editor/import/resource_importer_obj.cpp
+++ b/editor/import/resource_importer_obj.cpp
@@ -549,6 +549,10 @@ bool ResourceImporterOBJ::get_option_visibility(const String &p_path, const Stri
    return true;
 }

+int ResourceImporterOBJ::get_import_order() const {
+   return ResourceImporter::IMPORT_ORDER_SCENE;
+}
+
 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) {
    List<Ref<Mesh>> meshes;

diff --git a/editor/import/resource_importer_obj.h b/editor/import/resource_importer_obj.h
index faf0f336c0..35d63ef115 100644
--- a/editor/import/resource_importer_obj.h
+++ b/editor/import/resource_importer_obj.h
@@ -60,6 +60,7 @@ public:

    virtual void get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset = 0) const override;
    virtual bool get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const override;
+   virtual int get_import_order() const override;

    virtual Error 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 = nullptr, Variant *r_metadata = nullptr) override;

But that doesn't seem to solve the issue, it still complains about textures not being imported yet.

Scenes also seem to fail loading because of missing textures in this project, so there's something else going on that somehow prevents textures from getting imported when they should be.