UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 1.93 kB
import{packFloatRGBA as t}from"../../../../core/floatRGBA.js";import{disposeMaybe as e,isNone as r,isSome as o}from"../../../../core/maybe.js";import{NoParameters as n}from"../core/shaderModules/interfaces.js";import{PixelFormat as s,PixelType as i,TextureWrapMode as c}from"../../../webgl/enums.js";import{Texture as a}from"../../../webgl/Texture.js";class u{constructor(t){this._rctx=t,this._cache=new Map}destroy(){this._cache.forEach((t=>e(t.stippleTexture))),this._cache.clear()}_acquire(t){if(r(t))return null;const e=this._patternId(t),o=this._cache.get(e);if(o)return o.refCount++,o;const{encodedData:n,paddedPixels:u}=p(t),l=new h(new a(this._rctx,{width:u,height:1,internalFormat:s.RGBA,pixelFormat:s.RGBA,dataType:i.UNSIGNED_BYTE,wrapMode:c.CLAMP_TO_EDGE},n));return this._cache.set(e,l),l}release(t){if(r(t))return;const e=this._patternId(t),n=this._cache.get(e);n&&(n.refCount--,0===n.refCount&&(o(n.stippleTexture)&&n.stippleTexture.dispose(),this._cache.delete(e)))}swap(t,e){const r=this._acquire(e);return this.release(t),r}_patternId(t){return`${t.pattern.join(",")}-r${t.pixelRatio}`}}class h extends n{constructor(t){super(),this.stippleTexture=t,this.refCount=1}}function p(e){const r=l(e),o=1/e.pixelRatio,n=f(e),s=d(e),i=(Math.floor(.5*(s-1))+.5)*o,c=[];let a=1;for(const t of r){for(let e=0;e<t;e++){const r=a*(Math.min(e,t-1-e)+.5)*o/i*.5+.5;c.push(r)}a=-a}const u=Math.round(r[0]/2),h=[...c.slice(u),...c.slice(0,u)],p=n+m,x=new Uint8Array(4*p);let _=4;for(const l of h)t(l,x,_),_+=4;return x.copyWithin(0,_-4,_),x.copyWithin(_,4,8),{encodedData:x,paddedPixels:p}}function l(t){return t.pattern.map((e=>Math.round(e*t.pixelRatio)))}function f(t){if(r(t))return 1;const e=l(t);return Math.floor(e.reduce(((t,e)=>t+e)))}function d(t){return l(t).reduce(((t,e)=>Math.max(t,e)))}const m=2;export{m as STIPPLE_TEXTURE_PADDING,u as StippleTextureRepository,d as computeLongestPattern,f as computeTextureSize};