From b380da7cb0fdc40b60d0ddc18125e038f474e632 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Molteni Date: Thu, 4 Apr 2024 11:43:48 -0300 Subject: [PATCH] Enforce BC7 texturing, set texarray material by default, remove prefab instantiation Merge pull request #21 from decentraland/feat/adding-texture-array feat: Added texture array --- .gitignore | 7 +- .../Editor/CustomLODFBXPostProcessor.cs | 48 +++++++++++ .../Editor/CustomLODFBXPostProcessor.cs.meta | 11 +++ .../LODsConverter/Editor/LODClient.cs | 26 ++++-- .../LODsConverter/Scripts/LODConversion.cs | 83 ++++++++----------- .../LODsConverter/Utils/LODPathHandler.cs | 12 +-- .../git-submodules/unity-shared-dependencies | 2 +- 7 files changed, 126 insertions(+), 63 deletions(-) create mode 100644 asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/CustomLODFBXPostProcessor.cs create mode 100644 asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/CustomLODFBXPostProcessor.cs.meta diff --git a/.gitignore b/.gitignore index 62314fad..0b59409f 100644 --- a/.gitignore +++ b/.gitignore @@ -63,4 +63,9 @@ Unity_lic.* # This gets changed all the time, we ignore it to have a cleaner checkout asset-bundle-converter/Assets/git-submodules/unity-shared-dependencies/Runtime/Shaders/URP/Lit.shader.meta -.idea/ \ No newline at end of file +.idea/ + +# LODs + +asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/ExportLODToAssetBundle/ +asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/ExportLODToAssetBundle.meta \ No newline at end of file diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/CustomLODFBXPostProcessor.cs b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/CustomLODFBXPostProcessor.cs new file mode 100644 index 00000000..9dedcd27 --- /dev/null +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/CustomLODFBXPostProcessor.cs @@ -0,0 +1,48 @@ +using System; +using UnityEditor; +using UnityEngine; +using Object = UnityEngine.Object; + +public class CustomLODFBXPostProcessor : AssetPostprocessor +{ + private void GenerateColliders(GameObject model) + { + var meshFilters = model.GetComponentsInChildren(); + + foreach (var filter in meshFilters) + { + if (filter.name.Contains("_collider", StringComparison.OrdinalIgnoreCase)) + ConfigureColliders(filter.transform, filter); + } + + var renderers = model.GetComponentsInChildren(); + + foreach (var r in renderers) + { + if (r.name.Contains("_collider", StringComparison.OrdinalIgnoreCase)) + Object.DestroyImmediate(r); + } + } + + private void ConfigureColliders(Transform transform, MeshFilter filter) + { + if (filter != null) + { + Physics.BakeMesh(filter.sharedMesh.GetInstanceID(), false); + filter.gameObject.AddComponent(); + Object.DestroyImmediate(filter.GetComponent()); + } + + foreach (Transform child in transform) + { + var f = child.gameObject.GetComponent(); + ConfigureColliders(child, f); + } + } + + private void OnPostprocessModel(GameObject model) + { + if (model.name.Contains("_0")) + GenerateColliders(model); + } +} \ No newline at end of file diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/CustomLODFBXPostProcessor.cs.meta b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/CustomLODFBXPostProcessor.cs.meta new file mode 100644 index 00000000..f547981c --- /dev/null +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/CustomLODFBXPostProcessor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8d5403f9edeb4404ca1394def20f8683 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/LODClient.cs b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/LODClient.cs index 2ee74170..06e73403 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/LODClient.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Editor/LODClient.cs @@ -3,6 +3,7 @@ using UnityEditor; using System.IO; using System.Collections.Generic; +using System.Threading.Tasks; using AssetBundleConverter.LODsConverter.Utils; using AssetBundleConverter.Wrappers.Interfaces; using UnityEngine.Rendering; @@ -12,9 +13,8 @@ namespace DCL.ABConverter { public class LODClient : MonoBehaviour { - - [MenuItem("Assets/Export URL LODs")] - public static void ExportURLLODsToAssetBundles() + [MenuItem("Decentraland/LOD/Export URL LODs")] + public static async void ExportURLLODsToAssetBundles() { string[] commandLineArgs = Environment.GetCommandLineArgs(); @@ -30,15 +30,23 @@ public static void ExportURLLODsToAssetBundles() customOutputDirectory = outputDirectoryArg[0] + "/"; var lodConversion = new LODConversion(customOutputDirectory, lodsURL.Split(";")); - lodConversion.ConvertLODs(); + await lodConversion.ConvertLODs(); } - [MenuItem("Assets/Export FBX Folder To Asset Bundles")] - private static void ExportFBXToAssetBundles() + [MenuItem("Decentraland/LOD/Export FBX Folder To Asset Bundles")] + private static async void ExportFBXToAssetBundles() { - string[] fileEntries = Directory.GetFiles(Path.Combine(Application.dataPath, "ExportToAssetBundle"), "*.fbx", SearchOption.AllDirectories); - var lodConversion = new LODConversion(LODConstants.DEFAULT_OUTPUT_PATH, fileEntries); - lodConversion.ConvertLODs(); + string[] subdirectoryEntries = Directory.GetDirectories(Path.Combine(Application.dataPath, "AssetBundleConverter/LODsConverter/ExportLODToAssetBundle")); + foreach (string subdirectory in subdirectoryEntries) + { + string[] fileEntries = Directory.GetFiles(subdirectory, "*.fbx", SearchOption.AllDirectories); + if (fileEntries.Length > 0) + { + var lodConversion = new LODConversion(LODConstants.DEFAULT_OUTPUT_PATH, fileEntries); + await lodConversion.ConvertLODs(); + Directory.Delete(subdirectory, true); + } + } } } diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Scripts/LODConversion.cs b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Scripts/LODConversion.cs index 45b02111..cf641362 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Scripts/LODConversion.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Scripts/LODConversion.cs @@ -18,6 +18,9 @@ public class LODConversion { private readonly LODPathHandler lodPathHandler; private readonly string[] urlsToConvert; + private readonly Shader usedLODShader; + private readonly Shader usedLOD0Shader; + //TODO: CLEAN THIS UP HERE AND IN THE ASSET BUNDLE BUILDER. THIS IS NOT USED IN ALPHA private const string VERSION = "7.0"; @@ -26,9 +29,11 @@ public LODConversion(string customOutputPath, string[] urlsToConvert) { this.urlsToConvert = urlsToConvert; lodPathHandler = new LODPathHandler(customOutputPath); + usedLOD0Shader = Shader.Find("DCL/Scene"); + usedLODShader = Shader.Find("DCL/Scene_TexArray"); } - public async void ConvertLODs() + public async Task ConvertLODs() { PlatformUtils.currentTarget = EditorUserBuildSettings.activeBuildTarget; //TODO (Juani) Temporal hack. Clean with the regular asset bundle process @@ -53,17 +58,15 @@ public async void ConvertLODs() foreach (string downloadedFilePath in downloadedFilePaths) { lodPathHandler.SetCurrentFile(downloadedFilePath); - if (File.Exists(lodPathHandler.assetBundlePath)) - continue; dictionaryStringForMetadata.Add(lodPathHandler.fileNameWithoutExtension, lodPathHandler.fileNameWithoutExtension); lodPathHandler.MoveFileToMatchingFolder(); BuildPrefab(lodPathHandler); } - AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); - assetDatabase.AssignAssetBundle(Shader.Find("DCL/Scene"), false); - BuildAssetBundles(EditorUserBuildSettings.activeBuildTarget, dictionaryStringForMetadata); + assetDatabase.AssignAssetBundle(usedLOD0Shader, false); + assetDatabase.AssignAssetBundle(usedLODShader, false); + //BuildAssetBundles(EditorUserBuildSettings.activeBuildTarget, dictionaryStringForMetadata); } catch (Exception e) { @@ -72,10 +75,8 @@ public async void ConvertLODs() return; } - //Temporal hack lodPathHandler.RelocateOutputFolder(); - - Directory.Delete(lodPathHandler.tempPath, true); + //Directory.Delete(lodPathHandler.tempPath, true); Debug.Log("Conversion done"); Utils.Exit(); } @@ -102,63 +103,50 @@ private void BuildPrefab(LODPathHandler lodPathHandler) AssetDatabase.WriteImportSettingsIfDirty(lodPathHandler.filePathRelativeToDataPath); AssetDatabase.ImportAsset(lodPathHandler.filePathRelativeToDataPath, ImportAssetOptions.ForceUpdate); - var instantiated = Object.Instantiate(AssetDatabase.LoadAssetAtPath(lodPathHandler.filePathRelativeToDataPath)); + var asset = AssetDatabase.LoadAssetAtPath(lodPathHandler.filePathRelativeToDataPath); if (lodPathHandler.filePath.Contains("_0")) { - SetDCLShaderMaterial(lodPathHandler, instantiated, false); - GenerateColliders(instantiated); + SetDCLShaderMaterial(lodPathHandler, asset, false, usedLOD0Shader); } else - SetDCLShaderMaterial(lodPathHandler, instantiated, true); + { + EnsureTextureFormat(); + SetDCLShaderMaterial(lodPathHandler, asset, true, usedLODShader); + } importer.SearchAndRemapMaterials(ModelImporterMaterialName.BasedOnMaterialName, ModelImporterMaterialSearch.Local); AssetDatabase.WriteImportSettingsIfDirty(lodPathHandler.filePathRelativeToDataPath); AssetDatabase.ImportAsset(lodPathHandler.filePath, ImportAssetOptions.ForceUpdate); - PrefabUtility.SaveAsPrefabAsset(instantiated, lodPathHandler.prefabPathRelativeToDataPath); - Object.DestroyImmediate(instantiated); - var prefabImporter = AssetImporter.GetAtPath(lodPathHandler.fileDirectoryRelativeToDataPath); prefabImporter.SetAssetBundleNameAndVariant(lodPathHandler.assetBundleFileName, ""); } - private void GenerateColliders(GameObject instantiated) + private void EnsureTextureFormat() { - var meshFilters = instantiated.GetComponentsInChildren(); - - foreach (var filter in meshFilters) + string[] texturePaths = Directory.GetFiles(lodPathHandler.fileDirectory, "*.png", SearchOption.AllDirectories); + foreach (string texturePath in texturePaths) { - if (filter.name.Contains("_collider", StringComparison.OrdinalIgnoreCase)) - ConfigureColliders(filter.transform, filter); - } - - var renderers = instantiated.GetComponentsInChildren(); + string assetPath = PathUtils.GetRelativePathTo(Application.dataPath, texturePath); + var importer = AssetImporter.GetAtPath(assetPath) as TextureImporter; + var texture = AssetDatabase.LoadAssetAtPath(assetPath); - foreach (var r in renderers) - { - if (r.name.Contains("_collider", StringComparison.OrdinalIgnoreCase)) - Object.DestroyImmediate(r); - } - } - - private void ConfigureColliders(Transform transform, MeshFilter filter) - { - if (filter != null) - { - Physics.BakeMesh(filter.sharedMesh.GetInstanceID(), false); - filter.gameObject.AddComponent(); - Object.DestroyImmediate(filter.GetComponent()); - } - - foreach (Transform child in transform) - { - var f = child.gameObject.GetComponent(); - ConfigureColliders(child, f); + if (importer != null) + { + importer.textureType = TextureImporterType.Default; + importer.isReadable = true; + importer.SetPlatformTextureSettings(new TextureImporterPlatformSettings + { + overridden = true, maxTextureSize = texture.width, name = "Standalone", format = TextureImporterFormat.BC7, + textureCompression = TextureImporterCompression.Compressed + }); + AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); + } } } - private void SetDCLShaderMaterial(LODPathHandler lodPathHandler, GameObject transform, bool setDefaultTransparency) + private void SetDCLShaderMaterial(LODPathHandler lodPathHandler, GameObject transform, bool setDefaultTransparency, Shader shader) { var childrenRenderers = transform.GetComponentsInChildren(); var materialsDictionary = new Dictionary(); @@ -169,7 +157,8 @@ private void SetDCLShaderMaterial(LODPathHandler lodPathHandler, GameObject tran { var material = componentsInChild.sharedMaterials[i]; var duplicatedMaterial = new Material(material); - duplicatedMaterial.shader = Shader.Find("DCL/Scene"); + duplicatedMaterial.shader = shader; + if (duplicatedMaterial.name.Contains("FORCED_TRANSPARENT")) ApplyTransparency(duplicatedMaterial, setDefaultTransparency); diff --git a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/LODPathHandler.cs b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/LODPathHandler.cs index c7be7483..3689b9bf 100644 --- a/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/LODPathHandler.cs +++ b/asset-bundle-converter/Assets/AssetBundleConverter/LODsConverter/Utils/LODPathHandler.cs @@ -16,6 +16,7 @@ public class LODPathHandler public string filePathRelativeToDataPath; public string filePath; + public string fileDirectory; public string fileName; public string fileNameWithoutExtension; @@ -39,23 +40,23 @@ public void SetCurrentFile(string downloadedFilePath) { filePath = downloadedFilePath; fileName = Path.GetFileName(filePath); - fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath); + fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath).ToLower(); assetBundleFileName = fileNameWithoutExtension + PlatformUtils.GetPlatform(); assetBundlePath = Path.Combine(outputPath, assetBundleFileName); } public void MoveFileToMatchingFolder() { - string fileDirectory = Path.GetDirectoryName(filePath); + string newFileDirectory = Path.GetDirectoryName(filePath); - if (fileDirectory.EndsWith(fileNameWithoutExtension)) + if (newFileDirectory.EndsWith(fileNameWithoutExtension)) { Console.WriteLine("The file is already in the correct folder."); UpdatePaths(filePath); return; } - string targetFolderPath = Path.Combine(fileDirectory, fileNameWithoutExtension); + string targetFolderPath = Path.Combine(newFileDirectory, fileNameWithoutExtension.ToLower()); Directory.CreateDirectory(targetFolderPath); string newFilePath = Path.Combine(targetFolderPath, fileName); File.Move(filePath, newFilePath); @@ -66,13 +67,14 @@ public void MoveFileToMatchingFolder() private void UpdatePaths(string newFilePath) { filePath = newFilePath; - string fileDirectory = Path.GetDirectoryName(filePath); + fileDirectory = Path.GetDirectoryName(filePath); filePathRelativeToDataPath = PathUtils.GetRelativePathTo(Application.dataPath, filePath); fileDirectoryRelativeToDataPath = PathUtils.GetRelativePathTo(Application.dataPath, fileDirectory); string materialsPath = Path.Combine(fileDirectory, "Materials"); Directory.CreateDirectory(materialsPath); materialsPathRelativeToDataPath = PathUtils.GetRelativePathTo(Application.dataPath, materialsPath); + prefabPathRelativeToDataPath = PathUtils.GetRelativePathTo(Application.dataPath, fileDirectory + "/" + fileNameWithoutExtension + " (gameobject).prefab"); // Save assets and refresh the AssetDatabase diff --git a/asset-bundle-converter/Assets/git-submodules/unity-shared-dependencies b/asset-bundle-converter/Assets/git-submodules/unity-shared-dependencies index 135e9423..ee9e885c 160000 --- a/asset-bundle-converter/Assets/git-submodules/unity-shared-dependencies +++ b/asset-bundle-converter/Assets/git-submodules/unity-shared-dependencies @@ -1 +1 @@ -Subproject commit 135e94236dcb90cdda23675b9309d10f4a0ca577 +Subproject commit ee9e885c3cc86dd46b9e0e65dcfaac963cad3d4b