From 2c5c752bbbc96fa59835e7574c83478f1de43b99 Mon Sep 17 00:00:00 2001 From: PUM4CH3N <18906012399@163.com> Date: Tue, 18 Apr 2023 21:02:29 +0800 Subject: [PATCH] Add texture embedding option --- Editor/API/HDAProcessor.cs | 2 +- Editor/Parm.cs | 31 +++++++------------------------ 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/Editor/API/HDAProcessor.cs b/Editor/API/HDAProcessor.cs index 6c2b56d..3fa4e3c 100644 --- a/Editor/API/HDAProcessor.cs +++ b/Editor/API/HDAProcessor.cs @@ -99,7 +99,7 @@ public static void ProcessHDAAsync(string hda, IEnumerable parms, Actio } public static IEnumerator ProcessHDARoutine(string hda, IEnumerable parms, Action completed, - Action failed = null, int timeout = 120) + Action failed = null, int timeout = 300) { Uri uri = GetUri(hda); List formData = new List(); diff --git a/Editor/Parm.cs b/Editor/Parm.cs index e8112fe..367833d 100644 --- a/Editor/Parm.cs +++ b/Editor/Parm.cs @@ -122,6 +122,8 @@ public class StringParm : HouParm [SerializeField] public Texture2D texture; [SerializeField] + public bool embedTexture; + [SerializeField] public GameObject model; public string value; @@ -145,6 +147,7 @@ public override void GUILayout() case FileType.Usd: { model = EditorGUILayout.ObjectField(template.label, model, typeof(GameObject), true) as GameObject; + embedTexture = EditorGUILayout.Toggle("Embed Texture", embedTexture); value = ""; break; } @@ -197,43 +200,23 @@ byte[] rawModel } string path = Path.Combine(Application.temporaryCachePath, $"{template.name}_{model.GetInstanceID()}.fbx"); - ExportFBX(path, model); + ExportFBX(path, model, embedTexture); var rawFBX = File.ReadAllBytes(path); File.WriteAllBytes(Path.Combine(Application.temporaryCachePath, "TMP.fbx"), rawFBX); return rawFBX; } } - private static void ExportFBX(string path, GameObject model) + private static void ExportFBX(string path, GameObject model, bool embedTexture) { model = GameObject.Instantiate(model); ExportModelOptions exportSettings = new ExportModelOptions(); exportSettings.ExportFormat = ExportFormat.Binary; - exportSettings.EmbedTextures = true; + exportSettings.EmbedTextures = embedTexture; ModelExporter.ExportObject(path, model, exportSettings); Object.DestroyImmediate(model); } - private static void ExportBinaryFBX (string filePath, UnityEngine.Object singleObject) - { - // Find relevant internal types in Unity.Formats.Fbx.Editor assembly - Type[] types = AppDomain.CurrentDomain.GetAssemblies().First(x => x.FullName == "Unity.Formats.Fbx.Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null").GetTypes(); - Type optionsInterfaceType = types.First(x => x.Name == "IExportOptions"); - Type optionsType = types.First(x => x.Name == "ExportOptionsSettingsSerializeBase"); - - // Instantiate a settings object instance - MethodInfo optionsProperty = typeof(ModelExporter).GetProperty("DefaultOptions", BindingFlags.Static | BindingFlags.NonPublic).GetGetMethod(true); - object optionsInstance = optionsProperty.Invoke(null, null); - - // Change the export setting from ASCII to binary - FieldInfo exportFormatField = optionsType.GetField("exportFormat", BindingFlags.Instance | BindingFlags.NonPublic); - exportFormatField.SetValue(optionsInstance, 1); - - // Invoke the ExportObject method with the settings param - MethodInfo exportObjectMethod = typeof(ModelExporter).GetMethod("ExportObject", BindingFlags.Static | BindingFlags.NonPublic, Type.DefaultBinder, new Type[] { typeof(string), typeof(UnityEngine.Object), optionsInterfaceType }, null); - exportObjectMethod.Invoke(null, new object[] { filePath, singleObject, optionsInstance }); - } - public override IMultipartFormSection formSection { get @@ -248,7 +231,7 @@ public override IMultipartFormSection formSection && template.fileType == FileType.Geometry && model != null) { - return new MultipartFormFileSection(template.name, rawModel, $"{template.name}_{model.GetInstanceID()}.fbx", "image/aces"); + return new MultipartFormFileSection(template.name, rawModel, $"{template.name}_{model.GetInstanceID()}.fbx", "multipart/form-data"); } return new MultipartFormDataSection(template.name, JsonConvert.SerializeObject(value)); }