playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
76 lines (59 loc) • 2.14 kB
JavaScript
var skybox_default = (
/* glsl */
`
attribute vec4 aPosition;
uniform mat4 matrix_view;
uniform mat4 matrix_projectionSkybox;
uniform mat3 cubeMapRotationMatrix;
varying vec3 vViewDir;
varying vec3 vClipXYW;
// when skydome renders depth during prepass, generate linear depth
varying float vLinearDepth;
uniform mat4 matrix_model;
varying vec3 vWorldPos;
void main(void) {
mat4 view = matrix_view;
vec4 worldPos = matrix_model * aPosition;
vWorldPos = worldPos.xyz;
gl_Position = matrix_projectionSkybox * (view * worldPos);
// linear depth from the worldPosition, see getLinearDepth
vLinearDepth = -(matrix_view * vec4(vWorldPos, 1.0)).z;
view[3][0] = view[3][1] = view[3][2] = 0.0;
vViewDir = aPosition.xyz * cubeMapRotationMatrix;
// Bypass matrix_projectionSkybox which degenerates at extreme FOVs.
// Use a fixed ~90\xB0 perspective (p00=p11=1) so the box always covers the
// screen. The fragment shader recomputes view direction from screen
// coordinates, so only rasterization coverage matters here.
vec4 viewPos = view * aPosition;
gl_Position = vec4(viewPos.xy, 0.0, -viewPos.z);
vClipXYW = vec3(gl_Position.xy, gl_Position.w);
gl_Position = matrix_projectionSkybox * (view * aPosition);
// for infinite skybox, use negative gl_Position.w to get positive linear depth
vLinearDepth = -gl_Position.w;
// Force skybox to far Z, regardless of the clip planes on the camera
// Subtract a tiny fudge factor to ensure floating point errors don't
// still push pixels beyond far Z. See:
// https://community.khronos.org/t/skybox-problem/61857
gl_Position.z = gl_Position.w - 1.0e-7;
}
`
);
export {
skybox_default as default
};