170 lines
49 KiB
JavaScript
170 lines
49 KiB
JavaScript
import{E as h,U as Xe,b as se,a8 as L,M as R,a2 as Ne,l as D,h as me,w as O,a as k,P as Qe,R as q,H as xe,Y as G,a9 as Y,aa as Je,ab as P,z as b,ac as Ze,ad as _e,ae as et,u as be,o as Te,a3 as ye,t as Se,a6 as ve,af as we,s as tt,p as rt,q as st,a4 as at,a5 as nt,a7 as it,ag as ot,ah as dt,ai as lt,aj as j,ak as K,D as Be,m as A,O as ae,$ as ut,al as ct,am as ne,n as ie,e as S,an as ht}from"./index-C74jkVpW.js";import{T as w,S as Ce,c as X,a as ft,b as pt,B as Re}from"./colorToUniform-BrndAOXx.js";class Me{static init(e){Object.defineProperty(this,"resizeTo",{set(t){globalThis.removeEventListener("resize",this.queueResize),this._resizeTo=t,t&&(globalThis.addEventListener("resize",this.queueResize),this.resize())},get(){return this._resizeTo}}),this.queueResize=()=>{this._resizeTo&&(this._cancelResize(),this._resizeId=requestAnimationFrame(()=>this.resize()))},this._cancelResize=()=>{this._resizeId&&(cancelAnimationFrame(this._resizeId),this._resizeId=null)},this.resize=()=>{if(!this._resizeTo)return;this._cancelResize();let t,r;if(this._resizeTo===globalThis.window)t=globalThis.innerWidth,r=globalThis.innerHeight;else{const{clientWidth:s,clientHeight:n}=this._resizeTo;t=s,r=n}this.renderer.resize(t,r),this.render()},this._resizeId=null,this._resizeTo=null,this.resizeTo=e.resizeTo||null}static destroy(){globalThis.removeEventListener("resize",this.queueResize),this._cancelResize(),this._cancelResize=null,this.queueResize=null,this.resizeTo=null,this.resize=null}}Me.extension=h.Application;class Pe{static init(e){e=Object.assign({autoStart:!0,sharedTicker:!1},e),Object.defineProperty(this,"ticker",{set(t){this._ticker&&this._ticker.remove(this.render,this),this._ticker=t,t&&t.add(this.render,this,Xe.LOW)},get(){return this._ticker}}),this.stop=()=>{this._ticker.stop()},this.start=()=>{this._ticker.start()},this._ticker=null,this.ticker=e.sharedTicker?se.shared:new se,e.autoStart&&this.start()}static destroy(){if(this._ticker){const e=this._ticker;this.ticker=null,e.destroy()}}}Pe.extension=h.Application;class Ue{constructor(e){this._renderer=e}push(e,t,r){this._renderer.renderPipes.batch.break(r),r.add({renderPipeId:"filter",canBundle:!1,action:"pushFilter",container:t,filterEffect:e})}pop(e,t,r){this._renderer.renderPipes.batch.break(r),r.add({renderPipeId:"filter",action:"popFilter",canBundle:!1})}execute(e){e.action==="pushFilter"?this._renderer.filter.push(e):e.action==="popFilter"&&this._renderer.filter.pop()}destroy(){this._renderer=null}}Ue.extension={type:[h.WebGLPipes,h.WebGPUPipes,h.CanvasPipes],name:"filter"};const gt=new R;function mt(a,e){return e.clear(),ke(a,e),e.isValid||e.set(0,0,0,0),a.renderGroup?e.applyMatrix(a.renderGroup.localTransform):e.applyMatrix(a.parentRenderGroup.worldTransform),e}function ke(a,e){if(a.localDisplayStatus!==7||!a.measurable)return;const t=!!a.effects.length;let r=e;if((a.renderGroup||t)&&(r=L.get().clear()),a.boundsArea)e.addRect(a.boundsArea,a.worldTransform);else{if(a.renderPipeId){const n=a.bounds;r.addFrame(n.minX,n.minY,n.maxX,n.maxY,a.groupTransform)}const s=a.children;for(let n=0;n<s.length;n++)ke(s[n],r)}if(t){let s=!1;for(let n=0;n<a.effects.length;n++)a.effects[n].addBounds&&(s||(s=!0,r.applyMatrix(a.parentRenderGroup.worldTransform)),a.effects[n].addBounds(r,!0));s&&(r.applyMatrix(a.parentRenderGroup.worldTransform.copyTo(gt).invert()),e.addBounds(r,a.relativeGroupTransform)),e.addBounds(r),L.return(r)}else a.renderGroup&&(e.addBounds(r,a.relativeGroupTransform),L.return(r))}function xt(a,e){e.clear();const t=e.matrix;for(let r=0;r<a.length;r++){const s=a[r];s.globalDisplayStatus<7||(e.matrix=s.worldTransform,s.addBounds(e))}return e.matrix=t,e}const _t=new Ne({attributes:{aPosition:{buffer:new Float32Array([0,0,1,0,1,1,0,1]),format:"float32x2",stride:2*4,offset:0}},indexBuffer:new Uint32Array([0,1,2,0,2,3])});class Ge{constructor(e){this._filterStackIndex=0,this._filterStack=[],this._filterGlobalUniforms=new D({uInputSize:{value:new Float32Array(4),type:"vec4<f32>"},uInputPixel:{value:new Float32Array(4),type:"vec4<f32>"},uInputClamp:{value:new Float32Array(4),type:"vec4<f32>"},uOutputFrame:{value:new Float32Array(4),type:"vec4<f32>"},uGlobalFrame:{value:new Float32Array(4),type:"vec4<f32>"},uOutputTexture:{value:new Float32Array(4),type:"vec4<f32>"}}),this._globalFilterBindGroup=new me({}),this.renderer=e}get activeBackTexture(){var e;return(e=this._activeFilterData)==null?void 0:e.backTexture}push(e){var f;const t=this.renderer,r=e.filterEffect.filters;this._filterStack[this._filterStackIndex]||(this._filterStack[this._filterStackIndex]=this._getFilterData());const s=this._filterStack[this._filterStackIndex];if(this._filterStackIndex++,r.length===0){s.skip=!0;return}const n=s.bounds;e.renderables?xt(e.renderables,n):e.filterEffect.filterArea?(n.clear(),n.addRect(e.filterEffect.filterArea),n.applyMatrix(e.container.worldTransform)):mt(e.container,n);const i=t.renderTarget.renderTarget.colorTexture.source;let o=1/0,d=0,l=!0,u=!1,c=!1;for(let m=0;m<r.length;m++){const p=r[m];if(o=Math.min(o,p.resolution==="inherit"?i._resolution:p.resolution),d+=p.padding,p.antialias==="off"?l=!1:p.antialias==="inherit"&&l&&(l=i.antialias),!!!(p.compatibleRenderers&t.type)){c=!1;break}if(p.blendRequired&&!(((f=t.backBuffer)==null?void 0:f.useBackBuffer)??!0)){O("Blend filter requires backBuffer on WebGL renderer to be enabled. Set `useBackBuffer: true` in the renderer options."),c=!1;break}c=p.enabled||c,u=u||p.blendRequired}if(!c){s.skip=!0;return}const g=t.renderTarget.rootViewPort;if(n.scale(o).fitBounds(0,g.width,0,g.height).ceil().scale(1/o).pad(d|0),!n.isPositive){s.skip=!0;return}s.skip=!1,s.bounds=n,s.blendRequired=u,s.container=e.container,s.filterEffect=e.filterEffect,s.previousRenderSurface=t.renderTarget.renderSurface,s.inputTexture=w.getOptimalTexture(n.width,n.height,o,l),t.renderTarget.bind(s.inputTexture,!0),t.globalUniforms.push({offset:n})}pop(){const e=this.renderer;this._filterStackIndex--;const t=this._filterStack[this._filterStackIndex];if(t.skip)return;this._activeFilterData=t;const r=t.inputTexture,s=t.bounds;let n=k.EMPTY;if(e.renderTarget.finishRenderPass(),t.blendRequired){const o=this._filterStackIndex>0?this._filterStack[this._filterStackIndex-1].bounds:null,d=e.renderTarget.getRenderTarget(t.previousRenderSurface);n=this.getBackTexture(d,s,o)}t.backTexture=n;const i=t.filterEffect.filters;if(this._globalFilterBindGroup.setResource(r.source.style,2),this._globalFilterBindGroup.setResource(n.source,3),e.globalUniforms.pop(),i.length===1)i[0].apply(this,r,t.previousRenderSurface,!1),w.returnTexture(r);else{let o=t.inputTexture,d=w.getOptimalTexture(s.width,s.height,o.source._resolution,!1),l=0;for(l=0;l<i.length-1;++l){i[l].apply(this,o,d,!0);const c=o;o=d,d=c}i[l].apply(this,o,t.previousRenderSurface,!1),w.returnTexture(o),w.returnTexture(d)}t.blendRequired&&w.returnTexture(n)}getBackTexture(e,t,r){const s=e.colorTexture.source._resolution,n=w.getOptimalTexture(t.width,t.height,s,!1);let i=t.minX,o=t.minY;r&&(i-=r.minX,o-=r.minY),i=Math.floor(i*s),o=Math.floor(o*s);const d=Math.ceil(t.width*s),l=Math.ceil(t.height*s);return this.renderer.renderTarget.copyToTexture(e,n,{x:i,y:o},{width:d,height:l},{x:0,y:0}),n}applyFilter(e,t,r,s){const n=this.renderer,i=this._filterStack[this._filterStackIndex],o=i.bounds,d=Qe.shared,u=i.previousRenderSurface===r;let c=this.renderer.renderTarget.rootRenderTarget.colorTexture.source._resolution,g=this._filterStackIndex-1;for(;g>0&&this._filterStack[g].skip;)--g;g>0&&(c=this._filterStack[g].inputTexture.source._resolution);const f=this._filterGlobalUniforms,m=f.uniforms,p=m.uOutputFrame,_=m.uInputSize,x=m.uInputPixel,B=m.uInputClamp,T=m.uGlobalFrame,C=m.uOutputTexture;if(u){let M=this._filterStackIndex;for(;M>0;){M--;const v=this._filterStack[this._filterStackIndex-1];if(!v.skip){d.x=v.bounds.minX,d.y=v.bounds.minY;break}}p[0]=o.minX-d.x,p[1]=o.minY-d.y}else p[0]=0,p[1]=0;p[2]=t.frame.width,p[3]=t.frame.height,_[0]=t.source.width,_[1]=t.source.height,_[2]=1/_[0],_[3]=1/_[1],x[0]=t.source.pixelWidth,x[1]=t.source.pixelHeight,x[2]=1/x[0],x[3]=1/x[1],B[0]=.5*x[2],B[1]=.5*x[3],B[2]=t.frame.width*_[2]-.5*x[2],B[3]=t.frame.height*_[3]-.5*x[3];const F=this.renderer.renderTarget.rootRenderTarget.colorTexture;T[0]=d.x*c,T[1]=d.y*c,T[2]=F.source.width*c,T[3]=F.source.height*c;const U=this.renderer.renderTarget.getRenderTarget(r);if(n.renderTarget.bind(r,!!s),r instanceof k?(C[0]=r.frame.width,C[1]=r.frame.height):(C[0]=U.width,C[1]=U.height),C[2]=U.isRoot?-1:1,f.update(),n.renderPipes.uniformBatch){const M=n.renderPipes.uniformBatch.getUboResource(f);this._globalFilterBindGroup.setResource(M,0)}else this._globalFilterBindGroup.setResource(f,0);this._globalFilterBindGroup.setResource(t.source,1),this._globalFilterBindGroup.setResource(t.source.style,2),e.groups[0]=this._globalFilterBindGroup,n.encoder.draw({geometry:_t,shader:e,state:e._state,topology:"triangle-list"}),n.type===q.WEBGL&&n.renderTarget.finishRenderPass()}_getFilterData(){return{skip:!1,inputTexture:null,bounds:new xe,container:null,filterEffect:null,blendRequired:!1,previousRenderSurface:null}}calculateSpriteMatrix(e,t){const r=this._activeFilterData,s=e.set(r.inputTexture._source.width,0,0,r.inputTexture._source.height,r.bounds.minX,r.bounds.minY),n=t.worldTransform.copyTo(R.shared);return n.invert(),s.prepend(n),s.scale(1/t.texture.frame.width,1/t.texture.frame.height),s.translate(t.anchor.x,t.anchor.y),s}}Ge.extension={type:[h.WebGLSystem,h.WebGPUSystem],name:"filter"};function bt(a){const e=a._stroke,t=a._fill,s=[`div { ${[`color: ${G.shared.setValue(t.color).toHex()}`,`font-size: ${a.fontSize}px`,`font-family: ${a.fontFamily}`,`font-weight: ${a.fontWeight}`,`font-style: ${a.fontStyle}`,`font-variant: ${a.fontVariant}`,`letter-spacing: ${a.letterSpacing}px`,`text-align: ${a.align}`,`padding: ${a.padding}px`,`white-space: ${a.whiteSpace==="pre"&&a.wordWrap?"pre-wrap":a.whiteSpace}`,...a.lineHeight?[`line-height: ${a.lineHeight}px`]:[],...a.wordWrap?[`word-wrap: ${a.breakWords?"break-all":"break-word"}`,`max-width: ${a.wordWrapWidth}px`]:[],...e?[Ae(e)]:[],...a.dropShadow?[Fe(a.dropShadow)]:[],...a.cssOverrides].join(";")} }`];return Tt(a.tagStyles,s),s.join(" ")}function Fe(a){const e=G.shared.setValue(a.color).setAlpha(a.alpha).toHexa(),t=Math.round(Math.cos(a.angle)*a.distance),r=Math.round(Math.sin(a.angle)*a.distance),s=`${t}px ${r}px`;return a.blur>0?`text-shadow: ${s} ${a.blur}px ${e}`:`text-shadow: ${s} ${e}`}function Ae(a){return[`-webkit-text-stroke-width: ${a.width}px`,`-webkit-text-stroke-color: ${G.shared.setValue(a.color).toHex()}`,`text-stroke-width: ${a.width}px`,`text-stroke-color: ${G.shared.setValue(a.color).toHex()}`,"paint-order: stroke"].join(";")}const oe={fontSize:"font-size: {{VALUE}}px",fontFamily:"font-family: {{VALUE}}",fontWeight:"font-weight: {{VALUE}}",fontStyle:"font-style: {{VALUE}}",fontVariant:"font-variant: {{VALUE}}",letterSpacing:"letter-spacing: {{VALUE}}px",align:"text-align: {{VALUE}}",padding:"padding: {{VALUE}}px",whiteSpace:"white-space: {{VALUE}}",lineHeight:"line-height: {{VALUE}}px",wordWrapWidth:"max-width: {{VALUE}}px"},de={fill:a=>`color: ${G.shared.setValue(a).toHex()}`,breakWords:a=>`word-wrap: ${a?"break-all":"break-word"}`,stroke:Ae,dropShadow:Fe};function Tt(a,e){for(const t in a){const r=a[t],s=[];for(const n in r)de[n]?s.push(de[n](r[n])):oe[n]&&s.push(oe[n].replace("{{VALUE}}",r[n]));e.push(`${t} { ${s.join(";")} }`)}}class N extends Y{constructor(e={}){super(e),this._cssOverrides=[],this.cssOverrides??(this.cssOverrides=e.cssOverrides),this.tagStyles=e.tagStyles??{}}set cssOverrides(e){this._cssOverrides=e instanceof Array?e:[e],this.update()}get cssOverrides(){return this._cssOverrides}_generateKey(){return this._styleKey=Je(this)+this._cssOverrides.join("-"),this._styleKey}update(){this._cssStyle=null,super.update()}clone(){return new N({align:this.align,breakWords:this.breakWords,dropShadow:this.dropShadow?{...this.dropShadow}:null,fill:this._fill,fontFamily:this.fontFamily,fontSize:this.fontSize,fontStyle:this.fontStyle,fontVariant:this.fontVariant,fontWeight:this.fontWeight,letterSpacing:this.letterSpacing,lineHeight:this.lineHeight,padding:this.padding,stroke:this._stroke,whiteSpace:this.whiteSpace,wordWrap:this.wordWrap,wordWrapWidth:this.wordWrapWidth,cssOverrides:this.cssOverrides})}get cssStyle(){return this._cssStyle||(this._cssStyle=bt(this)),this._cssStyle}addOverride(...e){const t=e.filter(r=>!this.cssOverrides.includes(r));t.length>0&&(this.cssOverrides.push(...t),this.update())}removeOverride(...e){const t=e.filter(r=>this.cssOverrides.includes(r));t.length>0&&(this.cssOverrides=this.cssOverrides.filter(r=>!t.includes(r)),this.update())}set fill(e){typeof e!="string"&&typeof e!="number"&&O("[HTMLTextStyle] only color fill is not supported by HTMLText"),super.fill=e}set stroke(e){e&&typeof e!="string"&&typeof e!="number"&&O("[HTMLTextStyle] only color stroke is not supported by HTMLText"),super.stroke=e}}const le="http://www.w3.org/2000/svg",ue="http://www.w3.org/1999/xhtml";class De{constructor(){this.svgRoot=document.createElementNS(le,"svg"),this.foreignObject=document.createElementNS(le,"foreignObject"),this.domElement=document.createElementNS(ue,"div"),this.styleElement=document.createElementNS(ue,"style"),this.image=new Image;const{foreignObject:e,svgRoot:t,styleElement:r,domElement:s}=this;e.setAttribute("width","10000"),e.setAttribute("height","10000"),e.style.overflow="hidden",t.appendChild(e),e.appendChild(r),e.appendChild(s)}}let ce;function yt(a,e,t,r){r=r||ce||(ce=new De);const{domElement:s,styleElement:n,svgRoot:i}=r;s.innerHTML=`<style>${e.cssStyle};</style><div style='padding:0'>${a}</div>`,s.setAttribute("style","transform-origin: top left; display: inline-block"),t&&(n.textContent=t),document.body.appendChild(i);const o=s.getBoundingClientRect();i.remove();const d=P.measureFont(e.fontStyle).descent,l=e.padding*2;return{width:o.width-l,height:o.height+d-l}}class ze{constructor(e,t){this.state=Ce.for2d(),this._graphicsBatchesHash=Object.create(null),this._destroyRenderableBound=this.destroyRenderable.bind(this),this.renderer=e,this._adaptor=t,this._adaptor.init()}validateRenderable(e){const t=e.context,r=!!this._graphicsBatchesHash[e.uid],s=this.renderer.graphicsContext.updateGpuContext(t);return!!(s.isBatchable||r!==s.isBatchable)}addRenderable(e,t){const r=this.renderer.graphicsContext.updateGpuContext(e.context);e._didGraphicsUpdate&&(e._didGraphicsUpdate=!1,this._rebuild(e)),r.isBatchable?this._addToBatcher(e,t):(this.renderer.renderPipes.batch.break(t),t.add(e))}updateRenderable(e){const t=this._graphicsBatchesHash[e.uid];if(t)for(let r=0;r<t.length;r++){const s=t[r];s._batcher.updateElement(s)}}destroyRenderable(e){this._graphicsBatchesHash[e.uid]&&this._removeBatchForRenderable(e.uid),e.off("destroyed",this._destroyRenderableBound)}execute(e){if(!e.isRenderable)return;const t=this.renderer,r=e.context;if(!t.graphicsContext.getGpuContext(r).batches.length)return;const n=r.customShader||this._adaptor.shader;this.state.blendMode=e.groupBlendMode;const i=n.resources.localUniforms.uniforms;i.uTransformMatrix=e.groupTransform,i.uRound=t._roundPixels|e._roundPixels,X(e.groupColorAlpha,i.uColor,0),this._adaptor.execute(this,e)}_rebuild(e){const t=!!this._graphicsBatchesHash[e.uid],r=this.renderer.graphicsContext.updateGpuContext(e.context);t&&this._removeBatchForRenderable(e.uid),r.isBatchable&&this._initBatchesForRenderable(e),e.batched=r.isBatchable}_addToBatcher(e,t){const r=this.renderer.renderPipes.batch,s=this._getBatchesForRenderable(e);for(let n=0;n<s.length;n++){const i=s[n];r.addToBatch(i,t)}}_getBatchesForRenderable(e){return this._graphicsBatchesHash[e.uid]||this._initBatchesForRenderable(e)}_initBatchesForRenderable(e){const t=e.context,r=this.renderer.graphicsContext.getGpuContext(t),s=this.renderer._roundPixels|e._roundPixels,n=r.batches.map(i=>{const o=b.get(Ze);return i.copyTo(o),o.renderable=e,o.roundPixels=s,o});return this._graphicsBatchesHash[e.uid]===void 0&&e.on("destroyed",this._destroyRenderableBound),this._graphicsBatchesHash[e.uid]=n,n}_removeBatchForRenderable(e){this._graphicsBatchesHash[e].forEach(t=>{b.return(t)}),this._graphicsBatchesHash[e]=null}destroy(){this.renderer=null,this._adaptor.destroy(),this._adaptor=null,this.state=null;for(const e in this._graphicsBatchesHash)this._removeBatchForRenderable(e);this._graphicsBatchesHash=null}}ze.extension={type:[h.WebGLPipes,h.WebGPUPipes,h.CanvasPipes],name:"graphics"};class Q{constructor(){this.batcherName="default",this.packAsQuad=!1,this.indexOffset=0,this.attributeOffset=0,this.roundPixels=0,this._batcher=null,this._batch=null,this._uvUpdateId=-1,this._textureMatrixUpdateId=-1}get blendMode(){return this.renderable.groupBlendMode}reset(){this.renderable=null,this.texture=null,this._batcher=null,this._batch=null,this.geometry=null,this._uvUpdateId=-1,this._textureMatrixUpdateId=-1}get uvs(){const t=this.geometry.getBuffer("aUV"),r=t.data;let s=r;const n=this.texture.textureMatrix;return n.isSimple||(s=this._transformedUvs,(this._textureMatrixUpdateId!==n._updateID||this._uvUpdateId!==t._updateID)&&((!s||s.length<r.length)&&(s=this._transformedUvs=new Float32Array(r.length)),this._textureMatrixUpdateId=n._updateID,this._uvUpdateId=t._updateID,n.multiplyUvs(r,s))),s}get positions(){return this.geometry.positions}get indices(){return this.geometry.indices}get color(){return this.renderable.groupColorAlpha}get groupTransform(){return this.renderable.groupTransform}get attributeSize(){return this.geometry.positions.length/2}get indexSize(){return this.geometry.indices.length}}class He{constructor(e,t){this.localUniforms=new D({uTransformMatrix:{value:new R,type:"mat3x3<f32>"},uColor:{value:new Float32Array([1,1,1,1]),type:"vec4<f32>"},uRound:{value:0,type:"f32"}}),this.localUniformsBindGroup=new me({0:this.localUniforms}),this._meshDataHash=Object.create(null),this._gpuBatchableMeshHash=Object.create(null),this._destroyRenderableBound=this.destroyRenderable.bind(this),this.renderer=e,this._adaptor=t,this._adaptor.init()}validateRenderable(e){const t=this._getMeshData(e),r=t.batched,s=e.batched;if(t.batched=s,r!==s)return!0;if(s){const n=e._geometry;if(n.indices.length!==t.indexSize||n.positions.length!==t.vertexSize)return t.indexSize=n.indices.length,t.vertexSize=n.positions.length,!0;const i=this._getBatchableMesh(e),o=e.texture;if(i.texture._source!==o._source&&i.texture._source!==o._source)return!i._batcher.checkAndUpdateTexture(i,o)}return!1}addRenderable(e,t){const r=this.renderer.renderPipes.batch,{batched:s}=this._getMeshData(e);if(s){const n=this._getBatchableMesh(e);n.texture=e._texture,n.geometry=e._geometry,r.addToBatch(n,t)}else r.break(t),t.add(e)}updateRenderable(e){if(e.batched){const t=this._gpuBatchableMeshHash[e.uid];t.texture=e._texture,t.geometry=e._geometry,t._batcher.updateElement(t)}}destroyRenderable(e){this._meshDataHash[e.uid]=null;const t=this._gpuBatchableMeshHash[e.uid];t&&(b.return(t),this._gpuBatchableMeshHash[e.uid]=null),e.off("destroyed",this._destroyRenderableBound)}execute(e){if(!e.isRenderable)return;e.state.blendMode=_e(e.groupBlendMode,e.texture._source);const t=this.localUniforms;t.uniforms.uTransformMatrix=e.groupTransform,t.uniforms.uRound=this.renderer._roundPixels|e._roundPixels,t.update(),X(e.groupColorAlpha,t.uniforms.uColor,0),this._adaptor.execute(this,e)}_getMeshData(e){return this._meshDataHash[e.uid]||this._initMeshData(e)}_initMeshData(e){var t,r;return this._meshDataHash[e.uid]={batched:e.batched,indexSize:(t=e._geometry.indices)==null?void 0:t.length,vertexSize:(r=e._geometry.positions)==null?void 0:r.length},e.on("destroyed",this._destroyRenderableBound),this._meshDataHash[e.uid]}_getBatchableMesh(e){return this._gpuBatchableMeshHash[e.uid]||this._initBatchableMesh(e)}_initBatchableMesh(e){const t=b.get(Q);return t.renderable=e,t.texture=e._texture,t.transform=e.groupTransform,t.roundPixels=this.renderer._roundPixels|e._roundPixels,this._gpuBatchableMeshHash[e.uid]=t,t}destroy(){for(const e in this._gpuBatchableMeshHash)this._gpuBatchableMeshHash[e]&&b.return(this._gpuBatchableMeshHash[e]);this._gpuBatchableMeshHash=null,this._meshDataHash=null,this.localUniforms=null,this.localUniformsBindGroup=null,this._adaptor.destroy(),this._adaptor=null,this.renderer=null}}He.extension={type:[h.WebGLPipes,h.WebGPUPipes,h.CanvasPipes],name:"mesh"};class Oe{constructor(e){this._gpuSpriteHash=Object.create(null),this._destroyRenderableBound=this.destroyRenderable.bind(this),this._renderer=e}addRenderable(e,t){const r=this._getGpuSprite(e);e._didSpriteUpdate&&this._updateBatchableSprite(e,r),this._renderer.renderPipes.batch.addToBatch(r,t)}updateRenderable(e){const t=this._gpuSpriteHash[e.uid];e._didSpriteUpdate&&this._updateBatchableSprite(e,t),t._batcher.updateElement(t)}validateRenderable(e){const t=e._texture,r=this._getGpuSprite(e);return r.texture._source!==t._source?!r._batcher.checkAndUpdateTexture(r,t):!1}destroyRenderable(e){const t=this._gpuSpriteHash[e.uid];b.return(t.geometry),b.return(t),this._gpuSpriteHash[e.uid]=null,e.off("destroyed",this._destroyRenderableBound)}_updateBatchableSprite(e,t){e._didSpriteUpdate=!1,t.geometry.update(e),t.texture=e._texture}_getGpuSprite(e){return this._gpuSpriteHash[e.uid]||this._initGPUSprite(e)}_initGPUSprite(e){const t=b.get(Q);return t.geometry=b.get(et),t.renderable=e,t.transform=e.groupTransform,t.texture=e._texture,t.roundPixels=this._renderer._roundPixels|e._roundPixels,e._didSpriteUpdate=!0,this._gpuSpriteHash[e.uid]=t,e.on("destroyed",this._destroyRenderableBound),t}destroy(){for(const e in this._gpuSpriteHash)this._gpuSpriteHash[e].geometry.destroy();this._gpuSpriteHash=null,this._renderer=null}}Oe.extension={type:[h.WebGLPipes,h.WebGPUPipes,h.CanvasPipes],name:"nineSliceSprite"};const St={name:"tiling-bit",vertex:{header:`
|
|
struct TilingUniforms {
|
|
uMapCoord:mat3x3<f32>,
|
|
uClampFrame:vec4<f32>,
|
|
uClampOffset:vec2<f32>,
|
|
uTextureTransform:mat3x3<f32>,
|
|
uSizeAnchor:vec4<f32>
|
|
};
|
|
|
|
@group(2) @binding(0) var<uniform> tilingUniforms: TilingUniforms;
|
|
@group(2) @binding(1) var uTexture: texture_2d<f32>;
|
|
@group(2) @binding(2) var uSampler: sampler;
|
|
`,main:`
|
|
uv = (tilingUniforms.uTextureTransform * vec3(uv, 1.0)).xy;
|
|
|
|
position = (position - tilingUniforms.uSizeAnchor.zw) * tilingUniforms.uSizeAnchor.xy;
|
|
`},fragment:{header:`
|
|
struct TilingUniforms {
|
|
uMapCoord:mat3x3<f32>,
|
|
uClampFrame:vec4<f32>,
|
|
uClampOffset:vec2<f32>,
|
|
uTextureTransform:mat3x3<f32>,
|
|
uSizeAnchor:vec4<f32>
|
|
};
|
|
|
|
@group(2) @binding(0) var<uniform> tilingUniforms: TilingUniforms;
|
|
@group(2) @binding(1) var uTexture: texture_2d<f32>;
|
|
@group(2) @binding(2) var uSampler: sampler;
|
|
`,main:`
|
|
|
|
var coord = vUV + ceil(tilingUniforms.uClampOffset - vUV);
|
|
coord = (tilingUniforms.uMapCoord * vec3(coord, 1.0)).xy;
|
|
var unclamped = coord;
|
|
coord = clamp(coord, tilingUniforms.uClampFrame.xy, tilingUniforms.uClampFrame.zw);
|
|
|
|
var bias = 0.;
|
|
|
|
if(unclamped.x == coord.x && unclamped.y == coord.y)
|
|
{
|
|
bias = -32.;
|
|
}
|
|
|
|
outColor = textureSampleBias(uTexture, uSampler, coord, bias);
|
|
`}},vt={name:"tiling-bit",vertex:{header:`
|
|
uniform mat3 uTextureTransform;
|
|
uniform vec4 uSizeAnchor;
|
|
|
|
`,main:`
|
|
uv = (uTextureTransform * vec3(aUV, 1.0)).xy;
|
|
|
|
position = (position - uSizeAnchor.zw) * uSizeAnchor.xy;
|
|
`},fragment:{header:`
|
|
uniform sampler2D uTexture;
|
|
uniform mat3 uMapCoord;
|
|
uniform vec4 uClampFrame;
|
|
uniform vec2 uClampOffset;
|
|
`,main:`
|
|
|
|
vec2 coord = vUV + ceil(uClampOffset - vUV);
|
|
coord = (uMapCoord * vec3(coord, 1.0)).xy;
|
|
vec2 unclamped = coord;
|
|
coord = clamp(coord, uClampFrame.xy, uClampFrame.zw);
|
|
|
|
outColor = texture(uTexture, coord, unclamped == coord ? 0.0 : -32.0);// lod-bias very negative to force lod 0
|
|
|
|
`}};let I,E;class wt extends be{constructor(){I??(I=Te({name:"tiling-sprite-shader",bits:[ft,St,Se]})),E??(E=ye({name:"tiling-sprite-shader",bits:[pt,vt,ve]}));const e=new D({uMapCoord:{value:new R,type:"mat3x3<f32>"},uClampFrame:{value:new Float32Array([0,0,1,1]),type:"vec4<f32>"},uClampOffset:{value:new Float32Array([0,0]),type:"vec2<f32>"},uTextureTransform:{value:new R,type:"mat3x3<f32>"},uSizeAnchor:{value:new Float32Array([100,100,.5,.5]),type:"vec4<f32>"}});super({glProgram:E,gpuProgram:I,resources:{localUniforms:new D({uTransformMatrix:{value:new R,type:"mat3x3<f32>"},uColor:{value:new Float32Array([1,1,1,1]),type:"vec4<f32>"},uRound:{value:0,type:"f32"}}),tilingUniforms:e,uTexture:k.EMPTY.source,uSampler:k.EMPTY.source.style}})}updateUniforms(e,t,r,s,n,i){const o=this.resources.tilingUniforms,d=i.width,l=i.height,u=i.textureMatrix,c=o.uniforms.uTextureTransform;c.set(r.a*d/e,r.b*d/t,r.c*l/e,r.d*l/t,r.tx/e,r.ty/t),c.invert(),o.uniforms.uMapCoord=u.mapCoord,o.uniforms.uClampFrame=u.uClampFrame,o.uniforms.uClampOffset=u.uClampOffset,o.uniforms.uTextureTransform=c,o.uniforms.uSizeAnchor[0]=e,o.uniforms.uSizeAnchor[1]=t,o.uniforms.uSizeAnchor[2]=s,o.uniforms.uSizeAnchor[3]=n,i&&(this.resources.uTexture=i.source,this.resources.uSampler=i.source.style)}}class Bt extends we{constructor(){super({positions:new Float32Array([0,0,1,0,1,1,0,1]),uvs:new Float32Array([0,0,1,0,1,1,0,1]),indices:new Uint32Array([0,1,2,0,2,3])})}}function Ct(a,e){const t=a.anchor.x,r=a.anchor.y;e[0]=-t*a.width,e[1]=-r*a.height,e[2]=(1-t)*a.width,e[3]=-r*a.height,e[4]=(1-t)*a.width,e[5]=(1-r)*a.height,e[6]=-t*a.width,e[7]=(1-r)*a.height}function Rt(a,e,t,r){let s=0;const n=a.length/e,i=r.a,o=r.b,d=r.c,l=r.d,u=r.tx,c=r.ty;for(t*=e;s<n;){const g=a[t],f=a[t+1];a[t]=i*g+d*f+u,a[t+1]=o*g+l*f+c,t+=e,s++}}function Mt(a,e){const t=a.texture,r=t.frame.width,s=t.frame.height;let n=0,i=0;a._applyAnchorToTexture&&(n=a.anchor.x,i=a.anchor.y),e[0]=e[6]=-n,e[2]=e[4]=1-n,e[1]=e[3]=-i,e[5]=e[7]=1-i;const o=R.shared;o.copyFrom(a._tileTransform.matrix),o.tx/=a.width,o.ty/=a.height,o.invert(),o.scale(a.width/r,a.height/s),Rt(e,2,0,o)}const z=new Bt;class We{constructor(e){this._state=Ce.default2d,this._tilingSpriteDataHash=Object.create(null),this._destroyRenderableBound=this.destroyRenderable.bind(this),this._renderer=e}validateRenderable(e){const t=this._getTilingSpriteData(e),r=t.canBatch;this._updateCanBatch(e);const s=t.canBatch;if(s&&s===r){const{batchableMesh:n}=t;if(n&&n.texture._source!==e.texture._source)return!n._batcher.checkAndUpdateTexture(n,e.texture)}return r!==s}addRenderable(e,t){const r=this._renderer.renderPipes.batch;this._updateCanBatch(e);const s=this._getTilingSpriteData(e),{geometry:n,canBatch:i}=s;if(i){s.batchableMesh||(s.batchableMesh=new Q);const o=s.batchableMesh;e._didTilingSpriteUpdate&&(e._didTilingSpriteUpdate=!1,this._updateBatchableMesh(e),o.geometry=n,o.renderable=e,o.transform=e.groupTransform,o.texture=e._texture),o.roundPixels=this._renderer._roundPixels|e._roundPixels,r.addToBatch(o,t)}else r.break(t),s.shader||(s.shader=new wt),this.updateRenderable(e),t.add(e)}execute(e){const{shader:t}=this._tilingSpriteDataHash[e.uid];t.groups[0]=this._renderer.globalUniforms.bindGroup;const r=t.resources.localUniforms.uniforms;r.uTransformMatrix=e.groupTransform,r.uRound=this._renderer._roundPixels|e._roundPixels,X(e.groupColorAlpha,r.uColor,0),this._state.blendMode=_e(e.groupBlendMode,e.texture._source),this._renderer.encoder.draw({geometry:z,shader:t,state:this._state})}updateRenderable(e){const t=this._getTilingSpriteData(e),{canBatch:r}=t;if(r){const{batchableMesh:s}=t;e._didTilingSpriteUpdate&&this._updateBatchableMesh(e),s._batcher.updateElement(s)}else if(e._didTilingSpriteUpdate){const{shader:s}=t;s.updateUniforms(e.width,e.height,e._tileTransform.matrix,e.anchor.x,e.anchor.y,e.texture)}e._didTilingSpriteUpdate=!1}destroyRenderable(e){var r;const t=this._getTilingSpriteData(e);t.batchableMesh=null,(r=t.shader)==null||r.destroy(),this._tilingSpriteDataHash[e.uid]=null,e.off("destroyed",this._destroyRenderableBound)}_getTilingSpriteData(e){return this._tilingSpriteDataHash[e.uid]||this._initTilingSpriteData(e)}_initTilingSpriteData(e){const t=new we({indices:z.indices,positions:z.positions.slice(),uvs:z.uvs.slice()});return this._tilingSpriteDataHash[e.uid]={canBatch:!0,renderable:e,geometry:t},e.on("destroyed",this._destroyRenderableBound),this._tilingSpriteDataHash[e.uid]}_updateBatchableMesh(e){const t=this._getTilingSpriteData(e),{geometry:r}=t,s=e.texture.source.style;s.addressMode!=="repeat"&&(s.addressMode="repeat",s.update()),Mt(e,r.uvs),Ct(e,r.positions)}destroy(){for(const e in this._tilingSpriteDataHash)this.destroyRenderable(this._tilingSpriteDataHash[e].renderable);this._tilingSpriteDataHash=null,this._renderer=null}_updateCanBatch(e){const t=this._getTilingSpriteData(e),r=e.texture;let s=!0;return this._renderer.type===q.WEBGL&&(s=this._renderer.context.supports.nonPowOf2wrapping),t.canBatch=r.textureMatrix.isSimple&&(s||r.source.isPowerOfTwo),t.canBatch}}We.extension={type:[h.WebGLPipes,h.WebGPUPipes,h.CanvasPipes],name:"tilingSprite"};const Pt={name:"local-uniform-msdf-bit",vertex:{header:`
|
|
struct LocalUniforms {
|
|
uColor:vec4<f32>,
|
|
uTransformMatrix:mat3x3<f32>,
|
|
uDistance: f32,
|
|
uRound:f32,
|
|
}
|
|
|
|
@group(2) @binding(0) var<uniform> localUniforms : LocalUniforms;
|
|
`,main:`
|
|
vColor *= localUniforms.uColor;
|
|
modelMatrix *= localUniforms.uTransformMatrix;
|
|
`,end:`
|
|
if(localUniforms.uRound == 1)
|
|
{
|
|
vPosition = vec4(roundPixels(vPosition.xy, globalUniforms.uResolution), vPosition.zw);
|
|
}
|
|
`},fragment:{header:`
|
|
struct LocalUniforms {
|
|
uColor:vec4<f32>,
|
|
uTransformMatrix:mat3x3<f32>,
|
|
uDistance: f32
|
|
}
|
|
|
|
@group(2) @binding(0) var<uniform> localUniforms : LocalUniforms;
|
|
`,main:`
|
|
outColor = vec4<f32>(calculateMSDFAlpha(outColor, localUniforms.uColor, localUniforms.uDistance));
|
|
`}},Ut={name:"local-uniform-msdf-bit",vertex:{header:`
|
|
uniform mat3 uTransformMatrix;
|
|
uniform vec4 uColor;
|
|
uniform float uRound;
|
|
`,main:`
|
|
vColor *= uColor;
|
|
modelMatrix *= uTransformMatrix;
|
|
`,end:`
|
|
if(uRound == 1.)
|
|
{
|
|
gl_Position.xy = roundPixels(gl_Position.xy, uResolution);
|
|
}
|
|
`},fragment:{header:`
|
|
uniform float uDistance;
|
|
`,main:`
|
|
outColor = vec4(calculateMSDFAlpha(outColor, vColor, uDistance));
|
|
`}},kt={name:"msdf-bit",fragment:{header:`
|
|
fn calculateMSDFAlpha(msdfColor:vec4<f32>, shapeColor:vec4<f32>, distance:f32) -> f32 {
|
|
|
|
// MSDF
|
|
var median = msdfColor.r + msdfColor.g + msdfColor.b -
|
|
min(msdfColor.r, min(msdfColor.g, msdfColor.b)) -
|
|
max(msdfColor.r, max(msdfColor.g, msdfColor.b));
|
|
|
|
// SDF
|
|
median = min(median, msdfColor.a);
|
|
|
|
var screenPxDistance = distance * (median - 0.5);
|
|
var alpha = clamp(screenPxDistance + 0.5, 0.0, 1.0);
|
|
if (median < 0.01) {
|
|
alpha = 0.0;
|
|
} else if (median > 0.99) {
|
|
alpha = 1.0;
|
|
}
|
|
|
|
// Gamma correction for coverage-like alpha
|
|
var luma: f32 = dot(shapeColor.rgb, vec3<f32>(0.299, 0.587, 0.114));
|
|
var gamma: f32 = mix(1.0, 1.0 / 2.2, luma);
|
|
var coverage: f32 = pow(shapeColor.a * alpha, gamma);
|
|
|
|
return coverage;
|
|
|
|
}
|
|
`}},Gt={name:"msdf-bit",fragment:{header:`
|
|
float calculateMSDFAlpha(vec4 msdfColor, vec4 shapeColor, float distance) {
|
|
|
|
// MSDF
|
|
float median = msdfColor.r + msdfColor.g + msdfColor.b -
|
|
min(msdfColor.r, min(msdfColor.g, msdfColor.b)) -
|
|
max(msdfColor.r, max(msdfColor.g, msdfColor.b));
|
|
|
|
// SDF
|
|
median = min(median, msdfColor.a);
|
|
|
|
float screenPxDistance = distance * (median - 0.5);
|
|
float alpha = clamp(screenPxDistance + 0.5, 0.0, 1.0);
|
|
|
|
if (median < 0.01) {
|
|
alpha = 0.0;
|
|
} else if (median > 0.99) {
|
|
alpha = 1.0;
|
|
}
|
|
|
|
// Gamma correction for coverage-like alpha
|
|
float luma = dot(shapeColor.rgb, vec3(0.299, 0.587, 0.114));
|
|
float gamma = mix(1.0, 1.0 / 2.2, luma);
|
|
float coverage = pow(shapeColor.a * alpha, gamma);
|
|
|
|
return coverage;
|
|
}
|
|
`}};let $,V;class Ft extends be{constructor(){const e=new D({uColor:{value:new Float32Array([1,1,1,1]),type:"vec4<f32>"},uTransformMatrix:{value:new R,type:"mat3x3<f32>"},uDistance:{value:4,type:"f32"},uRound:{value:0,type:"f32"}}),t=tt();$??($=Te({name:"sdf-shader",bits:[rt,st(t),Pt,kt,Se]})),V??(V=ye({name:"sdf-shader",bits:[at,nt(t),Ut,Gt,ve]})),super({glProgram:V,gpuProgram:$,resources:{localUniforms:e,batchSamplers:it(t)}})}}class Le{constructor(e){this._gpuBitmapText={},this._destroyRenderableBound=this.destroyRenderable.bind(this),this._renderer=e}validateRenderable(e){const t=this._getGpuBitmapText(e);return e._didTextUpdate&&(e._didTextUpdate=!1,this._updateContext(e,t)),this._renderer.renderPipes.graphics.validateRenderable(t)}addRenderable(e,t){const r=this._getGpuBitmapText(e);he(e,r),e._didTextUpdate&&(e._didTextUpdate=!1,this._updateContext(e,r)),this._renderer.renderPipes.graphics.addRenderable(r,t),r.context.customShader&&this._updateDistanceField(e)}destroyRenderable(e){e.off("destroyed",this._destroyRenderableBound),this._destroyRenderableByUid(e.uid)}_destroyRenderableByUid(e){const t=this._gpuBitmapText[e].context;t.customShader&&(b.return(t.customShader),t.customShader=null),b.return(this._gpuBitmapText[e]),this._gpuBitmapText[e]=null}updateRenderable(e){const t=this._getGpuBitmapText(e);he(e,t),this._renderer.renderPipes.graphics.updateRenderable(t),t.context.customShader&&this._updateDistanceField(e)}_updateContext(e,t){const{context:r}=t,s=ot.getFont(e.text,e._style);r.clear(),s.distanceField.type!=="none"&&(r.customShader||(r.customShader=b.get(Ft)));const n=Array.from(e.text),i=e._style;let o=s.baseLineOffset;const d=dt(n,i,s,!0);let l=0;const u=i.padding,c=d.scale;let g=d.width,f=d.height+d.offsetY;i._stroke&&(g+=i._stroke.width/c,f+=i._stroke.width/c),r.translate(-e._anchor._x*g-u,-e._anchor._y*f-u).scale(c,c);const m=s.applyFillAsTint?i._fill.color:16777215;for(let p=0;p<d.lines.length;p++){const _=d.lines[p];for(let x=0;x<_.charPositions.length;x++){const B=n[l++],T=s.chars[B];T!=null&&T.texture&&r.texture(T.texture,m||"black",Math.round(_.charPositions[x]+T.xOffset),Math.round(o+T.yOffset))}o+=s.lineHeight}}_getGpuBitmapText(e){return this._gpuBitmapText[e.uid]||this.initGpuText(e)}initGpuText(e){const t=b.get(lt);return this._gpuBitmapText[e.uid]=t,this._updateContext(e,t),e.on("destroyed",this._destroyRenderableBound),this._gpuBitmapText[e.uid]}_updateDistanceField(e){const t=this._getGpuBitmapText(e).context,r=e._style.fontFamily,s=j.get(`${r}-bitmap`),{a:n,b:i,c:o,d}=e.groupTransform,l=Math.sqrt(n*n+i*i),u=Math.sqrt(o*o+d*d),c=(Math.abs(l)+Math.abs(u))/2,g=s.baseRenderedFontSize/e._style.fontSize,f=c*s.distanceField.range*(1/g);t.customShader.resources.localUniforms.uniforms.uDistance=f}destroy(){for(const e in this._gpuBitmapText)this._destroyRenderableByUid(e);this._gpuBitmapText=null,this._renderer=null}}Le.extension={type:[h.WebGLPipes,h.WebGPUPipes,h.CanvasPipes],name:"bitmapText"};function he(a,e){e.groupTransform=a.groupTransform,e.groupColorAlpha=a.groupColorAlpha,e.groupColor=a.groupColor,e.groupBlendMode=a.groupBlendMode,e.globalDisplayStatus=a.globalDisplayStatus,e.groupTransform=a.groupTransform,e.localDisplayStatus=a.localDisplayStatus,e.groupAlpha=a.groupAlpha,e._roundPixels=a._roundPixels}class Ie{constructor(e){this._gpuText=Object.create(null),this._destroyRenderableBound=this.destroyRenderable.bind(this),this._renderer=e,this._renderer.runners.resolutionChange.add(this)}resolutionChange(){for(const e in this._gpuText){const t=this._gpuText[e];if(!t)continue;const r=t.batchableSprite.renderable;r._autoResolution&&(r._resolution=this._renderer.resolution,r.onViewUpdate())}}validateRenderable(e){const t=this._getGpuText(e),r=e._getKey();return t.textureNeedsUploading?(t.textureNeedsUploading=!1,!0):t.currentKey!==r}addRenderable(e,t){const s=this._getGpuText(e).batchableSprite;e._didTextUpdate&&this._updateText(e),this._renderer.renderPipes.batch.addToBatch(s,t)}updateRenderable(e){const r=this._getGpuText(e).batchableSprite;e._didTextUpdate&&this._updateText(e),r._batcher.updateElement(r)}destroyRenderable(e){e.off("destroyed",this._destroyRenderableBound),this._destroyRenderableById(e.uid)}_destroyRenderableById(e){const t=this._gpuText[e];this._renderer.htmlText.decreaseReferenceCount(t.currentKey),b.return(t.batchableSprite),this._gpuText[e]=null}_updateText(e){const t=e._getKey(),r=this._getGpuText(e),s=r.batchableSprite;r.currentKey!==t&&this._updateGpuText(e).catch(i=>{console.error(i)}),e._didTextUpdate=!1;const n=e._style.padding;K(s.bounds,e._anchor,s.texture,n)}async _updateGpuText(e){e._didTextUpdate=!1;const t=this._getGpuText(e);if(t.generatingTexture)return;const r=e._getKey();this._renderer.htmlText.decreaseReferenceCount(t.currentKey),t.generatingTexture=!0,t.currentKey=r;const s=e.resolution??this._renderer.resolution,n=await this._renderer.htmlText.getManagedTexture(e.text,s,e._style,e._getKey()),i=t.batchableSprite;i.texture=t.texture=n,t.generatingTexture=!1,t.textureNeedsUploading=!0,e.onViewUpdate();const o=e._style.padding;K(i.bounds,e._anchor,i.texture,o)}_getGpuText(e){return this._gpuText[e.uid]||this.initGpuText(e)}initGpuText(e){const t={texture:k.EMPTY,currentKey:"--",batchableSprite:b.get(Re),textureNeedsUploading:!1,generatingTexture:!1},r=t.batchableSprite;return r.renderable=e,r.transform=e.groupTransform,r.texture=k.EMPTY,r.bounds={minX:0,maxX:1,minY:0,maxY:0},r.roundPixels=this._renderer._roundPixels|e._roundPixels,e._resolution=e._autoResolution?this._renderer.resolution:e.resolution,this._gpuText[e.uid]=t,e.on("destroyed",this._destroyRenderableBound),t}destroy(){for(const e in this._gpuText)this._destroyRenderableById(e);this._gpuText=null,this._renderer=null}}Ie.extension={type:[h.WebGLPipes,h.WebGPUPipes,h.CanvasPipes],name:"htmlText"};function At(){const{userAgent:a}=Be.get().getNavigator();return/^((?!chrome|android).)*safari/i.test(a)}const Dt=new xe;function Ee(a,e,t,r){const s=Dt;s.minX=0,s.minY=0,s.maxX=a.width/r|0,s.maxY=a.height/r|0;const n=w.getOptimalTexture(s.width,s.height,r,!1);return n.source.uploadMethodId="image",n.source.resource=a,n.source.alphaMode="premultiply-alpha-on-upload",n.frame.width=e/r,n.frame.height=t/r,n.source.emit("update",n.source),n.updateUvs(),n}function zt(a,e){const t=e.fontFamily,r=[],s={},n=/font-family:([^;"\s]+)/g,i=a.match(n);function o(d){s[d]||(r.push(d),s[d]=!0)}if(Array.isArray(t))for(let d=0;d<t.length;d++)o(t[d]);else o(t);i&&i.forEach(d=>{const l=d.split(":")[1].trim();o(l)});for(const d in e.tagStyles){const l=e.tagStyles[d].fontFamily;o(l)}return r}async function Ht(a){const t=await(await Be.get().fetch(a)).blob(),r=new FileReader;return await new Promise((n,i)=>{r.onloadend=()=>n(r.result),r.onerror=i,r.readAsDataURL(t)})}async function fe(a,e){const t=await Ht(e);return`@font-face {
|
|
font-family: "${a.fontFamily}";
|
|
src: url('${t}');
|
|
font-weight: ${a.fontWeight};
|
|
font-style: ${a.fontStyle};
|
|
}`}const H=new Map;async function Ot(a,e,t){const r=a.filter(s=>j.has(`${s}-and-url`)).map((s,n)=>{if(!H.has(s)){const{url:i}=j.get(`${s}-and-url`);n===0?H.set(s,fe({fontWeight:e.fontWeight,fontStyle:e.fontStyle,fontFamily:s},i)):H.set(s,fe({fontWeight:t.fontWeight,fontStyle:t.fontStyle,fontFamily:s},i))}return H.get(s)});return(await Promise.all(r)).join(`
|
|
`)}function Wt(a,e,t,r,s){const{domElement:n,styleElement:i,svgRoot:o}=s;n.innerHTML=`<style>${e.cssStyle}</style><div style='padding:0;'>${a}</div>`,n.setAttribute("style",`transform: scale(${t});transform-origin: top left; display: inline-block`),i.textContent=r;const{width:d,height:l}=s.image;return o.setAttribute("width",d.toString()),o.setAttribute("height",l.toString()),new XMLSerializer().serializeToString(o)}function Lt(a,e){const t=A.getOptimalCanvasAndContext(a.width,a.height,e),{context:r}=t;return r.clearRect(0,0,a.width,a.height),r.drawImage(a,0,0),A.returnCanvasAndContext(t),t.canvas}function It(a,e,t){return new Promise(async r=>{t&&await new Promise(s=>setTimeout(s,100)),a.onload=()=>{r()},a.src=`data:image/svg+xml;charset=utf8,${encodeURIComponent(e)}`,a.crossOrigin="anonymous"})}class J{constructor(e){this._activeTextures={},this._renderer=e,this._createCanvas=e.type===q.WEBGPU}getTexture(e){return this._buildTexturePromise(e.text,e.resolution,e.style)}getManagedTexture(e,t,r,s){if(this._activeTextures[s])return this._increaseReferenceCount(s),this._activeTextures[s].promise;const n=this._buildTexturePromise(e,t,r).then(i=>(this._activeTextures[s].texture=i,i));return this._activeTextures[s]={texture:null,promise:n,usageCount:1},n}async _buildTexturePromise(e,t,r){const s=b.get(De),n=zt(e,r),i=await Ot(n,r,N.defaultTextStyle),o=yt(e,r,i,s),d=Math.ceil(Math.ceil(Math.max(1,o.width)+r.padding*2)*t),l=Math.ceil(Math.ceil(Math.max(1,o.height)+r.padding*2)*t),u=s.image,c=2;u.width=(d|0)+c,u.height=(l|0)+c;const g=Wt(e,r,t,i,s);await It(u,g,At()&&n.length>0);let f=u;this._createCanvas&&(f=Lt(u,t));const m=Ee(f,u.width-c,u.height-c,t);return this._createCanvas&&this._renderer.texture.initSource(m.source),b.return(s),m}_increaseReferenceCount(e){this._activeTextures[e].usageCount++}decreaseReferenceCount(e){const t=this._activeTextures[e];t&&(t.usageCount--,t.usageCount===0&&(t.texture?this._cleanUp(t):t.promise.then(r=>{t.texture=r,this._cleanUp(t)}).catch(()=>{O("HTMLTextSystem: Failed to clean texture")}),this._activeTextures[e]=null))}_cleanUp(e){w.returnTexture(e.texture),e.texture.source.resource=null,e.texture.source.uploadMethodId="unknown"}getReferenceCount(e){return this._activeTextures[e].usageCount}destroy(){this._activeTextures=null}}J.extension={type:[h.WebGLSystem,h.WebGPUSystem,h.CanvasSystem],name:"htmlText"};J.defaultFontOptions={fontFamily:"Arial",fontStyle:"normal",fontWeight:"normal"};class $e{constructor(e){this._gpuText=Object.create(null),this._destroyRenderableBound=this.destroyRenderable.bind(this),this._renderer=e,this._renderer.runners.resolutionChange.add(this)}resolutionChange(){for(const e in this._gpuText){const t=this._gpuText[e];if(!t)continue;const r=t.batchableSprite.renderable;r._autoResolution&&(r._resolution=this._renderer.resolution,r.onViewUpdate())}}validateRenderable(e){const t=this._getGpuText(e),r=e._getKey();if(t.currentKey!==r){const{width:s,height:n}=this._renderer.canvasText.getTextureSize(e.text,e.resolution,e._style);return!(this._renderer.canvasText.getReferenceCount(t.currentKey)===1&&s===t.texture._source.width&&n===t.texture._source.height)}return!1}addRenderable(e,t){const s=this._getGpuText(e).batchableSprite;e._didTextUpdate&&this._updateText(e),this._renderer.renderPipes.batch.addToBatch(s,t)}updateRenderable(e){const r=this._getGpuText(e).batchableSprite;e._didTextUpdate&&this._updateText(e),r._batcher.updateElement(r)}destroyRenderable(e){e.off("destroyed",this._destroyRenderableBound),this._destroyRenderableById(e.uid)}_destroyRenderableById(e){const t=this._gpuText[e];this._renderer.canvasText.decreaseReferenceCount(t.currentKey),b.return(t.batchableSprite),this._gpuText[e]=null}_updateText(e){const t=e._getKey(),r=this._getGpuText(e),s=r.batchableSprite;r.currentKey!==t&&this._updateGpuText(e),e._didTextUpdate=!1;const n=e._style.padding;K(s.bounds,e._anchor,s.texture,n)}_updateGpuText(e){const t=this._getGpuText(e),r=t.batchableSprite;t.texture&&this._renderer.canvasText.decreaseReferenceCount(t.currentKey),t.texture=r.texture=this._renderer.canvasText.getManagedTexture(e),t.currentKey=e._getKey(),r.texture=t.texture}_getGpuText(e){return this._gpuText[e.uid]||this.initGpuText(e)}initGpuText(e){const t={texture:null,currentKey:"--",batchableSprite:b.get(Re)};return t.batchableSprite.renderable=e,t.batchableSprite.transform=e.groupTransform,t.batchableSprite.bounds={minX:0,maxX:1,minY:0,maxY:0},t.batchableSprite.roundPixels=this._renderer._roundPixels|e._roundPixels,this._gpuText[e.uid]=t,e._resolution=e._autoResolution?this._renderer.resolution:e.resolution,this._updateText(e),e.on("destroyed",this._destroyRenderableBound),t}destroy(){for(const e in this._gpuText)this._destroyRenderableById(e);this._gpuText=null,this._renderer=null}}$e.extension={type:[h.WebGLPipes,h.WebGPUPipes,h.CanvasPipes],name:"text"};function pe(a,e,t){for(let r=0,s=4*t*e;r<e;++r,s+=4)if(a[s+3]!==0)return!1;return!0}function ge(a,e,t,r,s){const n=4*e;for(let i=r,o=r*n+4*t;i<=s;++i,o+=n)if(a[o+3]!==0)return!1;return!0}function Et(a,e=1){const{width:t,height:r}=a,s=a.getContext("2d",{willReadFrequently:!0});if(s===null)throw new TypeError("Failed to get canvas 2D context");const i=s.getImageData(0,0,t,r).data;let o=0,d=0,l=t-1,u=r-1;for(;d<r&&pe(i,t,d);)++d;if(d===r)return ae.EMPTY;for(;pe(i,t,u);)--u;for(;ge(i,t,o,d,u);)++o;for(;ge(i,t,l,d,u);)--l;return++l,++u,new ae(o/e,d/e,(l-o)/e,(u-d)/e)}class Ve{constructor(e){this._activeTextures={},this._renderer=e}getTextureSize(e,t,r){const s=P.measureText(e||" ",r);let n=Math.ceil(Math.ceil(Math.max(1,s.width)+r.padding*2)*t),i=Math.ceil(Math.ceil(Math.max(1,s.height)+r.padding*2)*t);return n=Math.ceil(n-1e-6),i=Math.ceil(i-1e-6),n=ie(n),i=ie(i),{width:n,height:i}}getTexture(e,t,r,s){typeof e=="string"&&(ut("8.0.0","CanvasTextSystem.getTexture: Use object TextOptions instead of separate arguments"),e={text:e,style:r,resolution:t}),e.style instanceof Y||(e.style=new Y(e.style));const{texture:n,canvasAndContext:i}=this.createTextureAndCanvas(e);return this._renderer.texture.initSource(n._source),A.returnCanvasAndContext(i),n}createTextureAndCanvas(e){const{text:t,style:r}=e,s=e.resolution??this._renderer.resolution,n=P.measureText(t||" ",r),i=Math.ceil(Math.ceil(Math.max(1,n.width)+r.padding*2)*s),o=Math.ceil(Math.ceil(Math.max(1,n.height)+r.padding*2)*s),d=A.getOptimalCanvasAndContext(i,o),{canvas:l}=d;this.renderTextToCanvas(t,r,s,d);const u=Ee(l,i,o,s);if(r.trim){const c=Et(l,s);u.frame.copyFrom(c),u.updateUvs()}return{texture:u,canvasAndContext:d}}getManagedTexture(e){e._resolution=e._autoResolution?this._renderer.resolution:e.resolution;const t=e._getKey();if(this._activeTextures[t])return this._increaseReferenceCount(t),this._activeTextures[t].texture;const{texture:r,canvasAndContext:s}=this.createTextureAndCanvas(e);return this._activeTextures[t]={canvasAndContext:s,texture:r,usageCount:1},r}_increaseReferenceCount(e){this._activeTextures[e].usageCount++}decreaseReferenceCount(e){const t=this._activeTextures[e];if(t.usageCount--,t.usageCount===0){A.returnCanvasAndContext(t.canvasAndContext),w.returnTexture(t.texture);const r=t.texture.source;r.resource=null,r.uploadMethodId="unknown",r.alphaMode="no-premultiply-alpha",this._activeTextures[e]=null}}getReferenceCount(e){return this._activeTextures[e].usageCount}renderTextToCanvas(e,t,r,s){var T,C,F,U,M;const{canvas:n,context:i}=s,o=ct(t),d=P.measureText(e||" ",t),l=d.lines,u=d.lineHeight,c=d.lineWidths,g=d.maxLineWidth,f=d.fontProperties,m=n.height;i.resetTransform(),i.scale(r,r);const p=t.padding*2;if(i.clearRect(0,0,d.width+4+p,d.height+4+p),(T=t._stroke)!=null&&T.width){const v=t._stroke;i.lineWidth=v.width,i.miterLimit=v.miterLimit,i.lineJoin=v.join,i.lineCap=v.cap}i.font=o;let _,x;const B=t.dropShadow?2:1;for(let v=0;v<B;++v){const Z=t.dropShadow&&v===0,W=Z?Math.ceil(Math.max(1,m)+t.padding*2):0,Ye=W*r;if(Z){i.fillStyle="black",i.strokeStyle="black";const y=t.dropShadow,je=y.color,Ke=y.alpha;i.shadowColor=G.shared.setValue(je).setAlpha(Ke).toRgbaString();const qe=y.blur*r,re=y.distance*r;i.shadowBlur=qe,i.shadowOffsetX=Math.cos(y.angle)*re,i.shadowOffsetY=Math.sin(y.angle)*re+Ye}else i.globalAlpha=((C=t._fill)==null?void 0:C.alpha)??1,i.fillStyle=t._fill?ne(t._fill,i):null,(F=t._stroke)!=null&&F.width&&(i.strokeStyle=ne(t._stroke,i)),i.shadowColor="black";let ee=(u-f.fontSize)/2;u-f.fontSize<0&&(ee=0);const te=((U=t._stroke)==null?void 0:U.width)??0;for(let y=0;y<l.length;y++)_=te/2,x=te/2+y*u+f.ascent+ee,t.align==="right"?_+=g-c[y]:t.align==="center"&&(_+=(g-c[y])/2),(M=t._stroke)!=null&&M.width&&this._drawLetterSpacing(l[y],t,s,_+t.padding,x+t.padding-W,!0),t._fill!==void 0&&this._drawLetterSpacing(l[y],t,s,_+t.padding,x+t.padding-W)}}_drawLetterSpacing(e,t,r,s,n,i=!1){const{context:o}=r,d=t.letterSpacing;let l=!1;if(P.experimentalLetterSpacingSupported&&(P.experimentalLetterSpacing?(o.letterSpacing=`${d}px`,o.textLetterSpacing=`${d}px`,l=!0):(o.letterSpacing="0px",o.textLetterSpacing="0px")),d===0||l){i?o.strokeText(e,s,n):o.fillText(e,s,n);return}let u=s;const c=P.graphemeSegmenter(e);let g=o.measureText(e).width,f=0;for(let m=0;m<c.length;++m){const p=c[m];i?o.strokeText(p,u,n):o.fillText(p,u,n);let _="";for(let x=m+1;x<c.length;++x)_+=c[x];f=o.measureText(_).width,u+=g-f+d,g=f}}destroy(){this._activeTextures=null}}Ve.extension={type:[h.WebGLSystem,h.WebGPUSystem,h.CanvasSystem],name:"canvasText"};S.add(Me);S.add(Pe);S.add(ze);S.add(ht);S.add(He);S.add(Ve);S.add($e);S.add(Le);S.add(J);S.add(Ie);S.add(We);S.add(Oe);S.add(Ge);S.add(Ue);
|