diff --git a/Source/Editor/Managed/ManagedEditor.Internal.cpp b/Source/Editor/Managed/ManagedEditor.Internal.cpp index afcbb740a..49c3af87e 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 2d2ba4b3f..0ecc6db3f 100644 --- a/Source/Engine/Content/Asset.h +++ b/Source/Engine/Content/Asset.h @@ -246,3 +246,4 @@ 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 6aecd1778..eabf58cf2 100644 --- a/Source/Engine/Content/Content.cpp +++ b/Source/Engine/Content/Content.cpp @@ -447,6 +447,11 @@ 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 ad287be86..ad41028c9 100644 --- a/Source/Engine/Tools/ModelTool/ModelTool.cpp +++ b/Source/Engine/Tools/ModelTool/ModelTool.cpp @@ -31,6 +31,7 @@ #include "Engine/Tools/TextureTool/TextureTool.h" #include "Engine/ContentImporters/AssetsImportingManager.h" #include "Engine/ContentImporters/CreateMaterial.h" +#include "Engine/ContentImporters/CreateMaterialInstance.h" #include "Engine/ContentImporters/CreateCollisionData.h" #include "Engine/Serialization/Serialization.h" #include "Editor/Utilities/EditorUtilities.h" @@ -387,6 +388,8 @@ void ModelTool::Options::Serialize(SerializeStream& stream, const void* otherObj SERIALIZE(SloppyOptimization); SERIALIZE(LODTargetError); SERIALIZE(ImportMaterials); + SERIALIZE(ImportMaterialsAsInstances); + SERIALIZE(InstanceToImportAs); SERIALIZE(ImportTextures); SERIALIZE(RestoreMaterialsOnReimport); SERIALIZE(GenerateSDF); @@ -430,6 +433,8 @@ void ModelTool::Options::Deserialize(DeserializeStream& stream, ISerializeModifi DESERIALIZE(SloppyOptimization); DESERIALIZE(LODTargetError); DESERIALIZE(ImportMaterials); + DESERIALIZE(ImportMaterialsAsInstances); + DESERIALIZE(InstanceToImportAs); DESERIALIZE(ImportTextures); DESERIALIZE(RestoreMaterialsOnReimport); DESERIALIZE(GenerateSDF); @@ -503,11 +508,11 @@ bool ModelTool::ImportData(const String& path, ImportedModelData& data, Options& if (ImportDataAssimp(importPath.Get(), data, options, errorMsg)) return true; #elif USE_AUTODESK_FBX_SDK - if (ImportDataAutodeskFbxSdk(importPath.Get(), data, options, errorMsg)) - return true; + if (ImportDataAutodeskFbxSdk(importPath.Get(), data, options, errorMsg)) + return true; #elif USE_OPEN_FBX - if (ImportDataOpenFBX(importPath.Get(), data, options, errorMsg)) - return true; + if (ImportDataOpenFBX(importPath.Get(), data, options, errorMsg)) + return true; #else LOG(Error, "Compiled without model importing backend."); return true; @@ -620,62 +625,62 @@ bool ModelTool::ImportData(const String& path, ImportedModelData& data, Options& bool SortDepths(const Pair& a, const Pair& b) { - return a.First < b.First; + return a.First < b.First; } 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); - for (int32 i = 0; i < count; i++) - { - // Skip evaluated nodes - if (depths[i].First != -1) - continue; + const int32 count = nodes.Count(); + Array> depths(count); // Pair.First = Depth, Pair.Second = Node Index + depths.SetSize(count); + depths.Set(-1); + for (int32 i = 0; i < count; i++) + { + // Skip evaluated nodes + if (depths[i].First != -1) + continue; - // Find the first node with calculated depth and get the distance to it - int32 end = i; - int32 lastDepth; - int32 relativeDepth = 0; - do - { - lastDepth = depths[end].First; - end = nodes[end].ParentIndex; - relativeDepth++; - } while (end != -1 && lastDepth == -1); + // Find the first node with calculated depth and get the distance to it + int32 end = i; + int32 lastDepth; + int32 relativeDepth = 0; + do + { + lastDepth = depths[end].First; + end = nodes[end].ParentIndex; + relativeDepth++; + } while (end != -1 && lastDepth == -1); - // Set the depth (second item is the node index) - depths[i] = MakePair(lastDepth + relativeDepth, i); - } - for (int32 i = 0; i < count; i++) - { - // Strange divide by 2 but works - depths[i].First = depths[i].First >> 1; - } + // Set the depth (second item is the node index) + depths[i] = MakePair(lastDepth + relativeDepth, i); + } + for (int32 i = 0; i < count; i++) + { + // Strange divide by 2 but works + depths[i].First = depths[i].First >> 1; + } - // Order nodes by depth O(n*log(n)) - depths.Sort(SortDepths); + // Order nodes by depth O(n*log(n)) + depths.Sort(SortDepths); - // Extract nodes mapping O(n^2) - mapping.EnsureCapacity(count, false); - mapping.SetSize(count); - for (int32 i = 0; i < count; i++) - { - int32 newIndex = -1; - for (int32 j = 0; j < count; j++) - { - if (depths[j].Second == i) - { - newIndex = j; - break; - } - } - ASSERT(newIndex != -1); - mapping[i] = newIndex; - } + // Extract nodes mapping O(n^2) + mapping.EnsureCapacity(count, false); + mapping.SetSize(count); + for (int32 i = 0; i < count; i++) + { + int32 newIndex = -1; + for (int32 j = 0; j < count; j++) + { + if (depths[j].Second == i) + { + newIndex = j; + break; + } + } + ASSERT(newIndex != -1); + mapping[i] = newIndex; + } } #endif @@ -959,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 @@ -994,35 +999,65 @@ 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; - CreateMaterial::Options materialOptions; - materialOptions.Diffuse.Color = material.Diffuse.Color; - if (material.Diffuse.TextureIndex != -1) - materialOptions.Diffuse.Texture = data.Textures[material.Diffuse.TextureIndex].AssetID; - materialOptions.Diffuse.HasAlphaMask = material.Diffuse.HasAlphaMask; - materialOptions.Emissive.Color = material.Emissive.Color; - if (material.Emissive.TextureIndex != -1) - materialOptions.Emissive.Texture = data.Textures[material.Emissive.TextureIndex].AssetID; - materialOptions.Opacity.Value = material.Opacity.Value; - if (material.Opacity.TextureIndex != -1) - materialOptions.Opacity.Texture = data.Textures[material.Opacity.TextureIndex].AssetID; - if (material.Normals.TextureIndex != -1) - materialOptions.Normals.Texture = data.Textures[material.Normals.TextureIndex].AssetID; - if (material.TwoSided || material.Diffuse.HasAlphaMask) - materialOptions.Info.CullMode = CullMode::TwoSided; - if (!Math::IsOne(material.Opacity.Value) || material.Opacity.TextureIndex != -1) - materialOptions.Info.BlendMode = MaterialBlendMode::Transparent; - - // 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) + if (options.ImportMaterialsAsInstances) { - // Find that asset create previously - AssetInfo info; - if (Content::GetAssetInfo(assetPath, info)) - material.AssetID = info.ID; - continue; - } + 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); + AssetsImportingManager::Create(AssetsImportingManager::CreateMaterialInstanceTag, assetPath, material.AssetID); + MaterialInstance* materialInstance = (MaterialInstance*)LoadAsset(assetPath, MaterialInstance::TypeInitializer); + if (materialInstance->WaitForLoaded()) + { + 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 + { + CreateMaterial::Options materialOptions; + materialOptions.Diffuse.Color = material.Diffuse.Color; + if (material.Diffuse.TextureIndex != -1) + materialOptions.Diffuse.Texture = data.Textures[material.Diffuse.TextureIndex].AssetID; + materialOptions.Diffuse.HasAlphaMask = material.Diffuse.HasAlphaMask; + materialOptions.Emissive.Color = material.Emissive.Color; + if (material.Emissive.TextureIndex != -1) + materialOptions.Emissive.Texture = data.Textures[material.Emissive.TextureIndex].AssetID; + materialOptions.Opacity.Value = material.Opacity.Value; + if (material.Opacity.TextureIndex != -1) + materialOptions.Opacity.Texture = data.Textures[material.Opacity.TextureIndex].AssetID; + if (material.Normals.TextureIndex != -1) + materialOptions.Normals.Texture = data.Textures[material.Normals.TextureIndex].AssetID; + if (material.TwoSided || material.Diffuse.HasAlphaMask) + 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 } @@ -1389,24 +1424,24 @@ bool ModelTool::ImportModel(const String& path, ModelData& meshData, Options& op #if USE_SKELETON_NODES_SORTING // Sort skeleton nodes and bones hierarchy (parents first) - // Then it can be used with a simple linear loop update - { - const int32 nodesCount = data.Skeleton.Nodes.Count(); - const int32 bonesCount = data.Skeleton.Bones.Count(); - Array mapping; - CreateLinearListFromTree(data.Skeleton.Nodes, mapping); - for (int32 i = 0; i < nodesCount; i++) - { - auto& node = data.Skeleton.Nodes[i]; - node.ParentIndex = mapping[node.ParentIndex]; - } - for (int32 i = 0; i < bonesCount; i++) - { - auto& bone = data.Skeleton.Bones[i]; - bone.NodeIndex = mapping[bone.NodeIndex]; - } - } - reorder_nodes_and_test_it_out! + // Then it can be used with a simple linear loop update + { + const int32 nodesCount = data.Skeleton.Nodes.Count(); + const int32 bonesCount = data.Skeleton.Bones.Count(); + Array mapping; + CreateLinearListFromTree(data.Skeleton.Nodes, mapping); + for (int32 i = 0; i < nodesCount; i++) + { + auto& node = data.Skeleton.Nodes[i]; + node.ParentIndex = mapping[node.ParentIndex]; + } + for (int32 i = 0; i < bonesCount; i++) + { + auto& bone = data.Skeleton.Bones[i]; + bone.NodeIndex = mapping[bone.NodeIndex]; + } + } + 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 bfd91215e..99ca72df5 100644 --- a/Source/Engine/Tools/ModelTool/ModelTool.h +++ b/Source/Engine/Tools/ModelTool/ModelTool.h @@ -12,6 +12,8 @@ #include "Engine/Graphics/Models/ModelData.h" #include "Engine/Graphics/Models/SkeletonData.h" #include "Engine/Animations/AnimationData.h" +#include +#include class JsonWriter; @@ -338,6 +340,12 @@ public: // If checked, the importer will create materials for model meshes as specified in the file. API_FIELD(Attributes="EditorOrder(400), EditorDisplay(\"Materials\"), VisibleIf(nameof(ShowGeometry))") bool ImportMaterials = true; + // If checked, the importer will create the model's materials as instances of a base material. + API_FIELD(Attributes = "EditorOrder(401), EditorDisplay(\"Materials\"), VisibleIf(nameof(ImportMaterials))") + 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(); // 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;