@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 5.95 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import{create as e,fromValues as t}from"../../../../../core/libs/gl-matrix-2/factories/vec2f32.js";import{fadeDuration as i}from"../../vectorTiles/decluttering/config.js";import{RotationAlignment as a,SymbolPlacement as r,TranslateAnchor as n}from"../../vectorTiles/style/StyleDefinition.js";import{vtlTextureBindingUnitSprites as s,vtlTextureBindingUnitGlyphs as o}from"../definitions.js";import{degToByte as l}from"../GeometryUtils.js";import f from"./WGLBrush.js";import{TextureSamplingMode as c,PrimitiveType as u,DataType as p,CompareFunction as m}from"../../../../webgl/enums.js";const g=1/65536;class d extends f{constructor(){super(...arguments),this._iconProgramOptions={id:!1,sdf:!1},this._sdfProgramOptions={id:!1},this._spritesTextureSize=e()}dispose(){}drawMany(e,t){const i=e.styleLayer;this._drawIcons(e,i,t),this._drawText(e,i,t)}_drawIcons(e,t,o){const{context:f,displayLevel:c,painter:u,spriteMosaic:p,state:m,styleLayerUID:g,requestRender:d,allowDelayedRender:y}=e,_=t.iconMaterial,M=u.vectorTilesMaterialManager;let h,P=!1;for(const i of o)if(i.layerData.has(g)&&(h=i.layerData.get(g),h.iconPerPageElementsMap.size>0)){P=!0;break}if(!P)return;const U=t.getPaintValue("icon-translate",c),E=t.getPaintValue("icon-translate-anchor",c);let T=t.getLayoutValue("icon-rotation-alignment",c);T===a.AUTO&&(T=t.getLayoutValue("symbol-placement",c)===r.POINT?a.VIEWPORT:a.MAP);const x=T===a.MAP,S=t.getLayoutValue("icon-keep-upright",c)&&x,v=h.isIconSDF,D=this._iconProgramOptions;D.sdf=v;const V=M.getMaterialProgram(f,_,D);if(y&&null!=d&&!V.compiled)return void d();f.useProgram(V),V.setUniformMatrix3fv("u_displayViewMat3",T===a.MAP?m.displayViewMat3:m.displayMat3),V.setUniformMatrix3fv("u_displayMat3",E===n.VIEWPORT?m.displayMat3:m.displayViewMat3),V.setUniform2fv("u_iconTranslation",U),V.setUniform1f("u_depth",t.z),V.setUniform1f("u_mapRotation",l(m.rotation)),V.setUniform1f("u_keepUpright",S?1:0),V.setUniform1f("u_level",10*c),V.setUniform1i("u_texture",s),V.setUniform1f("u_fadeDuration",i/1e3);let R=-1;for(const i of o){if(!i.layerData.has(g))continue;if(i.key.level!==R&&(R=i.key.level,_.setDataUniforms(V,c,t,R,p)),h=i.layerData.get(g),0===h.iconPerPageElementsMap.size)continue;h.prepareForRendering(f),h.updateOpacityInfo();const a=h.iconVAO;if(null!=a){f.bindVAO(a),V.setUniformMatrix3fv("u_dvsMat3",i.transforms.displayViewScreenMat3),V.setUniform1f("u_time",(performance.now()-h.lastOpacityUpdate)/1e3);for(const[t,a]of h.iconPerPageElementsMap)this._renderIconRange(e,V,a,t,i)}}}_renderIconRange(e,t,i,a,r){const{context:n,spriteMosaic:o}=e;this._spritesTextureSize[0]=o.getWidth(a)/4,this._spritesTextureSize[1]=o.getHeight(a)/4,t.setUniform2fv("u_mosaicSize",this._spritesTextureSize),o.bind(n,c.LINEAR,a,s),this._setStencilState(e,r),n.drawElements(u.TRIANGLES,i[1],p.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*i[0]),r.triangleCount+=i[1]/3}_drawText(e,s,f){const{context:c,displayLevel:u,glyphMosaic:p,painter:m,pixelRatio:d,spriteMosaic:y,state:_,styleLayerUID:M,requestRender:h,allowDelayedRender:P}=e,U=s.textMaterial,E=m.vectorTilesMaterialManager;let T,x=!1;for(const t of f)if(t.layerData.has(M)&&(T=t.layerData.get(M),T.glyphPerPageElementsMap.size>0)){x=!0;break}if(!x)return;const S=s.getPaintProperty("text-opacity");if(S&&!S.isDataDriven&&0===S.getValue(u))return;const v=s.getPaintProperty("text-color"),D=!v||v.isDataDriven||v.getValue(u)[3]>0,V=s.getPaintProperty("text-halo-width"),R=s.getPaintProperty("text-halo-color"),I=(!V||V.isDataDriven||V.getValue(u)>0)&&(!R||R.isDataDriven||R.getValue(u)[3]>0);if(!D&&!I)return;const A=24/8;let w=s.getLayoutValue("text-rotation-alignment",u);w===a.AUTO&&(w=s.getLayoutValue("symbol-placement",u)===r.POINT?a.VIEWPORT:a.MAP);const L=w===a.MAP,O=s.getLayoutValue("text-keep-upright",u)&&L,N=.8*A/d;this._glyphTextureSize||(this._glyphTextureSize=t(p.width/4,p.height/4));const z=s.getPaintValue("text-translate",u),b=s.getPaintValue("text-translate-anchor",u),k=this._sdfProgramOptions,G=E.getMaterialProgram(c,U,k);if(P&&null!=h&&!G.compiled)return void h();c.useProgram(G),G.setUniformMatrix3fv("u_displayViewMat3",w===a.MAP?_.displayViewMat3:_.displayMat3),G.setUniformMatrix3fv("u_displayMat3",b===n.VIEWPORT?_.displayMat3:_.displayViewMat3),G.setUniform2fv("u_textTranslation",z),G.setUniform1f("u_depth",s.z+g),G.setUniform2fv("u_mosaicSize",this._glyphTextureSize),G.setUniform1f("u_mapRotation",l(_.rotation)),G.setUniform1f("u_keepUpright",O?1:0),G.setUniform1f("u_level",10*u),G.setUniform1i("u_texture",o),G.setUniform1f("u_antialiasingWidth",N),G.setUniform1f("u_fadeDuration",i/1e3);let W=-1;for(const t of f){if(!t.layerData.has(M))continue;if(t.key.level!==W&&(W=t.key.level,U.setDataUniforms(G,u,s,W,y)),T=t.layerData.get(M),0===T.glyphPerPageElementsMap.size)continue;T.prepareForRendering(c),T.updateOpacityInfo();const i=T.textVAO;if(null==i)continue;c.bindVAO(i),G.setUniformMatrix3fv("u_dvsMat3",t.transforms.displayViewScreenMat3),this._setStencilState(e,t);const a=(performance.now()-T.lastOpacityUpdate)/1e3;G.setUniform1f("u_time",a),T.glyphPerPageElementsMap.forEach(((e,i)=>{this._renderGlyphRange(c,e,i,p,G,I,D,t)}))}}_renderGlyphRange(e,t,i,a,r,n,s,l){a.bind(e,c.LINEAR,i,o),n&&(r.setUniform1f("u_halo",1),e.drawElements(u.TRIANGLES,t[1],p.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*t[0]),l.triangleCount+=t[1]/3),s&&(r.setUniform1f("u_halo",0),e.drawElements(u.TRIANGLES,t[1],p.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*t[0]),l.triangleCount+=t[1]/3)}_setStencilState(e,t){const{context:i,is3D:a,stencilSymbols:r}=e;if(i.setStencilTestEnabled(!0),r)return i.setStencilWriteMask(255),void i.setStencilFunction(m.ALWAYS,t.stencilRef,255);i.setStencilWriteMask(0),a?i.setStencilFunction(m.EQUAL,t.stencilRef,255):i.setStencilFunction(m.GREATER,255,255)}}export{d as WGLBrushVTLSymbol};