mirror of
https://github.com/billsonnn/nitro-renderer.git
synced 2025-02-07 22:32:35 +01:00
281 lines
69 KiB
JavaScript
281 lines
69 KiB
JavaScript
|
"use strict";const i=require("./index-H-H0n5qT.cjs");class Z{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:a}=this._resizeTo;t=s,r=a}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}}Z.extension=i.ExtensionType.Application;class ee{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,i.UPDATE_PRIORITY.LOW)},get(){return this._ticker}}),this.stop=()=>{this._ticker.stop()},this.start=()=>{this._ticker.start()},this._ticker=null,this.ticker=e.sharedTicker?i.Ticker.shared:new i.Ticker,e.autoStart&&this.start()}static destroy(){if(this._ticker){const e=this._ticker;this.ticker=null,e.destroy()}}}ee.extension=i.ExtensionType.Application;class te{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}}te.extension={type:[i.ExtensionType.WebGLPipes,i.ExtensionType.WebGPUPipes,i.ExtensionType.CanvasPipes],name:"filter"};const Re=new i.Matrix;function Ue(n,e){return e.clear(),re(n,e),e.isValid||e.set(0,0,0,0),n.renderGroup?e.applyMatrix(n.renderGroup.localTransform):e.applyMatrix(n.parentRenderGroup.worldTransform),e}function re(n,e){if(n.localDisplayStatus!==7||!n.measurable)return;const t=!!n.effects.length;let r=e;if((n.renderGroup||t)&&(r=i.boundsPool.get().clear()),n.boundsArea)e.addRect(n.boundsArea,n.worldTransform);else{if(n.renderPipeId){const a=n.bounds;r.addFrame(a.minX,a.minY,a.maxX,a.maxY,n.groupTransform)}const s=n.children;for(let a=0;a<s.length;a++)re(s[a],r)}if(t){let s=!1;for(let a=0;a<n.effects.length;a++)n.effects[a].addBounds&&(s||(s=!0,r.applyMatrix(n.parentRenderGroup.worldTransform)),n.effects[a].addBounds(r,!0));s&&(r.applyMatrix(n.parentRenderGroup.worldTransform.copyTo(Re).invert()),e.addBounds(r,n.relativeGroupTransform)),e.addBounds(r),i.boundsPool.return(r)}else n.renderGroup&&(e.addBounds(r,n.relativeGroupTransform),i.boundsPool.return(r))}function Ge(n,e){e.clear();const t=e.matrix;for(let r=0;r<n.length;r++){const s=n[r];s.globalDisplayStatus<7||(e.matrix=s.worldTransform,s.addBounds(e))}return e.matrix=t,e}const Fe=new i.Geometry({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 ie{constructor(e){this._filterStackIndex=0,this._filterStack=[],this._filterGlobalUniforms=new i.UniformGroup({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 i.BindGroup({}),this.renderer=e}get activeBackTexture(){var e;return(e=
|
||
|
|
||
|
var index = 0;
|
||
|
|
||
|
for (let i = 0; i < ps.length; ++i)
|
||
|
{
|
||
|
const p = ps[i];
|
||
|
|
||
|
`);let r=0;for(const a in n){const o=n[a];if(e!==o.dynamic)continue;t.push(`offset = index + ${r}`),t.push(o.code);const l=i.getAttributeInfoFromFormat(o.format);r+=l.stride/4}t.push(`
|
||
|
index += stride * 4;
|
||
|
}
|
||
|
`),t.unshift(`
|
||
|
var stride = ${r};
|
||
|
`);const s=t.join(`
|
||
|
`);return new Function("ps","f32v","u32v",s)}class Ve{constructor(e){this._size=0,this._generateParticleUpdateCache={};const t=this._size=e.size??1e3,r=e.properties;let s=0,a=0;for(const h in r){const u=r[h],f=i.getAttributeInfoFromFormat(u.format);u.dynamic?a+=f.stride:s+=f.stride}this._dynamicStride=a/4,this._staticStride=s/4,this.staticAttributeBuffer=new i.ViewableBuffer(t*4*s),this.dynamicAttributeBuffer=new i.ViewableBuffer(t*4*a),this.indexBuffer=X(t);const o=new i.Geometry;let l=0,d=0;this._staticBuffer=new i.Buffer({data:new Float32Array(1),label:"static-particle-buffer",shrinkToFit:!1,usage:i.BufferUsage.VERTEX|i.BufferUsage.COPY_DST}),this._dynamicBuffer=new i.Buffer({data:new Float32Array(1),label:"dynamic-particle-buffer",shrinkToFit:!1,usage:i.BufferUsage.VERTEX|i.BufferUsage.COPY_DST});for(const h in r){const u=r[h],f=i.getAttributeInfoFromFormat(u.format);u.dynamic?(o.addAttribute(u.attributeName,{buffer:this._dynamicBuffer,stride:this._dynamicStride*4,offset:l*4,format:u.format}),l+=f.size):(o.addAttribute(u.attributeName,{buffer:this._staticBuffer,stride:this._staticStride*4,offset:d*4,format:u.format}),d+=f.size)}o.addIndex(this.indexBuffer);const c=this.getParticleUpdate(r);this._dynamicUpload=c.dynamicUpdate,this._staticUpload=c.staticUpdate,this.geometry=o}getParticleUpdate(e){const t=Oe(e);return this._generateParticleUpdateCache[t]?this._generateParticleUpdateCache[t]:(this._generateParticleUpdateCache[t]=this.generateParticleUpdate(e),this._generateParticleUpdateCache[t])}generateParticleUpdate(e){return We(e)}update(e,t){e.length>this._size&&(t=!0,this._size=Math.max(e.length,this._size*1.5|0),this.staticAttributeBuffer=new i.ViewableBuffer(this._size*this._staticStride*4*4),this.dynamicAttributeBuffer=new i.ViewableBuffer(this._size*this._dynamicStride*4*4),this.indexBuffer=X(this._size),this.geometry.indexBuffer.setDataWithSize(this.indexBuffer,this.indexBuffer.byteLength,!0));const r=this.dynamicAttributeBuffer;if(this._dynamicUpload(e,r.float32View,r.uint32View),this._dynamicBuffer.setDataWithSize(this.dynamicAttributeBuffer.float32View,e.length*this._dynamicStride*4,!0),t){const s=this.staticAttributeBuffer;this._staticUpload(e,s.float32View,s.uint32View),this._staticBuffer.setDataWithSize(s.float32View,e.length*this._staticStride*4,!0)}}destroy(){this._staticBuffer.destroy(),this._dynamicBuffer.destroy(),this.geometry.destroy()}}function Oe(n){const e=[];for(const t in n){const r=n[t];e.push(t,r.code,r.dynamic?"d":"s")}return e.join("_")}var Ie=`varying vec2 vUV;
|
||
|
varying vec4 vColor;
|
||
|
|
||
|
uniform sampler2D uTexture;
|
||
|
|
||
|
void main(void){
|
||
|
vec4 color = texture2D(uTexture, vUV) * vColor;
|
||
|
gl_FragColor = color;
|
||
|
}`,Le=`attribute vec2 aVertex;
|
||
|
attribute vec2 aUV;
|
||
|
attribute vec4 aColor;
|
||
|
|
||
|
attribute vec2 aPosition;
|
||
|
attribute float aRotation;
|
||
|
|
||
|
uniform mat3 uTranslationMatrix;
|
||
|
uniform float uRound;
|
||
|
uniform vec2 uResolution;
|
||
|
uniform vec4 uColor;
|
||
|
|
||
|
varying vec2 vUV;
|
||
|
varying vec4 vColor;
|
||
|
|
||
|
vec2 roundPixels(vec2 position, vec2 targetSize)
|
||
|
{
|
||
|
return (floor(((position * 0.5 + 0.5) * targetSize) + 0.5) / targetSize) * 2.0 - 1.0;
|
||
|
}
|
||
|
|
||
|
void main(void){
|
||
|
float cosRotation = cos(aRotation);
|
||
|
float sinRotation = sin(aRotation);
|
||
|
float x = aVertex.x * cosRotation - aVertex.y * sinRotation;
|
||
|
float y = aVertex.x * sinRotation + aVertex.y * cosRotation;
|
||
|
|
||
|
vec2 v = vec2(x, y);
|
||
|
v = v + aPosition;
|
||
|
|
||
|
gl_Position = vec4((uTranslationMatrix * vec3(v, 1.0)).xy, 0.0, 1.0);
|
||
|
|
||
|
if(uRound == 1.0)
|
||
|
{
|
||
|
gl_Position.xy = roundPixels(gl_Position.xy, uResolution);
|
||
|
}
|
||
|
|
||
|
vUV = aUV;
|
||
|
vColor = aColor * uColor;
|
||
|
}
|
||
|
`,K=`
|
||
|
struct ParticleUniforms {
|
||
|
uProjectionMatrix:mat3x3<f32>,
|
||
|
uResolution:vec2<f32>,
|
||
|
uRoundPixels:f32,
|
||
|
};
|
||
|
|
||
|
@group(0) @binding(0) var<uniform> uniforms: ParticleUniforms;
|
||
|
|
||
|
@group(1) @binding(0) var uTexture: texture_2d<f32>;
|
||
|
@group(1) @binding(1) var uSampler : sampler;
|
||
|
|
||
|
struct VSOutput {
|
||
|
@builtin(position) position: vec4<f32>,
|
||
|
@location(0) uv : vec2<f32>,
|
||
|
@location(1) color : vec4<f32>,
|
||
|
};
|
||
|
@vertex
|
||
|
fn mainVertex(
|
||
|
@location(0) aVertex: vec2<f32>,
|
||
|
@location(1) aPosition: vec2<f32>,
|
||
|
@location(2) aUV: vec2<f32>,
|
||
|
@location(3) aColor: vec4<f32>,
|
||
|
@location(4) aRotation: f32,
|
||
|
) -> VSOutput {
|
||
|
|
||
|
let v = vec2(
|
||
|
aVertex.x * cos(aRotation) - aVertex.y * sin(aRotation),
|
||
|
aVertex.x * sin(aRotation) + aVertex.y * cos(aRotation)
|
||
|
) + aPosition;
|
||
|
|
||
|
let position = vec4((uniforms.uProjectionMatrix * vec3(v, 1.0)).xy, 0.0, 1.0);
|
||
|
|
||
|
return VSOutput(
|
||
|
position,
|
||
|
aUV,
|
||
|
aColor,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
@fragment
|
||
|
fn mainFragment(
|
||
|
@location(0) uv: vec2<f32>,
|
||
|
@location(1) color: vec4<f32>,
|
||
|
@builtin(position) position: vec4<f32>,
|
||
|
) -> @location(0) vec4<f32> {
|
||
|
|
||
|
var sample = textureSample(uTexture, uSampler, uv) * color;
|
||
|
|
||
|
return sample;
|
||
|
}`;class $e extends i.Shader{constructor(){const e=i.GlProgram.from({vertex:Le,fragment:Ie}),t=i.GpuProgram.from({fragment:{source:K,entryPoint:"mainFragment"},vertex:{source:K,entryPoint:"mainVertex"}});super({glProgram:e,gpuProgram:t,resources:{uTexture:i.Texture.WHITE.source,uSampler:new i.TextureStyle({}),uniforms:{uTranslationMatrix:{value:new i.Matrix,type:"mat3x3<f32>"},uColor:{value:new i.Color(16777215),type:"vec4<f32>"},uRound:{value:1,type:"f32"},uResolution:{value:[0,0],type:"vec2<f32>"}}}})}}class fe{constructor(e,t){this.state=i.State.for2d(),this._gpuBufferHash=Object.create(null),this._destroyRenderableBound=this.destroyRenderable.bind(this),this.localUniforms=new i.UniformGroup({uTranslationMatrix:{value:new i.Matrix,type:"mat3x3<f32>"},uColor:{value:new Float32Array(4),type:"vec4<f32>"},uRound:{value:1,type:"f32"},uResolution:{value:[0,0],type:"vec2<f32>"}}),this.renderer=e,this.adaptor=t,this.defaultShader=new $e,this.state=i.State.for2d()}validateRenderable(e){return!1}addRenderable(e,t){this.renderer.renderPipes.batch.break(t),t.add(e)}getBuffers(e){return this._gpuBufferHash[e.uid]||this._initBuffer(e)}_initBuffer(e){return this._gpuBufferHash[e.uid]=new Ve({size:e.particleChildren.length,properties:e._properties}),e.on("destroyed",this._destroyRenderableBound),this._gpuBufferHash[e.uid]}updateRenderable(e){}destroyRenderable(e){this._gpuBufferHash[e.uid].destroy(),this._gpuBufferHash[e.uid]=null,e.off("destroyed",this._destroyRenderableBound)}execute(e){const t=e.particleChildren;if(t.length===0)return;const r=this.renderer,s=this.getBuffers(e);e.texture||(e.texture=t[0].texture);const a=this.state;s.update(t,e._childrenDirty),e._childrenDirty=!1,a.blendMode=i.getAdjustedBlendModeBlend(e.blendMode,e.texture._source);const o=this.localUniforms.uniforms,l=o.uTranslationMatrix;e.worldTransform.copyTo(l),l.prepend(r.globalUniforms.globalUniformData.projectionMatrix),o.uResolution=r.globalUniforms.globalUniformData.resolution,o.uRound=r._roundPixels|e._roundPixels,i.color32BitToUniform(e.groupColorAlpha,o.uColor,0),this.adaptor.execute(this,e)}destroy(){this.defaultShader&&(this.defaultShader.destroy(),this.defaultShader=null)}}class pe extends fe{constructor(e){super(e,new De)}}pe.extension={type:[i.ExtensionType.WebGLPipes],name:"particle"};class ge extends fe{constructor(e){super(e,new ze)}}ge.extension={type:[i.ExtensionType.WebGPUPipes],name:"particle"};const xe=class me extends Ee{constructor(e={}){e={...me.defaultOptions,...e},super({width:e.width,height:e.height,verticesX:4,verticesY:4}),this.update(e)}update(e){this.width=e.width??this.width,this.height=e.height??this.height,this._originalWidth=e.originalWidth??this._originalWidth,this._originalHeight=e.originalHeight??this._originalHeight,this._leftWidth=e.leftWidth??this._leftWidth,this._rightWidth=e.rightWidth??this._rightWidth,this._topHeight=e.topHeight??this._topHeight,this._bottomHeight=e.bottomHeight??this._bottomHeight,this.updateUvs(),this.updatePositions()}updatePositions(){const e=this.positions,t=this._leftWidth+this._rightWidth,r=this.width>t?1:this.width/t,s=this._topHeight+this._bottomHeight,a=this.height>s?1:this.height/s,o=Math.min(r,a);e[9]=e[11]=e[13]=e[15]=this._topHeight*o,e[17]=e[19]=e[21]=e[23]=this.height-this._bottomHeight*o,e[25]=e[27]=e[29]=e[31]=this.height,e[2]=e[10]=e[18]=e[26]=this._leftWidth*o,e[4]=e[12]=e[20]=e[28]=this.width-this._rightWidth*o,e[6]=e[14]=e[22]=e[30]=this.width,this.getBuffer("aPosition").update()}updateUvs(){const e=this.uvs;e[0]=e[8]=e[16]=e[24]=0,e[1]=e[3]=e[5]=e[7]=0,e[6]=e[14]=e[22]=e[30]=1,e[25]=e[27]=e[29]=e[31]=1;const t=1/this._originalWidth,r=1/this._originalHeight;e[2]=e[10]=e[18]=e[26]=t*this._leftWidth,e[9]=e[11]=e[13]=e[15]=r*this._topHeight,e[4]=e[12]=e[20]=e[28]=1-t*this._rightWidth,e[17]=e[19]=e[21]=e[23]=1-r*this._bottomHeight,this.getBuffer("aUV").update()}};xe.defaultOptions={width:100,height:100,leftWidth:10,topHeight:10,rightWidth:10,bottomHeight:10,originalWidth:100,originalHeight:100};let Ye=xe;class _e{constructor(e){this._gpuSpriteHash=Object.create(null),this._destro
|
||
|
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);
|
||
|
`}},je={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 U,G;class Ke extends i.Shader{constructor(){U??(U=i.compileHighShaderGpuProgram({name:"tiling-sprite-shader",bits:[i.localUniformBit,Xe,i.roundPixelsBit]})),G??(G=i.compileHighShaderGlProgram({name:"tiling-sprite-shader",bits:[i.localUniformBitGl,je,i.roundPixelsBitGl]}));const e=new i.UniformGroup({uMapCoord:{value:new i.Matrix,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 i.Matrix,type:"mat3x3<f32>"},uSizeAnchor:{value:new Float32Array([100,100,.5,.5]),type:"vec4<f32>"}});super({glProgram:G,gpuProgram:U,resources:{localUniforms:new i.UniformGroup({uTransformMatrix:{value:new i.Matrix,type:"mat3x3<f32>"},uColor:{value:new Float32Array([1,1,1,1]),type:"vec4<f32>"},uRound:{value:0,type:"f32"}}),tilingUniforms:e,uTexture:i.Texture.EMPTY.source,uSampler:i.Texture.EMPTY.source.style}})}updateUniforms(e,t,r,s,a,o){const l=this.resources.tilingUniforms,d=o.width,c=o.height,h=o.textureMatrix,u=l.uniforms.uTextureTransform;u.set(r.a*d/e,r.b*d/t,r.c*c/e,r.d*c/t,r.tx/e,r.ty/t),u.invert(),l.uniforms.uMapCoord=h.mapCoord,l.uniforms.uClampFrame=h.uClampFrame,l.uniforms.uClampOffset=h.uClampOffset,l.uniforms.uTextureTransform=u,l.uniforms.uSizeAnchor[0]=e,l.uniforms.uSizeAnchor[1]=t,l.uniforms.uSizeAnchor[2]=s,l.uniforms.uSizeAnchor[3]=a,o&&(this.resources.uTexture=o.source,this.resources.uSampler=o.source.style)}}class Ne extends A{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 qe(n,e){const t=n.anchor.x,r=n.anchor.y;e[0]=-t*n.width,e[1]=-r*n.height,e[2]=(1-t)*n.width,e[3]=-r*n.height,e[4]=(1-t)*n.width,e[5]=(1-r)*n.height,e[6]=-t*n.width,e[7]=(1-r)*n.height}function Qe(n,e,t,r){let s=0;const a=n.length/e,o=r.a,l=r.b,d=r.c,c=r.d,h=r.tx,u=r.ty;for(t*=e;s<a;){const f=n[t],g=n[t+1];n[t]=o*f+d*g+h,n[t+1]=l*f+c*g+u,t+=e,s++}}function Je(n,e){const t=n.texture,r=t.frame.width,s=t.frame.height;let a=0,o=0;n._applyAnchorToTexture&&(a=n.anchor.x,o=n.anchor.y),e[0]=e[6]=-a,e[2]=e[4]=1-a,e[1]=e[3]=-o,e[5]=e[7]=1-o;const l=i.Matrix.shared;l.copyFrom(n._tileTransform.matrix),l.tx/=n.width,l.ty/=n.height,l.invert(),l.scale(n.width/r,n.height/s),Qe(e,2,0,l)}const P=new Ne;class be{constructor(e){this._state=i.State.default2d,this._tilingSpriteDataHash=Object.create(null),this._destroyRenderableBound=this.destroyRenderable.bind(this),this._renderer=e,this._renderer.renderableGC.addManagedHash(this,"_tilingSpriteDataHash")}validateRenderable(e){const t=this._getTilingSpriteData(e),r=t.canBatch;this._updateCanBatch(e);const s=t.canBatch;if(s&&s===r){const{batchableMesh:a}=t;if(a&&a.texture._source!==e.texture._source)return!a._batcher.checkAndUpdateTexture(a,e.texture)}return r!==s}addRenderable(e,t){const r=this._renderer.renderPipes.batch;this._updateCanBatch(e);const s=this._getTilingSpriteData(e),{geometry:a,canBatch:o}=s;if(o){s.batchableMesh||(s.batchableMesh=new E);const l=s.batchableMesh;e.didViewUpdate&&(this._updateBatchableMesh(e),l.geometry=a,l.renderable=e,l.transform=e.groupTransform,l.texture=e._texture),l.roundPixels=this._renderer._roundPixels|e._roundPixels,r.addToBatch(l,t)}else r.break(t),s.shader||(s.shader=new Ke),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,i.color32BitToUniform(e.groupColorAlpha,r.uColor,0),this._state.blendMode=i.getAdjustedBlendModeBlend(e.groupBlendMode,e.texture._source),this._renderer.encoder.draw({geometry:P,shader:t,state:this._state})}updateRenderable(e){const t=this._getTilingSpriteData(e),{canBatch:r}=t;if(r){const{batchableMesh:s}=t;e.didViewUpdate&&this._updateBatchableMesh(e),s._batcher.updateElement(s)}else if(e.didViewUpdate){const{shader:s}=t;s.updateUniforms(e.width,e.height,e._tileTransform.matrix,e.anchor.x,e.a
|
||
|
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));
|
||
|
`}},et={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));
|
||
|
`}},tt={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;
|
||
|
|
||
|
}
|
||
|
`}},rt={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 F,k;class it extends i.Shader{constructor(){const e=new i.UniformGroup({uColor:{value:new Float32Array([1,1,1,1]),type:"vec4<f32>"},uTransformMatrix:{value:new i.Matrix,type:"mat3x3<f32>"},uDistance:{value:4,type:"f32"},uRound:{value:0,type:"f32"}}),t=i.getMaxTexturesPerBatch();F??(F=i.compileHighShaderGpuProgram({name:"sdf-shader",bits:[i.colorBit,i.generateTextureBatchBit(t),Ze,tt,i.roundPixelsBit]})),k??(k=i.compileHighShaderGlProgram({name:"sdf-shader",bits:[i.colorBitGl,i.generateTextureBatchBitGl(t),et,rt,i.roundPixelsBitGl]})),super({glProgram:k,gpuProgram:F,resources:{localUniforms:e,batchSamplers:i.getBatchSamplersUniformGroup(t)}})}}class ye{constructor(e){this._gpuBitmapText={},this._destroyRenderableBound=this.destroyRenderable.bind(this),this._renderer=e,this._renderer.renderableGC.addManagedHash(this,"_gpuBitmapText")}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);N(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&&(i.BigPool.return(t.customShader),t.customShader=null),i.BigPool.return(this._gpuBitmapText[e]),this._gpuBitmapText[e]=null}updateRenderable(e){const t=this._getGpuBitmapText(e);N(e,t),this._renderer.renderPipes.graphics.updateRenderable(t),t.context.customShader&&this._updateDistanceField(e)}_updateContext(e,t){const{context:r}=t,s=i.BitmapFontManager.getFont(e.text,e._style);r.clear(),s.distanceField.type!=="none"&&(r.customShader||(r.customShader=i.BigPool.get(it)));const a=Array.from(e.text),o=e._style;let l=s.baseLineOffset;const d=i.getBitmapTextLayout(a,o,s,!0);let c=0;const h=o.padding,u=d.scale;let f=d.width,g=d.height+d.offsetY;o._stroke&&(f+=o._stroke.width/u,g+=o._stroke.width/u),r.translate(-e._anchor._x*f-h,-e._anchor._y*g-h).scale(u,u);const x=s.applyFillAsTint?o._fill.color:16777215;for(let p=0;p<d.lines.length;p++){const m=d.lines[p];for(let _=0;_<m.charPositions.length;_++){const v=a[c++],b=s.chars[v];b!=null&&b.texture&&r.texture(b.texture,x||"black",Math.round(m.charPositions[_]+b.xOffset),Math.round(l+b.yOffset))}l+=s.lineHeight}}_getGpuBitmapText(e){return this._gpuBitmapText[e.uid]||this.initGpuText(e)}initGpuText(e){const t=i.BigPool.get(R);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=i.Cache.get(`${r}-bitmap`),{a,b:o,c:l,d}=e.groupTransform,c=Math.sqrt(a*a+o*o),h=Math.sqrt(l*l+d*d),u=(Math.abs(c)+Math.abs(h))/2,f=s.baseRenderedFontSize/e._style.fontSize,g=u*s.distanceField.range*(1/f);t.customShader.resources.localUniforms.uniforms.uDistance=g}destroy(){for(const e in this._gpuBitmapText)this._destroyRenderableByUid(e);this._gpuBitmapText=null,this._renderer=null}}ye.extension={type:[i.ExtensionType.WebGLPipes,i.ExtensionType.WebGPUPipes,i.ExtensionType.CanvasPipes],name:"bitmapText"};function N(n,e){e.groupTransform=n.groupTransform,e.groupColorAlpha=n.groupColorAlpha,e.groupColor=n.groupColor,e.groupBlendMode=n.groupBlendMode,e.globalDisplayStatus=n.globalDisplayStatus,e.groupTransform=n.groupTransform,e.localDisplayStatus=n.localDisplayStatus,e.groupAlpha=n.groupAlpha,e._roundPixels=n._roundPixels}class Te{constructor(e){this._gpuText=Object.create(null),this._destroyRenderableBound=this.destroyRenderable.bind(this),this._renderer=e,this._renderer.runners.resolutionChange.add(this),this._renderer.renderableGC.addManagedHash(this,"_gpuText")}resolutionChange(){for(const e in this._gpuText){const t=this._gpuText[e];if(!t)continue;const r=t.batchableSprite.renderable;r._autoResolut
|
||
|
font-family: "${n.fontFamily}";
|
||
|
src: url('${t}');
|
||
|
font-weight: ${n.fontWeight};
|
||
|
font-style: ${n.fontStyle};
|
||
|
}`}const M=new Map;async function lt(n,e,t){const r=n.filter(s=>i.Cache.has(`${s}-and-url`)).map((s,a)=>{if(!M.has(s)){const{url:o}=i.Cache.get(`${s}-and-url`);a===0?M.set(s,q({fontWeight:e.fontWeight,fontStyle:e.fontStyle,fontFamily:s},o)):M.set(s,q({fontWeight:t.fontWeight,fontStyle:t.fontStyle,fontFamily:s},o))}return M.get(s)});return(await Promise.all(r)).join(`
|
||
|
`)}function dt(n,e,t,r,s){const{domElement:a,styleElement:o,svgRoot:l}=s;a.innerHTML=`<style>${e.cssStyle}</style><div style='padding:0;'>${n}</div>`,a.setAttribute("style",`transform: scale(${t});transform-origin: top left; display: inline-block`),o.textContent=r;const{width:d,height:c}=s.image;return l.setAttribute("width",d.toString()),l.setAttribute("height",c.toString()),new XMLSerializer().serializeToString(l)}function ut(n,e){const t=i.CanvasPool.getOptimalCanvasAndContext(n.width,n.height,e),{context:r}=t;return r.clearRect(0,0,n.width,n.height),r.drawImage(n,0,0),t}function ct(n,e,t){return new Promise(async r=>{t&&await new Promise(s=>setTimeout(s,100)),n.onload=()=>{r()},n.src=`data:image/svg+xml;charset=utf8,${encodeURIComponent(e)}`,n.crossOrigin="anonymous"})}class D{constructor(e){this._activeTextures={},this._renderer=e,this._createCanvas=e.type===i.RendererType.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 a=this._buildTexturePromise(e,t,r).then(o=>(this._activeTextures[s].texture=o,o));return this._activeTextures[s]={texture:null,promise:a,usageCount:1},a}async _buildTexturePromise(e,t,r){const s=i.BigPool.get(le),a=at(e,r),o=await lt(a,r,H.defaultTextStyle),l=He(e,r,o,s),d=Math.ceil(Math.ceil(Math.max(1,l.width)+r.padding*2)*t),c=Math.ceil(Math.ceil(Math.max(1,l.height)+r.padding*2)*t),h=s.image,u=2;h.width=(d|0)+u,h.height=(c|0)+u;const f=dt(e,r,t,o,s);await ct(h,f,st()&&a.length>0);const g=h;let x;this._createCanvas&&(x=ut(h,t));const p=ve(x?x.canvas:g,h.width-u,h.height-u,t);return this._createCanvas&&(this._renderer.texture.initSource(p.source),i.CanvasPool.returnCanvasAndContext(x)),i.BigPool.return(s),p}_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(()=>{i.warn("HTMLTextSystem: Failed to clean texture")}),this._activeTextures[e]=null))}_cleanUp(e){i.TexturePool.returnTexture(e.texture),e.texture.source.resource=null,e.texture.source.uploadMethodId="unknown"}getReferenceCount(e){return this._activeTextures[e].usageCount}destroy(){this._activeTextures=null}}D.extension={type:[i.ExtensionType.WebGLSystem,i.ExtensionType.WebGPUSystem,i.ExtensionType.CanvasSystem],name:"htmlText"};D.defaultFontOptions={fontFamily:"Arial",fontStyle:"normal",fontWeight:"normal"};class Se{constructor(e){this._gpuText=Object.create(null),this._destroyRenderableBound=this.destroyRenderable.bind(this),this._renderer=e,this._renderer.runners.resolutionChange.add(this),this._renderer.renderableGC.addManagedHash(this,"_gpuText")}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.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.canvasText.decreaseReferenceCount(t.currentKey),i.BigPool.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 a=e._style.padding;i.updateQuadBounds(s.bounds,e._anchor,s.texture,a)}_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.getManagedTextu
|