@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
114 lines (90 loc) • 6.45 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.19/LICENSE.txt */
import{addVerticalOffset as e}from"../attributes/VerticalOffset.glsl.js";import{ScreenSizePerspective as t,addScreenSizePerspectiveAlignment as o}from"../util/ScreenSizePerspective.glsl.js";import{addProjViewLocalOrigin as a,addCameraPosition as s,addViewNormal as i,addPixelRatio as n}from"../util/View.glsl.js";import{Float4BindUniform as r}from"../../shaderModules/Float4BindUniform.js";import{FloatBindUniform as f}from"../../shaderModules/FloatBindUniform.js";import{FloatPassUniform as l}from"../../shaderModules/FloatPassUniform.js";import{glsl as c}from"../../shaderModules/glsl.js";const p=.5;function d(d,u){d.include(t),d.attributes.add("position","vec3"),d.attributes.add("normal","vec3"),d.attributes.add("centerOffsetAndDistance","vec4");const v=d.vertex;a(v,u),s(v,u),v.uniforms.add(new r("viewport",e=>e.camera.fullViewport),new l("polygonOffset",e=>e.shaderPolygonOffset),new f("aboveGround",e=>e.camera.aboveGround?1:-1)),u.hasVerticalOffset&&e(v),v.code.add(c`struct ProjectHUDAux {
vec3 posModel;
vec3 posView;
vec3 vnormal;
float distanceToCamera;
float absCosAngle;
};`),v.code.add(c`
float applyHUDViewDependentPolygonOffset(float pointGroundDistance, float absCosAngle, inout vec3 posView) {
float pointGroundSign = ${u.terrainDepthTest?c.float(0):c`sign(pointGroundDistance)`};
if (pointGroundSign == 0.0) {
pointGroundSign = aboveGround;
}
// aboveGround is -1 if camera is below ground, 1 if above ground
// groundRelative is 1 if both camera and symbol are on the same side of the ground, -1 otherwise
float groundRelative = aboveGround * pointGroundSign;
// view angle dependent part of polygon offset emulation: we take the absolute value because the sign that is
// dropped is instead introduced using the ground-relative position of the symbol and the camera
if (polygonOffset > .0) {
float cosAlpha = clamp(absCosAngle, 0.01, 1.0);
float tanAlpha = sqrt(1.0 - cosAlpha * cosAlpha) / cosAlpha;
float factor = (1.0 - tanAlpha / viewport[2]);
// same side of the terrain
if (groundRelative > 0.0) {
posView *= factor;
}
// opposite sides of the terrain
else {
posView /= factor;
}
}
return groundRelative;
}
`),u.draped&&!u.hasVerticalOffset||i(v),u.draped||(v.uniforms.add(new f("perDistancePixelRatio",e=>Math.tan(e.camera.fovY/2)/(e.camera.fullViewport[2]/2))),v.code.add(c`
void applyHUDVerticalGroundOffset(vec3 normalModel, inout vec3 posModel, inout vec3 posView) {
float distanceToCamera = length(posView);
// Compute offset in world units for a half pixel shift
float pixelOffset = distanceToCamera * perDistancePixelRatio * ${c.float(p)};
// Apply offset along normal in the direction away from the ground surface
vec3 modelOffset = normalModel * aboveGround * pixelOffset;
// Apply the same offset also on the view space position
vec3 viewOffset = (viewNormal * vec4(modelOffset, 1.0)).xyz;
posModel += modelOffset;
posView += viewOffset;
}
`)),u.screenCenterOffsetUnitsEnabled&&n(v),u.hasScreenSizePerspective&&o(v),v.code.add(c`
vec4 projectPositionHUD(out ProjectHUDAux aux) {
vec3 centerOffset = centerOffsetAndDistance.xyz;
float pointGroundDistance = centerOffsetAndDistance.w;
aux.posModel = position;
aux.posView = (view * vec4(aux.posModel, 1.0)).xyz;
aux.vnormal = normal;
${u.draped?"":"applyHUDVerticalGroundOffset(aux.vnormal, aux.posModel, aux.posView);"}
// Screen sized offset in world space, used for example for line callouts
// Note: keep this implementation in sync with the CPU implementation, see
// - MaterialUtil.verticalOffsetAtDistance
// - HUDMaterial.applyVerticalOffsetTransformation
aux.distanceToCamera = length(aux.posView);
vec3 viewDirObjSpace = normalize(cameraPosition - aux.posModel);
float cosAngle = dot(aux.vnormal, viewDirObjSpace);
aux.absCosAngle = abs(cosAngle);
${u.hasScreenSizePerspective&&(u.hasVerticalOffset||u.screenCenterOffsetUnitsEnabled)?"vec3 perspectiveFactor = screenSizePerspectiveScaleFactor(aux.absCosAngle, aux.distanceToCamera, screenSizePerspectiveAlignment);":""}
${u.hasVerticalOffset?u.hasScreenSizePerspective?"float verticalOffsetScreenHeight = applyScreenSizePerspectiveScaleFactorFloat(verticalOffset.x, perspectiveFactor);":"float verticalOffsetScreenHeight = verticalOffset.x;":""}
${u.hasVerticalOffset?c`
float worldOffset = clamp(verticalOffsetScreenHeight * verticalOffset.y * aux.distanceToCamera, verticalOffset.z, verticalOffset.w);
vec3 modelOffset = aux.vnormal * worldOffset;
aux.posModel += modelOffset;
vec3 viewOffset = (viewNormal * vec4(modelOffset, 1.0)).xyz;
aux.posView += viewOffset;
// Since we elevate the object, we need to take that into account
// in the distance to ground
pointGroundDistance += worldOffset;`:""}
float groundRelative = applyHUDViewDependentPolygonOffset(pointGroundDistance, aux.absCosAngle, aux.posView);
${u.screenCenterOffsetUnitsEnabled?"":c`
// Apply x/y in view space, but z in screen space (i.e. along posView direction)
aux.posView += vec3(centerOffset.x, centerOffset.y, 0.0);
// Same material all have same z != 0.0 condition so should not lead to
// branch fragmentation and will save a normalization if it's not needed
if (centerOffset.z != 0.0) {
aux.posView -= normalize(aux.posView) * centerOffset.z;
}
`}
vec4 posProj = proj * vec4(aux.posView, 1.0);
${u.screenCenterOffsetUnitsEnabled?u.hasScreenSizePerspective?"float centerOffsetY = applyScreenSizePerspectiveScaleFactorFloat(centerOffset.y, perspectiveFactor);":"float centerOffsetY = centerOffset.y;":""}
${u.screenCenterOffsetUnitsEnabled?"posProj.xy += vec2(centerOffset.x, centerOffsetY) * pixelRatio * 2.0 / viewport.zw * posProj.w;":""}
// constant part of polygon offset emulation
posProj.z -= groundRelative * polygonOffset * posProj.w;
return posProj;
}
`)}export{d as HUD,p as HUDVerticalPixelOffset};