1 module re.util.vr_distortion;
2 
3 version (vr) {
4     enum VR_DISTORTION_SHADER_GL100 = `
5 #version 100
6 
7 precision mediump float;
8 
9 // Input vertex attributes (from vertex shader)
10 varying vec2 fragTexCoord;
11 varying vec4 fragColor;
12 
13 // Input uniform values
14 uniform sampler2D texture0;
15 uniform vec4 colDiffuse;
16 
17 // NOTE: Add here your custom variables
18 uniform vec2 leftLensCenter;
19 uniform vec2 rightLensCenter;
20 uniform vec2 leftScreenCenter;
21 uniform vec2 rightScreenCenter;
22 uniform vec2 scale;
23 uniform vec2 scaleIn;
24 uniform vec4 deviceWarpParam;
25 uniform vec4 chromaAbParam;
26 
27 void main()
28 {
29     // Compute lens distortion
30     vec2 lensCenter = fragTexCoord.x < 0.5? leftLensCenter : rightLensCenter;
31     vec2 screenCenter = fragTexCoord.x < 0.5? leftScreenCenter : rightScreenCenter;
32     vec2 theta = (fragTexCoord - lensCenter)*scaleIn;
33     float rSq = theta.x*theta.x + theta.y*theta.y;
34     vec2 theta1 = theta*(deviceWarpParam.x + deviceWarpParam.y*rSq + deviceWarpParam.z*rSq*rSq + deviceWarpParam.w*rSq*rSq*rSq);
35     vec2 thetaBlue = theta1*(chromaAbParam.z + chromaAbParam.w*rSq);
36     vec2 tcBlue = lensCenter + scale*thetaBlue;
37 
38     if (any(bvec2(clamp(tcBlue, screenCenter - vec2(0.25, 0.5), screenCenter + vec2(0.25, 0.5)) - tcBlue)))
39     {
40         // Set black fragment for everything outside the lens border
41         gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
42     }
43     else
44     {
45         // Compute color chroma aberration
46         float blue = texture2D(texture0, tcBlue).b;
47         vec2 tcGreen = lensCenter + scale*theta1;
48         float green = texture2D(texture0, tcGreen).g;
49 
50         vec2 thetaRed = theta1*(chromaAbParam.x + chromaAbParam.y*rSq);
51         vec2 tcRed = lensCenter + scale*thetaRed;
52 
53         float red = texture2D(texture0, tcRed).r;
54         gl_FragColor = vec4(red, green, blue, 1.0);
55     }
56 }
57 `;
58     enum VR_DISTORTION_SHADER_GL330 = `
59 #version 330
60 
61 // Input vertex attributes (from vertex shader)
62 in vec2 fragTexCoord;
63 in vec4 fragColor;
64 
65 // Input uniform values
66 uniform sampler2D texture0;
67 uniform vec4 colDiffuse;
68 
69 // Output fragment color
70 out vec4 finalColor;
71 
72 // NOTE: Add here your custom variables
73 uniform vec2 leftLensCenter = vec2(0.288, 0.5);
74 uniform vec2 rightLensCenter = vec2(0.712, 0.5);
75 uniform vec2 leftScreenCenter = vec2(0.25, 0.5);
76 uniform vec2 rightScreenCenter = vec2(0.75, 0.5);
77 uniform vec2 scale = vec2(0.25, 0.45);
78 uniform vec2 scaleIn = vec2(4, 2.2222);
79 uniform vec4 deviceWarpParam = vec4(1, 0.22, 0.24, 0);
80 uniform vec4 chromaAbParam = vec4(0.996, -0.004, 1.014, 0.0);
81 
82 void main()
83 {
84     // Compute lens distortion
85     vec2 lensCenter = fragTexCoord.x < 0.5? leftLensCenter : rightLensCenter;
86     vec2 screenCenter = fragTexCoord.x < 0.5? leftScreenCenter : rightScreenCenter;
87     vec2 theta = (fragTexCoord - lensCenter)*scaleIn;
88     float rSq = theta.x*theta.x + theta.y*theta.y;
89     vec2 theta1 = theta*(deviceWarpParam.x + deviceWarpParam.y*rSq + deviceWarpParam.z*rSq*rSq + deviceWarpParam.w*rSq*rSq*rSq);
90     vec2 thetaBlue = theta1*(chromaAbParam.z + chromaAbParam.w*rSq);
91     vec2 tcBlue = lensCenter + scale*thetaBlue;
92 
93     if (any(bvec2(clamp(tcBlue, screenCenter - vec2(0.25, 0.5), screenCenter + vec2(0.25, 0.5)) - tcBlue)))
94     {
95         // Set black fragment for everything outside the lens border
96         finalColor = vec4(0.0, 0.0, 0.0, 1.0);
97     }
98     else
99     {
100         // Compute color chroma aberration
101         float blue = texture(texture0, tcBlue).b;
102         vec2 tcGreen = lensCenter + scale*theta1;
103         float green = texture(texture0, tcGreen).g;
104 
105         vec2 thetaRed = theta1*(chromaAbParam.x + chromaAbParam.y*rSq);
106         vec2 tcRed = lensCenter + scale*thetaRed;
107 
108         float red = texture(texture0, tcRed).r;
109         finalColor = vec4(red, green, blue, 1.0);
110     }
111 }
112 
113 `;
114 }