-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCameraPath.cs
229 lines (196 loc) · 5.57 KB
/
CameraPath.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace CameraTools
{
public class CameraPath
{
public string pathName;
int keyCount = 0;
public int keyframeCount
{
get
{
return keyCount;
}
private set
{
keyCount = value;
}
}
public List<Vector3> points;
public List<Quaternion> rotations;
public List<float> times;
public List<float> zooms;
public float lerpRate = 15;
public float timeScale = 1;
Vector3Animation pointCurve;
RotationAnimation rotationCurve;
AnimationCurve zoomCurve;
public CameraPath()
{
pathName = "New Path";
points = new List<Vector3>();
rotations = new List<Quaternion>();
times = new List<float>();
zooms = new List<float>();
}
public static CameraPath Load(ConfigNode node)
{
CameraPath newPath = new CameraPath();
newPath.pathName = node.GetValue("pathName");
newPath.points = ParseVectorList(node.GetValue("points"));
newPath.rotations = ParseQuaternionList(node.GetValue("rotations"));
newPath.times = ParseFloatList(node.GetValue("times"));
newPath.zooms = ParseFloatList(node.GetValue("zooms"));
newPath.lerpRate = float.Parse(node.GetValue("lerpRate"));
newPath.timeScale = float.Parse(node.GetValue("timeScale"));
newPath.Refresh();
return newPath;
}
public void Save(ConfigNode node)
{
Debug.Log("Saving path: " + pathName);
ConfigNode pathNode = node.AddNode("CAMERAPATH");
pathNode.AddValue("pathName", pathName);
pathNode.AddValue("points", WriteVectorList(points));
pathNode.AddValue("rotations", WriteQuaternionList(rotations));
pathNode.AddValue("times", WriteFloatList(times));
pathNode.AddValue("zooms", WriteFloatList(zooms));
pathNode.AddValue("lerpRate", lerpRate);
pathNode.AddValue("timeScale", timeScale);
}
public static string WriteVectorList(List<Vector3> list)
{
string output = string.Empty;
foreach(var val in list)
{
output += ConfigNode.WriteVector(val) + ";";
}
return output;
}
public static string WriteQuaternionList(List<Quaternion> list)
{
string output = string.Empty;
foreach(var val in list)
{
output += ConfigNode.WriteQuaternion(val) + ";";
}
return output;
}
public static string WriteFloatList(List<float> list)
{
string output = string.Empty;
foreach(var val in list)
{
output += val.ToString() + ";";
}
return output;
}
public static List<Vector3> ParseVectorList(string arrayString)
{
string[] vectorStrings = arrayString.Split(new char[]{ ';' }, StringSplitOptions.RemoveEmptyEntries);
List<Vector3> vList = new List<Vector3>();
for(int i = 0; i < vectorStrings.Length; i++)
{
Debug.Log("attempting to parse vector: --" + vectorStrings[i] + "--");
vList.Add(ConfigNode.ParseVector3(vectorStrings[i]));
}
return vList;
}
public static List<Quaternion> ParseQuaternionList(string arrayString)
{
string[] qStrings = arrayString.Split(new char[]{ ';' }, StringSplitOptions.RemoveEmptyEntries);
List<Quaternion> qList = new List<Quaternion>();
for(int i = 0; i < qStrings.Length; i++)
{
qList.Add(ConfigNode.ParseQuaternion(qStrings[i]));
}
return qList;
}
public static List<float> ParseFloatList(string arrayString)
{
string[] fStrings = arrayString.Split(new char[]{ ';' }, StringSplitOptions.RemoveEmptyEntries);
List<float> fList = new List<float>();
for(int i = 0; i < fStrings.Length; i++)
{
fList.Add(float.Parse(fStrings[i]));
}
return fList;
}
public void AddTransform(Transform cameraTransform, float zoom, float time)
{
points.Add(cameraTransform.localPosition);
rotations.Add(cameraTransform.localRotation);
zooms.Add(zoom);
times.Add(time);
keyframeCount = times.Count;
Sort();
UpdateCurves();
}
public void SetTransform(int index, Transform cameraTransform, float zoom, float time)
{
points[index] = cameraTransform.localPosition;
rotations[index] = cameraTransform.localRotation;
zooms[index] = zoom;
times[index] = time;
Sort();
UpdateCurves();
}
public void Refresh()
{
keyframeCount = times.Count;
Sort();
UpdateCurves();
}
public void RemoveKeyframe(int index)
{
points.RemoveAt(index);
rotations.RemoveAt(index);
zooms.RemoveAt(index);
times.RemoveAt(index);
keyframeCount = times.Count;
UpdateCurves();
}
public void Sort()
{
List<CameraKeyframe> keyframes = new List<CameraKeyframe>();
for(int i = 0; i < points.Count; i++)
{
keyframes.Add(new CameraKeyframe(points[i], rotations[i], zooms[i], times[i]));
}
keyframes.Sort(new CameraKeyframeComparer());
for(int i = 0; i < keyframes.Count; i++)
{
points[i] = keyframes[i].position;
rotations[i] = keyframes[i].rotation;
zooms[i] = keyframes[i].zoom;
times[i] = keyframes[i].time;
}
}
public CameraKeyframe GetKeyframe(int index)
{
int i = index;
return new CameraKeyframe(points[i], rotations[i], zooms[i], times[i]);
}
public void UpdateCurves()
{
pointCurve = new Vector3Animation(points.ToArray(), times.ToArray());
rotationCurve = new RotationAnimation(rotations.ToArray(), times.ToArray());
zoomCurve = new AnimationCurve();
for(int i = 0; i < zooms.Count; i++)
{
zoomCurve.AddKey(new Keyframe(times[i], zooms[i]));
}
}
public CameraTransformation Evaulate(float time)
{
CameraTransformation tf = new CameraTransformation();
tf.position = pointCurve.Evaluate(time);
tf.rotation = rotationCurve.Evaluate(time);
tf.zoom = zoomCurve.Evaluate(time);
return tf;
}
}
}