Skip to content

Commit

Permalink
Merge pull request #2 from sketchfab/feature/sketchfab-plugin-release…
Browse files Browse the repository at this point in the history
…_D3D-3973

Feature/sketchfab plugin release d3 d 3973
  • Loading branch information
AurL authored Mar 20, 2018
2 parents d75fc63 + c1b9ee7 commit ea82f31
Show file tree
Hide file tree
Showing 56 changed files with 10,372 additions and 79 deletions.
111 changes: 111 additions & 0 deletions GLTFSerialization/GLTFSerialization/Extensions/GLTFJsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,59 @@ public static List<double> ReadDoubleList(this JsonReader reader)
return list;
}

public static List<T> ReadObjectList<T>(this JsonReader reader, Func<T> deserializerFunc)
{
if (reader.Read() && reader.TokenType != JsonToken.StartArray)
{
throw new Exception(string.Format("Invalid array at: {0}", reader.Path));
}

var list = new List<T>();

while (reader.Read() && reader.TokenType != JsonToken.EndArray)
{
list.Add(deserializerFunc());

// deserializerFunc can advance to EndArray. We need to check for this case as well.
if (reader.TokenType == JsonToken.EndArray)
{
break;
}
}

return list;
}

public static List<T> ReadDictList<T>(this JsonReader reader, Func<T> deserializerFunc)
{
if (reader.Read() && reader.TokenType != JsonToken.StartArray)
{
throw new Exception(string.Format("Invalid array at: {0}", reader.Path));
}

var list = new List<T>();
JsonReader re = reader;
bool dodo = true;
while (reader.Read() && reader.TokenType != JsonToken.EndArray)
{
list.Add(deserializerFunc());
if(dodo)
{
reader = re;
dodo = false;
}

// deserializerFunc can advance to EndArray. We need to check for this case as well.
if (reader.TokenType == JsonToken.EndArray)
{
break;
}
}


return list;
}

public static List<T> ReadList<T>(this JsonReader reader, Func<T> deserializerFunc)
{
if (reader.Read() && reader.TokenType != JsonToken.StartArray)
Expand Down Expand Up @@ -297,6 +350,23 @@ public static Quaternion ReadAsQuaternion(this JsonReader reader)
return quat;
}

public static Dictionary<string, T> ReadAsObject<T>(this JsonReader reader, Func<T> deserializerFunc)
{
if (reader.TokenType != JsonToken.StartObject)
{
throw new Exception(string.Format("Dictionary must be an object at: {0}", reader.Path));
}

var dict = new Dictionary<string, T>();

while (reader.Read() && reader.TokenType != JsonToken.EndObject)
{
dict.Add(reader.Value.ToString(), deserializerFunc());
}

return dict;
}

public static Dictionary<string, T> ReadAsDictionary<T>(this JsonReader reader, Func<T> deserializerFunc)
{
if (reader.Read() && reader.TokenType != JsonToken.StartObject)
Expand Down Expand Up @@ -366,5 +436,46 @@ public static T ReadStringEnum<T>(this JsonReader reader)
{
return (T) Enum.Parse(typeof(T), reader.ReadAsString());
}

public static JArray asJSONArray(this GLTF.Math.Vector2 color)
{
JArray array = new JArray();
array.Add(color.X);
array.Add(color.Y);

return array;
}

public static JArray asJSONArray(this GLTF.Math.Vector3 vector)
{
JArray array = new JArray();
array.Add(vector.X);
array.Add(vector.Y);
array.Add(vector.Z);

return array;
}

public static JArray asJSONArray(this GLTF.Math.Vector4 vector)
{
JArray array = new JArray();
array.Add(vector.X);
array.Add(vector.Y);
array.Add(vector.Z);
array.Add(vector.W);

return array;
}

public static JArray asJSONArray(this GLTF.Math.Color color)
{
JArray array = new JArray();
array.Add(color.R);
array.Add(color.G);
array.Add(color.B);
array.Add(color.A);

return array;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using GLTF.Schema;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using GLTF.Extensions;

namespace GLTF.Schema
{
Expand Down Expand Up @@ -66,26 +67,19 @@ public KHR_materials_pbrSpecularGlossinessExtension(Color diffuseFactor, Texture

public JProperty Serialize()
{
JProperty jProperty =
new JProperty(KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME,
new JObject(
new JProperty(KHR_materials_pbrSpecularGlossinessExtensionFactory.DIFFUSE_FACTOR, DiffuseFactor),
new JProperty(KHR_materials_pbrSpecularGlossinessExtensionFactory.DIFFUSE_TEXTURE,
new JObject(
new JProperty(TextureInfo.INDEX, DiffuseTexture.Index.Id)
)
),
new JProperty(KHR_materials_pbrSpecularGlossinessExtensionFactory.SPECULAR_FACTOR, SpecularFactor),
new JProperty(KHR_materials_pbrSpecularGlossinessExtensionFactory.GLOSSINESS_FACTOR, GlossinessFactor),
new JProperty(KHR_materials_pbrSpecularGlossinessExtensionFactory.SPECULAR_GLOSSINESS_TEXTURE,
new JObject(
new JProperty(TextureInfo.INDEX, SpecularGlossinessTexture.Index.Id)
)
)
)
);
JObject specularGloss = new JObject();
if (DiffuseFactor != Color.White)
specularGloss.Add(KHR_materials_pbrSpecularGlossinessExtensionFactory.DIFFUSE_FACTOR, DiffuseFactor.asJSONArray());
if (DiffuseTexture != null)
specularGloss.Add(KHR_materials_pbrSpecularGlossinessExtensionFactory.DIFFUSE_TEXTURE, new JObject(new JProperty(TextureInfo.INDEX, DiffuseTexture.Index.Id)));
if(SpecularFactor != SPEC_FACTOR_DEFAULT)
specularGloss.Add(KHR_materials_pbrSpecularGlossinessExtensionFactory.SPECULAR_FACTOR, SpecularFactor.asJSONArray());
if(GlossinessFactor != GLOSS_FACTOR_DEFAULT)
specularGloss.Add(KHR_materials_pbrSpecularGlossinessExtensionFactory.GLOSSINESS_FACTOR, GlossinessFactor);
if(SpecularGlossinessTexture != null)
specularGloss.Add(KHR_materials_pbrSpecularGlossinessExtensionFactory.SPECULAR_GLOSSINESS_TEXTURE, new JObject(new JProperty(TextureInfo.INDEX, SpecularGlossinessTexture.Index.Id)));

return jProperty;
return new JProperty(KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME, specularGloss);
}
}
}
24 changes: 24 additions & 0 deletions GLTFSerialization/GLTFSerialization/GLTFHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,29 @@ public static void BuildMeshAttributes(ref Dictionary<string, AttributeAccessor>
attributeBuilder.AccessorContent = resultArray;
}
}

public static Math.Vector4[] ParseRotationKeyframes(Accessor accessor, byte[] bufferData)
{
NumericArray array = new NumericArray();
return accessor.AsVector4Array(ref array, bufferData, true);
}

public static Math.Vector3[] ParseVector3Keyframes(Accessor accessor, byte[] bufferData)
{
NumericArray array = new NumericArray();
return accessor.AsVector3Array(ref array, bufferData, false);
}

public static float[] ParseKeyframeTimes(Accessor accessor, byte[] bufferData)
{
NumericArray array = new NumericArray();
return accessor.AsFloatArray(ref array, bufferData);
}

public static float[] ParseMorphWeights(Accessor accessor, byte[] bufferData)
{
NumericArray array = new NumericArray();
return accessor.AsFloatArray(ref array, bufferData);
}
}
}
23 changes: 23 additions & 0 deletions GLTFSerialization/GLTFSerialization/Math/Matrix4x4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ public Matrix4x4(Matrix4x4 other)
Array.Copy(other.mat, 0, mat, 0, 16);
}

public Matrix4x4(Vector4 column1, Vector4 column2, Vector4 column3, Vector4 column4)
{
M11 = column1.X;
M21 = column1.Y;
M31 = column1.Z;
M41 = column1.W;

M12 = column2.X;
M22 = column2.Y;
M32 = column2.Z;
M42 = column2.W;

M13 = column3.X;
M23 = column3.Y;
M33 = column3.Z;
M43 = column3.W;

M14 = column4.X;
M24 = column4.Y;
M34 = column4.Z;
M44 = column4.W;
}

public override bool Equals(object obj)
{
if (!(obj is Matrix4x4))
Expand Down
81 changes: 79 additions & 2 deletions GLTFSerialization/GLTFSerialization/Schema/Accessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,32 @@ public int[] AsIntArray(ref NumericArray contents, byte[] bufferData)
return arr;
}

public float[] AsFloatArray(ref NumericArray contents, byte[] bufferData)
{
if (contents.AsFloats != null) return contents.AsFloats;

if (Type != GLTFAccessorAttributeType.SCALAR) return null;

var arr = new float[Count];
var totalByteOffset = BufferView.Value.ByteOffset + ByteOffset;

int componentSize;
float maxValue;
Func<byte[], int, int> getDiscreteElement;
Func<byte[], int, float> getContinuousElement;
GetTypeDetails(ComponentType, out componentSize, out maxValue, out getDiscreteElement, out getContinuousElement);

var stride = BufferView.Value.ByteStride > 0 ? BufferView.Value.ByteStride : componentSize;

for (var idx = 0; idx < Count; idx++)
{
arr[idx] = getContinuousElement(bufferData, totalByteOffset + idx * stride);
}

contents.AsFloats = arr;
return arr;
}

public Vector2[] AsVector2Array(ref NumericArray contents, byte[] bufferData, bool normalizeIntValues = true)
{
if (contents.AsVec2s != null) return contents.AsVec2s;
Expand Down Expand Up @@ -448,6 +474,55 @@ public Vector4[] AsVector4Array(ref NumericArray contents, byte[] bufferData, bo
return arr;
}

public Matrix4x4[] AsMatrixArray(ref NumericArray contents, byte[] bufferData)
{
if (contents.AsMatrix4x4s != null) return contents.AsMatrix4x4s;

if (Type != GLTFAccessorAttributeType.MAT4) return null;

var arr = new Matrix4x4[Count];
var totalByteOffset = BufferView.Value.ByteOffset + ByteOffset;

int componentSize;
float maxValue;
Func<byte[], int, int> getDiscreteElement;
Func<byte[], int, float> getContinuousElement;
GetTypeDetails(ComponentType, out componentSize, out maxValue, out getDiscreteElement, out getContinuousElement);

var stride = BufferView.Value.ByteStride > 0 ? BufferView.Value.ByteStride : componentSize * 16;
for (var i = 0; i < Count; i++)
{
int startElement = totalByteOffset + i * stride;
if (ComponentType == GLTFComponentType.Float)
{
arr[i] = new Matrix4x4 (
getContinuousElement(bufferData, startElement + componentSize * 0),
getContinuousElement(bufferData, startElement + componentSize * 4),
getContinuousElement(bufferData, startElement + componentSize * 8),
getContinuousElement(bufferData, startElement + componentSize * 12),

getContinuousElement(bufferData, startElement + componentSize * 1),
getContinuousElement(bufferData, startElement + componentSize * 5),
getContinuousElement(bufferData, startElement + componentSize * 9),
getContinuousElement(bufferData, startElement + componentSize * 13),

getContinuousElement(bufferData, startElement + componentSize * 2),
getContinuousElement(bufferData, startElement + componentSize * 6),
getContinuousElement(bufferData, startElement + componentSize * 10),
getContinuousElement(bufferData, startElement + componentSize * 14),

getContinuousElement(bufferData, startElement + componentSize * 3),
getContinuousElement(bufferData, startElement + componentSize * 7),
getContinuousElement(bufferData, startElement + componentSize * 11),
getContinuousElement(bufferData, startElement + componentSize * 15)
);
}
}

contents.AsMatrix4x4s = arr;
return arr;
}

public Color[] AsColorArray(ref NumericArray contents, byte[] bufferData)
{
if (contents.AsColors != null) return contents.AsColors;
Expand Down Expand Up @@ -499,11 +574,11 @@ public Vector2[] AsTexcoordArray(ref NumericArray contents, byte[] bufferData)
if (contents.AsTexcoords != null) return contents.AsTexcoords;

var arr = AsVector2Array(ref contents, bufferData);
for (var i = 0; i < arr.Length; i++)
/* for (var i = 0; i < arr.Length; i++)
{
arr[i].Y = 1 - arr[i].Y;
}

*/
contents.AsTexcoords = arr;
contents.AsVec2s = null;

Expand Down Expand Up @@ -602,10 +677,12 @@ public enum GLTFAccessorAttributeType
public struct NumericArray
{
public int[] AsInts;
public float[] AsFloats;
public Vector2[] AsVec2s;
public Vector3[] AsVec3s;
public Vector4[] AsVec4s;
public Color[] AsColors;
public Matrix4x4[] AsMatrix4x4s;
public Vector2[] AsTexcoords;
public Vector3[] AsVertices;
public Vector3[] AsNormals;
Expand Down
8 changes: 7 additions & 1 deletion GLTFSerialization/GLTFSerialization/Schema/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public class Animation : GLTFChildOfRootProperty
/// </summary>
public List<AnimationSampler> Samplers;

public Animation()
{
Channels = new List<AnimationChannel>();
Samplers = new List<AnimationSampler>();
}

public static Animation Deserialize(GLTFRoot root, JsonReader reader)
{
var animation = new Animation();
Expand All @@ -33,7 +39,7 @@ public static Animation Deserialize(GLTFRoot root, JsonReader reader)
switch (curProp)
{
case "channels":
animation.Channels = reader.ReadList(() => AnimationChannel.Deserialize(root, reader));
animation.Channels = reader.ReadList(() => AnimationChannel.Deserialize(root, reader, animation));
break;
case "samplers":
animation.Samplers = reader.ReadList(() => AnimationSampler.Deserialize(root, reader));
Expand Down
Loading

0 comments on commit ea82f31

Please sign in to comment.