/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{fromValues as e}from"../../../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{Float2PassUniform as o}from"../../shaderModules/Float2PassUniform.js";import{Float3PassUniform as r}from"../../shaderModules/Float3PassUniform.js";import{Float4PassUniform as a}from"../../shaderModules/Float4PassUniform.js";import{Float4sPassUniform as i}from"../../shaderModules/Float4sPassUniform.js";import{FloatPassUniform as t}from"../../shaderModules/FloatPassUniform.js";import{FloatsPassUniform as l}from"../../shaderModules/FloatsPassUniform.js";import{glsl as v,If as s}from"../../shaderModules/glsl.js";import{VisualVariablePassParameters as c,vvColorNumber as n}from"../../../materials/VisualVariablePassParameters.js";const p=8;function f(e,c){const{attributes:f,vertex:u}=e;f.add("position","vec3"),f.add("profileVertexAndNormal","vec4"),f.add("profileAuxData","vec3"),f.add("profileRight","vec2"),f.add("profileUp","vec2"),u.code.add(v`bool isCapVertex() {
return profileAuxData.z == 1.0;
}`),u.uniforms.add(new o("size",e=>e.size));const{hasVVSize:d,hasVVColor:m,hasVVOpacity:x}=c;d?(f.add("sizeFeatureAttribute","float"),u.uniforms.add(new r("vvSizeMinSize",e=>e.vvSize.minSize),new r("vvSizeMaxSize",e=>e.vvSize.maxSize),new r("vvSizeOffset",e=>e.vvSize.offset),new r("vvSizeFactor",e=>e.vvSize.factor),new r("vvSizeFallback",e=>e.vvSize.fallback)),u.code.add(v`vec2 getSize() {
float value = sizeFeatureAttribute;
if (isnan(value)) {
return vvSizeFallback.xz;
}
return size * clamp(vvSizeOffset + value * vvSizeFactor, vvSizeMinSize, vvSizeMaxSize).xz;
}`)):u.code.add(v`vec2 getSize(){
return size;
}`),x?(f.add("opacityFeatureAttribute","float"),u.constants.add("vvOpacityNumber","int",p),u.uniforms.add(new l("vvOpacityValues",p,e=>e.vvOpacity.values),new l("vvOpacityOpacities",p,e=>e.vvOpacity.opacityValues),new t("vvOpacityFallback",e=>e.vvOpacity.fallback,{supportsNaN:!0})),u.code.add(v`
    vec4 applyOpacity(vec4 color) {
      // if we encounter NaN in the color it means the color is in the fallback case where the symbol color
      // is not defined and there is no valid color visual variable override. In this case just return a fully
      // transparent color
      if (isnan(color.r)) {
        return vec4(0);
      }

      float value = opacityFeatureAttribute;

      if (isnan(value)) {
        // If there is a color vv then it will already have taken care of applying the fallback
        return ${s(m,"color","vec4(color.rgb, vvOpacityFallback)")};
      }

      if (value <= vvOpacityValues[0]) {
        return vec4(color.rgb, vvOpacityOpacities[0]);
      }

      for (int i = 1; i < vvOpacityNumber; ++i) {
        if (vvOpacityValues[i] >= value) {
          float f = (value - vvOpacityValues[i-1]) / (vvOpacityValues[i] - vvOpacityValues[i-1]);
          return vec4(color.rgb, mix(vvOpacityOpacities[i-1], vvOpacityOpacities[i], f));
        }
      }

      return vec4( color.rgb, vvOpacityOpacities[vvOpacityNumber - 1]);
    }
    `)):u.code.add(v`vec4 applyOpacity(vec4 color){
return color;
}`),m?(f.add("colorFeatureAttribute","float"),u.constants.add("vvColorNumber","int",n),u.uniforms.add(new l("vvColorValues",n,e=>e.vvColor.values),new i("vvColorColors",n,e=>e.vvColor.colors),new a("vvColorFallback",e=>e.vvColor.fallback)),u.code.add(v`vec4 getColor() {
float value = colorFeatureAttribute;
if (isnan(value)) {
return applyOpacity(vvColorFallback);
}
if (value <= vvColorValues[0]) {
return applyOpacity(vvColorColors[0]);
}
for (int i = 1; i < vvColorNumber; ++i) {
if (vvColorValues[i] >= value) {
float f = (value - vvColorValues[i-1]) / (vvColorValues[i] - vvColorValues[i-1]);
return applyOpacity(mix(vvColorColors[i-1], vvColorColors[i], f));
}
}
return applyOpacity(vvColorColors[vvColorNumber - 1]);
}`)):u.code.add(v`vec4 getColor(){
return applyOpacity(vec4(1, 1, 1, 1));
}`),u.code.add(v`vec3 decompressAxis(vec2 axis) {
float z = 1.0 - abs(axis.x) - abs(axis.y);
return normalize(vec3(axis + sign(axis) * min(z, 0.0), z));
}
vec3 calculateVPos() {
vec2 size = getSize();
vec3 origin = position;
vec3 right = decompressAxis(profileRight);
vec3 up = decompressAxis(profileUp);
vec2 profileVertex = profileVertexAndNormal.xy * size;`),u.code.add(v`if(isCapVertex()) {
float positionOffsetAlongProfilePlaneNormal = profileAuxData.x * size[0];
vec3 forward = cross(up, right);
vec3 offset = right * profileVertex.x + up * profileVertex.y + forward * positionOffsetAlongProfilePlaneNormal;
return origin + offset;
}
vec2 rotationRight = vec2(profileAuxData.x, profileAuxData.y);
float maxDistance = length(rotationRight);`),u.code.add(v`rotationRight = maxDistance > 0.0 ? normalize(rotationRight) : vec2(0, 0);
float rx = dot(profileVertex, rotationRight);
if (abs(rx) > maxDistance) {
vec2 rotationUp = vec2(-rotationRight.y, rotationRight.x);
float ry = dot(profileVertex, rotationUp);
profileVertex = rotationRight * maxDistance * sign(rx) + rotationUp * ry;
}
vec3 offset = right * profileVertex.x + up * profileVertex.y;
return origin + offset;
}`),u.code.add(v`vec3 localNormal() {
vec3 right = decompressAxis(profileRight);
vec3 up = decompressAxis(profileUp);
vec3 normal = right * profileVertexAndNormal.z + up * profileVertexAndNormal.w;
if(isCapVertex()) {
vec3 forward = cross(up, right);
normal += forward * profileAuxData.y;
}
return normal;
}`)}class u extends c{constructor(){super(...arguments),this.size=e(1,1)}}export{f as PathVertexPosition,u as PathVertexPositionPassParameters};
