Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement glTF compatibility system for files imported in older Godot versions #84271

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/io/resource_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class ResourceImporter : public RefCounted {

virtual void get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset = 0) const = 0;
virtual bool get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const = 0;
virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {}
virtual String get_option_group_file() const { return String(); }

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) = 0;
Expand Down
5 changes: 5 additions & 0 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,11 @@ Error EditorFileSystem::_reimport_file(const String &p_file, const HashMap<Strin
}
}

if (FileAccess::exists(p_file + ".import")) {
// We only want to handle compat for existing files, not new ones.
importer->handle_compatibility_options(params);
}

//mix with default params, in case a parameter is missing

List<ResourceImporter::ImportOption> opts;
Expand Down
10 changes: 8 additions & 2 deletions editor/import/resource_importer_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,12 @@ void ResourceImporterScene::get_import_options(const String &p_path, List<Import
}
}

void ResourceImporterScene::handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {
for (Ref<EditorSceneFormatImporter> importer_elem : importers) {
importer_elem->handle_compatibility_options(p_import_params);
}
}

void ResourceImporterScene::_replace_owner(Node *p_node, Node *p_scene, Node *p_new_owner) {
if (p_node != p_new_owner && p_node->get_owner() == p_scene) {
p_node->set_owner(p_new_owner);
Expand Down Expand Up @@ -2658,10 +2664,10 @@ ResourceImporterScene *ResourceImporterScene::animation_singleton = nullptr;
Vector<Ref<EditorSceneFormatImporter>> ResourceImporterScene::importers;
Vector<Ref<EditorScenePostImportPlugin>> ResourceImporterScene::post_importer_plugins;

bool ResourceImporterScene::ResourceImporterScene::has_advanced_options() const {
bool ResourceImporterScene::has_advanced_options() const {
return true;
}
void ResourceImporterScene::ResourceImporterScene::show_advanced_options(const String &p_path) {
void ResourceImporterScene::show_advanced_options(const String &p_path) {
SceneImportSettings::get_singleton()->open_settings(p_path, animation_importer);
}

Expand Down
2 changes: 2 additions & 0 deletions editor/import/resource_importer_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class EditorSceneFormatImporter : public RefCounted {
virtual Node *import_scene(const String &p_path, uint32_t p_flags, const HashMap<StringName, Variant> &p_options, List<String> *r_missing_deps, Error *r_err = nullptr);
virtual void get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options);
virtual Variant get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options);
virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {}

EditorSceneFormatImporter() {}
};
Expand Down Expand Up @@ -276,6 +277,7 @@ class ResourceImporterScene : public ResourceImporter {

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 void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const override;
// Import scenes *after* everything else (such as textures).
virtual int get_import_order() const override { return ResourceImporter::IMPORT_ORDER_SCENE; }

Expand Down
13 changes: 13 additions & 0 deletions modules/gltf/editor/editor_scene_importer_gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Node *EditorSceneFormatImporterGLTF::import_scene(const String &p_path, uint32_t
gltf.instantiate();
Ref<GLTFState> state;
state.instantiate();
if (p_options.has("gltf/naming_version")) {
int naming_version = p_options["gltf/naming_version"];
gltf->set_naming_version(naming_version);
}
if (p_options.has("gltf/embedded_image_handling")) {
int32_t enum_option = p_options["gltf/embedded_image_handling"];
state->set_handle_binary_image(enum_option);
Expand All @@ -77,7 +81,16 @@ Node *EditorSceneFormatImporterGLTF::import_scene(const String &p_path, uint32_t

void EditorSceneFormatImporterGLTF::get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) {
r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "gltf/naming_version", PROPERTY_HINT_ENUM, "Godot 4.1 or 4.0,Godot 4.2 or later"), 1));
r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "gltf/embedded_image_handling", PROPERTY_HINT_ENUM, "Discard All Textures,Extract Textures,Embed as Basis Universal,Embed as Uncompressed", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), GLTFState::HANDLE_BINARY_EXTRACT_TEXTURES));
}

void EditorSceneFormatImporterGLTF::handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {
if (!p_import_params.has("gltf/naming_version")) {
// If an existing import file is missing the glTF
// compatibility version, we need to use version 0.
p_import_params["gltf/naming_version"] = 0;
}
}

#endif // TOOLS_ENABLED
1 change: 1 addition & 0 deletions modules/gltf/editor/editor_scene_importer_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class EditorSceneFormatImporterGLTF : public EditorSceneFormatImporter {
List<String> *r_missing_deps, Error *r_err = nullptr) override;
virtual void get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) override;
virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const override;
};

#endif // TOOLS_ENABLED
Expand Down
37 changes: 31 additions & 6 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ Error GLTFDocument::_parse_scenes(Ref<GLTFState> p_state) {
} else {
p_state->scene_name = p_state->filename;
}
if (_naming_version == 0) {
p_state->scene_name = _gen_unique_name(p_state, p_state->scene_name);
}
}

return OK;
Expand Down Expand Up @@ -3023,6 +3026,14 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
return OK;
}

void GLTFDocument::set_naming_version(int p_version) {
_naming_version = p_version;
}

int GLTFDocument::get_naming_version() const {
return _naming_version;
}

void GLTFDocument::set_image_format(const String &p_image_format) {
_image_format = p_image_format;
}
Expand Down Expand Up @@ -5358,12 +5369,22 @@ void GLTFDocument::_assign_node_names(Ref<GLTFState> p_state) {
}
String gltf_node_name = gltf_node->get_name();
if (gltf_node_name.is_empty()) {
if (gltf_node->mesh >= 0) {
gltf_node_name = "Mesh";
} else if (gltf_node->camera >= 0) {
gltf_node_name = "Camera";
if (_naming_version == 0) {
if (gltf_node->mesh >= 0) {
gltf_node_name = _gen_unique_name(p_state, "Mesh");
} else if (gltf_node->camera >= 0) {
gltf_node_name = _gen_unique_name(p_state, "Camera3D");
} else {
gltf_node_name = _gen_unique_name(p_state, "Node");
}
} else {
gltf_node_name = "Node";
if (gltf_node->mesh >= 0) {
gltf_node_name = "Mesh";
} else if (gltf_node->camera >= 0) {
gltf_node_name = "Camera";
} else {
gltf_node_name = "Node";
}
}
}
gltf_node->set_name(_gen_unique_name(p_state, gltf_node_name));
Expand Down Expand Up @@ -7405,7 +7426,11 @@ Node *GLTFDocument::_generate_scene_node_tree(Ref<GLTFState> p_state) {
if (unlikely(p_state->scene_name.is_empty())) {
p_state->scene_name = single_root->get_name();
} else if (single_root->get_name() == StringName()) {
single_root->set_name(_gen_unique_name(p_state, p_state->scene_name));
if (_naming_version == 0) {
single_root->set_name(p_state->scene_name);
} else {
single_root->set_name(_gen_unique_name(p_state, p_state->scene_name));
}
}
return single_root;
}
Expand Down
3 changes: 3 additions & 0 deletions modules/gltf/gltf_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class GLTFDocument : public Resource {

private:
const float BAKE_FPS = 30.0f;
int _naming_version = 1;
String _image_format = "PNG";
float _lossy_quality = 0.75f;
Ref<GLTFDocumentExtension> _image_save_extension;
Expand All @@ -86,6 +87,8 @@ class GLTFDocument : public Resource {
static void unregister_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension);
static void unregister_all_gltf_document_extensions();

void set_naming_version(int p_version);
int get_naming_version() const;
void set_image_format(const String &p_image_format);
String get_image_format() const;
void set_lossy_quality(float p_lossy_quality);
Expand Down
Loading