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