Debian Codegraph @

Data/Sys/Shaders/lens_distortion.glsl

raw
1 /*
2 [configuration]
3
4 [OptionRangeFloat]
5 GUIName = Distortion amount
6 OptionName = DISTORTION_FACTOR
7 MinValue = 1.0
8 MaxValue = 10.0
9 StepAmount = 0.5
10 DefaultValue = 4.0
11
12 [OptionRangeFloat]
13 GUIName = Eye Distance Offset
14 OptionName = EYE_OFFSET
15 MinValue = 0.0
16 MaxValue = 10.0
17 StepAmount = 0.25
18 DefaultValue = 5.0
19
20 [OptionRangeFloat]
21 GUIName = Zoom adjustment
22 OptionName = SIZE_ADJUST
23 MinValue = 0.0
24 MaxValue = 1.0
25 StepAmount = 0.025
26 DefaultValue = 0.5
27
28 [OptionRangeFloat]
29 GUIName = Aspect Ratio adjustment
30 OptionName = ASPECT_ADJUST
31 MinValue = 0.0
32 MaxValue = 1.0
33 StepAmount = 0.025
34 DefaultValue = 0.5
35
36 [/configuration]
37 */
38
39
40 void main()
41 {
42 // Base Cardboard distortion parameters
43 float factor = GetOption(DISTORTION_FACTOR) * 0.01f;
44 float ka = factor * 3.0f;
45 float kb = factor * 5.0f;
46
47 // size and aspect adjustment
48 float sizeAdjust = 1.0f - GetOption(SIZE_ADJUST) + 0.5f;
49 float aspectAdjustment = 1.25f - GetOption(ASPECT_ADJUST);
50
51 // offset centering per eye
52 float stereoOffset = GetOption(EYE_OFFSET) * 0.01f;
53 float offsetAdd;
54
55 // layer0 = left eye, layer1 = right eye
56 if (src_layer == 1)
57 {
58 offsetAdd = stereoOffset;
59 }
60 else
61 {
62 offsetAdd = 0.0 - stereoOffset;
63 }
64
65 // convert coordinates to NDC space
66 float2 fragPos = (GetCoordinates() - 0.5f - float2(offsetAdd, 0.0f)) * 2.0f;
67
68 // Calculate the source location "radius" (distance from the centre of the viewport)
69 float destR = length(fragPos);
70
71 // find the radius multiplier
72 float srcR = destR * sizeAdjust + ( ka * pow(destR, 2.0) + kb * pow(destR, 4.0));
73
74 // Calculate the source vector (radial)
75 float2 correctedRadial = normalize(fragPos) * srcR;
76
77 // fix aspect ratio
78 float2 widenedRadial = correctedRadial * float2(aspectAdjustment, 1.0f);
79
80 // Transform the coordinates (from [-1,1]^2 to [0, 1]^2)
81 float2 uv = (widenedRadial/2.0f) + float2(0.5f, 0.5f) + float2(offsetAdd, 0.0f);
82
83 // Sample the texture at the source location
84 if (clamp(uv, 0.0, 1.0) != uv)
85 {
86 // black if beyond bounds
87 SetOutput(float4(0.0, 0.0, 0.0, 0.0));
88 }
89 else
90 {
91 SetOutput(SampleLocation(uv));
92 }
93 }