/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{vtlTextureBindingUnitSprites as t,vtlHighResCutoff as e}from"../definitions.js";import i from"./WGLBrush.js";import{PrimitiveType as n,DataType as a}from"../../../../webgl/enums.js";const l=1/65536;class r extends i{constructor(){super(...arguments),this._fillProgramOptions={id:!1,pattern:!1},this._outlineProgramOptions={id:!1}}dispose(){}drawMany(t,e){const{displayLevel:i,renderPass:n,spriteMosaic:a,styleLayerUID:l}=t;let r=!1;for(const _ of e)if(_.layerData.has(l)){const t=_.layerData.get(l);if(t.fillIndexCount>0||t.outlineIndexCount>0){r=!0;break}}if(!r)return;const o=t.styleLayer,s=o.getPaintProperty("fill-pattern"),f=void 0!==s,u=f&&s.isDataDriven;let c;if(f&&!u){const t=s.getValue(i);c=a.getMosaicItemPosition(t,!0)}const d=!f&&o.getPaintValue("fill-antialias",i);let p=!0,m=1;if(!f){const t=o.getPaintProperty("fill-color"),e=o.getPaintProperty("fill-opacity");if(!t?.isDataDriven&&!e?.isDataDriven){const t=o.getPaintValue("fill-color",i);m=o.getPaintValue("fill-opacity",i)*t[3],m>=1&&(p=!1)}}if(p&&"opaque"===n)return;const y=o.getPaintValue("fill-translate",i),g=o.getPaintValue("fill-translate-anchor",i);(p||"translucent"!==n)&&this._drawFill(t,l,o,e,y,g,f,c,u);const M=!o.hasDataDrivenOutlineColor&&o.outlineUsesFillColor&&m<1;d&&"opaque"!==n&&!M&&this._drawOutline(t,l,o,e,y,g)}_drawFill(i,r,o,s,f,u,c,d,p){if(c&&!p&&null==d)return;const{context:m,displayLevel:y,state:g,painter:M,pixelRatio:_,spriteMosaic:v,requestRender:P,allowDelayedRender:E}=i,U=o.fillMaterial,x=M.vectorTilesMaterialManager,D=_>e?2:1,I=this._fillProgramOptions;I.pattern=c;const S=x.getMaterialProgram(m,U,I);if(E&&null!=P&&!S.compiled)return void P();if(m.useProgram(S),null!=d){const{page:e}=d,i=v.getPageSize(e);null!=i&&(v.bind(m,9729,e,t),S.setUniform2fv("u_mosaicSize",i),S.setUniform1i("u_texture",t))}S.setUniformMatrix3fv("u_displayMat3",1===u?g.displayMat3:g.displayViewMat3),S.setUniform2fv("u_fillTranslation",f),S.setUniform1f("u_depth",o.z+l);let h=-1;for(const e of s){if(!e.layerData.has(r))continue;e.key.level!==h&&(h=e.key.level,U.setDataUniforms(S,y,o,h,v));const i=e.layerData.get(r);if(!i.fillIndexCount)continue;i.prepareForRendering(m);const l=i.fillVAO;if(null!=l){if(m.bindVAO(l),S.setUniformMatrix3fv("u_dvsMat3",e.transforms.displayViewScreenMat3),m.setStencilFunction(514,e.stencilRef,255),c){const t=Math.max(2**(Math.round(y)-e.key.level),1),i=e.rangeX/(D*e.width*t);S.setUniform1f("u_patternFactor",i)}if(p){const e=i.patternMap;if(!e)continue;for(const[i,l]of e){const e=v.getPageSize(i);null!=e&&(v.bind(m,9729,i,t),S.setUniform2fv("u_mosaicSize",e),S.setUniform1i("u_texture",t),m.drawElements(n.TRIANGLES,l[1],a.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*l[0]))}}else m.drawElements(n.TRIANGLES,i.fillIndexCount,a.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*i.fillIndexStart);e.triangleCount+=i.fillIndexCount/3}}}_drawOutline(t,e,i,r,o,s){const{context:f,displayLevel:u,state:c,painter:d,pixelRatio:p,spriteMosaic:m,requestRender:y,allowDelayedRender:g}=t,M=i.outlineMaterial,_=d.vectorTilesMaterialManager,v=.75/p,P=this._outlineProgramOptions,E=_.getMaterialProgram(f,M,P);if(g&&null!=y&&!E.compiled)return void y();f.useProgram(E),E.setUniformMatrix3fv("u_displayMat3",1===s?c.displayMat3:c.displayViewMat3),E.setUniform2fv("u_fillTranslation",o),E.setUniform1f("u_depth",i.z+l),E.setUniform1f("u_outline_width",v);let U=-1;for(const l of r){if(!l.layerData.has(e))continue;l.key.level!==U&&(U=l.key.level,M.setDataUniforms(E,u,i,U,m));const t=l.layerData.get(e);if(t.prepareForRendering(f),!t.outlineIndexCount)continue;const r=t.outlineVAO;null!=r&&(f.bindVAO(r),E.setUniformMatrix3fv("u_dvsMat3",l.transforms.displayViewScreenMat3),f.setStencilFunction(514,l.stencilRef,255),f.drawElements(n.TRIANGLES,t.outlineIndexCount,a.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*t.outlineIndexStart),l.triangleCount+=t.outlineIndexCount/3)}}}export{r as WGLBrushVTLFill};
