<reponame>Rulial/simulateHF-Pi<filename>integrations/Unity/simulate-unity/Assets/Simulate/Runtime/GLTF/Converters/TranslationConverter.csusing UnityEngine;
// adapted from https://github.com/Siccity/GLTFUtility/tree/master/Scripts/Converters/TranslationConverter.cs
using Newtonsoft.Json;
using System;

public class TranslationConverter : JsonConverter {
	/// <summary>
	/// Converts from float array to Vector3 during deserialization, and back.
	/// Compensates for differing coordinate systems as well.
	/// </summary>
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
        Vector3 pos = (Vector3)value;
        writer.WriteStartArray();
        writer.WriteValue(-pos.x);
        writer.WriteValue(pos.y);
        writer.WriteValue(pos.z);
        writer.WriteEndArray();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
        float[] floatArray = serializer.Deserialize<float[]>(reader);
        return new Vector3(-floatArray[0], floatArray[1], floatArray[2]);
    }

    public override bool CanConvert(Type objectType) {
        return objectType == typeof(Vector3);
    }
}
