Skip to content

Commit

Permalink
Support Geometry Input
Browse files Browse the repository at this point in the history
  • Loading branch information
pumachen committed Apr 9, 2023
1 parent 870f048 commit c4886e5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
5 changes: 4 additions & 1 deletion Editor/Harpoon.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"references": [
"GUID:343deaaf83e0cee4ca978e7df0b80d21",
"GUID:478a2357cc57436488a56e564b08d223",
"GUID:2bafac87e7f4b9b418d9448d219b01ab"
"GUID:2bafac87e7f4b9b418d9448d219b01ab",
"GUID:0f9e8f73c69964f1e8176f9a0b86f9b2",
"GUID:ad318b1a7ea5a6f4aa9b396904ece70f",
"GUID:2b82e46c4ca0a4eb697ab8ccdfb39af1"
],
"includePlatforms": [
"Editor"
Expand Down
62 changes: 52 additions & 10 deletions Editor/Parm.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.IO;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
using UnityEditor;
using UnityEditor.Formats.Fbx.Exporter;
using UnityEngine;
using UnityEngine.Networking;

Expand Down Expand Up @@ -88,7 +89,9 @@ public class StringParm : HouParm
public StringParmTemplate template;

[SerializeField]
private Texture2D texture;
public Texture2D texture;
[SerializeField]
public GameObject model;

public string value;

Expand All @@ -110,13 +113,14 @@ public override void GUILayout()
case FileType.Fbx:
case FileType.Usd:
{
EditorGUILayout.LabelField("Geometry parameters are not supported");
model = EditorGUILayout.ObjectField(template.label, model, typeof(GameObject), true) as GameObject;
value = "";
break;
}
case FileType.Image:
{
texture = EditorGUILayout.ObjectField(template.label, texture, typeof(Texture2D), true) as Texture2D;
value = texture == null ? "" : Path.GetFileName(AssetDatabase.GetAssetPath(texture));
value = "";
break;
}
}
Expand All @@ -140,7 +144,6 @@ byte[] rawTexture
}
if (AssetDatabase.Contains(texture))
{
//return texture.EncodeToEXR();
string path = Path.GetFullPath(
AssetDatabase.GetAssetPath(texture),
Path.GetDirectoryName(Application.dataPath));
Expand All @@ -151,7 +154,43 @@ byte[] rawTexture
return texture.EncodeToEXR();
}
}

}

byte[] rawModel
{
get
{
if (model == null)
{
return new byte[0];
}
string path = Path.Combine(Application.temporaryCachePath,
$"{template.name}_{model.GetInstanceID()}.fbx");
ExportBinaryFBX(path, model);
var rawFBX = File.ReadAllBytes(path);
File.WriteAllBytes(Path.Combine(Application.temporaryCachePath, "TMP.fbx"), rawFBX);
return rawFBX;
}
}

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
Expand All @@ -162,12 +201,15 @@ public override IMultipartFormSection formSection
&& template.fileType == FileType.Image
&& texture != null)
{
return new MultipartFormFileSection(template.name, rawTexture, "Heightmap.exr", "image/aces");
return new MultipartFormFileSection(template.name, rawTexture, $"{template.name}_{texture.GetInstanceID()}.exr", "image/aces");
}
else
if (template.stringType == StringParmType.FileReference
&& template.fileType == FileType.Geometry
&& model != null)
{
return new MultipartFormDataSection(template.name, JsonConvert.SerializeObject(value));
return new MultipartFormFileSection(template.name, rawModel, $"{template.name}_{model.GetInstanceID()}.fbx", "image/aces");
}
return new MultipartFormDataSection(template.name, JsonConvert.SerializeObject(value));
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Editor/Window/HDAProcessorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ void Cook()
zip =>
{
string outputDir = EditorUtility.SaveFolderPanel("Output Dir", Application.dataPath, "Output");
zip.ExtractToDirectory(outputDir, true);
if (outputDir != null)
{
zip.ExtractToDirectory(outputDir, true);
AssetDatabase.Refresh();
}
progress = 1.0f;
});
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"changelogUrl": "",
"licenseUrl": "",
"dependencies": {
"com.unity.editorcoroutines": "1.0.0"
"com.unity.editorcoroutines": "1.0.0",
"com.unity.formats.fbx": "4.1.3"
},
"keywords": [],
"author": {
Expand Down

0 comments on commit c4886e5

Please sign in to comment.