@doegis/core
Version:
DOE GIS API
150 lines (114 loc) • 9.4 kB
JavaScript
import{s as e}from"./vec2.js";import{a as i}from"./vec2f64.js";import{SliceDraw as o}from"../views/3d/webgl-engine/core/shaderLibrary/Slice.glsl.js";import{multipassTerrainTest as s}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MultipassTerrainTest.glsl.js";import{Float2PassUniform as n}from"../views/3d/webgl-engine/core/shaderModules/Float2PassUniform.js";import{Float4PassUniform as t}from"../views/3d/webgl-engine/core/shaderModules/Float4PassUniform.js";import{FloatPassUniform as d}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{glsl as a}from"../views/3d/webgl-engine/core/shaderModules/interfaces.js";import{ShaderBuilder as l}from"../views/3d/webgl-engine/core/shaderModules/ShaderBuilder.js";import{Uniform as r}from"../views/3d/webgl-engine/core/shaderModules/Uniform.js";import{VertexAttribute as c}from"../views/3d/webgl-engine/lib/VertexAttribute.js";import{AdjustProjectedPosition as p}from"../views/3d/webgl-engine/shaders/sources/edgeRenderer/AdjustProjectedPosition.glsl.js";import{DiscardByCoverage as P}from"../views/3d/webgl-engine/shaders/sources/edgeRenderer/DiscardByCoverage.glsl.js";import{DiscardNonSilhouetteEdges as u}from"../views/3d/webgl-engine/shaders/sources/edgeRenderer/DiscardNonSilhouetteEdges.glsl.js";import{EdgeUtil as g,EdgeUtilMode as v}from"../views/3d/webgl-engine/shaders/sources/edgeRenderer/EdgeUtil.glsl.js";import{LineAmplitude as f}from"../views/3d/webgl-engine/shaders/sources/edgeRenderer/LineAmplitude.glsl.js";import{LineOffset as m}from"../views/3d/webgl-engine/shaders/sources/edgeRenderer/LineOffset.glsl.js";import{UnpackAttributes as x}from"../views/3d/webgl-engine/shaders/sources/edgeRenderer/UnpackAttributes.glsl.js";function w(i){const r=new l,{vertex:w,fragment:L}=r;return i.legacy&&w.uniforms.add([new A("model"),new A("localView")]),r.include(p,i),r.include(g,i),r.include(f,i),r.include(x,i),r.include(m,i),r.include(o,i),r.include(u,i),r.include(P,i),r.include(s,i),r.varyings.add("vColor","vec4"),r.varyings.add("vRadius","float"),r.varyings.add("vPosition","vec3"),r.varyings.add("vWorldPosition","vec3"),r.varyings.add("vViewPos","vec3"),r.varyings.add("vLineLengthPixels","float"),r.varyings.add("vSizeFalloffFactor","float"),w.uniforms.add([new n("pixelToNDC",((i,o)=>e(h,2/o.camera.fullViewport[2],2/o.camera.fullViewport[3]))),new t("viewport",((e,i)=>i.camera.fullViewport)),new d("pixelRatio",((e,i)=>i.camera.pixelRatio))]),r.attributes.add(c.POSITION0,"vec3"),r.attributes.add(c.POSITION1,"vec3"),r.attributes.add(c.VARIANTOFFSET,"float"),r.attributes.add(c.VARIANTSTROKE,"float"),r.attributes.add(c.VARIANTEXTENSION,"float"),w.code.add(a`
const float opaqueCutoff = 1.0 / 255.0;
void calculateGeometricOutputs(vec3 viewPosV0, vec3 viewPosV1, vec3 worldPosV0, vec3 worldPosV1, vec3 worldNormal, UnpackedAttributes unpackedAttributes) {
vec2 sideness = unpackedAttributes.sideness;
vec2 sidenessNorm = unpackedAttributes.sidenessNorm;
vWorldPosition = mix(worldPosV0, worldPosV1, sidenessNorm.y).xyz;
vec3 viewPos = mix(viewPosV0, viewPosV1, sidenessNorm.y);
vViewPos = viewPos;
vec4 projPosV0 = projFromViewPosition(viewPosV0);
vec4 projPosV1 = projFromViewPosition(viewPosV1);
vec4 projPos = projFromViewPosition(viewPos);
vec3 screenSpaceLineNDC = (projPosV1.xyz / projPosV1.w - projPosV0.xyz / projPosV0.w);
vec2 ndcToPixel = viewport.zw * 0.5;
vec2 screenSpaceLinePixels = screenSpaceLineNDC.xy * ndcToPixel;
float lineLengthPixels = length(screenSpaceLinePixels);
float dzPerPixel = screenSpaceLineNDC.z / lineLengthPixels;
vec2 screenSpaceDirection = screenSpaceLinePixels / lineLengthPixels;
vec2 perpendicularScreenSpaceDirection = vec2(screenSpaceDirection.y, -screenSpaceDirection.x) * sideness.x;
float falloffFactor = distanceBasedPerspectiveFactor(-viewPos.z) * pixelRatio;
float lineWidthPixels = unpackedAttributes.lineWidthPixels * falloffFactor;
float extensionLengthPixels = calculateExtensionLength(unpackedAttributes.extensionLengthPixels, lineLengthPixels) * falloffFactor;
float lineAmplitudePixels = calculateLineAmplitude(unpackedAttributes) * pixelRatio;
vSizeFalloffFactor = falloffFactor;
float lineWidthAndAmplitudePixels = lineWidthPixels + lineAmplitudePixels + lineAmplitudePixels;
float extendedLineLengthPixels = lineLengthPixels + extensionLengthPixels + extensionLengthPixels;
${i.antialiasing?a`
const float aaPaddingPixels = 1.0;
// Line size with padding
float halfAAPaddedLineWidthAndAmplitudePixels = lineWidthAndAmplitudePixels * 0.5 + aaPaddingPixels;
float aaPaddedRoundedCapSizePixels = lineWidthPixels * 0.5 + aaPaddingPixels;`:a`
float halfAAPaddedLineWidthAndAmplitudePixels = max(lineWidthAndAmplitudePixels, 1.0) * 0.5;
float aaPaddedRoundedCapSizePixels = max(lineWidthPixels, 1.0) * 0.5;`}
// Half line width in NDC including padding for anti aliasing
vec2 halfAAPaddedLineWidthAndAmplitudeNDC = halfAAPaddedLineWidthAndAmplitudePixels * pixelToNDC;
vec2 aaPaddedRoundedCapSizeNDC = aaPaddedRoundedCapSizePixels * pixelToNDC;
vec2 extensionLengthNDC = extensionLengthPixels * pixelToNDC;
// Compute screen space position of vertex, offsetting for line size and end caps
vec2 ndcOffset = (
screenSpaceDirection * sideness.y * (aaPaddedRoundedCapSizeNDC + extensionLengthNDC)
+ perpendicularScreenSpaceDirection * halfAAPaddedLineWidthAndAmplitudeNDC
);
projPos.xy += ndcOffset * projPos.w;
projPos.z += (dzPerPixel * (aaPaddedRoundedCapSizePixels + extensionLengthPixels)) * sideness.y * projPos.w;
projPos = adjustProjectedPosition(projPos, worldNormal, 1.0 + max((lineWidthAndAmplitudePixels - 1.0) * 0.5, 0.0));
// Line length with end caps
float aaPaddedLineWithCapsLengthPixels = extendedLineLengthPixels + aaPaddedRoundedCapSizePixels + aaPaddedRoundedCapSizePixels;
float pixelPositionAlongLine = aaPaddedLineWithCapsLengthPixels * sidenessNorm.y - aaPaddedRoundedCapSizePixels;
// Position in pixels with origin at first vertex of line segment
vPosition = vec3(
halfAAPaddedLineWidthAndAmplitudePixels * sideness.x,
pixelPositionAlongLine,
pixelPositionAlongLine / extendedLineLengthPixels
);
// The line width radius in pixels
vRadius = lineWidthPixels * 0.5;
vLineLengthPixels = extendedLineLengthPixels;
// discard short edges below a certain length threshold
${i.mode===v.SKETCH?a`
if (lineLengthPixels <= 3.0) {
gl_Position = vec4(10.0, 10.0, 10.0, 1.0);
return;
}`:i.mode===v.MIXED?a`
if (lineLengthPixels <= 3.0 && unpackedAttributes.type <= 0.0) {
gl_Position = vec4(10.0, 10.0, 10.0, 1.0);
return;
}`:""}
gl_Position = projPos;
}
void main() {
ComponentData component = readComponentData();
UnpackedAttributes unpackedAttributes = unpackAttributes(component);
vec3 worldPosV0, worldPosV1, viewPosV0, viewPosV1;
worldAndViewFromModelPosition(position0, component.verticalOffset, worldPosV0, viewPosV0);
worldAndViewFromModelPosition(position1, component.verticalOffset, worldPosV1, viewPosV1);
// Component color
vColor = component.color;
// Discard fully transparent edges
if (vColor.a < opaqueCutoff) {
gl_Position = vec4(10.0, 10.0, 10.0, 1.0);
return;
}
if (discardNonSilhouetteEdges(viewPosV0, worldPosV0)) {
return;
}
// General geometric computation for all types of edges
calculateGeometricOutputs(viewPosV0, viewPosV1, worldPosV0, worldPosV1, worldNormal(), unpackedAttributes);
// Specific computation for different edge styles
calculateStyleOutputs(unpackedAttributes);
}
`),L.code.add(a`
vec2 lineWithCapsDistance(float radius, vec2 position, float lineLength) {
float positionX = position.x - calculateLineOffset();
if (radius < 1.0) {
float coverageX = clamp(min(radius, positionX + 0.5) - max(-radius, positionX - 0.5), 0.0, 1.0);
float coverageY = clamp(min(lineLength, position.y + 0.5) - max(0.0, position.y - 0.5), 0.0, 1.0);
return vec2(0.5 - min(coverageX, coverageY), 0.0);
}
else {
// Between -radius -> 0 for start cap, 0 for line, 0 -> radius
float positionOnCap = position.y - clamp(position.y, 0.0, lineLength);
vec2 lineToPosition = vec2(positionX, positionOnCap);
return vec2(length(lineToPosition) - radius, positionOnCap / radius);
}
}
void main() {
${i.hasMultipassTerrain?"terrainDepthTest(gl_FragCoord, vViewPos.z);":""}
float radius = vRadius * calculateLinePressure();
vec2 distance = lineWithCapsDistance(radius, vPosition.xy, vLineLengthPixels);
float coverage = clamp(0.5 - distance.x, 0.0, 1.0);
discardByCoverage(radius, coverage);
discardBySlice(vWorldPosition);
gl_FragColor = vec4(vColor.rgb, vColor.a * coverage);
}
`),r}const h=i();class A extends r{constructor(e){super(e,"mat4")}}const L=Object.freeze(Object.defineProperty({__proto__:null,build:w},Symbol.toStringTag,{value:"Module"}));export{L as E,w as b};