/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/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{Float3DrawUniform as t}from"../../../core/shaderModules/Float3DrawUniform.js";import{Float3PassUniform as a}from"../../../core/shaderModules/Float3PassUniform.js";import{glsl as l,If as n}from"../../../core/shaderModules/glsl.js";import{Matrix3DrawUniform as d}from"../../../core/shaderModules/Matrix3DrawUniform.js";import{Matrix3PassUniform as m}from"../../../core/shaderModules/Matrix3PassUniform.js";import{Matrix4BindUniform as i}from"../../../core/shaderModules/Matrix4BindUniform.js";import{Texture2DUintDrawUniform as s}from"../../../core/shaderModules/Texture2DUintDrawUniform.js";import{edgeViewTextureLayout as f}from"../../../lib/edgeRendering/bufferLayouts.js";const c=new o({layout:f,itemIndexAttribute:"componentIndex",bufferUniform:new s("componentTextureBuffer",o=>o.componentDataTextureBuffer?.texture)});function u(o,s){const{vertex:f}=o;o.include(e,s);const{silhouette:u,legacy:v,spherical:w,hasModelRotationScale:p}=s;o.attributes.add("componentIndex","uint");const{getTextureAttribute:F,TextureBackedBufferModule:M}=c;o.include(M);const h=8,x=128;f.constants.add("lineWidthFractionFactor","float",h),f.constants.add("extensionLengthOffset","float",x),f.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 = ${u?l`decompressNormal(normal2Compressed)`:l`normal`};

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

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

      return ComponentData(
        vec4(colorValue.rgb, colorValue.a * opacity),
        normal, normal2,
        lineWidth,
        extensionLength,
        type,
        verticalOffset
      );
    }
  `),v?f.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;
}`):(p&&f.uniforms.add(new d("transformNormalGlobalFromModel",o=>o.transformNormalGlobalFromModel)),f.code.add(l`
      vec3 _modelToWorldNormal(vec3 normal) {
        return ${n(p,l`transformNormalGlobalFromModel * `)}normal;
      }
    `)),u?(o.attributes.add("normal2Compressed","vec2"),f.code.add(l`vec3 worldNormal(ComponentData data) {
return _modelToWorldNormal(normalize(data.normal + data.normal2));
}`)):f.code.add(l`vec3 worldNormal(ComponentData data) {
return _modelToWorldNormal(data.normal);
}`),v?f.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;
}`):(f.include(r,s),f.uniforms.add(new m("transformViewFromCameraRelativeRS",o=>o.transformViewFromCameraRelativeRS),new t("transformWorldFromModelTL",o=>o.transformWorldFromModelTL),new t("transformWorldFromModelTH",o=>o.transformWorldFromModelTH),new a("transformWorldFromViewTL",o=>o.transformWorldFromViewTL),new a("transformWorldFromViewTH",o=>o.transformWorldFromViewTH)),p&&f.uniforms.add(new d("transformWorldFromModelRS",o=>o.transformWorldFromModelRS)),f.code.add(l`
      void worldAndViewFromModelPosition(vec3 modelPos, float verticalOffset, out vec3 worldPos, out vec3 viewPos) {

        vec3 rotatedModelPosition = ${n(p,l`transformWorldFromModelRS * `)}modelPos;

        vec3 transformCameraRelativeFromModel = dpAdd(
          transformWorldFromModelTL,
          transformWorldFromModelTH,
          -transformWorldFromViewTL,
          -transformWorldFromViewTH
        );

        worldPos = transformCameraRelativeFromModel + rotatedModelPosition;

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

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