Skip to content

Commit

Permalink
start switching to export plugin callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Dec 29, 2023
1 parent db6bd69 commit 595b5f1
Show file tree
Hide file tree
Showing 20 changed files with 322 additions and 126 deletions.
2 changes: 1 addition & 1 deletion Editor/Scripts/GLTFExportMenu.cs
Expand Up @@ -108,7 +108,7 @@ private static void ExportSceneGLB()
private static void Export(Transform[] transforms, bool binary, string sceneName)
{
var settings = GLTFSettings.GetOrCreateSettings();
var exportOptions = new ExportOptions { TexturePathRetriever = RetrieveTexturePath };
var exportOptions = new ExportContext { TexturePathRetriever = RetrieveTexturePath };
var exporter = new GLTFSceneExporter(transforms, exportOptions);

var invokedByShortcut = Event.current?.type == EventType.KeyDown;
Expand Down
4 changes: 2 additions & 2 deletions Editor/Scripts/GLTFImporter.cs
Expand Up @@ -46,7 +46,7 @@ namespace UnityGLTF
#else
[ScriptedImporter(ImporterVersion, new[] { "glb" })]
#endif
public class GLTFImporter : ScriptedImporter, IGLTFImportRemap
public class GLTFImporter : ScriptedImporter
{
private const int ImporterVersion = 9;

Expand Down Expand Up @@ -236,7 +236,7 @@ public override void OnImportAsset(AssetImportContext ctx)
if (plugin != null && plugin.Enabled)
{
var instance = plugin.CreateInstance(context);
if(instance != null) plugins.Add(instance);
if (instance != null) plugins.Add(instance);
}
}

Expand Down
21 changes: 21 additions & 0 deletions Editor/Scripts/Plugins/GltfPluginEditor.cs
@@ -0,0 +1,21 @@
using UnityEditor;
using UnityGLTF.Plugins;

namespace UnityGLTF
{
[CustomEditor(typeof(GltfPlugin), true)]
public class GltfPluginEditor: Editor
{
// Follows the default implementation of OnInspectorGUI, but skips the script field
public override void OnInspectorGUI()
{
serializedObject.Update();
var iterator = serializedObject.GetIterator();
// skip script field
iterator.NextVisible(true);
while (iterator.NextVisible(false))
EditorGUILayout.PropertyField(iterator, true);
serializedObject.ApplyModifiedProperties();
}
}
}
3 changes: 3 additions & 0 deletions Editor/Scripts/Plugins/GltfPluginEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Editor/Scripts/ShaderGraph/MaterialEditorBridge.cs
Expand Up @@ -23,7 +23,7 @@ private static void OnImmutableMaterialChanged(Material material)
// var mainAsset = AssetDatabase.LoadMainAssetAtPath(assetPath);

// Transform[] rootTransforms = null;
var exporter = new GLTFSceneExporter((Transform[]) null, new ExportOptions());
var exporter = new GLTFSceneExporter((Transform[]) null, new ExportContext());
// load all materials from mainAsset
var importer = AssetImporter.GetAtPath(assetPath) as GLTFImporter;
if (!importer) return;
Expand Down
53 changes: 42 additions & 11 deletions Runtime/Scripts/Extensions/MaterialExtensions.cs
Expand Up @@ -2,23 +2,57 @@
using GLTF.Schema;
using UnityEngine;
using UnityGLTF.Extensions;
using UnityGLTF.Plugins;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace UnityGLTF
{
public static class MaterialExtensions
// When a plugin is registered with the default settings (the scriptable object in the project),
// it will be active "by default" when someone uses those default settings.
// e.g. it's used when someone uses the built-in editor methods for exporting objects.
// When using the API, one needs to manually register wanted plugins and configure them
// (can get the default settings and modify them).

// Plugins can contain any number of extensions, but are encouraged to specify in the description
// which extensions are imported/exported with that plugin.
// Theoretically there could be multiple plugins operating on the same extension in different ways, in
// which case we currently can't warn about conflicts; they would all run.
// If plugins were required to list the extensions they operate on, we could warn about conflicts.

// Plugins are ScriptableObjects which are added to the default GLTFSettings scriptable object.
// Their public serialized fields are exposed in the inspector, and they can be enabled/disabled.
// Plugins replace both GLTFSceneExporter.* static callbacks and GLTFSceneExporter.ExportOptions callbacks
// to allow for more control.

// Example cases where separate plugins operate on the same data:
// - exporting UI as custom extension vs. baking UI to mesh
// - exporting Audio in a custom extension vs. using KHR_audio
// - exporting LODs as custom extension vs. using MSFT_lod
// - exporting particle systems as custom extension vs. baking to mesh

// Plugins can either be added manually to ExportOptions.plugins / ImportContext.plugins
// or advertise themselves via a static callback which allows configuring their settings in the inspector.
// For each new instance of GLTFSceneExporter, new instances of plugins are created.
// For each new instance of GLTFSceneImporter, new instances of plugins are created.

public class MaterialExtensionsPlugin: GltfExportPlugin
{
#if UNITY_EDITOR
[InitializeOnLoadMethod]
#endif
[RuntimeInitializeOnLoadMethod]
static void InitExt()
public bool KHR_materials_ior = true;
public bool KHR_materials_transmission = true;
public bool KHR_materials_volume = true;

public override GltfExportPluginContext CreateInstance(ExportContext context)
{
GLTFSceneExporter.AfterMaterialExport += GLTFSceneExporterOnAfterMaterialExport;
return new MaterialExtensions();
}

public override string DisplayName => "Material Extensions";
}

public class MaterialExtensions: GltfExportPluginContext
{
private static readonly int thicknessTexture = Shader.PropertyToID("thicknessTexture");
private static readonly int thicknessFactor = Shader.PropertyToID("thicknessFactor");
private static readonly int attenuationDistance = Shader.PropertyToID("attenuationDistance");
Expand All @@ -45,8 +79,7 @@ static void InitExt()
private static readonly int clearcoatRoughnessTexture = Shader.PropertyToID("clearcoatRoughnessTexture");
private static readonly int clearcoatNormalTexture = Shader.PropertyToID("clearcoatNormalTexture");


private static void GLTFSceneExporterOnAfterMaterialExport(GLTFSceneExporter exporter, GLTFRoot gltfroot, Material material, GLTFMaterial materialnode)
public override void AfterMaterialExport(GLTFSceneExporter exporter, GLTFRoot gltfroot, Material material, GLTFMaterial materialnode)
{
if (!material) return;

Expand Down Expand Up @@ -193,8 +226,6 @@ private static void GLTFSceneExporterOnAfterMaterialExport(GLTFSceneExporter exp
cc.clearcoatRoughnessTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(clearcoatRoughnessTexture), nameof(clearcoatRoughnessTexture));
if (material.HasProperty(clearcoatNormalTexture))
cc.clearcoatNormalTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(clearcoatNormalTexture), nameof(clearcoatNormalTexture));


}
}
}
Expand Down

0 comments on commit 595b5f1

Please sign in to comment.