UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 7.74 kB
import{math as t}from"../../core/math/math.js";import{RenderTarget as e}from"./render-target.js";import{TextureUtils as s}from"./texture-utils.js";import{TEXTURELOCK_NONE as i,PIXELFORMAT_RGBA8 as h,isCompressedPixelFormat as r,isIntegerPixelFormat as l,FILTER_NEAREST as a,FILTER_LINEAR_MIPMAP_LINEAR as o,FILTER_LINEAR as p,ADDRESS_REPEAT as n,FUNC_LESS as d,TEXTURETYPE_DEFAULT as m,TEXTURETYPE_RGBM as _,TEXTURETYPE_SWIZZLEGGGR as u,TEXTUREPROJECTION_NONE as c,TEXTUREPROJECTION_CUBE as g,PIXELFORMAT_RGB16F as v,PIXELFORMAT_RGB32F as f,PIXELFORMAT_RGBA16F as y,PIXELFORMAT_RGBA32F as w,TEXTURETYPE_RGBP as F,TEXTURETYPE_RGBE as b,TEXTURELOCK_WRITE as M,getPixelFormatArrayType as U}from"./constants.js";let x=0;class k{constructor(t,e={}){var s,v,f,y,w,F,b,M,U,k,C,V,z,A,j,O,S,L,R,T,G,W;(this.name=void 0,this._gpuSize=0,this.id=x++,this._invalid=!1,this._lockedLevel=-1,this._lockedMode=i,this.renderVersionDirty=0,this._storage=!1,this.device=t,this.name=null!=(s=e.name)?s:"",this._width=Math.floor(null!=(v=e.width)?v:4),this._height=Math.floor(null!=(f=e.height)?f:4),this._format=null!=(y=e.format)?y:h,this._compressed=r(this._format),this._integerFormat=l(this._format),this._integerFormat&&(e.mipmaps=!1,e.minFilter=a,e.magFilter=a),t.supportsVolumeTextures)?(this._volume=null!=(T=e.volume)&&T,this._depth=Math.floor(null!=(G=e.depth)?G:1),this._arrayLength=Math.floor(null!=(W=e.arrayLength)?W:0)):(this._volume=!1,this._depth=1,this._arrayLength=0);this._storage=null!=(w=e.storage)&&w,this._cubemap=null!=(F=e.cubemap)&&F,this.fixCubemapSeams=null!=(b=e.fixCubemapSeams)&&b,this._flipY=null!=(M=e.flipY)&&M,this._premultiplyAlpha=null!=(U=e.premultiplyAlpha)&&U,this._mipmaps=null==(k=null!=(C=e.mipmaps)?C:e.autoMipmap)||k,this._minFilter=null!=(V=e.minFilter)?V:o,this._magFilter=null!=(z=e.magFilter)?z:p,this._anisotropy=null!=(A=e.anisotropy)?A:1,this._addressU=null!=(j=e.addressU)?j:n,this._addressV=null!=(O=e.addressV)?O:n,this._addressW=null!=(S=e.addressW)?S:n,this._compareOnRead=null!=(L=e.compareOnRead)&&L,this._compareFunc=null!=(R=e.compareFunc)?R:d,this.type=m,e.hasOwnProperty("type")?this.type=e.type:e.hasOwnProperty("rgbm")?this.type=e.rgbm?_:m:e.hasOwnProperty("swizzleGGGR")&&(this.type=e.swizzleGGGR?u:m),this.projection=c,this._cubemap?this.projection=g:e.projection&&e.projection!==g&&(this.projection=e.projection),this.impl=t.createTextureImpl(this),this.dirtyAll(),this._levels=e.levels,this._levels?this.upload():this._levels=this._cubemap?[[null,null,null,null,null,null]]:[null],t.textures.push(this)}destroy(){const t=this.device;if(t){const e=t.textures.indexOf(this);-1!==e&&t.textures.splice(e,1),t.scope.removeValue(this),this.impl.destroy(t),this.adjustVramSizeTracking(t._vram,-this._gpuSize),this._levels=null,this.device=null}}resize(t,e,s=1){const i=this.device;this.adjustVramSizeTracking(i._vram,-this._gpuSize),this.impl.destroy(i),this._width=Math.floor(t),this._height=Math.floor(e),this._depth=Math.floor(s),this.impl=i.createTextureImpl(this),this.dirtyAll()}loseContext(){this.impl.loseContext(),this.dirtyAll()}adjustVramSizeTracking(t,e){t.tex+=e}propertyChanged(t){this.impl.propertyChanged(t),this.renderVersionDirty=this.device.renderVersion}get requiredMipLevels(){return this.mipmaps?s.calcMipLevelsCount(this.width,this.height):1}get lockedMode(){return this._lockedMode}set minFilter(t){this._minFilter!==t&&(l(this._format)||(this._minFilter=t,this.propertyChanged(1)))}get minFilter(){return this._minFilter}set magFilter(t){this._magFilter!==t&&(l(this._format)||(this._magFilter=t,this.propertyChanged(2)))}get magFilter(){return this._magFilter}set addressU(t){this._addressU!==t&&(this._addressU=t,this.propertyChanged(4))}get addressU(){return this._addressU}set addressV(t){this._addressV!==t&&(this._addressV=t,this.propertyChanged(8))}get addressV(){return this._addressV}set addressW(t){this.device.supportsVolumeTextures&&this._volume&&t!==this._addressW&&(this._addressW=t,this.propertyChanged(16))}get addressW(){return this._addressW}set compareOnRead(t){this._compareOnRead!==t&&(this._compareOnRead=t,this.propertyChanged(32))}get compareOnRead(){return this._compareOnRead}set compareFunc(t){this._compareFunc!==t&&(this._compareFunc=t,this.propertyChanged(64))}get compareFunc(){return this._compareFunc}set anisotropy(t){this._anisotropy!==t&&(this._anisotropy=t,this.propertyChanged(128))}get anisotropy(){return this._anisotropy}set mipmaps(t){this._mipmaps!==t&&(this.device.isWebGPU||l(this._format)||(this._mipmaps=t),t&&(this._needsMipmapsUpload=!0))}get mipmaps(){return this._mipmaps}get storage(){return this._storage}get width(){return this._width}get height(){return this._height}get depth(){return this._depth}get format(){return this._format}get cubemap(){return this._cubemap}get gpuSize(){const t=this.pot&&this._mipmaps&&!(this._compressed&&1===this._levels.length);return s.calcGpuSize(this._width,this._height,this._depth,this._format,t,this._cubemap)}get array(){return this._arrayLength>0}get arrayLength(){return this._arrayLength}get volume(){return this._volume}set flipY(t){this._flipY!==t&&(this._flipY=t,this._needsUpload=!0)}get flipY(){return this._flipY}set premultiplyAlpha(t){this._premultiplyAlpha!==t&&(this._premultiplyAlpha=t,this._needsUpload=!0)}get premultiplyAlpha(){return this._premultiplyAlpha}get pot(){return t.powerOfTwo(this._width)&&t.powerOfTwo(this._height)}get encoding(){switch(this.type){case _:return"rgbm";case b:return"rgbe";case F:return"rgbp";default:return this.format===v||this.format===f||this.format===y||this.format===w||l(this.format)?"linear":"srgb"}}dirtyAll(){this._levelsUpdated=this._cubemap?[[!0,!0,!0,!0,!0,!0]]:[!0],this._needsUpload=!0,this._needsMipmapsUpload=this._mipmaps,this._mipmapsUploaded=!1,this.propertyChanged(255)}lock(t={}){null!=t.level||(t.level=0),null!=t.face||(t.face=0),null!=t.mode||(t.mode=M),this._lockedMode=t.mode,this._lockedLevel=t.level;const e=this.cubemap?this._levels[t.face]:this._levels;if(null===e[t.level]){const i=Math.max(1,this._width>>t.level),h=Math.max(1,this._height>>t.level),r=Math.max(1,this._depth>>t.level),l=new ArrayBuffer(s.calcLevelGpuSize(i,h,r,this._format));e[t.level]=new(U(this._format))(l)}return e[t.level]}setSource(t,e=0){let s,i,h=!1;if(this._cubemap){if(t[0]){s=t[0].width||0,i=t[0].height||0;for(let e=0;e<6;e++){const r=t[e];if(!r||r.width!==s||r.height!==i||!this.device._isBrowserInterface(r)){h=!0;break}}}else h=!0;if(!h)for(let s=0;s<6;s++)this._levels[e][s]!==t[s]&&(this._levelsUpdated[e][s]=!0)}else this.device._isBrowserInterface(t)||(h=!0),h||(t!==this._levels[e]&&(this._levelsUpdated[e]=!0),s=t.width,i=t.height);if(h)if(this._width=4,this._height=4,this._cubemap)for(let t=0;t<6;t++)this._levels[e][t]=null,this._levelsUpdated[e][t]=!0;else this._levels[e]=null,this._levelsUpdated[e]=!0;else 0===e&&(this._width=s,this._height=i),this._levels[e]=t;this._invalid===h&&h||(this._invalid=h,this.upload())}getSource(t=0){return this._levels[t]}unlock(){this._lockedMode,this._lockedMode===M&&this.upload(),this._lockedLevel=-1,this._lockedMode=i}upload(){var t,e;this._needsUpload=!0,this._needsMipmapsUpload=this._mipmaps,null==(t=(e=this.impl).uploadImmediate)||t.call(e,this.device,this)}async downloadAsync(){const t=[];for(let h=0;h<(this.cubemap?6:1);h++){var s,i;const r=new e({colorBuffer:this,depth:!1,face:h});this.device.setRenderTarget(r),this.device.initRenderTarget(r);const l=this.cubemap?this._levels[h]:this._levels;let a=l[0];l[0]&&this.device._isBrowserInterface(l[0])&&(l[0]=null),a=this.lock({face:h});const o=null==(s=(i=this.device).readPixelsAsync)?void 0:s.call(i,0,0,this.width,this.height,a).then((()=>r.destroy()));t.push(o)}await Promise.all(t)}}export{k as Texture};