Skip to content

Commit

Permalink
add KHR_materials_specular, clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed May 27, 2022
1 parent 39f7f34 commit 47a8673
Show file tree
Hide file tree
Showing 11 changed files with 3,397 additions and 694 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ public IExtension Clone(GLTFRoot gltfRoot)
return new KHR_materials_emissive_strength(this, gltfRoot);
}

public override void Serialize(JsonWriter writer)
{
writer.WritePropertyName(KHR_materials_emissive_strength_Factory.EXTENSION_NAME);
writer.WriteStartObject();
writer.WritePropertyName(nameof(emissiveStrength));
writer.WriteValue(emissiveStrength);
writer.WriteEndObject();
}

public JProperty Serialize()
{
JTokenWriter writer = new JTokenWriter();
Serialize(writer);
return (JProperty)writer.Token;
var jo = new JObject();
JProperty jProperty = new JProperty(KHR_materials_volume_Factory.EXTENSION_NAME, jo);

if(emissiveStrength != 0) jo.Add(new JProperty(nameof(emissiveStrength), emissiveStrength));

return jProperty;
}

public float emissiveStrength = 1.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public JProperty Serialize()
var jo = new JObject();
JProperty jProperty = new JProperty(KHR_materials_iridescence_Factory.EXTENSION_NAME, jo);

jo.Add(new JProperty(nameof(iridescenceFactor), iridescenceFactor));
jo.Add(new JProperty(nameof(iridescenceIor), iridescenceIor));
jo.Add(new JProperty(nameof(iridescenceIor), iridescenceIor));
jo.Add(new JProperty(nameof(iridescenceThicknessMinimum), iridescenceThicknessMaximum));
if(iridescenceTexture != null) {
if (iridescenceFactor != 0) jo.Add(new JProperty(nameof(iridescenceFactor), iridescenceFactor));
if (iridescenceIor != 1.3f) jo.Add(new JProperty(nameof(iridescenceIor), iridescenceIor));
if (iridescenceThicknessMinimum != 100.0f) jo.Add(new JProperty(nameof(iridescenceThicknessMinimum), iridescenceThicknessMinimum));
if (iridescenceThicknessMaximum != 400.0f) jo.Add(new JProperty(nameof(iridescenceThicknessMaximum), iridescenceThicknessMaximum));
if (iridescenceTexture != null) {
jo.Add(new JProperty(nameof(iridescenceTexture),
new JObject(
new JProperty(TextureInfo.INDEX, iridescenceTexture.Index.Id),
Expand All @@ -36,7 +36,7 @@ public JProperty Serialize()
)
);
}
if(iridescenceThicknessTexture != null) {
if (iridescenceThicknessTexture != null) {
jo.Add(new JProperty(nameof(iridescenceThicknessTexture),
new JObject(
new JProperty(TextureInfo.INDEX, iridescenceThicknessTexture.Index.Id),
Expand Down Expand Up @@ -75,8 +75,8 @@ public override IExtension Deserialize(GLTFRoot root, JProperty extensionToken)
var extension = new KHR_materials_iridescence();
extension.iridescenceFactor = extensionToken.Value[nameof(KHR_materials_iridescence.iridescenceFactor)]?.Value<float>() ?? 0;
extension.iridescenceIor = extensionToken.Value[nameof(KHR_materials_iridescence.iridescenceIor)]?.Value<float>() ?? 1.3f;
extension.iridescenceThicknessMinimum = extensionToken.Value[nameof(KHR_materials_iridescence.iridescenceIor)]?.Value<float>() ?? 100f;
extension.iridescenceThicknessMaximum = extensionToken.Value[nameof(KHR_materials_iridescence.iridescenceIor)]?.Value<float>() ?? 400f;
extension.iridescenceThicknessMinimum = extensionToken.Value[nameof(KHR_materials_iridescence.iridescenceThicknessMinimum)]?.Value<float>() ?? 100f;
extension.iridescenceThicknessMaximum = extensionToken.Value[nameof(KHR_materials_iridescence.iridescenceThicknessMaximum)]?.Value<float>() ?? 400f;
extension.iridescenceTexture = extensionToken.Value[nameof(KHR_materials_iridescence.iridescenceTexture)]?.DeserializeAsTexture(root);
extension.iridescenceThicknessTexture = extensionToken.Value[nameof(KHR_materials_iridescence.iridescenceThicknessTexture)]?.DeserializeAsTexture(root);
return extension;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using GLTF.Extensions;
using Newtonsoft.Json.Linq;
using Color = GLTF.Math.Color;

namespace GLTF.Schema
{
// https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_specular/README.md
[Serializable]
public class KHR_materials_specular : IExtension
{
public float specularFactor = 1f;
public TextureInfo specularTexture; // A channel
public Color specularColorFactor = COLOR_DEFAULT;
public TextureInfo specularColorTexture; // RGB channel

public static readonly Color COLOR_DEFAULT = Color.White;

public JProperty Serialize()
{
var jo = new JObject();
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 (specularTexture != null) {
jo.Add(new JProperty(nameof(specularTexture),
new JObject(
new JProperty(TextureInfo.INDEX, specularTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, specularTexture.TexCoord)
)
)
);
}
if (specularColorTexture != null) {
jo.Add(new JProperty(nameof(specularColorTexture),
new JObject(
new JProperty(TextureInfo.INDEX, specularColorTexture.Index.Id),
new JProperty(TextureInfo.TEXCOORD, specularColorTexture.TexCoord)
)
)
);
}
return jProperty;
}

public IExtension Clone(GLTFRoot root)
{
return new KHR_materials_specular()
{
specularFactor = specularFactor, specularTexture = specularTexture,
specularColorFactor = specularColorFactor, specularColorTexture = specularColorTexture,
};
}
}

public class KHR_materials_specular_Factory : ExtensionFactory
{
public const string EXTENSION_NAME = "KHR_materials_specular";

public KHR_materials_specular_Factory()
{
ExtensionName = EXTENSION_NAME;
}

public override IExtension Deserialize(GLTFRoot root, JProperty extensionToken)
{
if (extensionToken != null)
{
var extension = new KHR_materials_specular();
extension.specularFactor = extensionToken.Value[nameof(KHR_materials_specular.specularFactor)]?.Value<float>() ?? 0;
extension.specularColorFactor = extensionToken.Value[nameof(KHR_materials_specular.specularColorFactor)]?.DeserializeAsColor() ?? Color.White;
extension.specularTexture = extensionToken.Value[nameof(KHR_materials_specular.specularTexture)]?.DeserializeAsTexture(root);
extension.specularColorTexture = extensionToken.Value[nameof(KHR_materials_specular.specularColorTexture)]?.DeserializeAsTexture(root);
return extension;
}

return null;
}
}
}

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 @@ -13,10 +13,9 @@ public class KHR_materials_transmission : IExtension

public JProperty Serialize()
{
var jo = new JObject(
new JProperty(nameof(transmissionFactor), transmissionFactor)
);
if(transmissionTexture != null)
var jo = new JObject();
if (transmissionFactor != 0) jo.Add(new JProperty(nameof(transmissionFactor), transmissionFactor));
if (transmissionTexture != null)
jo.Add(new JProperty(nameof(transmissionTexture),
new JObject(
new JProperty(TextureInfo.INDEX, transmissionTexture.Index.Id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public JProperty Serialize()
var jo = new JObject();
JProperty jProperty = new JProperty(KHR_materials_volume_Factory.EXTENSION_NAME, jo);

jo.Add(new JProperty(nameof(thicknessFactor), thicknessFactor));
if(!float.IsPositiveInfinity(attenuationDistance))
if (thicknessFactor != 0) jo.Add(new JProperty(nameof(thicknessFactor), thicknessFactor));
if (!float.IsPositiveInfinity(attenuationDistance))
jo.Add(new JProperty(nameof(attenuationDistance), attenuationDistance));
jo.Add(new JProperty(nameof(attenuationColor), new JArray(attenuationColor.R, attenuationColor.G, attenuationColor.B)));
if(thicknessTexture != null) {
if (attenuationColor != COLOR_DEFAULT) jo.Add(new JProperty(nameof(attenuationColor), new JArray(attenuationColor.R, attenuationColor.G, attenuationColor.B)));
if (thicknessTexture != null) {
jo.Add(new JProperty(nameof(thicknessTexture),
new JObject(
new JProperty(TextureInfo.INDEX, thicknessTexture.Index.Id),
Expand Down Expand Up @@ -61,10 +61,10 @@ public override IExtension Deserialize(GLTFRoot root, JProperty extensionToken)
if (extensionToken != null)
{
var extension = new KHR_materials_volume();
extension.thicknessFactor = extensionToken.Value[nameof(KHR_materials_volume.thicknessFactor)]?.Value<float>() ?? 0;
extension.attenuationColor = extensionToken.Value[nameof(KHR_materials_volume.attenuationColor)]?.DeserializeAsColor() ?? Color.White;
extension.thicknessFactor = extensionToken.Value[nameof(KHR_materials_volume.thicknessFactor)]?.Value<float>() ?? 0;
extension.attenuationColor = extensionToken.Value[nameof(KHR_materials_volume.attenuationColor)]?.DeserializeAsColor() ?? Color.White;
extension.attenuationDistance = extensionToken.Value[nameof(KHR_materials_volume.attenuationDistance)]?.Value<float>() ?? float.PositiveInfinity;
extension.thicknessTexture = extensionToken.Value[nameof(KHR_materials_volume.thicknessTexture)]?.DeserializeAsTexture(root);
extension.thicknessTexture = extensionToken.Value[nameof(KHR_materials_volume.thicknessTexture)]?.DeserializeAsTexture(root);
return extension;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class GLTFProperty
{ KHR_materials_volume_Factory.EXTENSION_NAME, new KHR_materials_volume_Factory() },
{ KHR_materials_ior_Factory.EXTENSION_NAME, new KHR_materials_ior_Factory() },
{ KHR_materials_iridescence_Factory.EXTENSION_NAME, new KHR_materials_iridescence_Factory() },
{ KHR_materials_specular_Factory.EXTENSION_NAME, new KHR_materials_specular_Factory() },
{ MSFT_LODExtensionFactory.EXTENSION_NAME, new MSFT_LODExtensionFactory() }
};

Expand Down
51 changes: 49 additions & 2 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,21 @@ protected virtual Task ConstructMaterialImageBuffers(GLTFMaterial def)
}
}

if (def.Extensions != null && def.Extensions.ContainsKey(KHR_materials_specular_Factory.EXTENSION_NAME))
{
var specularDef = (KHR_materials_specular) def.Extensions[KHR_materials_specular_Factory.EXTENSION_NAME];
if (specularDef.specularTexture != null)
{
var textureId = specularDef.specularTexture.Index;
tasks.Add(ConstructImageBuffer(textureId.Value, textureId.Id));
}
if (specularDef.specularColorTexture != null)
{
var textureId = specularDef.specularColorTexture.Index;
tasks.Add(ConstructImageBuffer(textureId.Value, textureId.Id));
}
}

return Task.WhenAll(tasks);
}

Expand Down Expand Up @@ -2264,6 +2279,7 @@ protected virtual async Task ConstructMaterial(GLTFMaterial def, int materialInd
if (transmissionMapper.TransmissionFactor > 0)
{
mapper.Material.renderQueue = 3000;
mapper.Material.EnableKeyword("_VOLUME_TRANSMISSION_ON");
}
}
}
Expand All @@ -2283,7 +2299,7 @@ protected virtual async Task ConstructMaterial(GLTFMaterial def, int materialInd
if (volumeMapper.ThicknessFactor > 0)
{
mapper.Material.renderQueue = 3000;
mapper.Material.EnableKeyword("VOLUME_TRANSMISSION_ON");
mapper.Material.EnableKeyword("_VOLUME_TRANSMISSION_ON");
}
}
}
Expand All @@ -2297,14 +2313,35 @@ protected virtual async Task ConstructMaterial(GLTFMaterial def, int materialInd
iridescenceMapper.IridescenceFactor = iridescence.iridescenceFactor;
iridescenceMapper.IridescenceIor = iridescence.iridescenceIor;
iridescenceMapper.IridescenceThicknessMinimum = iridescence.iridescenceThicknessMinimum;
iridescenceMapper.IridescenceThicknessMaximum = iridescence.iridescenceThicknessMaximum;
var td = await FromTextureInfo(iridescence.iridescenceTexture);
iridescenceMapper.IridescenceTexture = td.Texture;
var td2 = await FromTextureInfo(iridescence.iridescenceThicknessTexture);
iridescenceMapper.IridescenceThicknessTexture = td2.Texture;

if (iridescenceMapper.IridescenceFactor > 0)
{
mapper.Material.EnableKeyword("IRIDESCENCE_ON");
mapper.Material.EnableKeyword("_IRIDESCENCE_ON");
}
}
}

var specularMapper = mapper as ISpecularMap;
if (specularMapper != null)
{
var specular = GetSpecular(def);
if (specular != null)
{
specularMapper.SpecularFactor = specular.specularFactor;
specularMapper.SpecularColorFactor = specular.specularColorFactor.ToUnityColorLinear();
var td = await FromTextureInfo(specular.specularTexture);
specularMapper.SpecularTexture = td.Texture;
var td2 = await FromTextureInfo(specular.specularColorTexture);
specularMapper.SpecularColorTexture = td2.Texture;

if (specularMapper.SpecularFactor > 0)
{
mapper.Material.EnableKeyword("_SPECULAR_ON");
}
}
}
Expand Down Expand Up @@ -2649,6 +2686,16 @@ protected virtual KHR_materials_iridescence GetIridescence(GLTFMaterial def)
return null;
}

protected virtual KHR_materials_specular GetSpecular(GLTFMaterial def)
{
if (_gltfRoot.ExtensionsUsed != null && _gltfRoot.ExtensionsUsed.Contains(KHR_materials_specular_Factory.EXTENSION_NAME) &&
def.Extensions != null && def.Extensions.TryGetValue(KHR_materials_specular_Factory.EXTENSION_NAME, out var extension))
{
return (KHR_materials_specular) extension;
}
return null;
}

protected async Task YieldOnTimeoutAndThrowOnLowMemory()
{
if (_options.ThrowOnLowMemory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine.Rendering;
using UnityGLTF;

public class PBRGraphMap : IMetalRoughUniformMap, IVolumeMap, ITransmissionMap, IIORMap, IIridescenceMap
public class PBRGraphMap : IMetalRoughUniformMap, IVolumeMap, ITransmissionMap, IIORMap, IIridescenceMap, ISpecularMap
{
protected Material _material;

Expand Down Expand Up @@ -471,4 +471,34 @@ public Texture IridescenceThicknessTexture
_material.SetTexture("_IridescenceThicknessTexture", value);
}
}

public double SpecularFactor
{
get => _material.GetFloat("_SpecularFactor");
set => _material.SetFloat("_SpecularFactor", (float) value);
}

public Texture SpecularTexture
{
get => _material.GetTexture("_SpecularTexture");
set
{
_material.SetTexture("_SpecularTexture", value);
}
}

public Color SpecularColorFactor
{
get => _material.GetColor("_SpecularColorFactor");
set => _material.SetColor("_SpecularColorFactor", value);
}

public Texture SpecularColorTexture
{
get => _material.GetTexture("_SpecularColorTexture");
set
{
_material.SetTexture("_SpecularColorTexture", value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ public interface IIORMap : IMetalRoughUniformMap
double IOR { get; set; }
}

public interface ISpecularMap : IMetalRoughUniformMap
{
double SpecularFactor { get; set; }
Texture SpecularTexture { get; set; }
Color SpecularColorFactor { get; set; }
Texture SpecularColorTexture { get; set; }
}

public interface IIridescenceMap : IMetalRoughUniformMap
{
double IridescenceFactor { get; set; }
Expand Down

0 comments on commit 47a8673

Please sign in to comment.