1 // Fragment shader for a skybox in equirectangular projection
2 precision mediump float;
4 uniform sampler2D uSkyBoxEquirectangularTexture;
7 uniform float uIntensity;
9 varying vec3 vTexCoord;
11 // Take the sample direction as interpolated from the cube's local position,
12 // and use this direction vector and the spherical to cartesian coordinate
13 // conversion to sample the equirectangular map as if it's a cube map.
15 const float M_1_PI = 0.3183; // The reciprocal of pi in radians
16 const float M_1_2PI = 0.1591; // The reciprocal of 2*pi in radians
18 const vec2 inverseAtan = vec2(M_1_2PI, M_1_PI);
20 vec2 SampleEquirectangularMapAsCubeMap(vec3 v)
22 vec2 uv = vec2(atan(v.z, v.x), asin(v.y));
30 // Project the equirectangular map to a cube's faces
31 vec2 uv = SampleEquirectangularMapAsCubeMap(normalize(vTexCoord));
33 // Flip the texture UVs vertically
34 vec2 uvFlippped = vec2(uv.x, 1.0 - uv.y);
36 vec4 texColor = texture2D( uSkyBoxEquirectangularTexture, uvFlippped ) * uIntensity;
37 gl_FragColor = texColor * uColor;