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

Export Unity animation clips and skeleton to GLTF #561

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
25 changes: 25 additions & 0 deletions GLTFSerialization/GLTFSerialization/Schema/GLTFId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,31 @@ public static MeshId Deserialize(GLTFRoot root, JsonReader reader)
}
}

public class AnimationId : GLTFId<GLTFAnimation>
{
public AnimationId()
{
}

public AnimationId(AnimationId id, GLTFRoot newRoot) : base(id, newRoot)
{
}

public override GLTFAnimation Value
{
get { return Root.Animations[Id]; }
}

public static AnimationId Deserialize(GLTFRoot root, JsonReader reader)
{
return new AnimationId
{
Id = reader.ReadAsInt32().Value,
Root = root
};
}
}

public class NodeId : GLTFId<Node>
{
public NodeId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public static void ConvertVector3CoordinateSpace(ref AttributeAccessor attribute
}

/// <summary>
/// Converts and copies based on the specified coordinate scale
/// Converts and copies based on the specified coordinate space
/// </summary>
/// <param name="array">The array to convert and copy</param>
/// <param name="coordinateSpaceCoordinateScale">The specified coordinate space</param>
Expand Down Expand Up @@ -453,7 +453,7 @@ public static void ConvertVector4CoordinateSpace(ref AttributeAccessor attribute
}

/// <summary>
/// Converts and copies based on the specified coordinate scale
/// Converts and copies based on the specified coordinate space
/// </summary>
/// <param name="array">The array to convert and copy</param>
/// <param name="coordinateSpaceCoordinateScale">The specified coordinate space</param>
Expand All @@ -472,6 +472,87 @@ public static Vector4[] ConvertVector4CoordinateSpaceAndCopy(Vector4[] array, GL

return returnArray;
}

/// <summary>
/// Converts and copies based on the specified coordinate space
/// </summary>
/// <param name="array">The array to convert and copy</param>
/// <returns>The copied and converted Quaternion array</returns>
public static Quaternion[] ConvertQuaternionCoordinateSpaceAndCopy(Quaternion[] array)
{
var returnArray = new Quaternion[array.Length];

for (var i = 0; i < array.Length; i++)
{
var unityQuat = array[i];
Vector3 fromAxisOfRotation = new Vector3(unityQuat.x, unityQuat.y, unityQuat.z);
float axisFlipScale = CoordinateSpaceConversionRequiresHandednessFlip ? -1.0f : 1.0f;
Vector3 toAxisOfRotation = axisFlipScale * Vector3.Scale(fromAxisOfRotation, CoordinateSpaceConversionScale.ToUnityVector3Raw());

returnArray[i] = new Quaternion(toAxisOfRotation.x, toAxisOfRotation.y, toAxisOfRotation.z, unityQuat.w);
}

return returnArray;
}

/// <summary>
/// Converts and copies based on the specified coordinate space
/// </summary>
/// <param name="array">The array to convert and copy</param>
/// <returns>The copied and converted Matrix4x4 array</returns>
public static Matrix4x4[] ConvertMatrix4x4CoordinateSpaceAndCopy(Matrix4x4[] array)
{
var returnArr = new Matrix4x4[array.Length];

for (int i = 0; i < array.Length; ++i)
{
Vector3 coordinateSpaceConversionScale = CoordinateSpaceConversionScale.ToUnityVector3Raw();
Matrix4x4 convert = Matrix4x4.Scale(coordinateSpaceConversionScale);
returnArr[i] = convert * array[i] * convert;
}

return returnArr;
}

/// <summary>
/// Extract the joint values
/// </summary>
/// <param name="array">The array to extract from and copy</param>
/// <returns>Copied Vector4 with joints</returns>
public static int[] ExtractJointAndCopy(BoneWeight[] array)
{
var returnArray = new int[array.Length * 4];

for (var i = 0; i < array.Length; i++)
{
returnArray[i * 4] = array[i].boneIndex0;
returnArray[i * 4 + 1] = array[i].boneIndex1;
returnArray[i * 4 + 2] = array[i].boneIndex2;
returnArray[i * 4 + 3] = array[i].boneIndex3;
}

return returnArray;
}

/// <summary>
/// Extract the weight values
/// </summary>
/// <param name="array">The array to extract from and copy</param>
/// <returns>Copied Vector4 with weights</returns>
public static Vector4[] ExtractWeightAndCopy(BoneWeight[] array)
{
var returnArray = new Vector4[array.Length];

for (var i = 0; i < array.Length; i++)
{
returnArray[i].x = array[i].weight0;
returnArray[i].y = array[i].weight1;
returnArray[i].z = array[i].weight2;
returnArray[i].w = array[i].weight3;
}

return returnArray;
}

/// <summary>
/// Rewinds the indicies into Unity coordinate space from glTF space
Expand Down
Loading