-
Notifications
You must be signed in to change notification settings - Fork 8
/
CameraTransform.cs
153 lines (134 loc) · 3.18 KB
/
CameraTransform.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Numerics;
using Newtonsoft.Json;
namespace Spark
{
[Serializable]
[JsonConverter(typeof(CameraTransformConverter))]
public class CameraTransform
{
public CameraTransform()
{
}
public CameraTransform(Vector3 position, Quaternion rotation, float fov = 1)
{
px = position.X;
py = position.Y;
pz = position.Z;
qx = rotation.X;
qy = rotation.Y;
qz = rotation.Z;
qw = rotation.W;
fovy = fov;
}
public float? px;
public float? py;
public float? pz;
public float? qx;
public float? qy;
public float? qz;
public float? qw;
public float? fovy = 1;
public Vector3 Position
{
get => new Vector3(px ?? 0, py ?? 0, pz ?? 0);
set
{
px = value.X;
py = value.Y;
pz = value.Z;
}
}
public Quaternion Rotation
{
get => new Quaternion(qx ?? 0, qy ?? 0, qz ?? 0, qw ?? 1);
set
{
qx = value.X;
qy = value.Y;
qz = value.Z;
qw = value.W;
}
}
}
public class CameraTransformConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (!(value is CameraTransform t)) return;
writer.WriteStartObject();
string[] vars = {
"px",
"py",
"pz",
"qx",
"qy",
"qz",
"qw",
"fovy"
};
foreach (string v in vars)
{
// reflection 🤢
object result = t.GetType().GetField(v)?.GetValue(t);
if (result == null) continue;
writer.WritePropertyName(v);
serializer.Serialize(writer, result);
}
writer.WriteEndObject();
// CameraTransform t = (CameraTransform)value;
//
// Dictionary<string, float> output = new Dictionary<string, float>();
//
// if (t.px != null) output["px"] = t.px ?? 0;
// if (t.py != null) output["py"] = t.py ?? 0;
// if (t.pz != null) output["pz"] = t.pz ?? 0;
//
// if (t.qx != null) output["qx"] = t.qx ?? 0;
// if (t.qy != null) output["qy"] = t.qy ?? 0;
// if (t.qz != null) output["qz"] = t.qz ?? 0;
// if (t.qw != null) output["qx"] = t.qw ?? 1;
//
// if (t.fovy != null) output["fovy"] = t.fovy ?? 1;
//
// writer.WriteValue(JsonConvert.SerializeObject(output));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}
public override bool CanRead => false;
public override bool CanConvert(Type objectType)
{
return typeof(CameraTransform).IsAssignableFrom(objectType);
}
}
[Serializable]
public class AnimationKeyframes
{
public string description;
public float duration;
public bool easeIn;
public bool easeOut;
public bool pauseWhenClockNotRunning;
public List<CameraTransform> keyframes;
public AnimationKeyframes()
{
description = "";
duration = 5;
keyframes = new List<CameraTransform>();
}
public AnimationKeyframes Copy()
{
return new AnimationKeyframes()
{
description = description,
duration = duration,
easeIn = easeIn,
easeOut = easeOut,
keyframes = new List<CameraTransform>(keyframes)
};
}
}
}