/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{neverReached as o}from"../../../../../../core/compilerUtils.js";import{DecodeSymbolColor as e}from"./DecodeSymbolColor.glsl.js";import{TextureBackedBufferModule as r}from"../../../core/shaderLibrary/TextureBackedBuffer.glsl.js";import{Float4DrawUniform as t}from"../../../core/shaderModules/Float4DrawUniform.js";import{FloatDrawUniform as d}from"../../../core/shaderModules/FloatDrawUniform.js";import{glsl as n,If as a}from"../../../core/shaderModules/glsl.js";import{IntegerDrawUniform as l}from"../../../core/shaderModules/IntegerDrawUniform.js";import{Texture2DUintDrawUniform as i}from"../../../core/shaderModules/Texture2DUintDrawUniform.js";import{olidEnabled as s}from"../../../effects/geometry/olidUtils.js";import{componentDataLayout as c,componentDataOlidLayout as m}from"../../../materials/DefaultLayouts.js";function u(e,r){switch(r.componentData){case 1:return M(e,r);case 0:return h(e,r);case 2:return;default:o(r.componentData)}}const v=(o,e)=>"emissiveSourceMode"===o||"emissiveStrength"===o?e.hasEmission:"olidColor"!==o||9===e.output,C=new i("componentTextureBuffer",o=>o.textureBackedBuffer?.texture),f=new r(c,"componentIndex",C,v),x=new r(m,"componentIndex",C,v);function p(){return s()?x:f}function M(o,r){const{vertex:t,fragment:d}=o,{output:l,hasEmission:i}=r,s=9===l,{getTextureAttribute:c,TextureBackedBufferModule:m}=p();o.include(m,r),o.attributes.add("componentIndex","uint"),o.varyings.add("vExternalColorMixMode","mediump float"),o.varyings.add("vExternalColor","vec4"),s&&o.varyings.add("vObjectAndLayerIdColor","vec4"),i&&(o.varyings.add("emissiveStrength","float"),o.varyings.add("emissiveSource","int")),o.include(e),t.code.add(n`
  float readElevationOffset() {
    return ${c("elevationOffset")};
  }

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

  void forwardObjectAndLayerIdColor() {
    ${a(s,n`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;
  }
`),d.code.add(n`
  void readExternalColor(out vec4 externalColor, out int externalColorMixMode) {
    externalColor = vExternalColor;
    externalColorMixMode = int(vExternalColorMixMode);
  }

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

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