Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eladg/2cdda165ab6a327d1b24a095abc966fd to your computer and use it in GitHub Desktop.
Save eladg/2cdda165ab6a327d1b24a095abc966fd to your computer and use it in GitHub Desktop.
WebGL: stereoscopic projection transformation (Fragment Shader)
// credits to: https://github.com/notlion/streetview-stereographic
precision mediump float;
uniform sampler2D texture;
uniform float scale, aspect, time;
uniform mat3 transform;
varying vec2 v_texcoord;
#define PI 3.141592653589793
void main(){
vec2 rads = vec2(PI * 2., PI);
vec2 pnt = (v_texcoord - .5) * vec2(scale, scale * aspect);
// Project to Sphere
float x2y2 = pnt.x * pnt.x + pnt.y * pnt.y;
vec3 sphere_pnt = vec3(2. * pnt, x2y2 - 1.) / (x2y2 + 1.);
sphere_pnt *= transform;
// Convert to Spherical Coordinates
float r = length(sphere_pnt);
float lon = atan(sphere_pnt.y, sphere_pnt.x);
float lat = acos(sphere_pnt.z / r);
gl_FragColor = texture2D(texture, vec2(lon, lat) / rads);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment