-
Notifications
You must be signed in to change notification settings - Fork 15
/
DVD Bounce.sksl
272 lines (217 loc) · 7.17 KB
/
DVD Bounce.sksl
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
// Original: https://www.shadertoy.com/view/wtcSzN
// Created by tdhooper in 2020-01-31
// For some reason the backgorund isn't black...
uniform vec2 iResolution;
uniform float iTime;
const float PI = 3.14159265359;
//#define DEBUG
float vmin(vec2 v) {
return min(v.x, v.y);
}
float vmax(vec2 v) {
return max(v.x, v.y);
}
float ellip(vec2 p, vec2 s) {
float m = vmin(s);
return (length(p / s) * m) - m;
}
float halfEllip(vec2 p, vec2 s) {
p.x = max(0., p.x);
float m = vmin(s);
return (length(p / s) * m) - m;
}
float fBox(vec2 p, vec2 b) {
return vmax(abs(p) - b);
}
float dvd_d(vec2 p) {
float d = halfEllip(p, vec2(.8, .5));
d = max(d, -p.x - .5);
float d2 = halfEllip(p, vec2(.45, .3));
d2 = max(d2, min(-p.y + .2, -p.x - .15));
d = max(d, -d2);
return d;
}
float dvd_v(vec2 p) {
vec2 pp = p;
p.y += .7;
p.x = abs(p.x);
vec2 a = normalize(vec2(1, -.55));
float d = dot(p, a);
float d2 = d + .3;
p = pp;
d = min(d, -p.y + .3);
d2 = min(d2, -p.y + .5);
d = max(d, -d2);
d = max(d, abs(p.x + .3) - 1.1);
return d;
}
float dvd_c(vec2 p) {
p.y += .95;
float d = ellip(p, vec2(1.8, .25));
float d2 = ellip(p, vec2(.45, .09));
d = max(d, -d2);
return d;
}
float dvd(vec2 p) {
p.y -= .345;
p.x -= .035;
p *= mat2(1, -.2, 0, 1);
float d = dvd_v(p);
d = min(d, dvd_c(p));
p.x += 1.3;
d = min(d, dvd_d(p));
p.x -= 2.4;
d = min(d, dvd_d(p));
return d;
}
float range(float vmin, float vmax, float value) {
return (value - vmin) / (vmax - vmin);
}
float rangec(float a, float b, float t) {
return clamp(range(a, b, t), 0., 1.);
}
// https://www.shadertoy.com/view/ll2GD3
vec3 pal(float t, vec3 a, vec3 b, vec3 c, vec3 d) {
return a + b * cos(6.28318 * (c * t + d));
}
vec3 spectrum(float n) {
return pal(n, vec3(0.5, 0.5, 0.5), vec3(0.5, 0.5, 0.5), vec3(1.0, 1.0, 1.0), vec3(0.0, 0.33, 0.67));
}
void drawHit(inout vec4 col, vec2 p, vec2 hitPos, float hitDist) {
float d = length(p - hitPos);
// #ifdef DEBUG
// col = mix(col, vec4(0, 1, 1, 0), step(d, .1));
// return;
// #endif
float wavefront = d - hitDist * 1.5;
float freq = 2.;
vec3 spec = (1. - spectrum(-wavefront * freq + hitDist * freq));
float ripple = sin((wavefront * freq) * PI * 2. - PI / 2.);
float blend = smoothstep(3., 0., hitDist);
blend *= smoothstep(.2, -.5, wavefront);
blend *= rangec(-4., .0, wavefront);
col.rgb *= mix(vec3(1), spec, pow(blend, 4.));
float height = (ripple * blend);
col.a -= height * 1.9 / freq;
}
vec2 ref(vec2 p, vec2 planeNormal, float offset) {
float t = dot(p, planeNormal) + offset;
p -= (2. * t) * planeNormal;
return p;
}
void drawReflectedHit(inout vec4 col, vec2 p, vec2 hitPos, float hitDist, vec2 screenSize) {
col.a += length(p) * .0001; // fix normal when flat
// drawHit(col, p, hitPos, hitDist);
// return;
drawHit(col, p, ref(hitPos, vec2(0, 1), 1.), hitDist);
drawHit(col, p, ref(hitPos, vec2(0, -1), 1.), hitDist);
drawHit(col, p, ref(hitPos, vec2(1, 0), screenSize.x / screenSize.y), hitDist);
drawHit(col, p, ref(hitPos, vec2(-1, 0), screenSize.x / screenSize.y), hitDist);
}
// Flip every second cell to create reflection
void flip(inout vec2 pos) {
vec2 flip = mod(floor(pos), 2.);
pos = abs(flip - mod(pos, 1.));
}
float stepSign(float a) {
// return sign(a);
return step(0., a) * 2. - 1.;
}
vec2 compassDir(vec2 p) {
// return sign(p - sign(p) * vmin(abs(p))); // this caused problems on some GPUs
vec2 a = vec2(stepSign(p.x), 0);
vec2 b = vec2(0, stepSign(p.y));
float s = stepSign(p.x - p.y) * stepSign(-p.x - p.y);
return mix(a, b, s * .5 + .5);
}
vec2 calcHitPos(vec2 move, vec2 dir, vec2 size) {
vec2 hitPos = mod(move, 1.);
vec2 xCross = hitPos - hitPos.x / (size / size.x) * (dir / dir.x);
vec2 yCross = hitPos - hitPos.y / (size / size.y) * (dir / dir.y);
hitPos = max(xCross, yCross);
hitPos += floor(move);
return hitPos;
}
vec4 main(vec2 fragCoord) {
vec2 p = (-iResolution + 2.0 * vec2(fragCoord.x, iResolution.y - fragCoord.y)) / iResolution.y;
// #ifdef DEBUG
// p.xy += vec2(1.2, .6);
// p *= 3.5;
// p *= 2.;
// #endif
vec2 screenSize = vec2(iResolution.x / iResolution.y, 1.) * 2.;
float t = iTime;
vec2 dir = normalize(vec2(9., 16) * screenSize);
vec2 move = dir * t / 1.5;
float logoScale = .1;
vec2 logoSize = vec2(2., .85) * logoScale * 1.;
vec2 size = screenSize - logoSize * 2.;
// Remap so (0,0) is bottom left, and (1,1) is top right
move = move / size + .5;
// Calculate the point we last crossed a cell boundry
vec2 lastHitPos = calcHitPos(move, dir, size);
vec4 col = vec4(1, 1, 1, 0);
vec4 colFx = vec4(1, 1, 1, 0);
vec4 colFy = vec4(1, 1, 1, 0);
vec2 e = vec2(.8, 0) / iResolution.y;
const int limit = 5;
for (int i = 0; i < limit; i++) {
vec2 hitPos = lastHitPos;
if (i > 0) {
// Nudge it before the boundry to find the previous hit point
hitPos = calcHitPos(hitPos - .00001 / size, dir, size);
}
lastHitPos = hitPos;
// How far are we from the hit point
float hitDist = distance(hitPos, move);
// Flip every second cell to create reflection
flip(hitPos);
// Remap back to screen space
hitPos = (hitPos - .5) * size;
// Push the hits to the edges of the screen
hitPos += logoSize * compassDir(hitPos / size);
drawReflectedHit(col, p, hitPos, hitDist, screenSize);
drawReflectedHit(colFx, p + e, hitPos, hitDist, screenSize);
drawReflectedHit(colFy, p + e.yx, hitPos, hitDist, screenSize);
}
// Flip every second cell to create reflection
flip(move);
// Remap back to screen space
move = (move - .5) * size;
// Calc normals
float bf = .1; // Bump factor
float fx = (col.a - colFx.a) * 2.; // Nearby horizontal samples.
float fy = (col.a - colFy.a) * 2.; // Nearby vertical samples.
vec3 nor = normalize(vec3(fx, fy, e.x / bf)); // Bumped normal.
float ff = length(vec2(fx, fy));
float ee = rangec(0., 10. / iResolution.y, ff);
nor = normalize(vec3(vec2(fx, fy) * ee, ff));
// invert colours
col.rgb = clamp(1. - col.rgb, vec3(0), vec3(1));
col.rgb /= 3.;
// lighting
// iq https://www.shadertoy.com/view/Xds3zN
vec3 lig = normalize(vec3(1, 2, 2.));
vec3 rd = normalize(vec3(p, -10.));
vec3 hal = normalize(lig - rd);
float dif = clamp(dot(lig, nor), 0., 1.);
float spe = pow(clamp(dot(nor, hal), 0.0, 1.0), 16.0) *
dif *
(0.04 + 0.96 * pow(clamp(1.0 + dot(hal, rd), 0.0, 1.0), 5.0));
vec3 lin = vec3(0.);
lin += 5. * dif;
lin += .2;
col.rgb = col.rgb * lin;
col.rgb += 5. * spe;
// dvd logo
float d = dvd((p - move) / logoScale);
// Replacing fwidth with a constant value as SKSL does not support fwidth
d /= 0.01; // fwidth replacement
d = 1. - clamp(d, 0., 1.);
col.rgb = mix(col.rgb, vec3(1), d);
// gamma
col.rgb = pow(col.rgb, vec3(1. / 1.5));
col.a = col.a * .5 + .5;
col.a *= .3;
return col;
}