/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{vtlTextureBindingUnitSprites as t,vtlHighResCutoff as e}from"../../../definitions.js";import{Technique as i}from"../Technique.js";import{VTLShaderFill as l}from"../shaders/vector-tiles/VTLShaderFill.js";import{VTLShaderOutline as s}from"../shaders/vector-tiles/VTLShaderOutline.js";const n=1/65536;class a extends i{constructor(){super(...arguments),this.type=37,this._fillMesh=null,this._outlineMesh=null,this.shaders={fill:new l,outline:new s}}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(i,l,s,n,a,o,r,u,c){if(r&&!c&&null==u)return;const{context:f,displayLevel:p,state:h,painter:d,pixelRatio:y,spriteMosaic:M}=i,g=y>e?2:1,m=s.fillMaterial,v={transform:{dvsMat:null,displayMat:1===o?h.displayMat3:h.displayViewMat3},config:{depth:s.z,fillTranslation:a},patternOptions:c||r&&null!=u?{mosaicSize:[0,0],patternFactor:1,tlbr:[0,0,0,0],texture:{texture:null,unit:0}}:null,color:null,opacity:null,colorMix:null,opacityMix:null},x=r?1:0;if(null!=u){const{page:e,tl:i,br:l}=u,s=i[0],n=l[1],a=l[0],o=i[1],r=M.getPageSize(e);null!=r&&(M.bind(f,9729,e,t),v.patternOptions={mosaicSize:[r[0],r[1]],patternFactor:1,tlbr:[s,n,a,o],texture:{texture:M.getTexture(e),unit:t}})}let _=-1;for(const e of n){if(!e.layerData.has(l))continue;if(e.key.level!==_){_=e.key.level;const t=m.getUniforms();for(const e of t){const t=e.getValue(s,p,_,M);v[e.name]={value:t}}if(r&&null!=v.patternOptions){const t=Math.max(2**(Math.round(p)-e.key.level),1),i=e.rangeX/(g*e.width*t);v.patternOptions.patternFactor=i}}const i=e.layerData.get(l);if(this._fillMesh=i.getMesh(f,{value:0}),!this._fillMesh)continue;const{transform:n}=v;n.dvsMat=e.transforms.displayViewScreenMat3;const a={shader:this.shaders.fill,uniforms:v,defines:{fillType:x},optionalAttributes:m.getUsedAttributes(),useComputeBuffer:!1,precisionFactors:m.getPrecisionFactors(),materialKey:m.key};if(c){const l=v.patternOptions,s=i.patternMap;if(!s||null==l)continue;let n=0;for(const[i,o]of s){const s=M.getPageSize(i);null!=s&&(M.bind(f,9729,i,t),l.mosaicSize=s,l.texture={texture:M.getTexture(i),unit:t},d.submitDrawMeshUntyped(f,a,this._fillMesh,{stencilRef:e.stencilRef,parts:[n++]}))}}else d.submitDrawMeshUntyped(f,a,this._fillMesh,{stencilRef:e.stencilRef});e.triangleCount+=i.fillIndexCount/3}}_drawOutline(t,e,i,l,s,a){const{context:o,displayLevel:r,state:u,spriteMosaic:c,painter:f,pixelRatio:p}=t,h=i.outlineMaterial,d=.75/p,y={transform:{dvsMat3:null,displayMat3:1===a?u.displayMat3:u.displayViewMat3},config:{depth:i.z+n,fillTranslation:s,outlineWidth:d},color:null,opacity:null,colorMix:null,opacityMix:null};let M=-1;for(const n of l){if(!n.layerData.has(e))continue;if(n.key.level!==M){M=n.key.level;const t=h.getUniforms();for(const e of t){const t=e.getValue(i,r,M,c);e.name in y&&(y[e.name]={value:t})}}const t=n.layerData.get(e);if(this._outlineMesh=t.getMesh(o,{value:1}),null==this._outlineMesh)continue;const{transform:l}=y;l.dvsMat3=n.transforms.displayViewScreenMat3;const s={shader:this.shaders.outline,uniforms:y,defines:{},optionalAttributes:h.getUsedAttributes(),useComputeBuffer:!1,precisionFactors:h.getPrecisionFactors(),materialKey:h.key};f.submitDrawMeshUntyped(o,s,this._outlineMesh,{stencilRef:n.stencilRef}),n.triangleCount+=t.outlineIndexCount/3}}}export{a as VTLTechniqueFill};
