Skip to content

Commit

Permalink
Fix GLTFSerialization building after recent merges
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Dec 14, 2020
1 parent aa265e4 commit bac31f3
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
using GLTF.Math;
using GLTF.Schema;
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace GLTF.Schema
{
/// <summary>
/// glTF extension that defines the unlit material model
///
/// This extension defines an unlit shading model for use in glTF 2.0 materials, as an alternative to the Physically Based Rendering (PBR) shading models provided by the core specification.
///
/// Spec can be found here:
/// https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit
/// </summary>
public class KHR_materials_unlitExtension : IExtension
public class KHR_MaterialsUnlitExtension : GLTFProperty, IExtension
{
public KHR_materials_unlitExtension() { }
public KHR_MaterialsUnlitExtension() { }

public KHR_MaterialsUnlitExtension(KHR_MaterialsUnlitExtension ext, GLTFRoot root) : base(ext, root) { }

public IExtension Clone(GLTFRoot gltfRoot)
{
return new KHR_materials_unlitExtension();
return new KHR_MaterialsUnlitExtension(this, gltfRoot);
}

public JProperty Serialize()
override public void Serialize(JsonWriter writer)
{
JProperty jProperty =
new JProperty(KHR_materials_unlitExtensionFactory.EXTENSION_NAME,
new JObject()
);
writer.WritePropertyName(KHR_MaterialsUnlitExtensionFactory.EXTENSION_NAME);
writer.WriteStartObject();
base.Serialize(writer);
writer.WriteEndObject();
}

return jProperty;
public JProperty Serialize()
{
JTokenWriter writer = new JTokenWriter();
Serialize(writer);
return (JProperty)writer.Token;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

namespace GLTF.Schema
{
public class KHR_materials_unlitExtensionFactory : ExtensionFactory
{
public const string EXTENSION_NAME = "KHR_materials_unlit";
public class KHR_MaterialsUnlitExtensionFactory : ExtensionFactory
{
public const string EXTENSION_NAME = "KHR_materials_unlit";

public KHR_materials_unlitExtensionFactory()
{
ExtensionName = EXTENSION_NAME;
}
public KHR_MaterialsUnlitExtensionFactory()
{
ExtensionName = EXTENSION_NAME;
}

public override IExtension Deserialize(GLTFRoot root, JProperty extensionToken)
{
return new KHR_materials_unlitExtension();
}
}
public override IExtension Deserialize(GLTFRoot root, JProperty extensionToken)
{
return new KHR_MaterialsUnlitExtension();
}
}
}
118 changes: 104 additions & 14 deletions GLTFSerialization/GLTFSerialization/GLTFHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.RegularExpressions;
using GLTF.Schema;
using GLTF.Math;
using System.Linq;

namespace GLTF
{
Expand Down Expand Up @@ -254,41 +255,41 @@ public static void BuildMeshAttributes(ref Dictionary<string, AttributeAccessor>
attributeAccessor.AccessorId.Value.AsNormalArray(ref resultArray, bufferViewCache, offset);
attributeAccessor.AccessorContent = resultArray;
}
if (attributes.ContainsKey(SemanticProperties.TexCoord(0)))
if (attributes.ContainsKey(SemanticProperties.TexCoord[0]))
{
var attributeAccessor = attributes[SemanticProperties.TexCoord(0)];
var attributeAccessor = attributes[SemanticProperties.TexCoord[0]];
NumericArray resultArray = attributeAccessor.AccessorContent;
uint offset = LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
attributeAccessor.AccessorId.Value.AsTexcoordArray(ref resultArray, bufferViewCache, offset);
attributeAccessor.AccessorContent = resultArray;
}
if (attributes.ContainsKey(SemanticProperties.TexCoord(1)))
if (attributes.ContainsKey(SemanticProperties.TexCoord[1]))
{
var attributeAccessor = attributes[SemanticProperties.TexCoord(1)];
var attributeAccessor = attributes[SemanticProperties.TexCoord[1]];
NumericArray resultArray = attributeAccessor.AccessorContent;
uint offset = LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
attributeAccessor.AccessorId.Value.AsTexcoordArray(ref resultArray, bufferViewCache, offset);
attributeAccessor.AccessorContent = resultArray;
}
if (attributes.ContainsKey(SemanticProperties.TexCoord(2)))
if (attributes.ContainsKey(SemanticProperties.TexCoord[2]))
{
var attributeAccessor = attributes[SemanticProperties.TexCoord(2)];
var attributeAccessor = attributes[SemanticProperties.TexCoord[2]];
NumericArray resultArray = attributeAccessor.AccessorContent;
uint offset = LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
attributeAccessor.AccessorId.Value.AsTexcoordArray(ref resultArray, bufferViewCache, offset);
attributeAccessor.AccessorContent = resultArray;
}
if (attributes.ContainsKey(SemanticProperties.TexCoord(3)))
if (attributes.ContainsKey(SemanticProperties.TexCoord[3]))
{
var attributeAccessor = attributes[SemanticProperties.TexCoord(3)];
var attributeAccessor = attributes[SemanticProperties.TexCoord[3]];
NumericArray resultArray = attributeAccessor.AccessorContent;
uint offset = LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
attributeAccessor.AccessorId.Value.AsTexcoordArray(ref resultArray, bufferViewCache, offset);
attributeAccessor.AccessorContent = resultArray;
}
if (attributes.ContainsKey(SemanticProperties.Color(0)))
if (attributes.ContainsKey(SemanticProperties.Color[0]))
{
var attributeAccessor = attributes[SemanticProperties.Color(0)];
var attributeAccessor = attributes[SemanticProperties.Color[0]];
NumericArray resultArray = attributeAccessor.AccessorContent;
uint offset = LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
attributeAccessor.AccessorId.Value.AsColorArray(ref resultArray, bufferViewCache, offset);
Expand All @@ -302,24 +303,48 @@ public static void BuildMeshAttributes(ref Dictionary<string, AttributeAccessor>
attributeAccessor.AccessorId.Value.AsTangentArray(ref resultArray, bufferViewCache, offset);
attributeAccessor.AccessorContent = resultArray;
}
if (attributes.ContainsKey(SemanticProperties.Weight(0)))
if (attributes.ContainsKey(SemanticProperties.Weight[0]))
{
var attributeAccessor = attributes[SemanticProperties.Weight(0)];
var attributeAccessor = attributes[SemanticProperties.Weight[0]];
NumericArray resultArray = attributeAccessor.AccessorContent;
uint offset = LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
attributeAccessor.AccessorId.Value.AsVector4Array(ref resultArray, bufferViewCache, offset);
attributeAccessor.AccessorContent = resultArray;
}
if (attributes.ContainsKey(SemanticProperties.Joint(0)))
if (attributes.ContainsKey(SemanticProperties.Joint[0]))
{
var attributeAccessor = attributes[SemanticProperties.Joint(0)];
var attributeAccessor = attributes[SemanticProperties.Joint[0]];
NumericArray resultArray = attributeAccessor.AccessorContent;
uint offset = LoadBufferView(attributeAccessor, out byte[] bufferViewCache);
attributeAccessor.AccessorId.Value.AsVector4Array(ref resultArray, bufferViewCache, offset);
attributeAccessor.AccessorContent = resultArray;
}
}

public static void BuildTargetAttributes(ref Dictionary<string, AttributeAccessor> attributes)
{
foreach (var kvp in attributes)
{
var attributeAccessor = kvp.Value;
NumericArray resultArray = attributeAccessor.AccessorContent;
byte[] bufferViewCache;
uint offset = LoadBufferView(attributeAccessor, out bufferViewCache);

switch (kvp.Key)
{
case SemanticProperties.POSITION:
case SemanticProperties.NORMAL:
case SemanticProperties.TANGENT:
attributeAccessor.AccessorId.Value.AsVector3Array(ref resultArray, bufferViewCache, offset);
break;
default:
throw new System.Exception($"Unrecognized morph target attribute {kvp.Key}");
}

attributeAccessor.AccessorContent = resultArray;
}
}

public static void BuildBindPoseSamplers(ref AttributeAccessor attributeAccessor)
{
NumericArray resultArray = attributeAccessor.AccessorContent;
Expand Down Expand Up @@ -907,5 +932,70 @@ private static string UpdateCanonicalPath(string oldPath, string newCanonicalPat
string fileName = Path.GetFileName(oldPath);
return newCanonicalPath + Path.DirectorySeparatorChar + fileName;
}

public static NodeId FindCommonAncestor(IEnumerable<NodeId> nodes)
{
// build parentage
GLTFRoot root = nodes.First().Root;
Dictionary<int, int> childToParent = new Dictionary<int, int>(root.Nodes.Count);
for (int i = 0; i < root.Nodes.Count; i++)
{
if (root.Nodes[i].Children == null)
{
continue;
}

foreach (NodeId child in root.Nodes[i].Children)
{
childToParent[child.Id] = i;
}
}

// scan for common ancestor
int? commonAncestorIndex = nodes
.Select(n => n.Id)
.Aggregate((int?)null, (elder, node) => FindCommonAncestor(elder, node));

return commonAncestorIndex != null ? new NodeId() { Id = commonAncestorIndex.Value, Root = root } : null;

int? FindCommonAncestor(int? a, int? b)
{
// trivial cases
if (a == null && b == null)
{
return null;
}
else if (a != null)
{
return a;
}
else if (b != null)
{
return b;
}
else if (AncestorOf(a.Value, b.Value))
{
return a;
}
else
{
return FindCommonAncestor(childToParent[a.Value], b.Value);
}
}

bool AncestorOf(int ancestor, int descendant)
{
while (childToParent.ContainsKey(descendant))
{
if (childToParent[descendant] == ancestor)
{
return true;
}
descendant = childToParent[descendant];
}

return false;
}
}
}
}
2 changes: 1 addition & 1 deletion GLTFSerialization/GLTFSerialization/Schema/GLTFProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class GLTFProperty
{
{ ExtTextureTransformExtensionFactory.EXTENSION_NAME, new ExtTextureTransformExtensionFactory() },
{ KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME, new KHR_materials_pbrSpecularGlossinessExtensionFactory() },
{ KHR_materials_unlitExtensionFactory.EXTENSION_NAME, new KHR_materials_unlitExtensionFactory() },
{ KHR_MaterialsUnlitExtensionFactory.EXTENSION_NAME, new KHR_MaterialsUnlitExtensionFactory() },
{ KHR_lights_punctualExtensionFactory.EXTENSION_NAME, new KHR_lights_punctualExtensionFactory() },
{ MSFT_LODExtensionFactory.EXTENSION_NAME, new MSFT_LODExtensionFactory() }
};
Expand Down
67 changes: 21 additions & 46 deletions GLTFSerialization/GLTFSerialization/Schema/MeshPrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,52 +319,27 @@ public override void Serialize(JsonWriter writer)

public static class SemanticProperties
{
public static readonly string POSITION = "POSITION";
public static readonly string NORMAL = "NORMAL";
public static readonly string JOINT = "JOINT";
public static readonly string WEIGHT = "WEIGHT";
public static readonly string TANGENT = "TANGENT";
public static readonly string INDICES = "INDICIES";

/// <summary>
/// Return the semantic property for the uv buffer.
/// </summary>
/// <param name="index">The index of the uv buffer</param>
/// <returns>The semantic property for the uv buffer</returns>
public static string TexCoord(int index)
{
return "TEXCOORD_" + index;
}

/// <summary>
/// Return the semantic property for the color buffer.
/// </summary>
/// <param name="index">The index of the color buffer</param>
/// <returns>The semantic property for the color buffer</returns>
public static string Color(int index)
{
return "COLOR_" + index;
}

/// <summary>
/// Return the semantic property for the bone weights buffer.
/// </summary>
/// <param name="index">The index of the bone weights buffer</param>
/// <returns>The semantic property for the bone weights buffer</returns>
public static string Weight(int index)
{
return "WEIGHTS_" + index;
}

/// <summary>
/// Return the semantic property for the joints buffer.
/// </summary>
/// <param name="index">The index of the joints buffer</param>
/// <returns>The semantic property for the joints buffer</returns>
public static string Joint(int index)
{
return "JOINTS_" + index;
}
public const string POSITION = "POSITION";
public const string NORMAL = "NORMAL";
public const string JOINT = "JOINT";
public const string WEIGHT = "WEIGHT";
public const string TANGENT = "TANGENT";
public const string INDICES = "INDICIES";

public const string TEXCOORD_0 = "TEXCOORD_0";
public const string TEXCOORD_1 = "TEXCOORD_1";
public const string TEXCOORD_2 = "TEXCOORD_2";
public const string TEXCOORD_3 = "TEXCOORD_3";
public static readonly string[] TexCoord = { TEXCOORD_0, TEXCOORD_1, TEXCOORD_2, TEXCOORD_3 };

public const string COLOR_0 = "COLOR_0";
public static readonly string[] Color = { COLOR_0 };

public const string WEIGHTS_0 = "WEIGHTS_0";
public static readonly string[] Weight = { WEIGHTS_0 };

public const string JOINTS_0 = "JOINTS_0";
public static readonly string[] Joint = { JOINTS_0 };

/// <summary>
/// Parse out the index of a given semantic property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ public static GLTF.Math.Color ToNumericsColorRaw(this UnityEngine.Color color)
return new GLTF.Math.Color(c.r, c.g, c.b, c.a);
}

public static GLTF.Math.Color ToNumericsColorLinear(this UnityEngine.Color color)
{
var lc = color.linear;
return new GLTF.Math.Color(lc.r, lc.g, lc.b, lc.a);
}

public static UnityEngine.Color[] ToUnityColorRaw(this GLTF.Math.Color[] inColorArr)
{
UnityEngine.Color[] outColorArr = new UnityEngine.Color[inColorArr.Length];
Expand Down

0 comments on commit bac31f3

Please sign in to comment.