15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//This file is automatically rebuilt by the Cesium build process.
export default "#ifdef GL_OES_standard_derivatives\n\
#extension GL_OES_standard_derivatives : enable\n\
#endif\n\
\n\
uniform sampler2D u_atlas;\n\
\n\
#ifdef VECTOR_TILE\n\
uniform vec4 u_highlightColor;\n\
#endif\n\
\n\
varying vec2 v_textureCoordinates;\n\
varying vec4 v_pickColor;\n\
varying vec4 v_color;\n\
\n\
#ifdef SDF\n\
varying vec4 v_outlineColor;\n\
varying float v_outlineWidth;\n\
#endif\n\
\n\
#ifdef FRAGMENT_DEPTH_CHECK\n\
varying vec4 v_textureCoordinateBounds;                  // the min and max x and y values for the texture coordinates\n\
varying vec4 v_originTextureCoordinateAndTranslate;      // texture coordinate at the origin, billboard translate (used for label glyphs)\n\
varying vec4 v_compressed;                               // x: eyeDepth, y: applyTranslate & enableDepthCheck, z: dimensions, w: imageSize\n\
varying mat2 v_rotationMatrix;\n\
\n\
const float SHIFT_LEFT12 = 4096.0;\n\
const float SHIFT_LEFT1 = 2.0;\n\
\n\
const float SHIFT_RIGHT12 = 1.0 / 4096.0;\n\
const float SHIFT_RIGHT1 = 1.0 / 2.0;\n\
\n\
float getGlobeDepth(vec2 adjustedST, vec2 depthLookupST, bool applyTranslate, vec2 dimensions, vec2 imageSize)\n\
{\n\
    vec2 lookupVector = imageSize * (depthLookupST - adjustedST);\n\
    lookupVector = v_rotationMatrix * lookupVector;\n\
    vec2 labelOffset = (dimensions - imageSize) * (depthLookupST - vec2(0.0, v_originTextureCoordinateAndTranslate.y)); // aligns label glyph with bounding rectangle.  Will be zero for billboards because dimensions and imageSize will be equal\n\
\n\
    vec2 translation = v_originTextureCoordinateAndTranslate.zw;\n\
\n\
    if (applyTranslate)\n\
    {\n\
        // this is only needed for labels where the horizontal origin is not LEFT\n\
        // it moves the label back to where the \"origin\" should be since all label glyphs are set to HorizontalOrigin.LEFT\n\
        translation += (dimensions * v_originTextureCoordinateAndTranslate.xy * vec2(1.0, 0.0));\n\
    }\n\
\n\
    vec2 st = ((lookupVector - translation + labelOffset) + gl_FragCoord.xy) / czm_viewport.zw;\n\
    float logDepthOrDepth = czm_unpackDepth(texture2D(czm_globeDepthTexture, st));\n\
\n\
    if (logDepthOrDepth == 0.0)\n\
    {\n\
        return 0.0; // not on the globe\n\
    }\n\
\n\
    vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, logDepthOrDepth);\n\
    return eyeCoordinate.z / eyeCoordinate.w;\n\
}\n\
#endif\n\
\n\
\n\
#ifdef SDF\n\
\n\
// Get the distance from the edge of a glyph at a given position sampling an SDF texture.\n\
float getDistance(vec2 position)\n\
{\n\
    return texture2D(u_atlas, position).r;\n\
}\n\
\n\
// Samples the sdf texture at the given position and produces a color based on the fill color and the outline.\n\
vec4 getSDFColor(vec2 position, float outlineWidth, vec4 outlineColor, float smoothing)\n\
{\n\
    float distance = getDistance(position);\n\
\n\
    if (outlineWidth > 0.0)\n\
    {\n\
        // Don't get the outline edge exceed the SDF_EDGE\n\
        float outlineEdge = clamp(SDF_EDGE - outlineWidth, 0.0, SDF_EDGE);\n\
        float outlineFactor = smoothstep(SDF_EDGE - smoothing, SDF_EDGE + smoothing, distance);\n\
        vec4 sdfColor = mix(outlineColor, v_color, outlineFactor);\n\
        float alpha = smoothstep(outlineEdge - smoothing, outlineEdge + smoothing, distance);\n\
        return vec4(sdfColor.rgb, sdfColor.a * alpha);\n\
    }\n\
    else\n\
    {\n\
        float alpha = smoothstep(SDF_EDGE - smoothing, SDF_EDGE + smoothing, distance);\n\
        return vec4(v_color.rgb, v_color.a * alpha);\n\
    }\n\
}\n\
#endif\n\
\n\
void main()\n\
{\n\
    vec4 color = texture2D(u_atlas, v_textureCoordinates);\n\
\n\
#ifdef SDF\n\
    float outlineWidth = v_outlineWidth;\n\
    vec4 outlineColor = v_outlineColor;\n\
\n\
    // Get the current distance\n\
    float distance = getDistance(v_textureCoordinates);\n\
\n\
#ifdef GL_OES_standard_derivatives\n\
    float smoothing = fwidth(distance);\n\
    // Get an offset that is approximately half the distance to the neighbor pixels\n\
    // 0.354 is approximately half of 1/sqrt(2)\n\
    vec2 sampleOffset = 0.354 * vec2(dFdx(v_textureCoordinates) + dFdy(v_textureCoordinates));\n\
\n\
    // Sample the center point\n\
    vec4 center = getSDFColor(v_textureCoordinates, outlineWidth, outlineColor, smoothing);\n\
\n\
    // Sample the 4 neighbors\n\
    vec4 color1 = getSDFColor(v_textureCoordinates + vec2(sampleOffset.x, sampleOffset.y), outlineWidth, outlineColor, smoothing);\n\
    vec4 color2 = getSDFColor(v_textureCoordinates + vec2(-sampleOffset.x, sampleOffset.y), outlineWidth, outlineColor, smoothing);\n\
    vec4 color3 = getSDFColor(v_textureCoordinates + vec2(-sampleOffset.x, -sampleOffset.y), outlineWidth, outlineColor, smoothing);\n\
    vec4 color4 = getSDFColor(v_textureCoordinates + vec2(sampleOffset.x, -sampleOffset.y), outlineWidth, outlineColor, smoothing);\n\
\n\
    // Equally weight the center sample and the 4 neighboring samples\n\
    color = (center + color1 + color2 + color3 + color4)/5.0;\n\
#else\n\
    // Just do a single sample\n\
    float smoothing = 1.0/32.0;\n\
    color = getSDFColor(v_textureCoordinates, outlineWidth, outlineColor, smoothing);\n\
#endif\n\
\n\
    color = czm_gammaCorrect(color);\n\
#else\n\
    color = czm_gammaCorrect(color);\n\
    color *= czm_gammaCorrect(v_color);\n\
#endif\n\
\n\
// Fully transparent parts of the billboard are not pickable.\n\
#if !defined(OPAQUE) && !defined(TRANSLUCENT)\n\
    if (color.a < 0.005)   // matches 0/255 and 1/255\n\
    {\n\
        discard;\n\
    }\n\
#else\n\
// The billboard is rendered twice. The opaque pass discards translucent fragments\n\
// and the translucent pass discards opaque fragments.\n\
#ifdef OPAQUE\n\
    if (color.a < 0.995)   // matches < 254/255\n\
    {\n\
        discard;\n\
    }\n\
#else\n\
    if (color.a >= 0.995)  // matches 254/255 and 255/255\n\
    {\n\
        discard;\n\
    }\n\
#endif\n\
#endif\n\
\n\
#ifdef VECTOR_TILE\n\
    color *= u_highlightColor;\n\
#endif\n\
    gl_FragColor = color;\n\
\n\
#ifdef LOG_DEPTH\n\
    czm_writeLogDepth();\n\
#endif\n\
\n\
#ifdef FRAGMENT_DEPTH_CHECK\n\
    float temp = v_compressed.y;\n\
\n\
    temp = temp * SHIFT_RIGHT1;\n\
\n\
    float temp2 = (temp - floor(temp)) * SHIFT_LEFT1;\n\
    bool enableDepthTest = temp2 != 0.0;\n\
    bool applyTranslate = floor(temp) != 0.0;\n\
\n\
    if (enableDepthTest) {\n\
        temp = v_compressed.z;\n\
        temp = temp * SHIFT_RIGHT12;\n\
\n\
        vec2 dimensions;\n\
        dimensions.y = (temp - floor(temp)) * SHIFT_LEFT12;\n\
        dimensions.x = floor(temp);\n\
\n\
        temp = v_compressed.w;\n\
        temp = temp * SHIFT_RIGHT12;\n\
\n\
        vec2 imageSize;\n\
        imageSize.y = (temp - floor(temp)) * SHIFT_LEFT12;\n\
        imageSize.x = floor(temp);\n\
\n\
        vec2 adjustedST = v_textureCoordinates - v_textureCoordinateBounds.xy;\n\
        adjustedST = adjustedST / vec2(v_textureCoordinateBounds.z - v_textureCoordinateBounds.x, v_textureCoordinateBounds.w - v_textureCoordinateBounds.y);\n\
\n\
        float epsilonEyeDepth = v_compressed.x + czm_epsilon1;\n\
        float globeDepth1 = getGlobeDepth(adjustedST, v_originTextureCoordinateAndTranslate.xy, applyTranslate, dimensions, imageSize);\n\
\n\
        // negative values go into the screen\n\
        if (globeDepth1 != 0.0 && globeDepth1 > epsilonEyeDepth)\n\
        {\n\
            float globeDepth2 = getGlobeDepth(adjustedST, vec2(0.0, 1.0), applyTranslate, dimensions, imageSize); // top left corner\n\
            if (globeDepth2 != 0.0 && globeDepth2 > epsilonEyeDepth)\n\
            {\n\
                float globeDepth3 = getGlobeDepth(adjustedST, vec2(1.0, 1.0), applyTranslate, dimensions, imageSize); // top right corner\n\
                if (globeDepth3 != 0.0 && globeDepth3 > epsilonEyeDepth)\n\
                {\n\
                    discard;\n\
                }\n\
            }\n\
        }\n\
    }\n\
#endif\n\
\n\
}\n\
";