UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 1.92 kB
import{extends as t}from"../../../../_virtual/_rollupPluginBabelHelpers.js";import{PIXELFORMAT_RGBA8 as o}from"../../../platform/graphics/constants.js";import{Texture as e}from"../../../platform/graphics/texture.js";import{http as r}from"../../../platform/net/http.js";import{ABSOLUTE_URL as i}from"../../asset/constants.js";import{TextureParser as s}from"./texture.js";class n extends s{constructor(t,o){super(),this.crossOrigin=t.prefix?"anonymous":null,this.maxRetries=0,this.device=o}load(t,o,e){var r;const s=!(null==e||null==(r=e.file)||!r.contents);if(s){if(this.device.supportsImageBitmap)return void this._loadImageBitmapFromBlob(new Blob([e.file.contents]),o);t={load:URL.createObjectURL(new Blob([e.file.contents])),original:t.original}}const n=(e,r)=>{s&&URL.revokeObjectURL(t.load),o(e,r)};let a;e&&e.options&&e.options.hasOwnProperty("crossOrigin")?a=e.options.crossOrigin:i.test(t.load)&&(a=this.crossOrigin),this.device.supportsImageBitmap?this._loadImageBitmap(t.load,t.original,a,n):this._loadImage(t.load,t.original,a,n)}open(r,i,s,n={}){const a=new e(s,t({name:r,width:i.width,height:i.height,format:o},n));return a.setSource(i),a}_loadImage(t,o,e,r){const i=new Image;e&&(i.crossOrigin=e);let s=0;const n=this.maxRetries;let a;i.onload=function(){r(null,i)},i.onerror=function(){if(!a)if(n>0&&++s<=n){const e=100*Math.pow(2,s);console.log(`Error loading Texture from: '${o}' - Retrying in ${e}ms...`);const r=t.indexOf("?")>=0?"&":"?";a=setTimeout((()=>{i.src=`${t+r}retry=${Date.now()}`,a=null}),e)}else r(`Error loading Texture from: '${o}'`)},i.src=t}_loadImageBitmap(t,o,e,i){const s={cache:!0,responseType:"blob",retry:this.maxRetries>0,maxRetries:this.maxRetries};r.get(t,s,((t,o)=>{t?i(t):this._loadImageBitmapFromBlob(o,i)}))}_loadImageBitmapFromBlob(t,o){createImageBitmap(t,{premultiplyAlpha:"none",colorSpaceConversion:"none"}).then((t=>o(null,t))).catch((t=>o(t)))}}export{n as ImgParser};