-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
vertexLogDepth.glsl
64 lines (58 loc) · 1.92 KB
/
vertexLogDepth.glsl
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
54
55
56
57
58
59
60
61
62
63
64
#ifdef LOG_DEPTH
// 1.0 at the near plane, increasing linearly from there.
varying float v_depthFromNearPlusOne;
#ifdef SHADOW_MAP
varying vec3 v_logPositionEC;
#endif
#endif
vec4 czm_updatePositionDepth(vec4 coords) {
#if defined(LOG_DEPTH)
#ifdef SHADOW_MAP
vec3 logPositionEC = (czm_inverseProjection * coords).xyz;
v_logPositionEC = logPositionEC;
#endif
// With the very high far/near ratios used with the logarithmic depth
// buffer, floating point rounding errors can cause linear depth values
// to end up on the wrong side of the far plane, even for vertices that
// are really nowhere near it. Since we always write a correct logarithmic
// depth value in the fragment shader anyway, we just need to make sure
// such errors don't cause the primitive to be clipped entirely before
// we even get to the fragment shader.
coords.z = clamp(coords.z / coords.w, -1.0, 1.0) * coords.w;
#endif
return coords;
}
/**
* Writes the logarithmic depth to gl_Position using the already computed gl_Position.
*
* @name czm_vertexLogDepth
* @glslFunction
*/
void czm_vertexLogDepth()
{
#ifdef LOG_DEPTH
v_depthFromNearPlusOne = (gl_Position.w - czm_currentFrustum.x) + 1.0;
gl_Position = czm_updatePositionDepth(gl_Position);
#endif
}
/**
* Writes the logarithmic depth to gl_Position using the provided clip coordinates.
* <p>
* An example use case for this function would be moving the vertex in window coordinates
* before converting back to clip coordinates. Use the original vertex clip coordinates.
* </p>
* @name czm_vertexLogDepth
* @glslFunction
*
* @param {vec4} clipCoords The vertex in clip coordinates.
*
* @example
* czm_vertexLogDepth(czm_projection * vec4(positionEyeCoordinates, 1.0));
*/
void czm_vertexLogDepth(vec4 clipCoords)
{
#ifdef LOG_DEPTH
v_depthFromNearPlusOne = (clipCoords.w - czm_currentFrustum.x) + 1.0;
czm_updatePositionDepth(clipCoords);
#endif
}