Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension FB_materials_modmap to support the export of lightmap #564

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using GLTF.Math;
using GLTF.Schema;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace GLTF.Schema
{
public class FB_materials_modmapExtension : IExtension
{
/// <summary>
/// A 3-component vector describing the attenuation of the modmap before baseColor application.
/// </summary>
public Vector3 ModmapFactor = MODMAP_FACTOR_DEFAULT;
public static readonly Vector3 MODMAP_FACTOR_DEFAULT = new Vector3(1.0f, 1.0f, 1.0f);

/// <summary>
/// The modmap texture.
/// This texture contains RGB components of the modmap data of the material in sRGB color space.
/// </summary>
public TextureInfo ModmapTexture = MODMAP_TEXTURE_DEFAULT;
public static readonly TextureInfo MODMAP_TEXTURE_DEFAULT = new TextureInfo();


public FB_materials_modmapExtension(
Vector3 modmapFactor,
TextureInfo modmapTexture)
{
ModmapFactor = modmapFactor;
ModmapTexture = modmapTexture;
}

public IExtension Clone(GLTFRoot gltfRoot)
{
return new FB_materials_modmapExtension(
ModmapFactor,
new TextureInfo(
ModmapTexture,
gltfRoot
));
}

public JProperty Serialize()
{
JObject ext = new JObject();

if (ModmapFactor != MODMAP_FACTOR_DEFAULT)
{
ext.Add(new JProperty(
FB_materials_modmapExtensionFactory.MODMAP_FACTOR,
new JArray(ModmapFactor.X, ModmapFactor.Y, ModmapFactor.Z)
));
}

if (ModmapTexture != MODMAP_TEXTURE_DEFAULT)
{
ext.Add(new JProperty(
FB_materials_modmapExtensionFactory.MODMAP_TEXTURE,
new JObject(
new JProperty(TextureInfo.INDEX, ModmapTexture.Index.Id)
)
)
);
}

return new JProperty(FB_materials_modmapExtensionFactory.EXTENSION_NAME, ext);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Newtonsoft.Json.Linq;
using GLTF.Extensions;
using GLTF.Math;

namespace GLTF.Schema
{
public class FB_materials_modmapExtensionFactory : ExtensionFactory
{
public const string EXTENSION_NAME = "FB_materials_modmap";

public const string MODMAP_FACTOR = "modmapFactor";
public const string MODMAP_TEXTURE = "modmapTexture";


public FB_materials_modmapExtensionFactory()
{
ExtensionName = EXTENSION_NAME;
}

public override IExtension Deserialize(GLTFRoot root, JProperty extensionToken)
{
Vector3 modmapFactor = FB_materials_modmapExtension.MODMAP_FACTOR_DEFAULT;
TextureInfo modmapTexture = FB_materials_modmapExtension.MODMAP_TEXTURE_DEFAULT;

if (extensionToken != null)
{
JToken modmapFactorToken = extensionToken.Value[MODMAP_FACTOR];
modmapFactor = modmapFactorToken != null ? modmapFactorToken.DeserializeAsVector3() : modmapFactor;

JToken modmapTextureToken = extensionToken.Value[MODMAP_TEXTURE];
modmapTexture = modmapTextureToken != null ? modmapTextureToken.DeserializeAsTexture(root) : modmapTexture;
}

return new FB_materials_modmapExtension(modmapFactor, modmapTexture);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ public static TextureInfo DeserializeAsTexture(this JToken token, GLTFRoot root)
return textureInfo;
}

public static bool DeserializeAsBool(this JToken token)
{
if (token != null)
{
JValue boolValue = token as JValue;
if (boolValue == null)
{
throw new Exception("JToken used for bool deserialization was not a JValue. It was a " + token.Type.ToString());
}

return (bool)boolValue;
}

return false;
}

public static int DeserializeAsInt(this JToken token)
{
if (token != null)
Expand All @@ -130,6 +146,22 @@ public static int DeserializeAsInt(this JToken token)
return 0;
}

public static float DeserializeAsFloat(this JToken token)
{
if (token != null)
{
JValue floatValue = token as JValue;
if (floatValue == null)
{
throw new Exception("JToken used for float deserialization was not a JValue. It was a " + token.Type.ToString());
}

return (float)floatValue;
}

return 0.0f;
}

public static double DeserializeAsDouble(this JToken token)
{
if (token != null)
Expand All @@ -146,6 +178,22 @@ public static double DeserializeAsDouble(this JToken token)
return 0d;
}

public static string DeserializeAsString(this JToken token)
{
if (token != null)
{
JValue stringValue = token as JValue;
if (stringValue == null)
{
throw new Exception("JToken used for string deserialization was not a JValue. It was a " + token.Type.ToString());
}

return (string)stringValue;
}

return "";
}

public static Color ReadAsRGBAColor(this JsonReader reader)
{
if (reader.Read() && reader.TokenType != JsonToken.StartArray)
Expand Down
Loading