/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{pt2px as e}from"../../../../../../core/screenUtils.js";import{set as r}from"../../../../../../core/libs/gl-matrix-2/math/vec4.js";import{create as t}from"../../../../../../core/libs/gl-matrix-2/factories/vec4f64.js";import{ScreenSizePerspective as l,addScreenSizePerspectiveAlignment as o}from"../util/ScreenSizePerspective.glsl.js";import{addCameraPosition as c}from"../util/View.glsl.js";import{Float4PassUniform as i}from"../../shaderModules/Float4PassUniform.js";import{glsl as s}from"../../shaderModules/glsl.js";class a{constructor(r){this.screenLength=e(r.screenLength),this.minWorldLength=r.minWorldLength??0,this.maxWorldLength=r.maxWorldLength??1/0}}function n(e,r){const t=e.vertex;r.hasVerticalOffset?(d(t),r.hasScreenSizePerspective&&(e.include(l),o(t),c(e.vertex,r)),t.code.add(s`
      vec3 calculateVerticalOffset(vec3 worldPos, vec3 localOrigin) {
        float viewDistance = length((view * vec4(worldPos, 1.0)).xyz);
        ${r.spherical?s`vec3 worldNormal = normalize(worldPos + localOrigin);`:s`vec3 worldNormal = vec3(0.0, 0.0, 1.0);`}
        ${r.hasScreenSizePerspective?s`
            float cosAngle = dot(worldNormal, normalize(worldPos - cameraPosition));
            float verticalOffsetScreenHeight = screenSizePerspectiveScaleFloat(verticalOffset.x, abs(cosAngle), viewDistance, screenSizePerspectiveAlignment);`:s`
            float verticalOffsetScreenHeight = verticalOffset.x;`}
        // Screen sized offset in world space, used for example for line callouts
        float worldOffset = clamp(verticalOffsetScreenHeight * verticalOffset.y * viewDistance, verticalOffset.z, verticalOffset.w);
        return worldNormal * worldOffset;
      }

      vec3 addVerticalOffset(vec3 worldPos, vec3 localOrigin) {
        return worldPos + calculateVerticalOffset(worldPos, localOrigin);
      }
    `)):t.code.add(s`vec3 addVerticalOffset(vec3 worldPos, vec3 localOrigin) { return worldPos; }`)}const f=t();function d(e){e.uniforms.add(new i("verticalOffset",(e,t)=>{const{minWorldLength:l,maxWorldLength:o,screenLength:c}=e.verticalOffset,i=Math.tan(.5*t.camera.fovY)/(.5*t.camera.fullViewport[3]),s=t.camera.pixelRatio||1;return r(f,c*s,i,l,o)}))}export{n as VerticalOffset,a as VerticalOffsetParameters,d as addVerticalOffset};
