-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPerlinCameraShake.cs
293 lines (238 loc) · 10.2 KB
/
PerlinCameraShake.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using System.Collections;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Shakes the camera in an organic way, based on Perlin noise.
/// Supports any initial camera position and rotation, but camera should be steady, i.e. parented to another GameObject.
/// </summary>
[RequireComponent(typeof(Camera))]
public class PerlinCameraShake : MonoBehaviour
{
//[SerializeField]
//private FloatGameEvent _playerTrauma;
[Header("Camera Shake Frequency:")]
[SerializeField]
[Tooltip("Defines the turbulence of the movement. Larger values cause more violent shaking.")]
[Range(0, 50)]
private float _frequency = 30f;
[Header("Camera Movement:")]
[SerializeField]
[Tooltip("Specifies the maximum distance the camera can move from its original position.")]
[Range(0, 10)]
private float _translationMasterMagnitude = 1f;
[Space(EDITOR_SPACING)]
[SerializeField]
private bool _translateAxisX = true;
[SerializeField]
private bool _translateAxisY = true;
[SerializeField]
private bool _translateAxisZ = false;
[Space(EDITOR_SPACING)]
[SerializeField]
[Range(0, 1)]
private float _translateAxisXMultiplier = 1f;
[SerializeField]
[Range(0, 1)]
private float _translateAxisYMultiplier = 1f;
[SerializeField]
[Range(0, 1)]
private float _translateAxisZMultiplier = 1f;
[Header("Camera Rotation:")]
[SerializeField]
[Range(0, 40)]
[Tooltip("Specifies the maximum rotational angle the camera can rotate from its original rotation.")]
private float _rotationMasterMagnitude = 15;
[Space(EDITOR_SPACING)]
[SerializeField]
private bool _rotateAxisX = true;
[SerializeField]
private bool _rotateAxisY = true;
[SerializeField]
private bool _rotateAxisZ = true;
[Space(EDITOR_SPACING)]
[SerializeField]
[Range(0, 1)]
private float _rotateAxisXMultiplier = 1f;
[SerializeField]
[Range(0, 1)]
private float _rotateAxisYMultiplier = 1f;
[SerializeField]
[Range(0, 1)]
private float _rotateAxisZMultiplier = 1f;
[Header("Camera Shake Duration:")]
[SerializeField]
[Range(0.1f, 5)]
[Tooltip("Specifies how fast the shaking will decay to zero.")]
private float _decay = 2f;
public float Trauma
{
get => _trauma;
set => OnNewTrauma(value);
}
[Header("CURRENT TRAUMA (for testing):")]
[SerializeField]
[Range(0, 1)]
private float _trauma = 0;
private Camera _camera;
private Vector3 _originalCamPosition;
private Quaternion _originalCamRotation;
private bool _shakeJobRunning = false;
private float _sharedSamplingPoint;
private const float VECTOR_EQUALITY_TOLERANCE = 0.001f;
private const float DURATION_DECAY = 0.3f;
private const float FREQUENCY_DECAY = 0.3f;
private const float EDITOR_SPACING = 5f;
// Unique seeds are important to ensure that no predictable patterns will emerge in the movement.
// Also, using the same seed for the same translation/rotation ensures fluid motion.
private const float TRANSLATION_X_SEED = 100;
private const float TRANSLATION_Y_SEED = 200;
private const float TRANSLATION_Z_SEED = 300;
private const float ROTATION_X_SEED = 400;
private const float ROTATION_Y_SEED = 500;
private const float ROTATION_Z_SEED = 600;
#if UNITY_EDITOR
private void OnValidate()
{
if (GUI.changed && EditorApplication.isPlaying && _trauma > 0)
OnNewTrauma(_trauma);
}
#endif
private void Start()
{
//if (_playerTrauma == null)
// throw new Exception("Component failed: GameEvent dependency not set.");
_camera = GetComponent<Camera>();
StoreCameraState();
if (_trauma > 0)
OnNewTrauma(_trauma);
}
//private void OnEnable()
// => _playerTrauma.Event += OnNewTrauma;
//private void OnDisable()
// => _playerTrauma.Event -= OnNewTrauma;
private void StoreCameraState()
{
_originalCamPosition = _camera.transform.localPosition;
_originalCamRotation = _camera.transform.localRotation;
}
private void RestoreCameraState()
{
_camera.transform.localPosition = _originalCamPosition;
_camera.transform.localRotation = _originalCamRotation;
}
private void OnNewTrauma(float value)
{
_trauma = Mathf.Clamp01(value);
if (_trauma > 0 && !_shakeJobRunning && CanShake())
StartCoroutine(ShakeJob());
}
/// <summary>
/// Returns true if current settings actually call for a camera shake.
/// </summary>
private bool CanShake()
{
bool shouldTranslateX = _translateAxisX && _translateAxisXMultiplier > 0;
bool shouldTranslateY = _translateAxisY && _translateAxisYMultiplier > 0;
bool shouldTranslateZ = _translateAxisZ && _translateAxisZMultiplier > 0;
bool shouldTranslateAnyAxis = shouldTranslateX || shouldTranslateY || shouldTranslateZ;
if (_translationMasterMagnitude > 0 && shouldTranslateAnyAxis)
return true;
bool shouldRotateX = _rotateAxisX && _rotateAxisXMultiplier > 0;
bool shouldRotateY = _rotateAxisY && _rotateAxisYMultiplier > 0;
bool shouldRotateZ = _rotateAxisZ && _rotateAxisZMultiplier > 0;
bool shouldRotateAnyAxis = shouldRotateX || shouldRotateY || shouldRotateZ;
if (_rotationMasterMagnitude > 0 && shouldRotateAnyAxis)
return true;
return false;
}
/// <summary>
/// Returns a Perlin noise float in the range of -1 and 1, based on a unique seed and a shared sampling point
/// </summary>
private float GetPerlin(float seed)
=> (Mathf.PerlinNoise(seed, _sharedSamplingPoint) - 0.5f) * 2f;
/// <summary>
/// Unity coroutine responsible for executing camera shake.
/// Exits after trauma decays to zero, and camera is interpolated back to original position and rotation.
/// </summary>
private IEnumerator ShakeJob()
{
_shakeJobRunning = true;
bool shouldTranslate = _translationMasterMagnitude > 0 && (_translateAxisX || _translateAxisY || _translateAxisZ);
bool shouldRotate = _rotationMasterMagnitude > 0 && (_rotateAxisX || _rotateAxisY || _rotateAxisZ);
// Prepare sampling point
_sharedSamplingPoint = UnityEngine.Random.Range(0, 1000f);
// Execute camera shake until trauma decays to zero
ProcessTrauma:
while (!Mathf.Approximately(_trauma, 0))
{
//TODO: Implement shake 'ramp-up', i.e. smooth out the transition to the first Perlin offset.
// Offset sampling point
_sharedSamplingPoint += Time.deltaTime * Mathf.Pow(_trauma, FREQUENCY_DECAY) * _frequency;
// Set new position, relative to original position
if (shouldTranslate)
{
Vector3 translationOffset = new Vector3(
_translateAxisX && _translateAxisXMultiplier > 0 ? GetOffset(TRANSLATION_X_SEED, _translateAxisXMultiplier) : 0,
_translateAxisY && _translateAxisYMultiplier > 0 ? GetOffset(TRANSLATION_Y_SEED, _translateAxisYMultiplier) : 0,
_translateAxisZ && _translateAxisZMultiplier > 0 ? GetOffset(TRANSLATION_Z_SEED, _translateAxisZMultiplier) : 0
);
_camera.transform.localPosition = translationOffset + _originalCamPosition;
float GetOffset(float seed, float multiplier)
=> GetPerlin(seed) * _translationMasterMagnitude * _trauma * multiplier;
}
// Set new rotation, relative to original rotation
if (shouldRotate)
{
Vector3 rotationOffset = new Vector3(
_rotateAxisX && _rotateAxisXMultiplier > 0 ? GetOffset(ROTATION_X_SEED, _rotateAxisXMultiplier) : 0,
_rotateAxisY && _rotateAxisYMultiplier > 0 ? GetOffset(ROTATION_Y_SEED, _rotateAxisYMultiplier) : 0,
_rotateAxisZ && _rotateAxisZMultiplier > 0 ? GetOffset(ROTATION_Z_SEED, _rotateAxisZMultiplier) : 0
);
_camera.transform.localRotation = Quaternion.Euler(rotationOffset) * _originalCamRotation;
float GetOffset(float seed, float multiplier)
=> GetPerlin(seed) * _rotationMasterMagnitude * _trauma * multiplier;
}
// Decay trauma
_trauma = Mathf.Clamp01(_trauma - (Time.deltaTime * _decay * (_trauma + DURATION_DECAY)));
yield return null;
}
// Interpolate back to original state
// - For the sake of simplicity only position equality is tested
// - Rotation should be close enough too, without any visible jumps
while (!ApproximatelyEqual(_camera.transform.localPosition, _originalCamPosition))
{
// If trauma was added meanwhile, reprocess
if (_trauma != 0)
goto ProcessTrauma;
if (shouldTranslate)
_camera.transform.localPosition = Vector3.MoveTowards(
current: _camera.transform.localPosition,
target: _originalCamPosition,
maxDistanceDelta: _translationMasterMagnitude * Time.deltaTime
);
if (shouldRotate)
_camera.transform.localRotation = Quaternion.RotateTowards(
from: _camera.transform.localRotation,
to: _originalCamRotation,
maxDegreesDelta: _rotationMasterMagnitude * Time.deltaTime
);
yield return null;
}
// Ensure that camera state is restored precisely
RestoreCameraState();
_shakeJobRunning = false;
}
public bool ApproximatelyEqual(Vector3 firstVector, Vector3 secondVector)
{
var dx = firstVector.x - secondVector.x;
if (Mathf.Abs(dx) > VECTOR_EQUALITY_TOLERANCE)
return false;
var dy = firstVector.y - secondVector.y;
if (Mathf.Abs(dy) > VECTOR_EQUALITY_TOLERANCE)
return false;
var dz = firstVector.z - secondVector.z;
if (Mathf.Abs(dz) > VECTOR_EQUALITY_TOLERANCE)
return false;
return true;
}
}