-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnimationRecorder.cs
209 lines (181 loc) · 6.41 KB
/
AnimationRecorder.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class AnimationRecorder : MonoBehaviour
{
[Header("Recording Variables")]
public int recordingFrameLength = 5 * 50; //In frames
public List<GameObject> objectsToRecord;
public List<RecordingData> recordingDataList;
//Debug
private Coroutine playback = null;
private void Awake()
{
GetInitialState();
StartCoroutine(StartRecording());
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.F11))
{
PlayRecording();
}
if (Input.GetKeyDown(KeyCode.F10))
{
RewindRecording();
}
}
public void GetInitialState()
{
foreach (var gameObject in objectsToRecord)
{
Vector3 initialPosition = gameObject.transform.position;
Quaternion initialRotation = gameObject.transform.rotation;
Rigidbody rb = gameObject.GetComponent<Rigidbody>();
recordingDataList.Add(new RecordingData(rb, initialPosition, initialRotation, Vector3.zero, Vector3.zero));
}
}
public void ResetToInitialState()
{
for (int i = 0; i < objectsToRecord.Count; i++)
{
objectsToRecord[i].transform.position = recordingDataList[i].initialPosition;
objectsToRecord[i].transform.rotation = recordingDataList[i].initialRotation;
if (!recordingDataList[i].rb.isKinematic)
{
recordingDataList[i].rb.velocity = recordingDataList[i].initialForce;
recordingDataList[i].rb.angularVelocity = recordingDataList[i].initialTorque;
}
}
}
public IEnumerator StartRecording()
{
//Clear the recorded animation
foreach (var data in recordingDataList)
{
data.recordedAnimation.Clear();
}
//Begin recording position and rotation for every frame
for (int i = 0; i < recordingFrameLength; i++)
{
//For every gameObject
for (int j = 0; j < objectsToRecord.Count; j++)
{
Vector3 position = objectsToRecord[j].transform.position;
Quaternion rotation = objectsToRecord[j].transform.rotation;
recordingDataList[j].recordedAnimation.Add(new RecordedFrame(position, rotation));
}
yield return new WaitForFixedUpdate();
}
}
public void PlayRecording()
{
if (playback == null && recordingDataList.Count > 0)
{
playback = StartCoroutine(PlayAnimation());
}
}
private IEnumerator PlayAnimation()
{
//Disable Rigidbody
for (int i = 0; i < recordingDataList.Count; i++)
{
recordingDataList[i].rb.useGravity = false;
recordingDataList[i].rb.isKinematic = true;
}
ResetToInitialState();
//Play the animation frame by frame
for (int i = 0; i < recordingFrameLength; i++)
{
//For every objects
for (int j = 0; j < recordingDataList.Count; j++)
{
Vector3 position = recordingDataList[j].recordedAnimation[i].position;
Quaternion rotation = recordingDataList[j].recordedAnimation[i].rotation;
objectsToRecord[j].transform.position = position;
objectsToRecord[j].transform.rotation = rotation;
}
yield return new WaitForFixedUpdate();
}
playback = null;
}
#region
//Just for funsies, I added a rewind mechanic
private Coroutine rewind = null;
public void RewindRecording()
{
if (rewind == null && recordingDataList.Count > 0)
{
rewind = StartCoroutine(RewindAnimation());
}
}
private IEnumerator RewindAnimation()
{
//Disable Rigidbody
for (int i = 0; i < recordingDataList.Count; i++)
{
recordingDataList[i].rb.useGravity = false;
recordingDataList[i].rb.isKinematic = true;
}
//Play the animation frame by frame
for (int i = recordingFrameLength - 1; i > 0; i--)
{
//For every objects
for (int j = 0; j < recordingDataList.Count; j++)
{
Vector3 position = recordingDataList[j].recordedAnimation[i].position;
Quaternion rotation = recordingDataList[j].recordedAnimation[i].rotation;
objectsToRecord[j].transform.position = position;
objectsToRecord[j].transform.rotation = rotation;
}
yield return new WaitForFixedUpdate();
}
rewind = null;
}
#endregion
/// <summary>
/// For optimization, this function is to check if the dice has stopped moving.
/// We can then stop recording this dice.
/// </summary>
/// <param name="rb"></param>
/// <returns></returns>
public bool CheckObjectHasStopped(Rigidbody rb)
{
if (rb.velocity == Vector3.zero && rb.angularVelocity == Vector3.zero)
{
return true;
}
else return false;
}
[System.Serializable]
public struct RecordedFrame
{
public Vector3 position;
public Quaternion rotation;
public RecordedFrame(Vector3 position, Quaternion rotation)
{
this.position = position;
this.rotation = rotation;
}
}
[System.Serializable]
public struct RecordingData
{
public Rigidbody rb;
public Vector3 initialPosition;
public Quaternion initialRotation;
public Vector3 initialForce;
public Vector3 initialTorque;
public List<RecordedFrame> recordedAnimation;
public RecordingData(Rigidbody rb, Vector3 initialPosition, Quaternion initialRotation, Vector3 initialForce, Vector3 initialTorque)
{
this.rb = rb;
this.initialPosition = initialPosition;
this.initialRotation = initialRotation;
this.initialForce = initialForce;
this.initialTorque = initialTorque;
this.recordedAnimation = new List<RecordedFrame>();
}
}
}