/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{MaskedColorDefinition as o,MultiplyMaskedColors as e,CreateMaskedFromNaNColor as r}from"../attributes/MaskedColor.glsl.js";import{Float3PassUniform as a}from"../../shaderModules/Float3PassUniform.js";import{Float4PassUniform as t}from"../../shaderModules/Float4PassUniform.js";import{Float4sPassUniform as v}from"../../shaderModules/Float4sPassUniform.js";import{FloatsPassUniform as l}from"../../shaderModules/FloatsPassUniform.js";import{glsl as i}from"../../shaderModules/glsl.js";import{Matrix3PassUniform as s}from"../../shaderModules/Matrix3PassUniform.js";import{vvColorNumber as n}from"../../../materials/VisualVariablePassParameters.js";function u(u,c){const{vertex:m,attributes:d}=u;c.hasVVInstancing&&(c.hasVVSize||c.hasVVColor)&&d.add("instanceFeatureAttribute","vec4"),c.hasVVSize?(m.uniforms.add(new a("vvSizeMinSize",o=>o.vvSize.minSize)),m.uniforms.add(new a("vvSizeMaxSize",o=>o.vvSize.maxSize)),m.uniforms.add(new a("vvSizeOffset",o=>o.vvSize.offset)),m.uniforms.add(new a("vvSizeFactor",o=>o.vvSize.factor)),m.uniforms.add(new a("vvSizeFallback",o=>o.vvSize.fallback)),m.uniforms.add(new s("vvSymbolRotationMatrix",o=>o.vvSize.symbolRotationMatrix)),m.uniforms.add(new a("vvSymbolAnchor",o=>o.vvSize.symbolAnchor)),m.code.add(i`vec3 vvScale(vec4 _featureAttribute) {
if (isnan(_featureAttribute.x)) {
return vvSizeFallback;
}
return clamp(vvSizeOffset + _featureAttribute.x * vvSizeFactor, vvSizeMinSize, vvSizeMaxSize);
}
vec4 vvTransformPosition(vec3 position, vec4 _featureAttribute) {
return vec4(vvSymbolRotationMatrix * ( vvScale(_featureAttribute) * (position + vvSymbolAnchor)), 1.0);
}`),m.code.add(i`
      const float eps = 1.192092896e-07;
      vec4 vvTransformNormal(vec3 _normal, vec4 _featureAttribute) {
        vec3 scale = max(vvScale(_featureAttribute), eps);
        return vec4(vvSymbolRotationMatrix * _normal / scale, 1.0);
      }

      ${c.hasVVInstancing?i`
      vec4 vvLocalNormal(vec3 _normal) {
        return vvTransformNormal(_normal, instanceFeatureAttribute);
      }

      vec4 localPosition() {
        return vvTransformPosition(${m.inputs.get("position")}, instanceFeatureAttribute);
      }`:""}
    `)):m.code.add(i`
      vec4 localPosition() { return vec4(${m.inputs.get("position")}, 1.0); }
      vec4 vvLocalNormal(vec3 _normal) { return vec4(_normal, 1.0); }
    `),u.vertex.include(o),c.hasVVColor?(m.constants.add("vvColorNumber","int",n),m.uniforms.add(new l("vvColorValues",n,o=>o.vvColor.values),new v("vvColorColors",n,o=>o.vvColor.colors),new t("vvColorFallback",o=>o.vvColor.fallback,{supportsNaN:!0})),c.hasVVInstancing&&(u.vertex.include(e),u.vertex.include(r)),m.code.add(i`
      vec4 interpolateVVColor(float value) {
        if (isnan(value)) {
          return vvColorFallback;
        }

        if (value <= vvColorValues[0]) {
          return 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 mix(vvColorColors[i-1], vvColorColors[i], f);
          }
        }
        return vvColorColors[vvColorNumber - 1];
      }

      vec4 vvGetColor(vec4 featureAttribute) {
        return interpolateVVColor(featureAttribute.y);
      }

      ${c.hasVVInstancing?i`
            vec4 vvColor() {
              return vvGetColor(instanceFeatureAttribute);
            }

            MaskedColor applyVVColor(MaskedColor color) {
              return multiplyMaskedColors(color, createMaskedFromNaNColor(vvColor()));
            }
            `:i`
            vec4 vvColor() {
              return vec4(1.0);
            }

            MaskedColor applyVVColor(MaskedColor color) {
              return color;
            }
            `}
    `)):m.code.add(i`vec4 vvColor() {
return vec4(1.0);
}
MaskedColor applyVVColor(MaskedColor color) {
return color;
}`)}export{u as VisualVariables};
