/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{set as e,identity as t,translate as i,rotate as s,multiply as l}from"../../../../core/libs/gl-matrix-2/math/mat3.js";import{create as n}from"../../../../core/libs/gl-matrix-2/factories/mat3f64.js";import{toRadian as r}from"../../../../core/libs/gl-matrix-2/math/common.js";import"../../tiling/TileInfoView.js";import a from"../../tiling/TileKey.js";import"../../tiling/TileQueue.js";import"../../tiling/TileStrategy.js";import{tilePixelSize as o,tileCoordSize as d,tileCoordinateScale as c}from"./constants.js";import{declutterSingleTile as h}from"./decluttering/jobsUtil.js";import{RenderableTile as u}from"../webgl/RenderableTile.js";import{TileReshuffleManager as m}from"../webgl/TileReshuffleManager.js";class p{constructor(){this._renderParams={reshuffleManager:new m,context:null,drawPhase:1,state:new g,stationary:!0,pixelRatio:1,displayLevel:-1,requiredLevel:-1,globalOpacity:1,renderPass:"background",styleLayer:null,styleLayerUID:-1,painter:null,glyphMosaic:null,spriteMosaic:null,profiler:null,renderingOptions:null,requestRender:null,allowDelayedRender:!1,deltaTime:-1,timeline:null,time:0,blendMode:null,dataUploadCounter:0,effects:null,inFadeTransition:!1,requireFBO:!1,highlightGradient:null,stencilSymbols:!0,is3D:!0,backgroundColor:null,animationsEnabled:!0},this._backgroundTile=new u(new a(0,0,0,0),0,0,0,o,o,d,d),this._heading=0,this._declutteredForHeading=new WeakMap}dispose(){this._renderParams=null}renderBackground(e,t,i,s,l,n,r,a,o,d){const c=this._backgroundTile;this._updateRenderParameters(e,t,c,i,l,n,r,a,o,d),this._renderParams.stencilSymbols=!1,i.drawBackground(this._renderParams,c,s)}renderContent(e,t,i,s,l,n,r,a,o,d,c,h){this._declutter(i),s.forAll(({sourceLayerInfo:{data:e}})=>this._declutter(e));const u=s.length>0;u&&this._stencilSymbols(e,t,i,s,l,n,r,a,o,d,c,h);let m=1;i.stencilRef=u?m:0,e.setStencilFunction(514,i.stencilRef,255),this._render(e,t,i,l,n,r,a,o,d,c,h),s.forAll(({sourceLod:t,offset:i,sourceLayerInfo:{data:s}})=>{s.stencilRef=++m,this._renderSymbols(e,t,s,l,n,r,a,i,d,c,h)})}_stencilSymbols(e,t,i,s,l,n,r,a,o,d,c,h){let u=1;i.stencilRef=u,e.setDepthTestEnabled(!1),e.setDepthWriteEnabled(!1),e.setStencilTestEnabled(!0),e.setBlendingEnabled(!1),e.setColorMask(!1,!1,!1,!1),e.setStencilOp(7680,7680,7681),e.setStencilWriteMask(255),e.setStencilFunction(519,i.stencilRef,255),this._stencilTileSymbols(e,t,i,l,n,r,a,o,d,c,h),s.forAll(({sourceLod:t,offset:i,sourceLayerInfo:{data:s}})=>{s.stencilRef=++u,e.setStencilFunction(519,s.stencilRef,255),this._stencilTileSymbols(e,t,s,l,n,r,a,i,d,c,h)}),e.setStencilWriteMask(0),e.setBlendingEnabled(!0),e.setDepthTestEnabled(!0),e.setColorMask(!0,!0,!0,!0)}_renderSymbols(e,t,i,s,l,n,r,a,o,d,c){e.setStencilFunction(514,i.stencilRef,255),this._render(e,t,i,s,l,n,r,a,o,d,c,3)}_render(e,t,i,s,l,n,r,a,o,d,c,h){this._updateRenderParameters(e,t,i,s,n,r,a,o,d,c),this._renderParams.stencilSymbols=!1,s.drawTile(this._renderParams,i,l,h)}_stencilTileSymbols(e,t,i,s,l,n,r,a,o,d,c){this._updateRenderParameters(e,t,i,s,n,r,a,o,d,c),this._renderParams.stencilSymbols=!0,i.triangleCount=0,s.drawSymbols(this._renderParams,i,l)}_updateRenderParameters(n,a,o,d,h,u,m,p,g,f){"type"in o&&"vector-tile"===o.type&&!o.stage&&o.attachWithContext(n);const y=a[0]-h.getLevelShift(a[0]),_=this._renderParams;_.context=n,_.painter=d,_.glyphMosaic=d.glyphMosaic,_.spriteMosaic=d.spriteMosaic,_.pixelRatio=g*f,_.displayLevel=y,_.requiredLevel=y;const b=h.getScale(a[0]),[S,M]=h.getOffset(a,u*b),w=c*u*b/p,R=o.transforms.displayViewScreenMat3;R[0]=w,R[4]=-w,R[6]=-1-S-m[0]*u*2,R[7]=1+M+(1-m[1])*u*2-2;const T=_.state,P=p/f;T.size[0]=P,T.size[1]=P,T.pixelRatio=f,T.rotation=(360-this._heading)%360;const j=2/P;e(T.displayViewMat3,j,0,0,0,-j,0,-1,1,1);const x=t(T.displayMat3),L=r(this._heading);i(x,x,[P/2,P/2]),s(x,x,L),i(x,x,[-P/2,-P/2]),l(x,T.displayViewMat3,x)}updateHeading(e){this._heading=e}_declutter(e){this._declutteredForHeading.get(e)!==this._heading&&(h(e,(360-this._heading)%360),this._declutteredForHeading.set(e,this._heading))}}class g{constructor(){this.size=[0,0],this.pixelRatio=1,this.rotation=0,this.displayMat3=n(),this.displayViewMat3=n(),this.transform=null,this.inverseTransform=null,this.viewMat2d=null,this.viewMat3=null,this.id=0,this.extent=null,this.center=null,this.scale=1,this.resolution=0,this.worldScreenWidth=0,this.spatialReference=null,this.viewpoint=null,this.getScreenTransform=null,this.toMap=null,this.toScreen=null,this.clone=null,this.copy=null,this.toJSON=null}}export{p as VectorTileRendererHelper3D};
