Skip to content

Commit

Permalink
renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Dec 29, 2023
1 parent 437d176 commit 34347b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
37 changes: 24 additions & 13 deletions Runtime/Scripts/Extensions/MaterialExtensions.cs
Expand Up @@ -3,9 +3,6 @@
using UnityEngine;
using UnityGLTF.Extensions;
using UnityGLTF.Plugins;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace UnityGLTF
{
Expand Down Expand Up @@ -42,18 +39,28 @@ public class MaterialExtensionsPlugin: GltfExportPlugin
public bool KHR_materials_ior = true;
public bool KHR_materials_transmission = true;
public bool KHR_materials_volume = true;
public bool KHR_materials_iridescence = true;
public bool KHR_materials_specular = true;
public bool KHR_materials_clearcoat = true;

public override GltfExportPluginContext CreateInstance(ExportContext context)
{
return new MaterialExtensions();
return new MaterialExtensions(this);
}

public override string DisplayName => "Material Extensions";
public override string DisplayName => "PBR Next Material Extensions";
public override string Description => "Exports various glTF PBR Material model extensions.";
}

public class MaterialExtensions: GltfExportPluginContext
{
private MaterialExtensionsPlugin settings;

public MaterialExtensions(MaterialExtensionsPlugin settings)
{
this.settings = settings;
}

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 @@ -80,15 +87,19 @@ public class MaterialExtensions: GltfExportPluginContext
private static readonly int clearcoatRoughnessTexture = Shader.PropertyToID("clearcoatRoughnessTexture");
private static readonly int clearcoatNormalTexture = Shader.PropertyToID("clearcoatNormalTexture");


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

var usesTransmission = material.IsKeywordEnabled("_VOLUME_TRANSMISSION_ON");
var usesVolume = material.HasProperty("_VOLUME_ON") && material.GetFloat("_VOLUME_ON") > 0.5f;
var hasIor = material.HasProperty(ior) && !Mathf.Approximately(material.GetFloat(ior), KHR_materials_ior.DefaultIor);

if (hasIor)
var hasNonDefaultIor = material.HasProperty(ior) && !Mathf.Approximately(material.GetFloat(ior), KHR_materials_ior.DefaultIor);
var usesIridescence = material.IsKeywordEnabled("_IRIDESCENCE_ON");
var usesSpecular = material.IsKeywordEnabled("_SPECULAR_ON");
var usesClearcoat = material.IsKeywordEnabled("_CLEARCOAT_ON");

if (hasNonDefaultIor && settings.KHR_materials_ior)
{
if (materialnode.Extensions == null)
materialnode.Extensions = new Dictionary<string, IExtension>();
Expand All @@ -105,7 +116,7 @@ public override void AfterMaterialExport(GLTFSceneExporter exporter, GLTFRoot gl
vi.ior = material.GetFloat(ior);
}

if (usesTransmission)
if (usesTransmission && settings.KHR_materials_transmission)
{
if (materialnode.Extensions == null)
materialnode.Extensions = new Dictionary<string, IExtension>();
Expand All @@ -126,7 +137,7 @@ public override void AfterMaterialExport(GLTFSceneExporter exporter, GLTFRoot gl
vt.transmissionTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(transmissionTexture), nameof(transmissionTexture));
}

if (usesVolume)
if (usesVolume && settings.KHR_materials_volume)
{
if (materialnode.Extensions == null)
materialnode.Extensions = new Dictionary<string, IExtension>();
Expand All @@ -151,7 +162,7 @@ public override void AfterMaterialExport(GLTFSceneExporter exporter, GLTFRoot gl
ve.attenuationColor = material.GetColor(attenuationColor).ToNumericsColorRaw();
}

if (material.IsKeywordEnabled("_IRIDESCENCE_ON"))
if (usesIridescence && settings.KHR_materials_iridescence)
{
exporter.DeclareExtensionUsage(KHR_materials_iridescence_Factory.EXTENSION_NAME, false);

Expand Down Expand Up @@ -179,7 +190,7 @@ public override void AfterMaterialExport(GLTFSceneExporter exporter, GLTFRoot gl
vir.iridescenceThicknessTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(iridescenceThicknessTexture), nameof(iridescenceThicknessTexture));
}

if (material.IsKeywordEnabled("_SPECULAR_ON"))
if (usesSpecular && settings.KHR_materials_specular)
{
exporter.DeclareExtensionUsage(KHR_materials_specular_Factory.EXTENSION_NAME, false);

Expand All @@ -203,7 +214,7 @@ public override void AfterMaterialExport(GLTFSceneExporter exporter, GLTFRoot gl
vir.specularColorTexture = exporter.ExportTextureInfoWithTextureTransform(material, material.GetTexture(specularColorTexture), nameof(specularColorTexture));
}

if (material.IsKeywordEnabled("_CLEARCOAT_ON"))
if (usesClearcoat && settings.KHR_materials_clearcoat)
{
exporter.DeclareExtensionUsage(KHR_materials_clearcoat_Factory.EXTENSION_NAME, false);

Expand Down
23 changes: 8 additions & 15 deletions Runtime/Scripts/GLTFSceneExporter.cs
Expand Up @@ -40,34 +40,27 @@ public ExportContext(GLTFSettings settings)

public GLTFSceneExporter.RetrieveTexturePathDelegate TexturePathRetriever = (texture) => texture.name;

[Obsolete("Register export plugins with GLTFSettings instead")]
// TODO Should we make all the callbacks on ExportContext obsolete?
// Pro: We can remove them from the API
// Con: No direct way to "just add callbacks" right now, always needs a plugin.
// See GLTFSceneExporter for a case here we "just want callbacks" instead of a new class/context
public GLTFSceneExporter.AfterSceneExportDelegate AfterSceneExport;
[Obsolete("Register export plugins with GLTFSettings instead")]
public GLTFSceneExporter.BeforeSceneExportDelegate BeforeSceneExport;
[Obsolete("Register export plugins with GLTFSettings instead")]
public GLTFSceneExporter.AfterNodeExportDelegate AfterNodeExport;
[Obsolete("Register export plugins with GLTFSettings instead")]
public GLTFSceneExporter.BeforeMaterialExportDelegate BeforeMaterialExport;
[Obsolete("Register export plugins with GLTFSettings instead")]
public GLTFSceneExporter.AfterMaterialExportDelegate AfterMaterialExport;
[Obsolete("Register export plugins with GLTFSettings instead")]
public GLTFSceneExporter.BeforeTextureExportDelegate BeforeTextureExport;
[Obsolete("Register export plugins with GLTFSettings instead")]
public GLTFSceneExporter.AfterTextureExportDelegate AfterTextureExport;
[Obsolete("Register export plugins with GLTFSettings instead")]
public GLTFSceneExporter.AfterPrimitiveExportDelegate AfterPrimitiveExport;

internal GltfExportPluginContext GetImplicitPlugin()
{
return new ImplicitPlugin(this);
}
internal GltfExportPluginContext GetExportContextCallbacks() => new ExportContextCallbacks(this);

#pragma warning disable CS0618 // Type or member is obsolete
internal class ImplicitPlugin : GltfExportPluginContext
internal class ExportContextCallbacks : GltfExportPluginContext
{
private readonly ExportContext _exportContext;

internal ImplicitPlugin(ExportContext context)
internal ExportContextCallbacks(ExportContext context)
{
_exportContext = context;
}
Expand Down Expand Up @@ -574,7 +567,7 @@ public GLTFSceneExporter(Transform[] rootTransforms, ExportContext context)
// legacy: implicit plugin for all the static methods on GLTFSceneExporter
_plugins.Add(new LegacyCallbacksPlugin());
// legacy: implicit plugin for all the methods on ExportContext
_plugins.Add(context.GetImplicitPlugin());
_plugins.Add(context.GetExportContextCallbacks());

// create export plugin instances
foreach (var plugin in settings.ExportPlugins)
Expand Down

0 comments on commit 34347b3

Please sign in to comment.