/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
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{Float3DrawUniform as t}from"../../../core/shaderModules/Float3DrawUniform.js";import{Float3PassUniform as a}from"../../../core/shaderModules/Float3PassUniform.js";import{glsl as l}from"../../../core/shaderModules/glsl.js";import{Matrix3DrawUniform as n}from"../../../core/shaderModules/Matrix3DrawUniform.js";import{Matrix3PassUniform as d}from"../../../core/shaderModules/Matrix3PassUniform.js";import{Matrix4BindUniform as m}from"../../../core/shaderModules/Matrix4BindUniform.js";import{Texture2DUintDrawUniform as i}from"../../../core/shaderModules/Texture2DUintDrawUniform.js";import{edgeViewTextureLayout as s}from"../../../lib/edgeRendering/bufferLayouts.js";const c=new o(s,"componentIndex",new i("componentTextureBuffer",o=>o.componentDataTextureBuffer?.texture));function f(o,i){const{vertex:s}=o;o.include(e,i);const{silhouette:f,legacy:u,spherical:v}=i;o.attributes.add("componentIndex","uint");const{getTextureAttribute:w,TextureBackedBufferModule:p}=c;o.include(p);const F=8,M=128;s.constants.add("lineWidthFractionFactor","float",F),s.constants.add("extensionLengthOffset","float",M),s.code.add(l`
    struct ComponentData {
      vec4 color;
      vec3 normal;
      vec3 normal2;
      float lineWidth;
      float extensionLength;
      float type;
      float verticalOffset;
    };

    ComponentData readComponentData() {
      vec3 normal = normalModel();
      vec3 normal2 = ${f?l`decompressNormal(normal2Compressed)`:l`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
      );
    }
  `),u?s.code.add(l`vec3 _modelToWorldNormal(vec3 normal) {
return (model * vec4(normal, 0.0)).xyz;
}
vec3 _modelToViewNormal(vec3 normal) {
return (localView * model * vec4(normal, 0.0)).xyz;
}`):(s.uniforms.add(new n("transformNormalGlobalFromModel",o=>o.transformNormalGlobalFromModel)),s.code.add(l`vec3 _modelToWorldNormal(vec3 normal) {
return transformNormalGlobalFromModel * normal;
}`)),f?(o.attributes.add("normal2Compressed","vec2"),s.code.add(l`vec3 worldNormal(ComponentData data) {
return _modelToWorldNormal(normalize(data.normal + data.normal2));
}`)):s.code.add(l`vec3 worldNormal(ComponentData data) {
return _modelToWorldNormal(data.normal);
}`),u?s.code.add(l`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;
}`):(s.include(r,i),s.uniforms.add(new d("transformViewFromCameraRelativeRS",o=>o.transformViewFromCameraRelativeRS),new n("transformWorldFromModelRS",o=>o.transformWorldFromModelRS),new t("transformWorldFromModelTL",o=>o.transformWorldFromModelTL),new t("transformWorldFromModelTH",o=>o.transformWorldFromModelTH),new a("transformWorldFromViewTL",o=>o.transformWorldFromViewTL),new a("transformWorldFromViewTH",o=>o.transformWorldFromViewTH)),s.code.add(l`
      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 = ${v?"normalize(transformWorldFromModelTL + rotatedModelPosition);":"vec3(0.0, 0.0, 1.0);"}
          worldPos += verticalOffset * vUp;
        }

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