-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathFrustumJitter.cs
More file actions
328 lines (298 loc) · 10.1 KB
/
FrustumJitter.cs
File metadata and controls
328 lines (298 loc) · 10.1 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright (c) <2015> <Playdead>
// This file is subject to the MIT License as seen in the root of this folder structure (LICENSE.TXT)
// AUTHOR: Lasse Jon Fuglsang Pedersen <lasse@playdead.com>
#if UNITY_5_5_OR_NEWER
#define SUPPORT_STEREO
#endif
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Playdead/FrustumJitter")]
public class FrustumJitter : MonoBehaviour
{
#region Static point data
private static float[] points_Still = new float[] {
0.5f, 0.5f,
};
private static float[] points_Uniform2 = new float[] {
-0.25f, -0.25f,//ll
0.25f, 0.25f,//ur
};
private static float[] points_Uniform4 = new float[] {
-0.25f, -0.25f,//ll
0.25f, -0.25f,//lr
0.25f, 0.25f,//ur
-0.25f, 0.25f,//ul
};
private static float[] points_Uniform4_Helix = new float[] {
-0.25f, -0.25f,//ll 3 1
0.25f, 0.25f,//ur \/|
0.25f, -0.25f,//lr /\|
-0.25f, 0.25f,//ul 0 2
};
private static float[] points_Uniform4_DoubleHelix = new float[] {
-0.25f, -0.25f,//ll 3 1
0.25f, 0.25f,//ur \/|
0.25f, -0.25f,//lr /\|
-0.25f, 0.25f,//ul 0 2
-0.25f, -0.25f,//ll 6--7
0.25f, -0.25f,//lr \
-0.25f, 0.25f,//ul \
0.25f, 0.25f,//ur 4--5
};
private static float[] points_SkewButterfly = new float[] {
-0.250f, -0.250f,
0.250f, 0.250f,
0.125f, -0.125f,
-0.125f, 0.125f,
};
private static float[] points_Rotated4 = new float[] {
-0.125f, -0.375f,//ll
0.375f, -0.125f,//lr
0.125f, 0.375f,//ur
-0.375f, 0.125f,//ul
};
private static float[] points_Rotated4_Helix = new float[] {
-0.125f, -0.375f,//ll 3 1
0.125f, 0.375f,//ur \/|
0.375f, -0.125f,//lr /\|
-0.375f, 0.125f,//ul 0 2
};
private static float[] points_Rotated4_Helix2 = new float[] {
-0.125f, -0.375f,//ll 2--1
0.125f, 0.375f,//ur \/
-0.375f, 0.125f,//ul /\
0.375f, -0.125f,//lr 0 3
};
private static float[] points_Poisson10 = new float[] {
-0.16795960f*0.25f, 0.65544910f*0.25f,
-0.69096030f*0.25f, 0.59015970f*0.25f,
0.49843820f*0.25f, 0.83099720f*0.25f,
0.17230150f*0.25f, -0.03882703f*0.25f,
-0.60772670f*0.25f, -0.06013587f*0.25f,
0.65606390f*0.25f, 0.24007600f*0.25f,
0.80348370f*0.25f, -0.48096900f*0.25f,
0.33436540f*0.25f, -0.73007030f*0.25f,
-0.47839520f*0.25f, -0.56005300f*0.25f,
-0.12388120f*0.25f, -0.96633990f*0.25f,
};
private static float[] points_Pentagram = new float[] {
0.000000f*0.5f, 0.525731f*0.5f,// head
-0.309017f*0.5f, -0.425325f*0.5f,// lleg
0.500000f*0.5f, 0.162460f*0.5f,// rarm
-0.500000f*0.5f, 0.162460f*0.5f,// larm
0.309017f*0.5f, -0.425325f*0.5f,// rleg
};
private static float[] points_Halton_2_3_x8 = new float[8 * 2];
private static float[] points_Halton_2_3_x16 = new float[16 * 2];
private static float[] points_Halton_2_3_x32 = new float[32 * 2];
private static float[] points_Halton_2_3_x256 = new float[256 * 2];
private static float[] points_MotionPerp2 = new float[] {
0.00f, -0.25f,
0.00f, 0.25f,
};
#endregion
#region Static point data, static initialization
private static void TransformPattern(float[] seq, float theta, float scale)
{
float cs = Mathf.Cos(theta);
float sn = Mathf.Sin(theta);
for (int i = 0, j = 1, n = seq.Length; i != n; i += 2, j += 2)
{
float x = scale * seq[i];
float y = scale * seq[j];
seq[i] = x * cs - y * sn;
seq[j] = x * sn + y * cs;
}
}
// http://en.wikipedia.org/wiki/Halton_sequence
private static float HaltonSeq(int prime, int index = 1/* NOT! zero-based */)
{
float r = 0.0f;
float f = 1.0f;
int i = index;
while (i > 0)
{
f /= prime;
r += f * (i % prime);
i = (int)Mathf.Floor(i / (float)prime);
}
return r;
}
private static void InitializeHalton_2_3(float[] seq)
{
for (int i = 0, n = seq.Length / 2; i != n; i++)
{
float u = HaltonSeq(2, i + 1) - 0.5f;
float v = HaltonSeq(3, i + 1) - 0.5f;
seq[2 * i + 0] = u;
seq[2 * i + 1] = v;
}
}
static FrustumJitter()
{
// points_Pentagram
Vector2 vh = new Vector2(points_Pentagram[0] - points_Pentagram[2], points_Pentagram[1] - points_Pentagram[3]);
Vector2 vu = new Vector2(0.0f, 1.0f);
TransformPattern(points_Pentagram, Mathf.Deg2Rad * (0.5f * Vector2.Angle(vu, vh)), 1.0f);
// points_Halton_2_3_xN
InitializeHalton_2_3(points_Halton_2_3_x8);
InitializeHalton_2_3(points_Halton_2_3_x16);
InitializeHalton_2_3(points_Halton_2_3_x32);
InitializeHalton_2_3(points_Halton_2_3_x256);
}
#endregion
#region Static point data accessors
public enum Pattern
{
Still,
Uniform2,
Uniform4,
Uniform4_Helix,
Uniform4_DoubleHelix,
SkewButterfly,
Rotated4,
Rotated4_Helix,
Rotated4_Helix2,
Poisson10,
Pentagram,
Halton_2_3_X8,
Halton_2_3_X16,
Halton_2_3_X32,
Halton_2_3_X256,
MotionPerp2,
};
private static float[] AccessPointData(Pattern pattern)
{
switch (pattern)
{
case Pattern.Still:
return points_Still;
case Pattern.Uniform2:
return points_Uniform2;
case Pattern.Uniform4:
return points_Uniform4;
case Pattern.Uniform4_Helix:
return points_Uniform4_Helix;
case Pattern.Uniform4_DoubleHelix:
return points_Uniform4_DoubleHelix;
case Pattern.SkewButterfly:
return points_SkewButterfly;
case Pattern.Rotated4:
return points_Rotated4;
case Pattern.Rotated4_Helix:
return points_Rotated4_Helix;
case Pattern.Rotated4_Helix2:
return points_Rotated4_Helix2;
case Pattern.Poisson10:
return points_Poisson10;
case Pattern.Pentagram:
return points_Pentagram;
case Pattern.Halton_2_3_X8:
return points_Halton_2_3_x8;
case Pattern.Halton_2_3_X16:
return points_Halton_2_3_x16;
case Pattern.Halton_2_3_X32:
return points_Halton_2_3_x32;
case Pattern.Halton_2_3_X256:
return points_Halton_2_3_x256;
case Pattern.MotionPerp2:
return points_MotionPerp2;
default:
Debug.LogError("missing point distribution");
return points_Halton_2_3_x16;
}
}
public static int AccessLength(Pattern pattern)
{
return AccessPointData(pattern).Length / 2;
}
public Vector2 Sample(Pattern pattern, int index)
{
float[] points = AccessPointData(pattern);
int n = points.Length / 2;
int i = index % n;
float x = patternScale * points[2 * i + 0];
float y = patternScale * points[2 * i + 1];
if (pattern != Pattern.MotionPerp2)
return new Vector2(x, y);
else
return new Vector2(x, y).Rotate(Vector2.right.SignedAngle(focalMotionDir));
}
#endregion
private Camera _camera;
private Vector3 focalMotionPos = Vector3.zero;
private Vector3 focalMotionDir = Vector3.right;
public Pattern pattern = Pattern.Halton_2_3_X16;
public float patternScale = 1.0f;
public Vector4 activeSample = Vector4.zero;// xy = current sample, zw = previous sample
public int activeIndex = -2;
void Reset()
{
_camera = GetComponent<Camera>();
}
void Clear()
{
_camera.ResetProjectionMatrix();
activeSample = Vector4.zero;
activeIndex = -2;
}
void Awake()
{
Reset();
Clear();
}
void OnPreCull()
{
// update motion dir
{
Vector3 oldWorld = focalMotionPos;
Vector3 newWorld = _camera.transform.TransformVector(_camera.nearClipPlane * Vector3.forward);
Vector3 oldPoint = (_camera.worldToCameraMatrix * oldWorld);
Vector3 newPoint = (_camera.worldToCameraMatrix * newWorld);
Vector3 newDelta = (newPoint - oldPoint).WithZ(0.0f);
var mag = newDelta.magnitude;
if (mag != 0.0f)
{
var dir = newDelta / mag;// yes, apparently this is necessary instead of newDelta.normalized... because facepalm
if (dir.sqrMagnitude != 0.0f)
{
focalMotionPos = newWorld;
focalMotionDir = Vector3.Slerp(focalMotionDir, dir, 0.2f);
//Debug.Log("CHANGE focalMotionDir " + focalMotionDir.ToString("G4") + " delta was " + newDelta.ToString("G4") + " delta.mag " + newDelta.magnitude);
}
}
}
// update jitter
#if SUPPORT_STEREO
if (_camera.stereoEnabled)
{
Clear();
}
else
#endif
{
if (activeIndex == -2)
{
activeSample = Vector4.zero;
activeIndex += 1;
_camera.projectionMatrix = _camera.GetProjectionMatrix();
}
else
{
activeIndex += 1;
activeIndex %= AccessLength(pattern);
Vector2 sample = Sample(pattern, activeIndex);
activeSample.z = activeSample.x;
activeSample.w = activeSample.y;
activeSample.x = sample.x;
activeSample.y = sample.y;
_camera.projectionMatrix = _camera.GetProjectionMatrix(sample.x, sample.y);
}
}
}
void OnDisable()
{
Clear();
}
}