Files
2026-08-11 09:53:42 -04:00

76 lines
3.2 KiB
JavaScript

//This file is automatically rebuilt by the Cesium build process.
export default "uniform highp sampler2D u_vectorSegmentTexture;\n\
uniform highp sampler2D u_vectorWidthTexture;\n\
uniform highp sampler2D u_vectorColorTexture;\n\
uniform highp sampler2D u_vectorSegmentPrimitiveIndicesTexture;\n\
uniform highp sampler2D u_vectorGridCellIndicesTexture;\n\
\n\
// UV-space offset from the closest point on the segment to p.\n\
vec2 vectorOffsetToLine(vec2 p, vec4 line)\n\
{\n\
vec2 a = line.xy;\n\
vec2 b = line.zw;\n\
vec2 ab = b - a;\n\
float abLengthSquared = dot(ab, ab);\n\
if (abLengthSquared < 1.0e-8)\n\
{\n\
return p - a;\n\
}\n\
float t = clamp(dot(p - a, ab) / abLengthSquared, 0.0, 1.0);\n\
return p - (a + t * ab);\n\
}\n\
\n\
ivec2 vectorIndexToUv(int index, ivec2 size)\n\
{\n\
int v = index / size.x;\n\
int u = index - v * size.x;\n\
return ivec2(u, v);\n\
}\n\
\n\
// Drape clamped vector polylines onto the terrain surface. The fragment's\n\
// tile UV picks a grid cell, then only that cell's line segments (packed in\n\
// tile-local UV space) are tested for proximity. Within the line width, the\n\
// vector color is alpha-composited over the terrain (no discard).\n\
vec4 vectorPolylineRender(vec2 vectorUv, vec4 baseColor)\n\
{\n\
// Inverse UV-per-pixel Jacobian: measures line distance in screen pixels so\n\
// width stays constant under anisotropic (oblique) foreshortening.\n\
mat2 screenFromUv = inverse(mat2(dFdx(vectorUv), dFdy(vectorUv)));\n\
int gridWidth = int(texelFetch(u_vectorGridCellIndicesTexture, ivec2(0, 0), 0).r);\n\
int gridHeight = int(texelFetch(u_vectorGridCellIndicesTexture, ivec2(1, 0), 0).r);\n\
int cellX = clamp(int(vectorUv.x * float(gridWidth)), 0, gridWidth - 1);\n\
int cellY = clamp(int(vectorUv.y * float(gridHeight)), 0, gridHeight - 1);\n\
int cellIndex = cellX + cellY * gridWidth;\n\
\n\
int indexEnd = int(texelFetch(u_vectorGridCellIndicesTexture, ivec2(cellIndex + 2, 0), 0).r);\n\
int indexStart = cellIndex == 0\n\
? 0\n\
: int(texelFetch(u_vectorGridCellIndicesTexture, ivec2(cellIndex + 1, 0), 0).r);\n\
\n\
ivec2 segmentTextureSize = textureSize(u_vectorSegmentTexture, 0);\n\
ivec2 primitiveTextureSize = textureSize(u_vectorWidthTexture, 0);\n\
\n\
for (int i = indexStart; i < indexEnd; i++)\n\
{\n\
ivec2 segmentUv = vectorIndexToUv(i, segmentTextureSize);\n\
vec4 segment = texelFetch(u_vectorSegmentTexture, segmentUv, 0);\n\
\n\
int primitiveIndex = int(texelFetch(u_vectorSegmentPrimitiveIndicesTexture, segmentUv, 0).r);\n\
ivec2 primitiveUv = vectorIndexToUv(primitiveIndex, primitiveTextureSize);\n\
\n\
float lineWidth = texelFetch(u_vectorWidthTexture, primitiveUv, 0).r * 255.0;\n\
\n\
vec2 offsetUv = vectorOffsetToLine(vectorUv, segment);\n\
if (length(screenFromUv * offsetUv) < lineWidth)\n\
{\n\
// Alpha-composite vector over terrain.\n\
vec4 vectorColor = texelFetch(u_vectorColorTexture, primitiveUv, 0);\n\
baseColor = vectorColor * vec4(vectorColor.aaa, 1.0) + baseColor * (1.0 - vectorColor.a);\n\
break;\n\
}\n\
}\n\
\n\
return baseColor;\n\
}\n\
";