Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Fixing gltf serialization #676

Merged
merged 3 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using UnityEngine;

namespace XRTK.Utilities.Gltf.Schema
{
/// <summary>
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/accessor.schema.json
/// </summary>
[Serializable]
public class GltfAccessor : GltfChildOfRootProperty
public class GltfAccessor : GltfChildOfRootProperty, ISerializationCallbackReceiver
{
/// <summary>
/// The index of the bufferView.
Expand All @@ -31,7 +32,10 @@ public class GltfAccessor : GltfChildOfRootProperty
/// 5125 (UNSIGNED_INT) is only allowed when the accessor contains indices
/// i.e., the accessor is only referenced by `primitive.indices`.
/// </summary>
public GltfComponentType componentType;
public GltfComponentType ComponentType { get; set; }

[SerializeField]
private string componentType = string.Empty;

/// <summary>
/// Specifies whether integer data values should be normalized
Expand Down Expand Up @@ -100,5 +104,26 @@ public class GltfAccessor : GltfChildOfRootProperty
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/bufferView.schema.json
/// </summary>
public GltfBufferView BufferView { get; internal set; }

#region ISerializationCallbackReceiver

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (Enum.TryParse(componentType, out GltfComponentType result))
{
ComponentType = result;
}
else
{
ComponentType = default;
}
}

void ISerializationCallbackReceiver.OnBeforeSerialize()
{
componentType = ComponentType.ToString();
}

#endregion ISerializationCallbackReceiver
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using UnityEngine;

namespace XRTK.Utilities.Gltf.Schema
{
Expand All @@ -10,7 +11,7 @@ namespace XRTK.Utilities.Gltf.Schema
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/accessor.sparse.indices.schema.json
/// </summary>
[Serializable]
public class GltfAccessorSparseIndices : GltfProperty
public class GltfAccessorSparseIndices : GltfProperty, ISerializationCallbackReceiver
{
/// <summary>
/// The index of the bufferView with sparse indices.
Expand All @@ -30,6 +31,30 @@ public class GltfAccessorSparseIndices : GltfProperty
/// `5123` (UNSIGNED_SHORT)
/// `5125` (UNSIGNED_INT)
/// </summary>
public GltfComponentType ComponentType;
public GltfComponentType ComponentType { get; set; }

[SerializeField]
private string componentType = string.Empty;

#region ISerializationCallbackReceiver

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (Enum.TryParse(componentType, out GltfComponentType result))
{
ComponentType = result;
}
else
{
ComponentType = default;
}
}

void ISerializationCallbackReceiver.OnBeforeSerialize()
{
componentType = ComponentType.ToString();
}

#endregion ISerializationCallbackReceiver
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using UnityEngine;

namespace XRTK.Utilities.Gltf.Schema
{
Expand All @@ -10,7 +11,7 @@ namespace XRTK.Utilities.Gltf.Schema
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/animation.channel.target.schema.json
/// </summary>
[Serializable]
public class GltfAnimationChannelTarget : GltfProperty
public class GltfAnimationChannelTarget : GltfProperty, ISerializationCallbackReceiver
{
/// <summary>
/// The index of the node to target.
Expand All @@ -20,6 +21,30 @@ public class GltfAnimationChannelTarget : GltfProperty
/// <summary>
/// The name of the node's TRS property to modify.
/// </summary>
public GltfAnimationChannelPath path;
public GltfAnimationChannelPath Path { get; set; }

[SerializeField]
private string path = string.Empty;

#region ISerializationCallbackReceiver

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (Enum.TryParse(path, out GltfAnimationChannelPath result))
{
Path = result;
}
else
{
Path = default;
}
}

void ISerializationCallbackReceiver.OnBeforeSerialize()
{
path = Path.ToString();
}

#endregion ISerializationCallbackReceiver
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using UnityEngine;

namespace XRTK.Utilities.Gltf.Schema
{
Expand All @@ -10,7 +11,7 @@ namespace XRTK.Utilities.Gltf.Schema
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/animation.sampler.schema.json
/// </summary>
[Serializable]
public class GltfAnimationSampler : GltfProperty
public class GltfAnimationSampler : GltfProperty, ISerializationCallbackReceiver
{
/// <summary>
/// The index of an accessor containing keyframe input values, e.G., time.
Expand All @@ -27,13 +28,37 @@ public class GltfAnimationSampler : GltfProperty
/// interpolation is `\"STEP\"`, animated value remains constant to the value
/// of the first point of the timeframe, until the next timeframe.
/// </summary>
public GltfInterpolationType interpolation = GltfInterpolationType.LINEAR;
public GltfInterpolationType Interpolation { get; set; }

[SerializeField]
private string interpolation = string.Empty;

/// <summary>
/// The index of an accessor, containing keyframe output values. Output and input
/// accessors must have the same `count`. When sampler is used with TRS target,
/// output accessors componentType must be `FLOAT`.
/// </summary>
public int output;

#region ISerializationCallbackReceiver

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (Enum.TryParse(interpolation, out GltfInterpolationType result))
{
Interpolation = result;
}
else
{
Interpolation = GltfInterpolationType.LINEAR;
}
}

void ISerializationCallbackReceiver.OnBeforeSerialize()
{
interpolation = Interpolation.ToString();
}

#endregion ISerializationCallbackReceiver
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using UnityEngine;

namespace XRTK.Utilities.Gltf.Schema
{
Expand All @@ -10,7 +11,7 @@ namespace XRTK.Utilities.Gltf.Schema
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/bufferView.schema.json
/// </summary>
[Serializable]
public class GltfBufferView : GltfChildOfRootProperty
public class GltfBufferView : GltfChildOfRootProperty, ISerializationCallbackReceiver
{
/// <summary>
/// The index of the buffer.
Expand Down Expand Up @@ -42,11 +43,35 @@ public class GltfBufferView : GltfChildOfRootProperty
/// All valid values correspond to WebGL enums.
/// When this is not provided, the bufferView contains animation or skin data.
/// </summary>
public GltfBufferViewTarget target = GltfBufferViewTarget.None;
public GltfBufferViewTarget Target { get; set; }

[SerializeField]
private string target = string.Empty;

/// <summary>
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/buffer.schema.json
/// </summary>
public GltfBuffer Buffer { get; internal set; }

#region ISerializationCallbackReceiver

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (Enum.TryParse(target, out GltfBufferViewTarget result))
{
Target = result;
}
else
{
Target = GltfBufferViewTarget.None;
}
}

void ISerializationCallbackReceiver.OnBeforeSerialize()
{
target = Target.ToString();
}

#endregion ISerializationCallbackReceiver
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using UnityEngine;

namespace XRTK.Utilities.Gltf.Schema
{
Expand All @@ -11,7 +12,7 @@ namespace XRTK.Utilities.Gltf.Schema
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/camera.schema.json
/// </summary>
[Serializable]
public class GltfCamera : GltfChildOfRootProperty
public class GltfCamera : GltfChildOfRootProperty, ISerializationCallbackReceiver
{
/// <summary>
/// An orthographic camera containing properties to create an orthographic
Expand All @@ -30,6 +31,30 @@ public class GltfCamera : GltfChildOfRootProperty
/// Based on this, either the camera's `perspective` or `orthographic` property
/// will be defined.
/// </summary>
public GltfCameraType type;
public GltfCameraType Type { get; set; }

[SerializeField]
private string type = string.Empty;

#region ISerializationCallbackReceiver

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (Enum.TryParse(type, out GltfCameraType result))
{
Type = result;
}
else
{
Type = default;
}
}

void ISerializationCallbackReceiver.OnBeforeSerialize()
{
type = Type.ToString();
}

#endregion ISerializationCallbackReceiver
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace XRTK.Utilities.Gltf.Schema
/// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/mesh.primitive.schema.json
/// </summary>
[Serializable]
public class GltfMeshPrimitive : GltfProperty
public class GltfMeshPrimitive : GltfProperty, ISerializationCallbackReceiver
{
#region Serialized Fields

Expand All @@ -37,7 +37,10 @@ public class GltfMeshPrimitive : GltfProperty
/// <summary>
/// The type of primitives to render. All valid values correspond to WebGL enums.
/// </summary>
public GltfDrawMode mode = GltfDrawMode.Triangles;
public GltfDrawMode Mode { get; set; }

[SerializeField]
private string mode = string.Empty;

#endregion Serialized Fields

Expand All @@ -59,5 +62,26 @@ public class GltfMeshPrimitive : GltfProperty
/// Unity Mesh wrapper for the GltfMeshPrimitive SubMesh
/// </summary>
public Mesh SubMesh { get; internal set; }

#region ISerializationCallbackReceiver

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (Enum.TryParse(mode, out GltfDrawMode result))
{
Mode = result;
}
else
{
Mode = GltfDrawMode.Triangles;
}
}

void ISerializationCallbackReceiver.OnBeforeSerialize()
{
mode = Mode.ToString();
}

#endregion ISerializationCallbackReceiver
}
}
Loading