UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 2.54 kB
import{WasmModule as s}from"../../core/wasm-module.js";import{DracoWorker as e}from"./draco-worker.js";import{http as t}from"../../platform/net/http.js";class r{constructor(){this.workers=[[],[],[]],this.jobId=0,this.jobQueue=[],this.jobCallbacks=new Map,this.run=(s,e)=>{s.postMessage({type:"decodeMesh",jobId:e.jobId,buffer:e.buffer},[e.buffer])}}init(s){for(s.forEach((s=>{s.addEventListener("message",(e=>{const t=e.data,r=this.jobCallbacks.get(t.jobId);if(r&&r(t.error,{indices:t.indices,vertices:t.vertices,attributes:t.attributes}),this.jobCallbacks.delete(t.jobId),this.jobQueue.length>0){const e=this.jobQueue.shift();this.run(s,e)}else{const e=this.workers[2].indexOf(s);if(-1!==e)this.workers[2].splice(e,1),this.workers[1].push(s);else{const e=this.workers[1].indexOf(s);-1!==e&&(this.workers[1].splice(e,1),this.workers[0].push(s))}}}))})),this.workers[0]=s;this.jobQueue.length&&(this.workers[0].length||this.workers[1].length);){const s=this.jobQueue.shift();if(this.workers[0].length>0){const e=this.workers[0].shift();this.workers[1].push(e),this.run(e,s)}else{const e=this.workers[1].shift();this.workers[2].push(e),this.run(e,s)}}}enqueueJob(s,e){const t={jobId:this.jobId++,buffer:s};if(this.jobCallbacks.set(t.jobId,e),this.workers[0].length>0){const s=this.workers[0].shift();this.workers[1].push(s),this.run(s,t)}else if(this.workers[1].length>0){const s=this.workers[1].shift();this.workers[2].push(s),this.run(s,t)}else this.jobQueue.push(t)}}const o=s=>{const e=()=>fetch(s).then((s=>s.arrayBuffer())).then((s=>WebAssembly.compile(s)));return WebAssembly.compileStreaming?WebAssembly.compileStreaming(fetch(s)).catch((s=>e())):e()};let i,n;const h=h=>{if(i)return!0;if(!h)if(n)h=n;else{const e=s.getConfig("DracoDecoderModule");h=e?{jsUrl:e.glueUrl,wasmUrl:e.wasmUrl,numWorkers:e.numWorkers}:{jsUrl:"draco.wasm.js",wasmUrl:"draco.wasm.wasm",numWorkers:1}}return!(!h.jsUrl||!h.wasmUrl)&&(i=new r,Promise.all([(l=h.jsUrl,new Promise(((s,e)=>{const r={cache:!0,responseType:"text",retry:!0,maxRetries:3};t.get(l,r,((t,r)=>{t?e(t):s(r)}))}))),o(h.wasmUrl)]).then((([s,t])=>{const r=["/* draco */",s,"/* worker */",`(\n${e.toString()}\n)()\n\n`].join("\n"),o=new Blob([r],{type:"application/javascript"}),n=URL.createObjectURL(o),l=Math.max(1,Math.min(16,h.numWorkers||1)),a=[];for(let s=0;s<l;++s){const s=new Worker(n);s.postMessage({type:"init",module:t}),a.push(s)}i.init(a)})),!0);var l},l=s=>{null!=s&&s.lazyInit?n=s:h(s)},a=(s,e)=>!!h()&&(i.enqueueJob(s,e),!0);export{a as dracoDecode,l as dracoInitialize};