@omnimedia/omnitool
Version:
open source video processing tools
219 lines (168 loc) • 25.8 kB
JavaScript
import{G as at,H as lt,J as ct,M as ft,N as xt,O as M,P as j,Q as ht,d as nt,l as it,o as ot,p as st,q as ut}from"./chunk-ERLCDDYH.js";function $(r,t,e=2){let n=t&&t.length,i=n?t[0]*e:r.length,s=mt(r,0,i,e,!0),o=[];if(!s||s.next===s.prev)return o;let u,a,l;if(n&&(s=ne(r,t,s,e)),r.length>80*e){u=r[0],a=r[1];let m=u,p=a;for(let d=e;d<i;d+=e){let v=r[d],h=r[d+1];v<u&&(u=v),h<a&&(a=h),v>m&&(m=v),h>p&&(p=h)}l=Math.max(m-u,p-a),l=l!==0?32767/l:0}return U(s,o,e,u,a,l,0),o}function mt(r,t,e,n,i){let s;if(i===pe(r,t,e,n)>0)for(let o=t;o<e;o+=n)s=pt(o/n|0,r[o],r[o+1],s);else for(let o=e-n;o>=t;o-=n)s=pt(o/n|0,r[o],r[o+1],s);return s&&A(s,s.next)&&(T(s),s=s.next),s}function w(r,t){if(!r)return r;t||(t=r);let e=r,n;do if(n=!1,!e.steiner&&(A(e,e.next)||g(e.prev,e,e.next)===0)){if(T(e),e=t=e.prev,e===e.next)break;n=!0}else e=e.next;while(n||e!==t);return t}function U(r,t,e,n,i,s,o){if(!r)return;!o&&s&&ae(r,n,i,s);let u=r;for(;r.prev!==r.next;){let a=r.prev,l=r.next;if(s?te(r,n,i,s):qt(r)){t.push(a.i,r.i,l.i),T(r),r=l.next,u=l.next;continue}if(r=l,r===u){o?o===1?(r=ee(w(r),t),U(r,t,e,n,i,s,2)):o===2&&re(r,t,e,n,i,s):U(w(r),t,e,n,i,s,1);break}}}function qt(r){let t=r.prev,e=r,n=r.next;if(g(t,e,n)>=0)return!1;let i=t.x,s=e.x,o=n.x,u=t.y,a=e.y,l=n.y,m=Math.min(i,s,o),p=Math.min(u,a,l),d=Math.max(i,s,o),v=Math.max(u,a,l),h=n.next;for(;h!==t;){if(h.x>=m&&h.x<=d&&h.y>=p&&h.y<=v&&P(i,u,s,a,o,l,h.x,h.y)&&g(h.prev,h,h.next)>=0)return!1;h=h.next}return!0}function te(r,t,e,n){let i=r.prev,s=r,o=r.next;if(g(i,s,o)>=0)return!1;let u=i.x,a=s.x,l=o.x,m=i.y,p=s.y,d=o.y,v=Math.min(u,a,l),h=Math.min(m,p,d),_=Math.max(u,a,l),y=Math.max(m,p,d),f=N(v,h,t,e,n),S=N(_,y,t,e,n),c=r.prevZ,x=r.nextZ;for(;c&&c.z>=f&&x&&x.z<=S;){if(c.x>=v&&c.x<=_&&c.y>=h&&c.y<=y&&c!==i&&c!==o&&P(u,m,a,p,l,d,c.x,c.y)&&g(c.prev,c,c.next)>=0||(c=c.prevZ,x.x>=v&&x.x<=_&&x.y>=h&&x.y<=y&&x!==i&&x!==o&&P(u,m,a,p,l,d,x.x,x.y)&&g(x.prev,x,x.next)>=0))return!1;x=x.nextZ}for(;c&&c.z>=f;){if(c.x>=v&&c.x<=_&&c.y>=h&&c.y<=y&&c!==i&&c!==o&&P(u,m,a,p,l,d,c.x,c.y)&&g(c.prev,c,c.next)>=0)return!1;c=c.prevZ}for(;x&&x.z<=S;){if(x.x>=v&&x.x<=_&&x.y>=h&&x.y<=y&&x!==i&&x!==o&&P(u,m,a,p,l,d,x.x,x.y)&&g(x.prev,x,x.next)>=0)return!1;x=x.nextZ}return!0}function ee(r,t){let e=r;do{let n=e.prev,i=e.next.next;!A(n,i)&&vt(n,e,e.next,i)&&z(n,i)&&z(i,n)&&(t.push(n.i,e.i,i.i),T(e),T(e.next),e=r=i),e=e.next}while(e!==r);return w(e)}function re(r,t,e,n,i,s){let o=r;do{let u=o.next.next;for(;u!==o.prev;){if(o.i!==u.i&&fe(o,u)){let a=gt(o,u);o=w(o,o.next),a=w(a,a.next),U(o,t,e,n,i,s,0),U(a,t,e,n,i,s,0);return}u=u.next}o=o.next}while(o!==r)}function ne(r,t,e,n){let i=[];for(let s=0,o=t.length;s<o;s++){let u=t[s]*n,a=s<o-1?t[s+1]*n:r.length,l=mt(r,u,a,n,!1);l===l.next&&(l.steiner=!0),i.push(ce(l))}i.sort(ie);for(let s=0;s<i.length;s++)e=oe(i[s],e);return e}function ie(r,t){let e=r.x-t.x;if(e===0&&(e=r.y-t.y,e===0)){let n=(r.next.y-r.y)/(r.next.x-r.x),i=(t.next.y-t.y)/(t.next.x-t.x);e=n-i}return e}function oe(r,t){let e=se(r,t);if(!e)return t;let n=gt(e,r);return w(n,n.next),w(e,e.next)}function se(r,t){let e=t,n=r.x,i=r.y,s=-1/0,o;if(A(r,e))return e;do{if(A(r,e.next))return e.next;if(i<=e.y&&i>=e.next.y&&e.next.y!==e.y){let p=e.x+(i-e.y)*(e.next.x-e.x)/(e.next.y-e.y);if(p<=n&&p>s&&(s=p,o=e.x<e.next.x?e:e.next,p===n))return o}e=e.next}while(e!==t);if(!o)return null;let u=o,a=o.x,l=o.y,m=1/0;e=o;do{if(n>=e.x&&e.x>=a&&n!==e.x&&dt(i<l?n:s,i,a,l,i<l?s:n,i,e.x,e.y)){let p=Math.abs(i-e.y)/(n-e.x);z(e,r)&&(p<m||p===m&&(e.x>o.x||e.x===o.x&&ue(o,e)))&&(o=e,m=p)}e=e.next}while(e!==u);return o}function ue(r,t){return g(r.prev,r,t.prev)<0&&g(t.next,r,r.next)<0}function ae(r,t,e,n){let i=r;do i.z===0&&(i.z=N(i.x,i.y,t,e,n)),i.prevZ=i.prev,i.nextZ=i.next,i=i.next;while(i!==r);i.prevZ.nextZ=null,i.prevZ=null,le(i)}function le(r){let t,e=1;do{let n=r,i;r=null;let s=null;for(t=0;n;){t++;let o=n,u=0;for(let l=0;l<e&&(u++,o=o.nextZ,!!o);l++);let a=e;for(;u>0||a>0&&o;)u!==0&&(a===0||!o||n.z<=o.z)?(i=n,n=n.nextZ,u--):(i=o,o=o.nextZ,a--),s?s.nextZ=i:r=i,i.prevZ=s,s=i;n=o}s.nextZ=null,e*=2}while(t>1);return r}function N(r,t,e,n,i){return r=(r-e)*i|0,t=(t-n)*i|0,r=(r|r<<8)&16711935,r=(r|r<<4)&252645135,r=(r|r<<2)&858993459,r=(r|r<<1)&1431655765,t=(t|t<<8)&16711935,t=(t|t<<4)&252645135,t=(t|t<<2)&858993459,t=(t|t<<1)&1431655765,r|t<<1}function ce(r){let t=r,e=r;do(t.x<e.x||t.x===e.x&&t.y<e.y)&&(e=t),t=t.next;while(t!==r);return e}function dt(r,t,e,n,i,s,o,u){return(i-o)*(t-u)>=(r-o)*(s-u)&&(r-o)*(n-u)>=(e-o)*(t-u)&&(e-o)*(s-u)>=(i-o)*(n-u)}function P(r,t,e,n,i,s,o,u){return!(r===o&&t===u)&&dt(r,t,e,n,i,s,o,u)}function fe(r,t){return r.next.i!==t.i&&r.prev.i!==t.i&&!xe(r,t)&&(z(r,t)&&z(t,r)&&he(r,t)&&(g(r.prev,r,t.prev)||g(r,t.prev,t))||A(r,t)&&g(r.prev,r,r.next)>0&&g(t.prev,t,t.next)>0)}function g(r,t,e){return(t.y-r.y)*(e.x-t.x)-(t.x-r.x)*(e.y-t.y)}function A(r,t){return r.x===t.x&&r.y===t.y}function vt(r,t,e,n){let i=R(g(r,t,e)),s=R(g(r,t,n)),o=R(g(e,n,r)),u=R(g(e,n,t));return!!(i!==s&&o!==u||i===0&&D(r,e,t)||s===0&&D(r,n,t)||o===0&&D(e,r,n)||u===0&&D(e,t,n))}function D(r,t,e){return t.x<=Math.max(r.x,e.x)&&t.x>=Math.min(r.x,e.x)&&t.y<=Math.max(r.y,e.y)&&t.y>=Math.min(r.y,e.y)}function R(r){return r>0?1:r<0?-1:0}function xe(r,t){let e=r;do{if(e.i!==r.i&&e.next.i!==r.i&&e.i!==t.i&&e.next.i!==t.i&&vt(e,e.next,r,t))return!0;e=e.next}while(e!==r);return!1}function z(r,t){return g(r.prev,r,r.next)<0?g(r,t,r.next)>=0&&g(r,r.prev,t)>=0:g(r,t,r.prev)<0||g(r,r.next,t)<0}function he(r,t){let e=r,n=!1,i=(r.x+t.x)/2,s=(r.y+t.y)/2;do e.y>s!=e.next.y>s&&e.next.y!==e.y&&i<(e.next.x-e.x)*(s-e.y)/(e.next.y-e.y)+e.x&&(n=!n),e=e.next;while(e!==r);return n}function gt(r,t){let e=W(r.i,r.x,r.y),n=W(t.i,t.x,t.y),i=r.next,s=t.prev;return r.next=t,t.prev=r,e.next=i,i.prev=e,n.next=e,e.prev=n,s.next=n,n.prev=s,n}function pt(r,t,e,n){let i=W(r,t,e);return n?(i.next=n.next,i.prev=n,n.next.prev=i,n.next=i):(i.prev=i,i.next=i),i}function T(r){r.next.prev=r.prev,r.prev.next=r.next,r.prevZ&&(r.prevZ.nextZ=r.nextZ),r.nextZ&&(r.nextZ.prevZ=r.prevZ)}function W(r,t,e){return{i:r,x:t,y:e,prev:null,next:null,z:0,prevZ:null,nextZ:null,steiner:!1}}function pe(r,t,e,n){let i=0;for(let s=t,o=e-n;s<e;s+=n)i+=(r[o]-r[s])*(r[s+1]+r[o+1]),o=s;return i}var Te=$.default||$;function Q(r,t,e){if(r)for(let n in r){let i=n.toLocaleLowerCase(),s=t[i];if(s){let o=r[n];n==="header"&&(o=o.replace(/@in\s+[^;]+;\s*/g,"").replace(/@out\s+[^;]+;\s*/g,"")),e&&s.push(`//----${e}----//`),s.push(o)}else st(`${n} placement hook does not exist in shader`)}}var me=/\{\{(.*?)\}\}/g;function K(r){let t={};return(r.match(me)?.map(n=>n.replace(/[{()}]/g,""))??[]).forEach(n=>{t[n]=[]}),t}function yt(r,t){let e,n=/@in\s+([^;]+);/g;for(;(e=n.exec(r))!==null;)t.push(e[1])}function L(r,t,e=!1){let n=[];yt(t,n),r.forEach(u=>{u.header&&yt(u.header,n)});let i=n;e&&i.sort();let s=i.map((u,a)=>` @location(${a}) ${u},`).join(`
`),o=t.replace(/@in\s+[^;]+;\s*/g,"");return o=o.replace("{{in}}",`
${s}
`),o}function _t(r,t){let e,n=/@out\s+([^;]+);/g;for(;(e=n.exec(r))!==null;)t.push(e[1])}function de(r){let e=/\b(\w+)\s*:/g.exec(r);return e?e[1]:""}function ve(r){let t=/@.*?\s+/g;return r.replace(t,"")}function St(r,t){let e=[];_t(t,e),r.forEach(a=>{a.header&&_t(a.header,e)});let n=0,i=e.sort().map(a=>a.indexOf("builtin")>-1?a:`@location(${n++}) ${a}`).join(`,
`),s=e.sort().map(a=>` var ${ve(a)};`).join(`
`),o=`return VSOutput(
${e.sort().map(a=>` ${de(a)}`).join(`,
`)});`,u=t.replace(/@out\s+[^;]+;\s*/g,"");return u=u.replace("{{struct}}",`
${i}
`),u=u.replace("{{start}}",`
${s}
`),u=u.replace("{{return}}",`
${o}
`),u}function X(r,t){let e=r;for(let n in t){let i=t[n];i.join(`
`).length?e=e.replace(`{{${n}}}`,`//-----${n} START-----//
${i.join(`
`)}
//----${n} FINISH----//`):e=e.replace(`{{${n}}}`,"")}return e}var B=Object.create(null),Y=new Map,ge=0;function bt({template:r,bits:t}){let e=wt(r,t);if(B[e])return B[e];let{vertex:n,fragment:i}=ye(r,t);return B[e]=At(n,i,t),B[e]}function Bt({template:r,bits:t}){let e=wt(r,t);return B[e]||(B[e]=At(r.vertex,r.fragment,t)),B[e]}function ye(r,t){let e=t.map(o=>o.vertex).filter(o=>!!o),n=t.map(o=>o.fragment).filter(o=>!!o),i=L(e,r.vertex,!0);i=St(e,i);let s=L(n,r.fragment,!0);return{vertex:i,fragment:s}}function wt(r,t){return t.map(e=>(Y.has(e)||Y.set(e,ge++),Y.get(e))).sort((e,n)=>e-n).join("-")+r.vertex+r.fragment}function At(r,t,e){let n=K(r),i=K(t);return e.forEach(s=>{Q(s.vertex,n,s.name),Q(s.fragment,i,s.name)}),{vertex:X(r,n),fragment:X(t,i)}}var It=`
@in aPosition: vec2<f32>;
@in aUV: vec2<f32>;
@out @builtin(position) vPosition: vec4<f32>;
@out vUV : vec2<f32>;
@out vColor : vec4<f32>;
{{header}}
struct VSOutput {
{{struct}}
};
@vertex
fn main( {{in}} ) -> VSOutput {
var worldTransformMatrix = globalUniforms.uWorldTransformMatrix;
var modelMatrix = mat3x3<f32>(
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
);
var position = aPosition;
var uv = aUV;
{{start}}
vColor = vec4<f32>(1., 1., 1., 1.);
{{main}}
vUV = uv;
var modelViewProjectionMatrix = globalUniforms.uProjectionMatrix * worldTransformMatrix * modelMatrix;
vPosition = vec4<f32>((modelViewProjectionMatrix * vec3<f32>(position, 1.0)).xy, 0.0, 1.0);
vColor *= globalUniforms.uWorldColorAlpha;
{{end}}
{{return}}
};
`,Mt=`
@in vUV : vec2<f32>;
@in vColor : vec4<f32>;
{{header}}
@fragment
fn main(
{{in}}
) -> @location(0) vec4<f32> {
{{start}}
var outColor:vec4<f32>;
{{main}}
var finalColor:vec4<f32> = outColor * vColor;
{{end}}
return finalColor;
};
`,Pt=`
in vec2 aPosition;
in vec2 aUV;
out vec4 vColor;
out vec2 vUV;
{{header}}
void main(void){
mat3 worldTransformMatrix = uWorldTransformMatrix;
mat3 modelMatrix = mat3(
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
);
vec2 position = aPosition;
vec2 uv = aUV;
{{start}}
vColor = vec4(1.);
{{main}}
vUV = uv;
mat3 modelViewProjectionMatrix = uProjectionMatrix * worldTransformMatrix * modelMatrix;
gl_Position = vec4((modelViewProjectionMatrix * vec3(position, 1.0)).xy, 0.0, 1.0);
vColor *= uWorldColorAlpha;
{{end}}
}
`,Ut=`
in vec4 vColor;
in vec2 vUV;
out vec4 finalColor;
{{header}}
void main(void) {
{{start}}
vec4 outColor;
{{main}}
finalColor = outColor * vColor;
{{end}}
}
`;var zt={name:"global-uniforms-bit",vertex:{header:`
struct GlobalUniforms {
uProjectionMatrix:mat3x3<f32>,
uWorldTransformMatrix:mat3x3<f32>,
uWorldColorAlpha: vec4<f32>,
uResolution: vec2<f32>,
}
@group(0) @binding(0) var<uniform> globalUniforms : GlobalUniforms;
`}};var Tt={name:"global-uniforms-bit",vertex:{header:`
uniform mat3 uProjectionMatrix;
uniform mat3 uWorldTransformMatrix;
uniform vec4 uWorldColorAlpha;
uniform vec2 uResolution;
`}};function Ct({bits:r,name:t}){let e=bt({template:{fragment:Mt,vertex:It},bits:[zt,...r]});return ct.from({name:t,vertex:{source:e.vertex,entryPoint:"main"},fragment:{source:e.fragment,entryPoint:"main"}})}function Gt({bits:r,name:t}){return new lt({name:t,...Bt({template:{vertex:Pt,fragment:Ut},bits:[Tt,...r]})})}var Et={name:"color-bit",vertex:{header:`
@in aColor: vec4<f32>;
`,main:`
vColor *= vec4<f32>(aColor.rgb * aColor.a, aColor.a);
`}},Vt={name:"color-bit",vertex:{header:`
in vec4 aColor;
`,main:`
vColor *= vec4(aColor.rgb * aColor.a, aColor.a);
`}};var J={};function _e(r){let t=[];if(r===1)t.push("@group(1) @binding(0) var textureSource1: texture_2d<f32>;"),t.push("@group(1) @binding(1) var textureSampler1: sampler;");else{let e=0;for(let n=0;n<r;n++)t.push(`@group(1) @binding(${e++}) var textureSource${n+1}: texture_2d<f32>;`),t.push(`@group(1) @binding(${e++}) var textureSampler${n+1}: sampler;`)}return t.join(`
`)}function Se(r){let t=[];if(r===1)t.push("outColor = textureSampleGrad(textureSource1, textureSampler1, vUV, uvDx, uvDy);");else{t.push("switch vTextureId {");for(let e=0;e<r;e++)e===r-1?t.push(" default:{"):t.push(` case ${e}:{`),t.push(` outColor = textureSampleGrad(textureSource${e+1}, textureSampler${e+1}, vUV, uvDx, uvDy);`),t.push(" break;}");t.push("}")}return t.join(`
`)}function kt(r){return J[r]||(J[r]={name:"texture-batch-bit",vertex:{header:`
@in aTextureIdAndRound: vec2<u32>;
@out @interpolate(flat) vTextureId : u32;
`,main:`
vTextureId = aTextureIdAndRound.y;
`,end:`
if(aTextureIdAndRound.x == 1)
{
vPosition = vec4<f32>(roundPixels(vPosition.xy, globalUniforms.uResolution), vPosition.zw);
}
`},fragment:{header:`
@in @interpolate(flat) vTextureId: u32;
${_e(r)}
`,main:`
var uvDx = dpdx(vUV);
var uvDy = dpdy(vUV);
${Se(r)}
`}}),J[r]}var O={};function be(r){let t=[];for(let e=0;e<r;e++)e>0&&t.push("else"),e<r-1&&t.push(`if(vTextureId < ${e}.5)`),t.push("{"),t.push(` outColor = texture(uTextures[${e}], vUV);`),t.push("}");return t.join(`
`)}function Dt(r){return O[r]||(O[r]={name:"texture-batch-bit",vertex:{header:`
in vec2 aTextureIdAndRound;
out float vTextureId;
`,main:`
vTextureId = aTextureIdAndRound.y;
`,end:`
if(aTextureIdAndRound.x == 1.)
{
gl_Position.xy = roundPixels(gl_Position.xy, uResolution);
}
`},fragment:{header:`
in float vTextureId;
uniform sampler2D uTextures[${r}];
`,main:`
${be(r)}
`}}),O[r]}var Rt={name:"round-pixels-bit",vertex:{header:`
fn roundPixels(position: vec2<f32>, targetSize: vec2<f32>) -> vec2<f32>
{
return (floor(((position * 0.5 + 0.5) * targetSize) + 0.5) / targetSize) * 2.0 - 1.0;
}
`}},$t={name:"round-pixels-bit",vertex:{header:`
vec2 roundPixels(vec2 position, vec2 targetSize)
{
return (floor(((position * 0.5 + 0.5) * targetSize) + 0.5) / targetSize) * 2.0 - 1.0;
}
`}};function q(r,t,e,n){if(e??(e=0),n??(n=Math.min(r.byteLength-e,t.byteLength)),!(e&7)&&!(n&7)){let i=n/8;new Float64Array(t,0,i).set(new Float64Array(r,e,i))}else if(!(e&3)&&!(n&3)){let i=n/4;new Float32Array(t,0,i).set(new Float32Array(r,e,i))}else new Uint8Array(t).set(new Uint8Array(r,e,n))}var Ft={normal:"normal-npm",add:"add-npm",screen:"screen-npm"},Be=(r=>(r[r.DISABLED=0]="DISABLED",r[r.RENDERING_MASK_ADD=1]="RENDERING_MASK_ADD",r[r.MASK_ACTIVE=2]="MASK_ACTIVE",r[r.INVERSE_MASK_ACTIVE=3]="INVERSE_MASK_ACTIVE",r[r.RENDERING_MASK_REMOVE=4]="RENDERING_MASK_REMOVE",r[r.NONE=5]="NONE",r))(Be||{});var we=["precision mediump float;","void main(void){","float test = 0.1;","%forloop%","gl_FragColor = vec4(0.0);","}"].join(`
`);function Ae(r){let t="";for(let e=0;e<r;++e)e>0&&(t+=`
else `),e<r-1&&(t+=`if(test == ${e}.0){}`);return t}function Zt(r,t){if(r===0)throw new Error("Invalid value of `0` passed to `checkMaxIfStatementsInShader`");let e=t.createShader(t.FRAGMENT_SHADER);try{for(;;){let n=we.replace(/%forloop%/gi,Ae(r));if(t.shaderSource(e,n),t.compileShader(e),!t.getShaderParameter(e,t.COMPILE_STATUS))r=r/2|0;else break}}finally{t.deleteShader(e)}return r}var Ht={};function jt(r){let t=Ht[r];if(t)return t;let e=new Int32Array(r);for(let n=0;n<r;n++)e[n]=n;return t=Ht[r]=new ft({uTextures:{value:e,type:"i32",size:r}},{isStatic:!0}),t}var C=class{constructor(t){typeof t=="number"?this.rawBinaryData=new ArrayBuffer(t):t instanceof Uint8Array?this.rawBinaryData=t.buffer:this.rawBinaryData=t,this.uint32View=new Uint32Array(this.rawBinaryData),this.float32View=new Float32Array(this.rawBinaryData),this.size=this.rawBinaryData.byteLength}get int8View(){return this._int8View||(this._int8View=new Int8Array(this.rawBinaryData)),this._int8View}get uint8View(){return this._uint8View||(this._uint8View=new Uint8Array(this.rawBinaryData)),this._uint8View}get int16View(){return this._int16View||(this._int16View=new Int16Array(this.rawBinaryData)),this._int16View}get int32View(){return this._int32View||(this._int32View=new Int32Array(this.rawBinaryData)),this._int32View}get float64View(){return this._float64Array||(this._float64Array=new Float64Array(this.rawBinaryData)),this._float64Array}get bigUint64View(){return this._bigUint64Array||(this._bigUint64Array=new BigUint64Array(this.rawBinaryData)),this._bigUint64Array}view(t){return this[`${t}View`]}destroy(){this.rawBinaryData=null,this.uint32View=null,this.float32View=null,this.uint16View=null,this._int8View=null,this._uint8View=null,this._int16View=null,this._int32View=null,this._float64Array=null,this._bigUint64Array=null}static sizeOf(t){switch(t){case"int8":case"uint8":return 1;case"int16":case"uint16":return 2;case"int32":case"uint32":case"float32":return 4;default:throw new Error(`${t} isn't a valid view type`)}}};function tt(r,t){return t.alphaMode==="no-premultiply-alpha"&&Ft[r]||r}var I=null;function Nt(){if(I)return I;let r=at();return I=r.getParameter(r.MAX_TEXTURE_IMAGE_UNITS),I=Zt(I,r),r.getExtension("WEBGL_lose_context")?.loseContext(),I}var F=class{constructor(){this.ids=Object.create(null),this.textures=[],this.count=0}clear(){for(let t=0;t<this.count;t++){let e=this.textures[t];this.textures[t]=null,this.ids[e.uid]=null}this.count=0}};var et=class{constructor(){this.renderPipeId="batch",this.action="startBatch",this.start=0,this.size=0,this.textures=new F,this.blendMode="normal",this.topology="triangle-strip",this.canBundle=!0}destroy(){this.textures=null,this.gpuBindGroup=null,this.bindGroup=null,this.batcher=null,this.elements=null}},E=[],Z=0;ut.register({clear:()=>{if(E.length>0)for(let r of E)r&&r.destroy();E.length=0,Z=0}});function Wt(){return Z>0?E[--Z]:new et}function Qt(r){r.elements=null,E[Z++]=r}var G=0,Kt=class Lt{constructor(t){this.uid=it("batcher"),this.dirty=!0,this.batchIndex=0,this.batches=[],this._elements=[],t={...Lt.defaultOptions,...t},t.maxTextures||(ot("v8.8.0","maxTextures is a required option for Batcher now, please pass it in the options"),t.maxTextures=Nt());let{maxTextures:e,attributesInitialSize:n,indicesInitialSize:i}=t;this.attributeBuffer=new C(n*4),this.indexBuffer=new Uint16Array(i),this.maxTextures=e}begin(){this.elementSize=0,this.elementStart=0,this.indexSize=0,this.attributeSize=0;for(let t=0;t<this.batchIndex;t++)Qt(this.batches[t]);this.batchIndex=0,this._batchIndexStart=0,this._batchIndexSize=0,this.dirty=!0}add(t){this._elements[this.elementSize++]=t,t._indexStart=this.indexSize,t._attributeStart=this.attributeSize,t._batcher=this,this.indexSize+=t.indexSize,this.attributeSize+=t.attributeSize*this.vertexSize}checkAndUpdateTexture(t,e){let n=t._batch.textures.ids[e._source.uid];return!n&&n!==0?!1:(t._textureId=n,t.texture=e,!0)}updateElement(t){this.dirty=!0;let e=this.attributeBuffer;t.packAsQuad?this.packQuadAttributes(t,e.float32View,e.uint32View,t._attributeStart,t._textureId):this.packAttributes(t,e.float32View,e.uint32View,t._attributeStart,t._textureId)}break(t){let e=this._elements;if(!e[this.elementStart])return;let n=Wt(),i=n.textures;i.clear();let s=e[this.elementStart],o=tt(s.blendMode,s.texture._source),u=s.topology;this.attributeSize*4>this.attributeBuffer.size&&this._resizeAttributeBuffer(this.attributeSize*4),this.indexSize>this.indexBuffer.length&&this._resizeIndexBuffer(this.indexSize);let a=this.attributeBuffer.float32View,l=this.attributeBuffer.uint32View,m=this.indexBuffer,p=this._batchIndexSize,d=this._batchIndexStart,v="startBatch",h=[],_=this.maxTextures;for(let y=this.elementStart;y<this.elementSize;++y){let f=e[y];e[y]=null;let c=f.texture._source,x=tt(f.blendMode,c),b=o!==x||u!==f.topology;if(c._batchTick===G&&!b){f._textureId=c._textureBindLocation,p+=f.indexSize,f.packAsQuad?(this.packQuadAttributes(f,a,l,f._attributeStart,f._textureId),this.packQuadIndex(m,f._indexStart,f._attributeStart/this.vertexSize)):(this.packAttributes(f,a,l,f._attributeStart,f._textureId),this.packIndex(f,m,f._indexStart,f._attributeStart/this.vertexSize)),f._batch=n,h.push(f);continue}c._batchTick=G,(i.count>=_||b)&&(this._finishBatch(n,d,p-d,i,o,u,t,v,h),v="renderBatch",d=p,o=x,u=f.topology,n=Wt(),i=n.textures,i.clear(),h=[],++G),f._textureId=c._textureBindLocation=i.count,i.ids[c.uid]=i.count,i.textures[i.count++]=c,f._batch=n,h.push(f),p+=f.indexSize,f.packAsQuad?(this.packQuadAttributes(f,a,l,f._attributeStart,f._textureId),this.packQuadIndex(m,f._indexStart,f._attributeStart/this.vertexSize)):(this.packAttributes(f,a,l,f._attributeStart,f._textureId),this.packIndex(f,m,f._indexStart,f._attributeStart/this.vertexSize))}i.count>0&&(this._finishBatch(n,d,p-d,i,o,u,t,v,h),d=p,++G),this.elementStart=this.elementSize,this._batchIndexStart=d,this._batchIndexSize=p}_finishBatch(t,e,n,i,s,o,u,a,l){t.gpuBindGroup=null,t.bindGroup=null,t.action=a,t.batcher=this,t.textures=i,t.blendMode=s,t.topology=o,t.start=e,t.size=n,t.elements=l,++G,this.batches[this.batchIndex++]=t,u.add(t)}finish(t){this.break(t)}ensureAttributeBuffer(t){t*4<=this.attributeBuffer.size||this._resizeAttributeBuffer(t*4)}ensureIndexBuffer(t){t<=this.indexBuffer.length||this._resizeIndexBuffer(t)}_resizeAttributeBuffer(t){let e=Math.max(t,this.attributeBuffer.size*2),n=new C(e);q(this.attributeBuffer.rawBinaryData,n.rawBinaryData),this.attributeBuffer=n}_resizeIndexBuffer(t){let e=this.indexBuffer,n=Math.max(t,e.length*1.5);n+=n%2;let i=n>65535?new Uint32Array(n):new Uint16Array(n);if(i.BYTES_PER_ELEMENT!==e.BYTES_PER_ELEMENT)for(let s=0;s<e.length;s++)i[s]=e[s];else q(e.buffer,i.buffer);this.indexBuffer=i}packQuadIndex(t,e,n){t[e]=n+0,t[e+1]=n+1,t[e+2]=n+2,t[e+3]=n+0,t[e+4]=n+2,t[e+5]=n+3}packIndex(t,e,n,i){let s=t.indices,o=t.indexSize,u=t.indexOffset,a=t.attributeOffset;for(let l=0;l<o;l++)e[n++]=i+s[l+u]-a}destroy(t={}){if(this.batches!==null){for(let e=0;e<this.batchIndex;e++)Qt(this.batches[e]);this.batches=null,this.geometry.destroy(!0),this.geometry=null,t.shader&&(this.shader?.destroy(),this.shader=null);for(let e=0;e<this._elements.length;e++)this._elements[e]&&(this._elements[e]._batch=null);this._elements=null,this.indexBuffer=null,this.attributeBuffer.destroy(),this.attributeBuffer=null}}};Kt.defaultOptions={maxTextures:null,attributesInitialSize:4,indicesInitialSize:6};var Xt=Kt;var Ie=new Float32Array(1),Me=new Uint32Array(1),H=class extends ht{constructor(){let e=new j({data:Ie,label:"attribute-batch-buffer",usage:M.VERTEX|M.COPY_DST,shrinkToFit:!1}),n=new j({data:Me,label:"index-batch-buffer",usage:M.INDEX|M.COPY_DST,shrinkToFit:!1}),i=24;super({attributes:{aPosition:{buffer:e,format:"float32x2",stride:i,offset:0},aUV:{buffer:e,format:"float32x2",stride:i,offset:8},aColor:{buffer:e,format:"unorm8x4",stride:i,offset:16},aTextureIdAndRound:{buffer:e,format:"uint16x2",stride:i,offset:20}},indexBuffer:n})}};var V=class extends xt{constructor(t){let e=Gt({name:"batch",bits:[Vt,Dt(t),$t]}),n=Ct({name:"batch",bits:[Et,kt(t),Rt]});super({glProgram:e,gpuProgram:n,resources:{batchSamplers:jt(t)}}),this.maxTextures=t}};var k=null,Yt=class Jt extends Xt{constructor(t){super(t),this.geometry=new H,this.name=Jt.extension.name,this.vertexSize=6,k??(k=new V(t.maxTextures)),this.shader=k}packAttributes(t,e,n,i,s){let o=s<<16|t.roundPixels&65535,u=t.transform,a=u.a,l=u.b,m=u.c,p=u.d,d=u.tx,v=u.ty,{positions:h,uvs:_}=t,y=t.color,f=t.attributeOffset,S=f+t.attributeSize;for(let c=f;c<S;c++){let x=c*2,b=h[x],rt=h[x+1];e[i++]=a*b+m*rt+d,e[i++]=p*rt+l*b+v,e[i++]=_[x],e[i++]=_[x+1],n[i++]=y,n[i++]=o}}packQuadAttributes(t,e,n,i,s){let o=t.texture,u=t.transform,a=u.a,l=u.b,m=u.c,p=u.d,d=u.tx,v=u.ty,h=t.bounds,_=h.maxX,y=h.minX,f=h.maxY,S=h.minY,c=o.uvs,x=t.color,b=s<<16|t.roundPixels&65535;e[i+0]=a*y+m*S+d,e[i+1]=p*S+l*y+v,e[i+2]=c.x0,e[i+3]=c.y0,n[i+4]=x,n[i+5]=b,e[i+6]=a*_+m*S+d,e[i+7]=p*S+l*_+v,e[i+8]=c.x1,e[i+9]=c.y1,n[i+10]=x,n[i+11]=b,e[i+12]=a*_+m*f+d,e[i+13]=p*f+l*_+v,e[i+14]=c.x2,e[i+15]=c.y2,n[i+16]=x,n[i+17]=b,e[i+18]=a*y+m*f+d,e[i+19]=p*f+l*y+v,e[i+20]=c.x3,e[i+21]=c.y3,n[i+22]=x,n[i+23]=b}_updateMaxTextures(t){this.shader.maxTextures!==t&&(k=new V(t),this.shader=k)}destroy(){this.shader=null,super.destroy()}};Yt.extension={type:[nt.Batcher],name:"default"};var $r=Yt;var Ot=class{constructor(t){this.items=Object.create(null);let{renderer:e,type:n,onUnload:i,priority:s,name:o}=t;this._renderer=e,e.gc.addResourceHash(this,"items",n,s??0),this._onUnload=i,this.name=o}add(t){return this.items[t.uid]?!1:(this.items[t.uid]=t,t.once("unload",this.remove,this),t._gcLastUsed=this._renderer.gc.now,!0)}remove(t,...e){if(!this.items[t.uid])return;let n=t._gpuData[this._renderer.uid];n&&(this._onUnload?.(t,...e),n.destroy(),t._gpuData[this._renderer.uid]=null,this.items[t.uid]=null)}removeAll(...t){Object.values(this.items).forEach(e=>e&&this.remove(e,...t))}destroy(...t){this.removeAll(...t),this.items=Object.create(null),this._renderer=null,this._onUnload=null}};export{Te as a,Ct as b,Gt as c,Et as d,Vt as e,kt as f,Dt as g,Rt as h,$t as i,q as j,Be as k,Zt as l,jt as m,$r as n,Ot as o};
//# sourceMappingURL=chunk-LFJVYUTB.js.map