UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 3.81 kB
import{path as e}from"../../core/path.js";import{Vec2 as r}from"../../core/math/vec2.js";import{Vec4 as a}from"../../core/math/vec4.js";import{TEXTURETYPE_RGBM as t,TEXTURETYPE_DEFAULT as s,ADDRESS_REPEAT as o,ADDRESS_CLAMP_TO_EDGE as d,ADDRESS_MIRRORED_REPEAT as i,FILTER_NEAREST as n,FILTER_LINEAR as m,FILTER_NEAREST_MIPMAP_NEAREST as c,FILTER_LINEAR_MIPMAP_NEAREST as p,FILTER_NEAREST_MIPMAP_LINEAR as u,FILTER_LINEAR_MIPMAP_LINEAR as l}from"../../platform/graphics/constants.js";import{http as f}from"../../platform/net/http.js";import{TextureAtlas as _}from"../../scene/texture-atlas.js";import{ResourceHandler as h}from"./handler.js";const g={repeat:o,clamp:d,mirror:i},v={nearest:n,linear:m,nearest_mip_nearest:c,linear_mip_nearest:p,nearest_mip_linear:u,linear_mip_linear:l},y=/^data\.frames\.(\d+)$/;class x extends h{constructor(e){super(e,"textureatlas"),this._loader=e.loader}load(r,a){"string"==typeof r&&(r={load:r,original:r});const t=this,s=this._loader.getHandler("texture");".json"===e.getExtension(r.original)?f.get(r.load,{retry:this.maxRetries>0,maxRetries:this.maxRetries},((e,s)=>{if(e)a(e);else{const e=r.original.replace(".json",".png");t._loader.load(e,"texture",((e,r)=>{e?a(e):a(null,{data:s,texture:r})}))}})):s.load(r,a)}open(e,r){const a=new _;if(r.texture&&r.data)a.texture=r.texture,a.__data=r.data;else{const t=this._loader.getHandler("texture").open(e,r);if(!t)return null;a.texture=t}return a}patch(e,o){if(!e.resource)return;e.resource.__data&&(void 0!==e.resource.__data.minfilter&&(e.data.minfilter=e.resource.__data.minfilter),void 0!==e.resource.__data.magfilter&&(e.data.magfilter=e.resource.__data.magfilter),void 0!==e.resource.__data.addressu&&(e.data.addressu=e.resource.__data.addressu),void 0!==e.resource.__data.addressv&&(e.data.addressv=e.resource.__data.addressv),void 0!==e.resource.__data.mipmaps&&(e.data.mipmaps=e.resource.__data.mipmaps),void 0!==e.resource.__data.anisotropy&&(e.data.anisotropy=e.resource.__data.anisotropy),void 0!==e.resource.__data.rgbm&&(e.data.rgbm=!!e.resource.__data.rgbm),e.data.frames=e.resource.__data.frames,delete e.resource.__data);const d=e.resource.texture;if(d&&(d.name=e.name,e.data.hasOwnProperty("minfilter")&&d.minFilter!==v[e.data.minfilter]&&(d.minFilter=v[e.data.minfilter]),e.data.hasOwnProperty("magfilter")&&d.magFilter!==v[e.data.magfilter]&&(d.magFilter=v[e.data.magfilter]),e.data.hasOwnProperty("addressu")&&d.addressU!==g[e.data.addressu]&&(d.addressU=g[e.data.addressu]),e.data.hasOwnProperty("addressv")&&d.addressV!==g[e.data.addressv]&&(d.addressV=g[e.data.addressv]),e.data.hasOwnProperty("mipmaps")&&d.mipmaps!==e.data.mipmaps&&(d.mipmaps=e.data.mipmaps),e.data.hasOwnProperty("anisotropy")&&d.anisotropy!==e.data.anisotropy&&(d.anisotropy=e.data.anisotropy),e.data.hasOwnProperty("rgbm"))){const r=e.data.rgbm?t:s;d.type!==r&&(d.type=r)}e.resource.texture=d;const i={};for(const t in e.data.frames){const s=e.data.frames[t];i[t]={rect:new a(s.rect),pivot:new r(s.pivot),border:new a(s.border)}}e.resource.frames=i,e.off("change",this._onAssetChange,this),e.on("change",this._onAssetChange,this)}_onAssetChange(e,t,s){let o;if("data"===t||"data.frames"===t){const t={};for(const e in s.frames)o=s.frames[e],t[e]={rect:new a(o.rect),pivot:new r(o.pivot),border:new a(o.border)};e.resource.frames=t}else{const d=t.match(y);if(d){const t=d[1];s?(e.resource.frames[t]?(o=e.resource.frames[t],o.rect.set(s.rect[0],s.rect[1],s.rect[2],s.rect[3]),o.pivot.set(s.pivot[0],s.pivot[1]),o.border.set(s.border[0],s.border[1],s.border[2],s.border[3])):e.resource.frames[t]={rect:new a(s.rect),pivot:new r(s.pivot),border:new a(s.border)},e.resource.fire("set:frame",t,e.resource.frames[t])):e.resource.frames[t]&&(delete e.resource.frames[t],e.resource.fire("remove:frame",t))}}}}export{x as TextureAtlasHandler};