-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarrelDistortion.frag
More file actions
50 lines (39 loc) · 2 KB
/
Copy pathBarrelDistortion.frag
File metadata and controls
50 lines (39 loc) · 2 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
// BarrelDistortion.frag
// 21-09-2024 vecnode
// Uniforms for controlling distortion
uniform float strength;
uniform float power;
// 'sTD2DInputs' sampler2D array and 'vUV' varying are declared elsewhere
// Function to apply barrel distortion
vec2 barrelDistortion(vec2 coord, float amt) {
vec2 centeredCoord = coord - 0.5;
float distanceSquared = dot(centeredCoord, centeredCoord);
return coord + centeredCoord * pow(distanceSquared, power) * amt * strength;
}
out vec4 fragColor;
void main() {
vec2 uv = vUV.st;
// Distortion amounts
const float distortionAmounts[12] = float[](
0.0, 0.2, 0.4, 0.6,
0.8, 1.0, 1.2, 1.4,
1.6, 1.8, 2.0, 2.2
);
// Initialize accumulator for color
vec4 accumulatedColor = vec4(0.0);
// Sample texture with different distortion amounts
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[0]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[1]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[2]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[3]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[4]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[5]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[6]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[7]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[8]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[9]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[10]));
accumulatedColor += texture(sTD2DInputs[0], barrelDistortion(uv, distortionAmounts[11]));
// Average the accumulated color
fragColor = accumulatedColor / 12.0;
}