UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 3.98 kB
import{path as e}from"../../core/path.js";import{TEXTURETYPE_RGBM as r,TEXTURETYPE_SWIZZLEGGGR as t,PIXELFORMAT_RGB8 as s,ADDRESS_REPEAT as i,ADDRESS_CLAMP_TO_EDGE as a,ADDRESS_MIRRORED_REPEAT as o,FILTER_NEAREST as n,FILTER_LINEAR as p,FILTER_NEAREST_MIPMAP_NEAREST as l,FILTER_LINEAR_MIPMAP_NEAREST as m,FILTER_NEAREST_MIPMAP_LINEAR as h,FILTER_LINEAR_MIPMAP_LINEAR as f,TEXTURETYPE_DEFAULT as c,TEXTURETYPE_RGBE as g,TEXTURETYPE_RGBP as u,PIXELFORMAT_RGBA8 as _,PIXELFORMAT_RGBA32F as d}from"../../platform/graphics/constants.js";import{Texture as x}from"../../platform/graphics/texture.js";import{TextureUtils as w}from"../../platform/graphics/texture-utils.js";import{BasisParser as v}from"../parsers/texture/basis.js";import{ImgParser as P}from"../parsers/texture/img.js";import{KtxParser as y}from"../parsers/texture/ktx.js";import{Ktx2Parser as O}from"../parsers/texture/ktx2.js";import{DdsParser as b}from"../parsers/texture/dds.js";import{HdrParser as j}from"../parsers/texture/hdr.js";import{ResourceHandler as M}from"./handler.js";const R={repeat:i,clamp:a,mirror:o},G={nearest:n,linear:p,nearest_mip_nearest:l,linear_mip_nearest:m,nearest_mip_linear:h,linear_mip_linear:f},T={default:c,rgbm:r,rgbe:g,rgbp:u,swizzleGGGR:t};class k extends M{constructor(e){super(e,"texture");const r=e.assets,t=e.graphicsDevice;this._device=t,this._assets=r,this.imgParser=new P(r,t),this.parsers={dds:new b(r),ktx:new y(r),ktx2:new O(r,t),basis:new v(r,t),hdr:new j(r)}}set crossOrigin(e){this.imgParser.crossOrigin=e}get crossOrigin(){return this.imgParser.crossOrigin}set maxRetries(e){this.imgParser.maxRetries=e;for(const r in this.parsers)this.parsers.hasOwnProperty(r)&&(this.parsers[r].maxRetries=e)}get maxRetries(){return this.imgParser.maxRetries}_getUrlWithoutParams(e){return e.indexOf("?")>=0?e.split("?")[0]:e}_getParser(r){const t=e.getExtension(this._getUrlWithoutParams(r)).toLowerCase().replace(".","");return this.parsers[t]||this.imgParser}_getTextureOptions(e){const s={};if(e){var i;(null==(i=e.name)?void 0:i.length)>0&&(s.name=e.name);const a=e.data;a.hasOwnProperty("minfilter")&&(s.minFilter=G[a.minfilter]),a.hasOwnProperty("magfilter")&&(s.magFilter=G[a.magfilter]),a.hasOwnProperty("addressu")&&(s.addressU=R[a.addressu]),a.hasOwnProperty("addressv")&&(s.addressV=R[a.addressv]),a.hasOwnProperty("mipmaps")&&(s.mipmaps=a.mipmaps),a.hasOwnProperty("anisotropy")&&(s.anisotropy=a.anisotropy),a.hasOwnProperty("flipY")&&(s.flipY=!!a.flipY),a.hasOwnProperty("type")?s.type=T[a.type]:a.hasOwnProperty("rgbm")&&a.rgbm?s.type=r:e.file&&8&e.file.opt&&(s.type=t)}return s}load(e,r,t){"string"==typeof e&&(e={load:e,original:e}),this._getParser(e.original).load(e,r,t)}open(e,r,t){if(!e)return;const i=this._getTextureOptions(t);let a=this._getParser(e).open(e,r,this._device,i);return null===a?a=new x(this._device,{width:4,height:4,format:s}):(!function(e){const r=w.calcMipLevelsCount(e._width,e._height);if(e._format!==_&&e._format!==d||e._volume||e._compressed||1===e._levels.length||e._levels.length===r||(t=e._cubemap?e._levels[0][0]:e._levels[0])instanceof HTMLCanvasElement||t instanceof HTMLImageElement||t instanceof HTMLVideoElement)return;var t;const s=function(e,r,t){const s=Math.max(1,e>>1),i=Math.max(1,r>>1),a=new t.constructor(s*i*4),o=Math.floor(e/s),n=Math.floor(r/i),p=o*n;for(let r=0;r<i;++r)for(let i=0;i<s;++i)for(let l=0;l<4;++l){let m=0;for(let s=0;s<n;++s)for(let a=0;a<o;++a)m+=t[4*(i*o+a+(r*n+s)*e)+l];a[4*(i+r*s)+l]=m/p}return a};for(let t=e._levels.length;t<r;++t){const r=Math.max(1,e._width>>t-1),i=Math.max(1,e._height>>t-1);if(e._cubemap){const a=[];for(let o=0;o<6;++o)a.push(s(r,i,e._levels[t-1][o]));e._levels.push(a)}else e._levels.push(s(r,i,e._levels[t-1]))}e._levelsUpdated=e._cubemap?[[!0,!0,!0,!0,!0,!0]]:[!0]}(a),r.unswizzledGGGR&&(t.file.variants.basis.opt&=-9)),a}patch(e,r){const t=e.resource;if(!t)return;const s=this._getTextureOptions(e);for(const e of Object.keys(s))t[e]=s[e]}}export{k as TextureHandler};