From f46cc32715f9faffb4afff5d98fb337c98aa7b73 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 4 Oct 2023 20:15:23 +0200 Subject: [PATCH] Cleanup code #1115 --- .../Editor/Managed/ManagedEditor.Internal.cpp | 2 +- Source/Engine/Content/Asset.h | 1 - Source/Engine/Content/Content.cpp | 5 -- Source/Engine/Tools/ModelTool/ModelTool.cpp | 63 ++++++++----------- Source/Engine/Tools/ModelTool/ModelTool.h | 7 +-- 5 files changed, 30 insertions(+), 48 deletions(-) diff --git a/Source/Editor/Managed/ManagedEditor.Internal.cpp b/Source/Editor/Managed/ManagedEditor.Internal.cpp index 49c3af87e..afcbb740a 100644 --- a/Source/Editor/Managed/ManagedEditor.Internal.cpp +++ b/Source/Editor/Managed/ManagedEditor.Internal.cpp @@ -829,4 +829,4 @@ bool ManagedEditor::TryRestoreImportOptions(ModelTool::Options& options, String // Get options from model FileSystem::NormalizePath(assetPath); return ImportModelFile::TryGetImportOptions(assetPath, options); -} \ No newline at end of file +} diff --git a/Source/Engine/Content/Asset.h b/Source/Engine/Content/Asset.h index 0ecc6db3f..2d2ba4b3f 100644 --- a/Source/Engine/Content/Asset.h +++ b/Source/Engine/Content/Asset.h @@ -246,4 +246,3 @@ public: // Don't include Content.h but just Load method extern FLAXENGINE_API Asset* LoadAsset(const Guid& id, const ScriptingTypeHandle& type); -extern FLAXENGINE_API Asset* LoadAsset(const StringView& path, const ScriptingTypeHandle& type); diff --git a/Source/Engine/Content/Content.cpp b/Source/Engine/Content/Content.cpp index eabf58cf2..6aecd1778 100644 --- a/Source/Engine/Content/Content.cpp +++ b/Source/Engine/Content/Content.cpp @@ -447,11 +447,6 @@ FLAXENGINE_API Asset* LoadAsset(const Guid& id, const ScriptingTypeHandle& type) return Content::LoadAsync(id, type); } -FLAXENGINE_API Asset* LoadAsset(const StringView& path, const ScriptingTypeHandle& type) -{ - return Content::LoadAsync(path, type); -} - Asset* Content::LoadAsync(const StringView& path, MClass* type) { CHECK_RETURN(type, nullptr); diff --git a/Source/Engine/Tools/ModelTool/ModelTool.cpp b/Source/Engine/Tools/ModelTool/ModelTool.cpp index ad41028c9..b703b47d1 100644 --- a/Source/Engine/Tools/ModelTool/ModelTool.cpp +++ b/Source/Engine/Tools/ModelTool/ModelTool.cpp @@ -633,8 +633,8 @@ void CreateLinearListFromTree(Array& nodes, Array& mapping) // Customized breadth first tree algorithm (each node has no direct reference to the children so we build the cache for the nodes depth level) const int32 count = nodes.Count(); Array> depths(count); // Pair.First = Depth, Pair.Second = Node Index - depths.SetSize(count); - depths.Set(-1); + depths.Resize(count); + depths.SetAll(-1); for (int32 i = 0; i < count; i++) { // Skip evaluated nodes @@ -653,7 +653,7 @@ void CreateLinearListFromTree(Array& nodes, Array& mapping) } while (end != -1 && lastDepth == -1); // Set the depth (second item is the node index) - depths[i] = MakePair(lastDepth + relativeDepth, i); + depths[i] = ToPair(lastDepth + relativeDepth, i); } for (int32 i = 0; i < count; i++) { @@ -666,7 +666,7 @@ void CreateLinearListFromTree(Array& nodes, Array& mapping) // Extract nodes mapping O(n^2) mapping.EnsureCapacity(count, false); - mapping.SetSize(count); + mapping.Resize(count); for (int32 i = 0; i < count; i++) { int32 newIndex = -1; @@ -964,7 +964,7 @@ bool ModelTool::ImportModel(const String& path, ModelData& meshData, Options& op case TextureEntry::TypeHint::Normals: textureOptions.Type = TextureFormatType::NormalMap; break; - default:; + default: ; } AssetsImportingManager::ImportIfEdited(texture.FilePath, assetPath, texture.AssetID, &textureOptions); #endif @@ -999,37 +999,34 @@ bool ModelTool::ImportModel(const String& path, ModelData& meshData, Options& op importedFileNames.Add(filename); #if COMPILE_WITH_ASSETS_IMPORTER auto assetPath = autoImportOutput / filename + ASSET_FILES_EXTENSION_WITH_DOT; + + // When splitting imported meshes allow only the first mesh to import assets (mesh[0] is imported after all following ones so import assets during mesh[1]) + if (!options.SplitObjects && options.ObjectIndex != 1 && options.ObjectIndex != -1) + { + // Find that asset create previously + AssetInfo info; + if (Content::GetAssetInfo(assetPath, info)) + material.AssetID = info.ID; + continue; + } + if (options.ImportMaterialsAsInstances) { - if (!options.SplitObjects && options.ObjectIndex != 1 && options.ObjectIndex != -1) - { - // Find that asset create previously - AssetInfo info; - if (Content::GetAssetInfo(assetPath, info)) - material.AssetID = info.ID; - continue; - } - + // Create material instance AssetsImportingManager::Create(AssetsImportingManager::CreateMaterialInstanceTag, assetPath, material.AssetID); - MaterialInstance* materialInstance = (MaterialInstance*)LoadAsset(assetPath, MaterialInstance::TypeInitializer); - if (materialInstance->WaitForLoaded()) + if (MaterialInstance* materialInstance = Content::Load(assetPath)) + { + materialInstance->SetBaseMaterial(options.InstanceToImportAs); + materialInstance->Save(); + } + else { LOG(Error, "Failed to load material instance after creation. ({0})", assetPath); - return true; } - - MaterialBase* materialInstanceOf = options.InstanceToImportAs; - if (materialInstanceOf->WaitForLoaded()) - { - LOG(Error, "Failed to load material to create an instance of. ({0})", options.InstanceToImportAs->GetID()); - return true; - } - - materialInstance->SetBaseMaterial(materialInstanceOf); - materialInstance->Save(); } else { + // Create material CreateMaterial::Options materialOptions; materialOptions.Diffuse.Color = material.Diffuse.Color; if (material.Diffuse.TextureIndex != -1) @@ -1047,15 +1044,6 @@ bool ModelTool::ImportModel(const String& path, ModelData& meshData, Options& op materialOptions.Info.CullMode = CullMode::TwoSided; if (!Math::IsOne(material.Opacity.Value) || material.Opacity.TextureIndex != -1) materialOptions.Info.BlendMode = MaterialBlendMode::Transparent; - - if (!options.SplitObjects && options.ObjectIndex != 1 && options.ObjectIndex != -1) - { - // Find that asset create previously - AssetInfo info; - if (Content::GetAssetInfo(assetPath, info)) - material.AssetID = info.ID; - continue; - } AssetsImportingManager::Create(AssetsImportingManager::CreateMaterialTag, assetPath, material.AssetID, &materialOptions); } #endif @@ -1441,7 +1429,8 @@ bool ModelTool::ImportModel(const String& path, ModelData& meshData, Options& op bone.NodeIndex = mapping[bone.NodeIndex]; } } - reorder_nodes_and_test_it_out! + reorder_nodes_and_test_it_out + ! #endif } else if (options.Type == ModelType::Animation) diff --git a/Source/Engine/Tools/ModelTool/ModelTool.h b/Source/Engine/Tools/ModelTool/ModelTool.h index 99ca72df5..36bb9b12c 100644 --- a/Source/Engine/Tools/ModelTool/ModelTool.h +++ b/Source/Engine/Tools/ModelTool/ModelTool.h @@ -12,8 +12,7 @@ #include "Engine/Graphics/Models/ModelData.h" #include "Engine/Graphics/Models/SkeletonData.h" #include "Engine/Animations/AnimationData.h" -#include -#include +#include "Engine/Content/Assets/MaterialBase.h" class JsonWriter; @@ -265,7 +264,7 @@ public: // If specified, all meshes which name starts with this prefix will be imported as a separate collision data (excluded used for rendering). API_FIELD(Attributes="EditorOrder(100), EditorDisplay(\"Geometry\"), VisibleIf(nameof(ShowGeometry))") String CollisionMeshesPrefix = TEXT(""); - // The type of collision that should be generated if has collision prefix especified + // The type of collision that should be generated if has collision prefix specified. API_FIELD(Attributes = "EditorOrder(105), EditorDisplay(\"Geometry\"), VisibleIf(nameof(ShowGeometry))") CollisionDataType CollisionType = CollisionDataType::TriangleMesh; @@ -345,7 +344,7 @@ public: bool ImportMaterialsAsInstances = false; // The material to import the model's materials as an instance of. API_FIELD(Attributes = "EditorOrder(402), EditorDisplay(\"Materials\"), VisibleIf(nameof(ImportMaterialsAsInstances))") - AssetReference InstanceToImportAs = (Material*) GPUDevice::Instance->GetDefaultMaterial(); + AssetReference InstanceToImportAs; // If checked, the importer will import texture files used by the model and any embedded texture resources. API_FIELD(Attributes="EditorOrder(410), EditorDisplay(\"Materials\"), VisibleIf(nameof(ShowGeometry))") bool ImportTextures = true;