/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{nextPowerOfTwo as t}from"../../../../../../../core/mathUtils.js";import{create as e}from"../../../../../../../core/libs/gl-matrix-2/factories/mat3f32.js";import{fromValues as r}from"../../../../../../../core/libs/gl-matrix-2/factories/vec4f32.js";import{vtlTextureBindingUnitSprites as s,vtlHighResCutoff as o}from"../../../definitions.js";import{Mesh as i}from"../../../meshing/Mesh.js";import{simplePipelineState as a}from"../../utils.js";import{Technique as n}from"../Technique.js";import{VTLShaderBackground as l}from"../shaders/vector-tiles/VTLShaderBackground.js";import{PrimitiveType as c}from"../../../../../../webgl/enums.js";class u extends n{constructor(){super(...arguments),this.type=35,this._color=r(1,0,0,1),this._patternMatrix=e(),this._mesh=null,this.shaders={background:new l}}render(r,i){if(0===i.tiles.length)return;this._loadResources(r);const{context:n,displayLevel:l,painter:c,spriteMosaic:u,state:h,styleLayer:m}=r,d=m,p=d.backgroundMaterial,f=d.getPaintValue("background-color",l),g=d.getPaintValue("background-opacity",l),y=d.getPaintValue("background-pattern",l),_=void 0!==y;let M,b;const x=(1|window.devicePixelRatio)>o?2:1,w={layerInfo:{depth:d.z,coordRange:i.tiles[0].rangeX},tileInfo:{dvsMat:h.displayViewMat3},solidColor:null,pattern:null,patternMat:{mat:e()}};if(_){const t=u.getMosaicItemPosition(y,!0);if(null!=t){const{tl:e,br:r,page:o}=t;M=r[0]-e[0],b=r[1]-e[1];const i=u.getPageSize(o);null!=i&&(u.bind(n,9729,o,s),w.pattern={tlbr:[e[0],e[1],r[0],r[1]],mosaicSize:i,texture:{texture:u.getTexture(o),unit:s},opacity:g})}}else{const t=f[3]*g;this._color[0]=t*f[0],this._color[1]=t*f[1],this._color[2]=t*f[2],this._color[3]=t,w.solidColor={color:this._color}}c.setPipelineState(a);for(const e of i.tiles){if(w.tileInfo.dvsMat=e.transforms.displayViewScreenMat3,w.layerInfo.coordRange=e.rangeX,_){const r=Math.max(2**(Math.round(l)-e.key.level),1),s=x*e.width*r,o=s/t(M),i=s/t(b);this._patternMatrix[0]=o,this._patternMatrix[4]=i,w.patternMat={mat:this._patternMatrix}}const r={shader:this.shaders.background,uniforms:w,defines:{renderMode:_?1:0},optionalAttributes:p.getUsedAttributes(),useComputeBuffer:!1,precisionFactors:p.getPrecisionFactors(),materialKey:p.key};c.submitDrawMeshUntyped(n,r,this._mesh,{stencilRef:e.stencilRef})}}shutdown(){this._mesh&&(this._mesh.destroy(),this._mesh=null)}_loadResources(t){if(this._mesh)return;const{context:e,styleLayer:r}=t,s=r,o=new Int16Array([0,0,1,0,0,1,1,1]),a=s.backgroundMaterial.geometryLayout;if(!a)return;const n={vertex:{geometryVB:{data:o,layout:a}},groups:[{primitive:c.TRIANGLE_STRIP}],parts:[{group:0,start:0,count:4}]};this._mesh=new i(e,n)}}export{u as VTLTechniqueBackground};
