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