/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{DecodeSymbolColor as o}from"./DecodeSymbolColor.glsl.js";import{TextureBackedBufferModule as e}from"../../../core/shaderLibrary/TextureBackedBuffer.glsl.js";import{Float4DrawUniform as r}from"../../../core/shaderModules/Float4DrawUniform.js";import{FloatDrawUniform as t}from"../../../core/shaderModules/FloatDrawUniform.js";import{glsl as d,If as n}from"../../../core/shaderModules/glsl.js";import{IntegerDrawUniform as a}from"../../../core/shaderModules/IntegerDrawUniform.js";import{Texture2DUintDrawUniform as l}from"../../../core/shaderModules/Texture2DUintDrawUniform.js";import{olidEnabled as i}from"../../../effects/geometry/olidUtils.js";import{componentDataLayout as s,componentDataOlidLayout as c}from"../../../materials/DefaultLayouts.js";import{oitResolution as u}from"../../../shaders/oitResolution.glsl.js";function m(o,e){switch(e.componentDataType){case 1:return M(o,e);case 0:return h(o,e);case 2:return;default:e.componentDataType}}const f=(o,e)=>"emissiveSourceMode"===o||"emissiveStrength"===o?e.hasEmission:"olidColor"!==o||11===e.output,v=new l("componentTextureBuffer",o=>o.textureBackedBuffer?.texture),C=new e({layout:s,itemIndexAttribute:"componentIndex",bufferUniform:v,fieldFilter:f}),x=new e({layout:c,itemIndexAttribute:"componentIndex",bufferUniform:v,fieldFilter:f});function p(){return i()?x:C}function M(e,r){const{vertex:t,fragment:a}=e,{output:l,hasEmission:i}=r,s=11===l,{getTextureAttribute:c,TextureBackedBufferModule:m}=p();e.include(m,r),e.attributes.add("componentIndex","uint"),e.varyings.add("vExternalColorMixMode","mediump float"),e.varyings.add("vExternalColor","vec4"),s&&e.varyings.add("vObjectAndLayerIdColor","vec4"),i&&(e.varyings.add("emissiveStrength","float"),e.varyings.add("emissiveSource","int")),e.include(o),t.include(u,r),t.code.add(d`
  float readElevationOffset() {
    return ${c("elevationOffset")};
  }

  void forwardEmissiveStrength() {
    ${n(i,d`emissiveStrength = clamp(${c("emissiveStrength")}, 0.0, maxEmissiveStrength);
           emissiveSource = ${c("emissiveSourceMode")} == 0u ? 0 : 1;`)}
  }

  void forwardObjectAndLayerIdColor() {
    ${n(s,d`vObjectAndLayerIdColor = vec4(${c("olidColor")})/255.0;`)}
  }

  void decodeColorAndCastShadow(uvec4 colorAndCastShadowEncoded, out vec4 color, out bool castShadow) {
    uvec4 componentColor = colorAndCastShadowEncoded;
    castShadow = bool(componentColor.b & 1u);
    componentColor.b = componentColor.b & 254u;
    color = vec4(componentColor);
  }

  vec4 forwardExternalColor(out bool castShadows) {
    vec4 componentColor;
    decodeColorAndCastShadow(${c("colorAndCastShadows")}, componentColor, castShadows);

    int decodedColorMixMode;
    vExternalColor = decodeSymbolColor(componentColor, decodedColorMixMode) * 0.003921568627451; // = 1/255;
    vExternalColorMixMode = float(decodedColorMixMode) + 0.5; // add 0.5 to avoid interpolation artifacts

    return vExternalColor;
  }
`),a.code.add(d`
  void readExternalColor(out vec4 externalColor, out int externalColorMixMode) {
    externalColor = vExternalColor;
    externalColorMixMode = int(vExternalColorMixMode);
  }

  void outputObjectAndLayerIdColor() {
     ${s?d`fragColor = vObjectAndLayerIdColor;`:""}
  }
`)}function h(o,e){const{vertex:l,fragment:i}=o;o.varyings.add("vExternalColor","vec4"),i.uniforms.add(new t("emissiveStrength",o=>o.componentParameters.emissiveStrength)),l.uniforms.add(new r("externalColor",o=>o.componentParameters.externalColor)).code.add(d`float readElevationOffset() {
return 0.0;
}
void forwardObjectAndLayerIdColor() {}
void forwardEmissiveStrength() {}
vec4 forwardExternalColor(out bool castShadows) {
vExternalColor = externalColor;
castShadows = true;
return externalColor;
}`);const s=11===e.output;i.uniforms.add(new a("externalColorMixMode",o=>o.componentParameters.externalColorMixMode)).code.add(d`
    void readExternalColor(out vec4 color, out int colorMixMode) {
      color = vExternalColor;
      colorMixMode = externalColorMixMode;
    }

    void outputObjectAndLayerIdColor() {
      ${n(s,"fragColor = vec4(0, 0, 0, 0);")}
    }
  `)}export{m as ComponentData};
