Code commit
This commit is contained in:
65
CustomizePlus/Anamnesis/Data/PoseFile.cs
Normal file
65
CustomizePlus/Anamnesis/Data/PoseFile.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CustomizePlus.Anamnesis.Data;
|
||||
|
||||
[Serializable]
|
||||
public class PoseFile
|
||||
{
|
||||
public Vector? Scale { get; set; }
|
||||
|
||||
public Dictionary<string, Bone?>? Bones { get; set; }
|
||||
|
||||
[Serializable]
|
||||
public class Bone
|
||||
{
|
||||
public Vector? Scale { get; set; }
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Vector
|
||||
{
|
||||
public float X { get; set; }
|
||||
public float Y { get; set; }
|
||||
public float Z { get; set; }
|
||||
|
||||
public static Vector FromString(string str)
|
||||
{
|
||||
var parts = str.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (parts.Length != 3)
|
||||
{
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
Vector v = new()
|
||||
{
|
||||
X = float.Parse(parts[0], CultureInfo.InvariantCulture),
|
||||
Y = float.Parse(parts[1], CultureInfo.InvariantCulture),
|
||||
Z = float.Parse(parts[2], CultureInfo.InvariantCulture)
|
||||
};
|
||||
return v;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{X}, {Y}, {Z}";
|
||||
}
|
||||
}
|
||||
|
||||
public class VectorConverter : JsonConverter<Vector>
|
||||
{
|
||||
public override Vector? ReadJson(JsonReader reader, Type objectType, Vector? existingValue,
|
||||
bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
return reader.Value is not string str ? null : Vector.FromString(str);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, Vector? value, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
74
CustomizePlus/Anamnesis/PoseFileBoneLoader.cs
Normal file
74
CustomizePlus/Anamnesis/PoseFileBoneLoader.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using CustomizePlus.Anamnesis.Data;
|
||||
using CustomizePlus.Core.Data;
|
||||
using CustomizePlus.Core.Extensions;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Numerics;
|
||||
|
||||
namespace CustomizePlus.Anamnesis;
|
||||
|
||||
public class PoseFileBoneLoader
|
||||
{
|
||||
public Dictionary<string, BoneTransform>? LoadBoneTransformsFromFile(string path)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
return null;
|
||||
|
||||
var json = File.ReadAllText(path);
|
||||
|
||||
JsonSerializerSettings settings = new()
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
Converters = new List<JsonConverter> { new PoseFile.VectorConverter() }
|
||||
};
|
||||
|
||||
var pose = JsonConvert.DeserializeObject<PoseFile>(json, settings);
|
||||
|
||||
if (pose == null)
|
||||
{
|
||||
throw new Exception("Failed to deserialize pose file");
|
||||
}
|
||||
|
||||
if (pose.Bones == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var retDict = new Dictionary<string, BoneTransform>();
|
||||
|
||||
foreach (var kvp in pose.Bones)
|
||||
{
|
||||
if (kvp.Key == Constants.RootBoneName || kvp.Value == null || kvp.Value.Scale == null)
|
||||
continue;
|
||||
|
||||
var scale = kvp.Value!.Scale!.GetAsNumericsVector();
|
||||
if (scale == Vector3.One)
|
||||
continue;
|
||||
|
||||
retDict[kvp.Key] = new BoneTransform
|
||||
{
|
||||
Scaling = scale
|
||||
};
|
||||
}
|
||||
|
||||
//load up root, but check it more rigorously
|
||||
|
||||
var validRoot = pose.Bones.TryGetValue(Constants.RootBoneName, out var root)
|
||||
&& root != null
|
||||
&& root.Scale != null
|
||||
&& root.Scale.GetAsNumericsVector() != Vector3.Zero
|
||||
&& root.Scale.GetAsNumericsVector() != Vector3.One;
|
||||
|
||||
if (validRoot)
|
||||
{
|
||||
retDict[Constants.RootBoneName] = new BoneTransform
|
||||
{
|
||||
Scaling = root!.Scale!.GetAsNumericsVector()
|
||||
};
|
||||
}
|
||||
|
||||
return retDict;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user