Skip to content

Commit

Permalink
clean up extension registry, add KHR_materials_emissive_strength impo…
Browse files Browse the repository at this point in the history
…rt support (conversion factor wip - not fully working yet)
  • Loading branch information
hybridherbst committed Apr 27, 2022
1 parent f25e6aa commit 4eaa25d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ public class GLTFProperty
{ KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME, new KHR_materials_pbrSpecularGlossinessExtensionFactory() },
{ KHR_MaterialsUnlitExtensionFactory.EXTENSION_NAME, new KHR_MaterialsUnlitExtensionFactory() },
{ KHR_lights_punctualExtensionFactory.EXTENSION_NAME, new KHR_lights_punctualExtensionFactory() },
{ KHR_materials_emissive_strength_Factory.EXTENSION_NAME, new KHR_materials_emissive_strength_Factory() },
{ MSFT_LODExtensionFactory.EXTENSION_NAME, new MSFT_LODExtensionFactory() }
};

private static DefaultExtensionFactory _defaultExtensionFactory = new DefaultExtensionFactory();
private static KHR_materials_pbrSpecularGlossinessExtensionFactory _KHRExtensionFactory = new KHR_materials_pbrSpecularGlossinessExtensionFactory();
private static ExtTextureTransformExtensionFactory _TexTransformFactory = new ExtTextureTransformExtensionFactory();

public static bool IsExtensionRegistered(string extensionName)
{
Expand Down Expand Up @@ -192,19 +191,10 @@ private void SkipArray(JsonReader reader)

lock (_extensionRegistry)
{

if (_extensionRegistry.TryGetValue(extensionName, out extensionFactory))
{
extensionsCollection.Add(extensionName, extensionFactory.Deserialize(root, childAsJProperty));
}
else if (extensionName.Equals(KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME))
{
extensionsCollection.Add(extensionName, _KHRExtensionFactory.Deserialize(root, childAsJProperty));
}
else if (extensionName.Equals(ExtTextureTransformExtensionFactory.EXTENSION_NAME))
{
extensionsCollection.Add(extensionName, _TexTransformFactory.Deserialize(root, childAsJProperty));
}
else
{
extensionsCollection.Add(extensionName, _defaultExtensionFactory.Deserialize(root, childAsJProperty));
Expand Down
15 changes: 15 additions & 0 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,11 @@ protected virtual async Task ConstructMaterial(GLTFMaterial def, int materialInd
}

mapper.EmissiveFactor = def.EmissiveFactor.ToUnityColorRaw();
var emissiveExt = GetEmissiveStrength(def);
if (emissiveExt != null)
{
mapper.EmissiveFactor = def.EmissiveFactor.ToUnityColorRaw() * emissiveExt.emissiveStrength;
}

var vertColorMapper = mapper.Clone();
vertColorMapper.VertexColorsEnabled = true;
Expand Down Expand Up @@ -2434,6 +2439,16 @@ protected virtual ExtTextureTransformExtension GetTextureTransform(TextureInfo d
else return null;
}

protected virtual KHR_materials_emissive_strength GetEmissiveStrength(GLTFMaterial def)
{
if (_gltfRoot.ExtensionsUsed != null && _gltfRoot.ExtensionsUsed.Contains(KHR_materials_emissive_strength_Factory.EXTENSION_NAME) &&
def.Extensions != null && def.Extensions.TryGetValue(KHR_materials_emissive_strength_Factory.EXTENSION_NAME, out var extension))
{
return (KHR_materials_emissive_strength) extension;
}
else return null;
}

protected async Task YieldOnTimeoutAndThrowOnLowMemory()
{
if (_options.ThrowOnLowMemory)
Expand Down

0 comments on commit 4eaa25d

Please sign in to comment.