/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{fromValues as o}from"../../../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{IsNaN as r}from"../../../core/shaderLibrary/util/IsNaN.glsl.js";import{Float2BindUniform as e}from"../../../core/shaderModules/Float2BindUniform.js";import{glsl as a}from"../../../core/shaderModules/glsl.js";import{Matrix3PassUniform as s}from"../../../core/shaderModules/Matrix3PassUniform.js";import{Matrix4BindUniform as t}from"../../../core/shaderModules/Matrix4BindUniform.js";const l=o(.5,-4e-4);function i(o,i){const c=o.vertex;c.include(r),c.constants.add("depthBias","vec2",l),c.uniforms.add(new e("inverseViewport",o=>o.inverseViewport)),i.legacy?(c.uniforms.add(new t("proj",o=>o.camera.projectionMatrix)),c.code.add(a`vec2 calculateProjectedBiasXY(vec4 projPos, vec3 globalNormal) {
float offsetXY = depthBias.x;
vec4 projNormal = proj * localView * vec4(globalNormal, 0.0);
return offsetXY * projPos.w * 2.0 * inverseViewport * normalize(projNormal.xyz).xy;
}`)):(c.uniforms.add(new s("transformNormalViewFromGlobal",o=>o.transformNormalViewFromGlobal),new t("transformProjFromView",o=>o.camera.projectionMatrix)),c.code.add(a`vec2 calculateProjectedBiasXY(vec4 projPos, vec3 globalNormal) {
float offsetXY = depthBias.x;
vec4 projNormal = transformProjFromView * vec4(transformNormalViewFromGlobal * globalNormal, 0.0);
return offsetXY * projPos.w * 2.0 * inverseViewport * normalize(projNormal.xyz).xy;
}`)),c.code.add(a`float _calculateProjectedBiasZ(vec4 projPos) {
float offsetZ = depthBias.y;
return sqrt(max(projPos.z,0.0)) * offsetZ;
}
vec4 adjustProjectedPosition(vec4 projPos, vec3 worldNormal, float lineWidth) {
vec2 offsetXY = calculateProjectedBiasXY(projPos, worldNormal);
if (!isNaN(offsetXY.x) && !isNaN(offsetXY.y)) {
projPos.xy += offsetXY;
}
projPos.z += _calculateProjectedBiasZ(projPos);
return projPos;
}`)}export{i as AdjustProjectedPosition};
