/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{TextureBackedBufferModule as o}from"../../../core/shaderLibrary/TextureBackedBuffer.glsl.js";import{NormalAttribute as e}from"../../../core/shaderLibrary/attributes/NormalAttribute.glsl.js";import{DoublePrecision as r}from"../../../core/shaderLibrary/util/DoublePrecision.glsl.js";import{Float3PassUniform as t}from"../../../core/shaderModules/Float3PassUniform.js";import{glsl as a}from"../../../core/shaderModules/glsl.js";import{Matrix3PassUniform as l}from"../../../core/shaderModules/Matrix3PassUniform.js";import{Matrix4BindUniform as n}from"../../../core/shaderModules/Matrix4BindUniform.js";import{Texture2DUintDrawUniform as d}from"../../../core/shaderModules/Texture2DUintDrawUniform.js";import{edgeViewTextureLayout as m}from"../../../lib/edgeRendering/bufferLayouts.js";import{Uniform as s}from"../../../../../webgl/Uniform.js";const i=new o({layout:m,itemIndexAttribute:"componentIndex",bufferUniform:new d("componentTextureBuffer",o=>o.componentDataTextureBuffer?.texture)});function c(o,d){const{vertex:m}=o;o.include(e,d);const{silhouette:s,legacy:c,spherical:f}=d;o.attributes.add("componentIndex","uint");const{getTextureAttribute:w,TextureBackedBufferModule:p}=i;o.include(p);const x=8,F=128;m.constants.add("lineWidthFractionFactor","float",x),m.constants.add("extensionLengthOffset","float",F),m.code.add(a`
    struct ComponentData {
      vec4 color;
      vec3 normal;
      vec3 normal2;
      float lineWidth;
      float extensionLength;
      float type;
      float verticalOffset;
    };

    ComponentData readComponentData() {
      vec3 normal = normalModel();
      vec3 normal2 = ${s?a`decompressNormal(normal2Compressed)`:a`normal`};

      vec4 colorValue = ${w("color")};
      float lineWidth = float(${w("lineWidth")}) / lineWidthFractionFactor;
      float extensionLength = float(${w("extensionLength")}) - extensionLengthOffset;
      // SOLID (=0/255) needs to be > 0.0, SKETCHY (=1/255) needs to be <= 0;
      float type = -(float(${w("materialType")})) + 0.5;
      float opacity = ${w("opacity")};

      float verticalOffset = ${w("elevationOffset")};

      return ComponentData(
        vec4(colorValue.rgb, colorValue.a * opacity),
        normal, normal2,
        lineWidth,
        extensionLength,
        type,
        verticalOffset
      );
    }
  `),c?m.code.add(a`vec3 _modelToWorldNormal(vec3 normal) {
return (model * vec4(normal, 0.0)).xyz;
}
vec3 _modelToViewNormal(vec3 normal) {
return (localView * model * vec4(normal, 0.0)).xyz;
}`):m.uniforms.add(new u("transformNormalGlobalFromModel")).code.add(a`vec3 _modelToWorldNormal(vec3 normal) {
return transformNormalGlobalFromModel * normal;
}`),s?(o.attributes.add("normal2Compressed","vec2"),m.code.add(a`vec3 worldNormal(ComponentData data) {
return _modelToWorldNormal(normalize(data.normal + data.normal2));
}`)):m.code.add(a`vec3 worldNormal(ComponentData data) {
return _modelToWorldNormal(data.normal);
}`),c?m.code.add(a`void worldAndViewFromModelPosition(vec3 modelPos, float verticalOffset, out vec3 worldPos, out vec3 viewPos) {
worldPos = (model * vec4(modelPos, 1.0)).xyz;
viewPos = (localView * vec4(worldPos, 1.0)).xyz;
}`):(m.include(r),m.uniforms.add(new v("transformWorldFromModelTL"),new v("transformWorldFromModelTH"),new u("transformWorldFromModelRS"),new l("transformViewFromCameraRelativeRS",o=>o.transformViewFromCameraRelativeRS),new t("transformWorldFromViewTL",o=>o.transformWorldFromViewTL),new t("transformWorldFromViewTH",o=>o.transformWorldFromViewTH)).code.add(a`
      void worldAndViewFromModelPosition(vec3 modelPos, float verticalOffset, out vec3 worldPos, out vec3 viewPos) {
        vec3 rotatedModelPosition = transformWorldFromModelRS * modelPos;
        vec3 transformCameraRelativeFromModel = dpAdd(
          transformWorldFromModelTL,
          transformWorldFromModelTH,
          -transformWorldFromViewTL,
          -transformWorldFromViewTH
        );

        worldPos = transformCameraRelativeFromModel + rotatedModelPosition;

        if (verticalOffset != 0.0) {
          vec3 vUp = ${f?"normalize(transformWorldFromModelTL + rotatedModelPosition);":"vec3(0.0, 0.0, 1.0);"}
          worldPos += verticalOffset * vUp;
        }

        viewPos = transformViewFromCameraRelativeRS * worldPos;
      }
    `)),m.uniforms.add(new n("transformProjFromView",o=>o.camera.projectionMatrix)).code.add(a`vec4 projFromViewPosition(vec3 position) {
return transformProjFromView * vec4(position, 1.0);
}`),m.code.add(a`float calculateExtensionLength(float extensionLength, float lineLength) {
return extensionLength / (log2(max(1.0, 256.0 / lineLength)) * 0.2 + 1.0);
}`)}function f(o){return 1===o||2===o}class u extends s{constructor(o){super(o,"mat3")}}class v extends s{constructor(o){super(o,"vec3")}}export{c as EdgeUtil,f as usesSketchLogic};
