-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
oculusrift-barrel.fsh
53 lines (48 loc) · 2.18 KB
/
oculusrift-barrel.fsh
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
#version 120
uniform sampler2D texture;
varying highp vec2 vTexCoord;
const float aspectRatio = 1.0;
const float distortionScale = 1.714; // TODO check this
const vec2 screenSize = vec2(0.14976, 0.0936);
const vec2 screenCenter = 0.5 * screenSize;
const vec2 lensCenter = vec2(0.57265, 0.5); // left eye
const vec2 inputCenter = vec2(0.5, 0.5); // I rendered center at center of unwarped image
const vec2 scale = vec2(0.5/distortionScale, 0.5*aspectRatio/distortionScale);
const vec2 scaleIn = vec2(2.0, 2.0/aspectRatio);
const vec4 hmdWarpParam = vec4(1.0, 0.220, 0.240, 0.000);
const vec4 chromAbParam = vec4(0.996, -0.004, 1.014, 0.0);
void main() {
vec2 tcIn = vTexCoord;
vec2 uv = vec2(tcIn.x*2, tcIn.y); // unwarped image coordinates (left eye)
if (tcIn.x > 0.5) // right eye
uv.x = 2 - 2*tcIn.x;
vec2 theta = (uv - lensCenter) * scaleIn;
float rSq = theta.x * theta.x + theta.y * theta.y;
vec2 rvector = theta * ( hmdWarpParam.x +
hmdWarpParam.y * rSq +
hmdWarpParam.z * rSq * rSq +
hmdWarpParam.w * rSq * rSq * rSq);
// Chromatic aberration correction
vec2 thetaBlue = rvector * (chromAbParam.z + chromAbParam.w * rSq);
vec2 tcBlue = inputCenter + scale * thetaBlue;
// Blue is farthest out
if ( (abs(tcBlue.x - 0.5) > 0.5) || (abs(tcBlue.y - 0.5) > 0.5) ) {
gl_FragColor = vec4(0, 0, 0, 1);
return;
}
vec2 thetaRed = rvector * (chromAbParam.x + chromAbParam.y * rSq);
vec2 tcRed = inputCenter + scale * thetaRed;
vec2 tcGreen = inputCenter + scale * rvector; // green
tcRed.x *= 0.5; // because output only goes to 0-0.5 (left eye)
tcGreen.x *= 0.5; // because output only goes to 0-0.5 (left eye)
tcBlue.x *= 0.5; // because output only goes to 0-0.5 (left eye)
if (tcIn.x > 0.5) { // right eye 0.5-1.0
tcRed.x = 1 - tcRed.x;
tcGreen.x = 1 - tcGreen.x;
tcBlue.x = 1 - tcBlue.x;
}
float red = texture2D(texture, tcRed).r;
vec2 green = texture2D(texture, tcGreen).ga;
float blue = texture2D(texture, tcBlue).b;
gl_FragColor = vec4(red, green.x, blue, green.y);
}