@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 5.95 kB
JavaScript
import{isSome as e,isNone as t}from"../../../../../core/maybe.js";import{c as i,f as a}from"../../../../../chunks/vec2f32.js";import{FADE_DURATION as r}from"../../vectorTiles/decluttering/config.js";import{RotationAlignment as n,SymbolPlacement as s,TranslateAnchor as o}from"../../vectorTiles/style/StyleDefinition.js";import{VTL_TEXTURE_BINDING_UNIT_SPRITES as l,VTL_TEXTURE_BINDING_UNIT_GLYPHS as f}from"../definitions.js";import{WGLDrawPhase as c}from"../enums.js";import{degToByte as u}from"../GeometryUtils.js";import{u32to4Xu8 as m}from"../number.js";import p from"./WGLBrush.js";import{TextureSamplingMode as d,CompareFunction as y,PrimitiveType as g,DataType as _}from"../../../../webgl/enums.js";const h=1/65536;class M extends p{constructor(){super(...arguments),this._iconProgramOptions={id:!1,sdf:!1},this._sdfProgramOptions={id:!1},this._spritesTextureSize=i()}dispose(){}drawMany(e,t){const{drawPhase:i,styleLayerUID:a}=e,r=e.styleLayer;let n;i===c.HITTEST&&(n=m(a+1)),this._drawIcons(e,r,t,n),this._drawText(e,r,t,n)}_drawIcons(i,a,f,m){const{context:p,displayLevel:d,drawPhase:y,painter:g,spriteMosaic:_,state:h,styleLayerUID:M,requestRender:P,allowDelayedRender:T}=i,U=a.iconMaterial,E=g.vectorTilesMaterialManager;let x,v=!1;for(const e of f)if(e.layerData.has(M)&&(x=e.layerData.get(M),x.iconPerPageElementsMap.size>0)){v=!0;break}if(!v)return;const D=a.getPaintValue("icon-translate",d),I=a.getPaintValue("icon-translate-anchor",d);let R=a.getLayoutValue("icon-rotation-alignment",d);R===n.AUTO&&(R=a.getLayoutValue("symbol-placement",d)===s.POINT?n.VIEWPORT:n.MAP);const S=R===n.MAP,V=a.getLayoutValue("icon-keep-upright",d)&&S,w=x.isIconSDF,A=y===c.HITTEST,L=this._iconProgramOptions;L.id=A,L.sdf=w;const O=E.getMaterialProgram(p,U,L);if(T&&e(P)&&!O.compiled)return void P();p.useProgram(O),O.setUniformMatrix3fv("u_displayViewMat3",R===n.MAP?h.displayViewMat3:h.displayMat3),O.setUniformMatrix3fv("u_displayMat3",I===o.VIEWPORT?h.displayMat3:h.displayViewMat3),O.setUniform2fv("u_iconTranslation",D),O.setUniform1f("u_depth",a.z),O.setUniform1f("u_mapRotation",u(h.rotation)),O.setUniform1f("u_keepUpright",V?1:0),O.setUniform1f("u_level",10*d),O.setUniform1i("u_texture",l),O.setUniform1f("u_fadeDuration",r/1e3),A&&O.setUniform4fv("u_id",m);let N=-1;for(const e of f){if(!e.layerData.has(M))continue;if(e.key.level!==N&&(N=e.key.level,U.setDataUniforms(O,d,a,N,_)),x=e.layerData.get(M),0===x.iconPerPageElementsMap.size)continue;x.prepareForRendering(p),x.updateOpacityInfo();const r=x.iconVertexArrayObject;if(!t(r)){p.bindVAO(r),O.setUniformMatrix3fv("u_dvsMat3",e.transforms.dvs),O.setUniform1f("u_time",(performance.now()-x.lastOpacityUpdate)/1e3);for(const[t,a]of x.iconPerPageElementsMap)this._renderIconRange(i,O,a,t,e)}}}_renderIconRange(e,t,i,a,r){const{context:n,spriteMosaic:s}=e;this._spritesTextureSize[0]=s.getWidth(a)/4,this._spritesTextureSize[1]=s.getHeight(a)/4,t.setUniform2fv("u_mosaicSize",this._spritesTextureSize),s.bind(n,d.LINEAR,a,l),n.setStencilTestEnabled(!0),n.setStencilFunction(y.GREATER,255,255),n.setStencilWriteMask(0),n.drawElements(g.TRIANGLES,i[1],_.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*i[0]),r.triangleCount+=i[1]/3}_drawText(i,l,m,p){const{context:d,displayLevel:g,drawPhase:_,glyphMosaic:M,painter:P,pixelRatio:T,spriteMosaic:U,state:E,styleLayerUID:x,requestRender:v,allowDelayedRender:D}=i,I=l.textMaterial,R=P.vectorTilesMaterialManager;let S,V=!1;for(const e of m)if(e.layerData.has(x)&&(S=e.layerData.get(x),S.glyphPerPageElementsMap.size>0)){V=!0;break}if(!V)return;const w=l.getPaintProperty("text-opacity");if(w&&!w.isDataDriven&&0===w.getValue(g))return;const A=l.getPaintProperty("text-color"),L=!A||A.isDataDriven||A.getValue(g)[3]>0,O=l.getPaintProperty("text-halo-width"),N=l.getPaintProperty("text-halo-color"),b=(!O||O.isDataDriven||O.getValue(g)>0)&&(!N||N.isDataDriven||N.getValue(g)[3]>0);if(!L&&!b)return;const z=24/8;let k=l.getLayoutValue("text-rotation-alignment",g);k===n.AUTO&&(k=l.getLayoutValue("symbol-placement",g)===s.POINT?n.VIEWPORT:n.MAP);const j=k===n.MAP,G=l.getLayoutValue("text-keep-upright",g)&&j,W=_===c.HITTEST,F=.8*z/T;this._glyphTextureSize||(this._glyphTextureSize=a(M.width/4,M.height/4));const B=l.getPaintValue("text-translate",g),H=l.getPaintValue("text-translate-anchor",g),C=this._sdfProgramOptions;C.id=W;const Y=R.getMaterialProgram(d,I,C);if(D&&e(v)&&!Y.compiled)return void v();d.useProgram(Y),Y.setUniformMatrix3fv("u_displayViewMat3",k===n.MAP?E.displayViewMat3:E.displayMat3),Y.setUniformMatrix3fv("u_displayMat3",H===o.VIEWPORT?E.displayMat3:E.displayViewMat3),Y.setUniform2fv("u_textTranslation",B),Y.setUniform1f("u_depth",l.z+h),Y.setUniform2fv("u_mosaicSize",this._glyphTextureSize),Y.setUniform1f("u_mapRotation",u(E.rotation)),Y.setUniform1f("u_keepUpright",G?1:0),Y.setUniform1f("u_level",10*g),Y.setUniform1i("u_texture",f),Y.setUniform1f("u_antialiasingWidth",F),Y.setUniform1f("u_fadeDuration",r/1e3),W&&Y.setUniform4fv("u_id",p);let q=-1;for(const e of m){if(!e.layerData.has(x))continue;if(e.key.level!==q&&(q=e.key.level,I.setDataUniforms(Y,g,l,q,U)),S=e.layerData.get(x),0===S.glyphPerPageElementsMap.size)continue;S.prepareForRendering(d),S.updateOpacityInfo();const i=S.textVertexArrayObject;if(t(i))continue;d.bindVAO(i),Y.setUniformMatrix3fv("u_dvsMat3",e.transforms.dvs),d.setStencilTestEnabled(!0),d.setStencilFunction(y.GREATER,255,255),d.setStencilWriteMask(0);const a=(performance.now()-S.lastOpacityUpdate)/1e3;Y.setUniform1f("u_time",a),S.glyphPerPageElementsMap.forEach(((t,i)=>{this._renderGlyphRange(d,t,i,M,Y,b,L,e)}))}}_renderGlyphRange(e,t,i,a,r,n,s,o){a.bind(e,d.LINEAR,i,f),n&&(r.setUniform1f("u_halo",1),e.drawElements(g.TRIANGLES,t[1],_.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*t[0]),o.triangleCount+=t[1]/3),s&&(r.setUniform1f("u_halo",0),e.drawElements(g.TRIANGLES,t[1],_.UNSIGNED_INT,Uint32Array.BYTES_PER_ELEMENT*t[0]),o.triangleCount+=t[1]/3)}}export{M as WGLBrushVTLSymbol};