/* COPYRIGHT Esri - https://js.arcgis.com/5.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 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 y=!0,M=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);M=u.getPaintValue("fill-opacity",i)*t[3],M>=1&&(y=!1)}}if(y&&"opaque"===l)return;const g=u.getPaintValue("fill-translate",i),m=u.getPaintValue("fill-translate-anchor",i);(y||"translucent"!==l)&&this._drawFill(t,a,u,o,g,m,f,h,p);const v=!u.hasDataDrivenOutlineColor&&u.outlineUsesFillColor&&M<1;d&&"opaque"!==l&&!v&&this._drawOutline(t,a,u,o,g,m)}_drawFill(l,s,n,a,o,r,u,c,f){if(u&&!f&&null==c)return;const{context:p,displayLevel:h,state:d,painter:y,pixelRatio:M,spriteMosaic:g}=l,m=M>i?2:1,v=n.fillMaterial,x={transform:{dvsMat:null,displayMat:1===r?d.displayMat3:d.displayViewMat3},config:{depth:n.z,fillTranslation:o},patternOptions:f||u&&null!=c?{mosaicSize:[0,0],patternFactor:1,tlbr:[0,0,0,0],texture:{texture:null,unit:0}}:null,color:null,opacity:null,colorMix:null,opacityMix:null},_=u?1:0,D=v.key+t({hasPattern:u});if(null!=c){const{page:t,tl:i,br:l}=c,s=i[0],n=l[1],a=l[0],o=i[1],r=g.getPageSize(t);null!=r&&(g.bind(p,9729,t,e),x.patternOptions={mosaicSize:[r[0],r[1]],patternFactor:1,tlbr:[s,n,a,o],texture:{texture:g.getTexture(t),unit:e}})}let P=-1;for(const t of a){if(!t.layerData.has(s))continue;if(t.key.level!==P){P=t.key.level;const e=v.getUniforms();for(const t of e){const e=t.getValue(n,h,P,g);x[t.name]={value:e}}if(u&&null!=x.patternOptions){const e=Math.max(2**(Math.round(h)-t.key.level),1),i=t.rangeX/(m*t.width*e);x.patternOptions.patternFactor=i}}const i=t.layerData.get(s);if(this._fillMesh=i.getMesh(p,{value:0}),!this._fillMesh)continue;const{transform:l}=x;l.dvsMat=t.transforms.displayViewScreenMat3;const a={shader:this.shaders.fill,uniforms:x,defines:{fillType:_},optionalAttributes:v.getUsedAttributes(),useComputeBuffer:!1,precisionFactors:v.getPrecisionFactors(),materialKey:D};if(f){const l=x.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(p,9729,i,e),l.mosaicSize=s,l.texture={texture:g.getTexture(i),unit:e},y.submitDrawMeshUntyped(p,a,this._fillMesh,{stencilRef:t.stencilRef,parts:[n++]}))}}else y.submitDrawMeshUntyped(p,a,this._fillMesh,{stencilRef:t.stencilRef});t.triangleCount+=i.fillIndexCount/3}}_drawOutline(t,e,i,l,s,n){const{context:o,displayLevel:r,state:u,spriteMosaic:c,painter:f,pixelRatio:p}=t,h=i.outlineMaterial,d=h.key,y=.75/p,M={transform:{dvsMat3:null,displayMat3:1===n?u.displayMat3:u.displayViewMat3},config:{depth:i.z+a,fillTranslation:s,outlineWidth:y},color:null,opacity:null,colorMix:null,opacityMix:null};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=h.getUniforms();for(const e of t){const t=e.getValue(i,r,g,c);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:h.getUsedAttributes(),useComputeBuffer:!1,precisionFactors:h.getPrecisionFactors(),materialKey:d};f.submitDrawMeshUntyped(o,s,this._outlineMesh,{stencilRef:a.stencilRef}),a.triangleCount+=t.outlineIndexCount/3}}}export{o as VTLTechniqueFill};
