/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{getDefinesKey as t}from"../../../../vectorTiles/shaders/vtlMaterialUtils.js";import{vtlTextureBindingUnitSprites as e,vtlHighResCutoff as i}from"../../../definitions.js";import{Technique as l}from"../Technique.js";import{VTLShaderFill as s}from"../shaders/vector-tiles/VTLShaderFill.js";import{VTLShaderOutline as n}from"../shaders/vector-tiles/VTLShaderOutline.js";const a=1/65536;class o extends l{constructor(){super(...arguments),this.type=37,this._fillMesh=null,this._outlineMesh=null,this.shaders={fill:new s,outline:new n}}shutdown(){this._fillMesh&&(this._fillMesh.destroy(),this._fillMesh=null),this._outlineMesh&&(this._outlineMesh.destroy(),this._outlineMesh=null)}render(t,e){const{displayLevel:i,renderPass:l,spriteMosaic:s,styleLayer:n,styleLayerUID:a}=t,{tiles:o}=e;let r=!1;for(const x of o)if(x.layerData.has(a)){const t=x.layerData.get(a);if(t.fillIndexCount>0||t.outlineIndexCount>0){r=!0;break}}if(!r)return;const u=n,c=u.getPaintProperty("fill-pattern"),f=void 0!==c,p=f&&c.isDataDriven;let h;if(f&&!p){const t=c.getValue(i);h=s.getMosaicItemPosition(t,!0)}const d=!f&&u.getPaintValue("fill-antialias",i);let M=!0,y=1;if(!f){const t=u.getPaintProperty("fill-color"),e=u.getPaintProperty("fill-opacity");if(!t?.isDataDriven&&!e?.isDataDriven){const t=u.getPaintValue("fill-color",i);y=u.getPaintValue("fill-opacity",i)*t[3],y>=1&&(M=!1)}}if(M&&"opaque"===l)return;const m=u.getPaintValue("fill-translate",i),g=u.getPaintValue("fill-translate-anchor",i);(M||"translucent"!==l)&&this._drawFill(t,a,u,o,m,g,f,h,p);const v=!u.hasDataDrivenOutlineColor&&u.outlineUsesFillColor&&y<1;d&&"opaque"!==l&&!v&&this._drawOutline(t,a,u,o,m,g)}_drawFill(l,s,n,o,r,u,c,f,p){if(c&&!p&&null==f)return;const{context:h,displayLevel:d,state:M,painter:y,pixelRatio:m,spriteMosaic:g}=l,v=m>i?2:1,x=n.fillMaterial,P={transform:{dvsMat:null,displayMat:1===u?M.displayMat3:M.displayViewMat3},config:{depth:n.z+a,fillTranslation:r},patternOptions:p||c&&null!=f?{mosaicSize:[0,0],patternFactor:1,tlbr:[0,0,0,0],texture:{texture:null,unit:0}}:null,color:null,opacity:null,colorMix:null,opacityMix:null},D=c?1:0,_=x.key+t({hasPattern:c});if(null!=f){const{page:t,tl:i,br:l}=f,s=i[0],n=l[1],a=l[0],o=i[1],r=g.getPageSize(t);null!=r&&(g.bind(h,9729,t,e),P.patternOptions={mosaicSize:[r[0],r[1]],patternFactor:1,tlbr:[s,n,a,o],texture:{texture:g.getTexture(t),unit:e}})}let w=-1;for(const t of o){if(!t.layerData.has(s))continue;if(t.key.level!==w){w=t.key.level;const e=x.getUniforms();for(const t of e){const e=t.getValue(n,d,w,g);P[t.name]={value:e}}if(c&&null!=P.patternOptions){const e=Math.max(2**(Math.round(d)-t.key.level),1),i=t.rangeX/(v*t.width*e);P.patternOptions.patternFactor=i}}const i=t.layerData.get(s);if(this._fillMesh=i.getMesh(h,{value:0}),!this._fillMesh)continue;const{transform:l}=P;l.dvsMat=t.transforms.displayViewScreenMat3;const a={shader:this.shaders.fill,uniforms:P,defines:{fillType:D},optionalAttributes:x.getUsedAttributes(),useComputeBuffer:!1,precisionFactors:x.getPrecisionFactors(),materialKey:_};if(p){const l=P.patternOptions,s=i.patternMap;if(!s||null==l)continue;let n=0;for(const[i,o]of s){const s=g.getPageSize(i);null!=s&&(g.bind(h,9729,i,e),l.mosaicSize=s,l.texture={texture:g.getTexture(i),unit:e},y.submitDrawMeshUntyped(h,a,this._fillMesh,{stencilRef:t.stencilRef,parts:[n++]}))}}else y.submitDrawMeshUntyped(h,a,this._fillMesh,{stencilRef:t.stencilRef});t.triangleCount+=i.fillIndexCount/3}}_drawOutline(t,e,i,l,s,n){const{context:o,displayLevel:r,is3D:u,state:c,spriteMosaic:f,painter:p,pixelRatio:h}=t,d=i.outlineMaterial,M=d.key,y=.75/h,m={transform:{dvsMat3:null,displayMat3:1===n?c.displayMat3:c.displayViewMat3},config:{depth:i.z+a,fillTranslation:s,outlineWidth:y},color:null,opacity:null,colorMix:null,opacityMix:null};u||p.setPipelineState({color:{write:[!0,!0,!0,!0],blendMode:"composite"},stencil:{write:!1,test:{compare:514,op:{fail:7680,zFail:7680,zPass:7681},mask:255}},depth:{write:!1,test:515}});let g=-1;for(const a of l){if(!a.layerData.has(e))continue;if(a.key.level!==g){g=a.key.level;const t=d.getUniforms();for(const e of t){const t=e.getValue(i,r,g,f);e.name in m&&(m[e.name]={value:t})}}const t=a.layerData.get(e);if(this._outlineMesh=t.getMesh(o,{value:1}),null==this._outlineMesh)continue;const{transform:l}=m;l.dvsMat3=a.transforms.displayViewScreenMat3;const s={shader:this.shaders.outline,uniforms:m,defines:{},optionalAttributes:d.getUsedAttributes(),useComputeBuffer:!1,precisionFactors:d.getPrecisionFactors(),materialKey:M};p.submitDrawMeshUntyped(o,s,this._outlineMesh,{stencilRef:a.stencilRef}),a.triangleCount+=t.outlineIndexCount/3}}}export{o as VTLTechniqueFill};
