Skip to content

Commit

Permalink
bump package version, clean up material ext sample, move to core
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed May 27, 2022
1 parent 47a8673 commit e198de7
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public JProperty Serialize()
JProperty jProperty = new JProperty(KHR_materials_specular_Factory.EXTENSION_NAME, jo);

if (specularFactor != 1) jo.Add(new JProperty(nameof(specularFactor), specularFactor));
if (specularColorFactor != COLOR_DEFAULT) jo.Add(new JProperty(nameof(specularColorFactor), specularColorFactor));
if (specularColorFactor != COLOR_DEFAULT) jo.Add(new JProperty(nameof(specularColorFactor), new JArray(specularColorFactor.R, specularColorFactor.G, specularColorFactor.B)));
if (specularTexture != null) {
jo.Add(new JProperty(nameof(specularTexture),
new JObject(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using GLTF.Schema;
using UnityEditor;
using UnityEngine;
using UnityGLTF.Extensions;

namespace UnityGLTF
{
public class MaterialExtensions : MonoBehaviour
{
[InitializeOnLoadMethod]
[RuntimeInitializeOnLoadMethod]
static void InitExt()
{
GLTFSceneExporter.AfterMaterialExport += GLTFSceneExporterOnAfterMaterialExport;
}

private static readonly int ThicknessTexture = Shader.PropertyToID("_ThicknessTexture");
private static readonly int ThicknessFactor = Shader.PropertyToID("_ThicknessFactor");
private static readonly int AttenuationDistance = Shader.PropertyToID("_AttenuationDistance");
private static readonly int AttenuationColor = Shader.PropertyToID("_AttenuationColor");
private static readonly int IOR = Shader.PropertyToID("_IOR");
private static readonly int TransmissionFactor = Shader.PropertyToID("_TransmissionFactor");
private static readonly int TransmissionTexture = Shader.PropertyToID("_TransmissionTexture");

private static readonly int IridescenceFactor = Shader.PropertyToID("_IridescenceFactor");
private static readonly int IridescenceIor = Shader.PropertyToID("_IridescenceIor");
private static readonly int IridescenceThicknessMinimum = Shader.PropertyToID("_IridescenceThicknessMinimum");
private static readonly int IridescenceThicknessMaximum = Shader.PropertyToID("_IridescenceThicknessMaximum");
private static readonly int IridescenceTexture = Shader.PropertyToID("_IridescenceTexture");
private static readonly int IridescenceThicknessTexture = Shader.PropertyToID("_IridescenceThicknessTexture");
private static readonly int SpecularFactor = Shader.PropertyToID("_SpecularFactor");
private static readonly int SpecularColorFactor = Shader.PropertyToID("_SpecularColorFactor");
private static readonly int SpecularTexture = Shader.PropertyToID("_SpecularTexture");
private static readonly int SpecularColorTexture = Shader.PropertyToID("_SpecularColorTexture");

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

if (material.IsKeywordEnabled("_VOLUME_TRANSMISSION_ON"))
{
exporter.DeclareExtensionUsage(KHR_materials_volume_Factory.EXTENSION_NAME, false);
exporter.DeclareExtensionUsage(KHR_materials_ior_Factory.EXTENSION_NAME, false);
exporter.DeclareExtensionUsage(KHR_materials_transmission_Factory.EXTENSION_NAME, false);

var ve = new KHR_materials_volume();
var vi = new KHR_materials_ior();
var vt = new KHR_materials_transmission();

if (material.HasProperty(ThicknessFactor))
ve.thicknessFactor = material.GetFloat(ThicknessFactor);
if (material.HasProperty(ThicknessTexture) && material.GetTexture(ThicknessTexture))
ve.thicknessTexture = exporter.ExportTextureInfo(material.GetTexture(ThicknessTexture), GLTFSceneExporter.TextureMapType.Custom_Unknown);
if (material.HasProperty(AttenuationDistance))
ve.attenuationDistance = material.GetFloat(AttenuationDistance);
if (material.HasProperty(AttenuationColor))
ve.attenuationColor = material.GetColor(AttenuationColor).ToNumericsColorRaw();

if (material.HasProperty(IOR))
vi.ior = material.GetFloat(IOR);

if (material.HasProperty(TransmissionFactor))
vt.transmissionFactor = material.GetFloat(TransmissionFactor);
if (material.HasProperty(TransmissionTexture) && material.GetTexture(TransmissionTexture))
vt.transmissionTexture = exporter.ExportTextureInfo(material.GetTexture(TransmissionTexture), GLTFSceneExporter.TextureMapType.Custom_Unknown);

materialnode.AddExtension(KHR_materials_volume_Factory.EXTENSION_NAME, ve);
materialnode.AddExtension(KHR_materials_ior_Factory.EXTENSION_NAME, vi);
materialnode.AddExtension(KHR_materials_transmission_Factory.EXTENSION_NAME, vt);
}

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

var vir = new KHR_materials_iridescence();
if (material.HasProperty(IridescenceFactor))
vir.iridescenceFactor = material.GetFloat(IridescenceFactor);
if (material.HasProperty(IridescenceIor))
vir.iridescenceIor = material.GetFloat(IridescenceIor);
if (material.HasProperty(IridescenceThicknessMinimum))
vir.iridescenceThicknessMinimum = material.GetFloat(IridescenceThicknessMinimum);
if (material.HasProperty(IridescenceThicknessMaximum))
vir.iridescenceThicknessMaximum = material.GetFloat(IridescenceThicknessMaximum);
if (material.HasProperty(IridescenceTexture) && material.GetTexture(IridescenceTexture))
vir.iridescenceTexture = exporter.ExportTextureInfo(material.GetTexture(IridescenceTexture), GLTFSceneExporter.TextureMapType.Custom_Unknown);
if (material.HasProperty(IridescenceThicknessTexture) && material.GetTexture(IridescenceThicknessTexture))
vir.iridescenceThicknessTexture = exporter.ExportTextureInfo(material.GetTexture(IridescenceThicknessTexture), GLTFSceneExporter.TextureMapType.Custom_Unknown);

materialnode.AddExtension(KHR_materials_iridescence_Factory.EXTENSION_NAME, vir);
}

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

var vir = new KHR_materials_specular();
if (material.HasProperty(SpecularFactor))
vir.specularFactor = material.GetFloat(SpecularFactor);
if (material.HasProperty(SpecularColorFactor))
vir.specularColorFactor = material.GetColor(SpecularColorFactor).ToNumericsColorRaw();
if (material.HasProperty(SpecularTexture) && material.GetTexture(SpecularTexture))
vir.specularTexture = exporter.ExportTextureInfo(material.GetTexture(SpecularTexture), GLTFSceneExporter.TextureMapType.Custom_Unknown);
if (material.HasProperty(SpecularColorTexture) && material.GetTexture(SpecularColorTexture))
vir.specularColorTexture = exporter.ExportTextureInfo(material.GetTexture(SpecularColorTexture), GLTFSceneExporter.TextureMapType.Custom_Unknown);

materialnode.AddExtension(KHR_materials_specular_Factory.EXTENSION_NAME, vir);
}
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -127,49 +127,22 @@ private static void CollectSettings(GLTFSceneExporter exporter, GLTFRoot gltfroo
private static readonly int TransmissionFactor = Shader.PropertyToID("_TransmissionFactor");
private static readonly int TransmissionTexture = Shader.PropertyToID("_TransmissionTexture");

private static readonly int IridescenceFactor = Shader.PropertyToID("_IridescenceFactor");
private static readonly int IridescenceIor = Shader.PropertyToID("_IridescenceIor");
private static readonly int IridescenceThicknessMinimum = Shader.PropertyToID("_IridescenceThicknessMinimum");
private static readonly int IridescenceThicknessMaximum = Shader.PropertyToID("_IridescenceThicknessMaximum");
private static readonly int IridescenceTexture = Shader.PropertyToID("_IridescenceTexture");
private static readonly int IridescenceThicknessTexture = Shader.PropertyToID("_IridescenceThicknessTexture");


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

// check if any setting applies here
var settings = profiles?.FirstOrDefault(x => x && (x.targetAllMaterials || (x.targetMaterials != null && x.targetMaterials.Contains(material))));

if (!settings || !settings.enabled)
{
if (material.IsKeywordEnabled("_TRANSMISSION"))
{
exporter.DeclareExtensionUsage(KHR_materials_volume_Factory.EXTENSION_NAME, false);
exporter.DeclareExtensionUsage(KHR_materials_ior_Factory.EXTENSION_NAME, false);
exporter.DeclareExtensionUsage(KHR_materials_transmission_Factory.EXTENSION_NAME, false);

var ve = new KHR_materials_volume();
var vi = new KHR_materials_ior();
var vt = new KHR_materials_transmission();

if (material.HasProperty(ThicknessFactor))
ve.thicknessFactor = material.GetFloat(ThicknessFactor);
if (material.HasProperty(ThicknessTexture) && material.GetTexture(ThicknessTexture))
ve.thicknessTexture = exporter.ExportTextureInfo(material.GetTexture(ThicknessTexture), GLTFSceneExporter.TextureMapType.Custom_Unknown);
if (material.HasProperty(AttenuationDistance))
ve.attenuationDistance = material.GetFloat(AttenuationDistance);
if (material.HasProperty(AttenuationColor))
ve.attenuationColor = material.GetColor(AttenuationColor).ToNumericsColorRaw();

if (material.HasProperty(IOR))
vi.ior = material.GetFloat(IOR);

if (material.HasProperty(TransmissionFactor))
vt.transmissionFactor = material.GetFloat(TransmissionFactor);
if (material.HasProperty(TransmissionTexture) && material.GetTexture(TransmissionTexture))
vt.transmissionTexture = exporter.ExportTextureInfo(material.GetTexture(TransmissionTexture), GLTFSceneExporter.TextureMapType.Custom_Unknown);

materialnode.AddExtension(KHR_materials_volume_Factory.EXTENSION_NAME, ve);
materialnode.AddExtension(KHR_materials_ior_Factory.EXTENSION_NAME, vi);
materialnode.AddExtension(KHR_materials_transmission_Factory.EXTENSION_NAME, vt);
}

return;
}
if (!settings || !settings.enabled) return;

// override existing PBR for testing
if (settings.overridePBR)
Expand All @@ -187,32 +160,32 @@ private static void GLTFSceneExporterOnAfterMaterialExport(GLTFSceneExporter exp
{
settings.volume.ConvertData(exporter);
exporter.DeclareExtensionUsage(KHR_materials_volume_Factory.EXTENSION_NAME, false);
materialnode.AddExtension(KHR_materials_volume_Factory.EXTENSION_NAME, settings.volume.Clone(exporter.GetRoot()));
materialnode.Extensions[KHR_materials_volume_Factory.EXTENSION_NAME] = settings.volume.Clone(exporter.GetRoot());
}

if(settings.ior.enabled)
{
exporter.DeclareExtensionUsage(KHR_materials_ior_Factory.EXTENSION_NAME, false);
materialnode.AddExtension(KHR_materials_ior_Factory.EXTENSION_NAME, settings.ior.Clone(exporter.GetRoot()));
materialnode.Extensions[KHR_materials_ior_Factory.EXTENSION_NAME] = settings.ior.Clone(exporter.GetRoot());
}

if(settings.transmission.enabled)
{
settings.transmission.ConvertData(exporter);
exporter.DeclareExtensionUsage(KHR_materials_transmission_Factory.EXTENSION_NAME, false);
materialnode.AddExtension(KHR_materials_transmission_Factory.EXTENSION_NAME, settings.transmission.Clone(exporter.GetRoot()));
materialnode.Extensions[KHR_materials_transmission_Factory.EXTENSION_NAME] = settings.transmission.Clone(exporter.GetRoot());
}

if(settings.sheen.enabled)
{
exporter.DeclareExtensionUsage(KHR_MaterialsSheenExtension.ExtensionName, false);
materialnode.AddExtension(KHR_MaterialsSheenExtension.ExtensionName, settings.sheen.Clone(exporter.GetRoot()));
materialnode.Extensions[KHR_MaterialsSheenExtension.ExtensionName] = settings.sheen.Clone(exporter.GetRoot());
}

if(settings.clearcoat.enabled)
{
exporter.DeclareExtensionUsage(KHR_MaterialsClearcoatExtension.ExtensionName, false);
materialnode.AddExtension(KHR_MaterialsClearcoatExtension.ExtensionName, settings.clearcoat.Clone(exporter.GetRoot()));
materialnode.Extensions[KHR_MaterialsClearcoatExtension.ExtensionName] = settings.clearcoat.Clone(exporter.GetRoot());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion UnityGLTF/Assets/UnityGLTF/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "org.khronos.unitygltf",
"displayName": "UnityGLTF",
"version": "1.6.1-pre.3",
"version": "1.7.0-pre",
"unity": "2018.4",
"description": "Unity3D library for importing and exporting GLTF 2.0 assets. https://github.com/KhronosGroup/UnityGLTF",
"keywords": [
Expand Down

0 comments on commit e198de7

Please sign in to comment.