import{e as y,ao as u,M as g,bj as f,ay as x,aG as b,aH as A}from"./index-T8LZyOXI.js";import{_ as w}from"./environmentTextureTools-BTwhBmLN.js";import{CubeTexture as T}from"./cubeTexture-PLqn8O7Q.js";import{GLTFLoader as S,ArrayItem as h}from"./glTFLoader-f5CXkgzs.js";import"./index-BkylzejB.js";import"./dumpTools-D0vSQubr.js";import"./abstractEngine.cubeTexture-DfaI3mgQ.js";import"./bone-DGunMt7J.js";import"./skeleton-00tPGJ0X.js";import"./rawTexture-Duv-4ySK.js";import"./assetContainer-DNTqZfyb.js";import"./objectModelMapping-D1pI71RW.js";class m extends T{constructor(t,e,a,n=5,r=0,s=!1,o=!1,l=3,i=null){super("",t),this._texture=t.getEngine().createRawCubeTexture(e,a,n,r,s,o,l,i)}update(t,e,a,n,r=null){this._texture.getEngine().updateRawCubeTexture(this._texture,t,e,a,n,r)}updateRGBDAsync(t,e=null,a=.8,n=0){return w(this._texture,t,e,a,n).then(()=>{})}clone(){return y.Clone(()=>{const t=this.getScene(),e=this._texture,a=new m(t,e._bufferViewArray,e.width,e.format,e.type,e.generateMipMaps,e.invertY,e.samplingMode,e._compression);return e.source===13&&a.updateRGBDAsync(e._bufferViewArrayArray,e._sphericalPolynomial,e._lodGenerationScale,e._lodGenerationOffset),a},this)}}const c="EXT_lights_image_based";class G{constructor(t){this.name=c,this._loader=t,this.enabled=this._loader.isExtensionUsed(c)}dispose(){this._loader=null,delete this._lights}onLoading(){const t=this._loader.gltf.extensions;if(t&&t[this.name]){const e=t[this.name];this._lights=e.lights}}loadSceneAsync(t,e){return S.LoadExtensionAsync(t,e,this.name,async(a,n)=>{this._loader._allMaterialsDirtyRequired=!0;const r=new Array;r.push(this._loader.loadSceneAsync(t,e)),this._loader.logOpen(`${a}`);const s=h.Get(`${a}/light`,this._lights,n.light);return r.push(this._loadLightAsync(`/extensions/${this.name}/lights/${n.light}`,s).then(o=>{this._loader.babylonScene.environmentTexture=o})),this._loader.logClose(),await Promise.all(r).then(()=>{})})}_loadLightAsync(t,e){if(!e._loaded){const a=new Array;this._loader.logOpen(`${t}`);const n=new Array(e.specularImages.length);for(let r=0;r<e.specularImages.length;r++){const s=e.specularImages[r];n[r]=new Array(s.length);for(let o=0;o<s.length;o++){const l=`${t}/specularImages/${r}/${o}`;this._loader.logOpen(`${l}`);const i=s[o],p=h.Get(l,this._loader.gltf.images,i);a.push(this._loader.loadImageAsync(`/images/${i}`,p).then(_=>{n[r][o]=_})),this._loader.logClose()}}this._loader.logClose(),e._loaded=Promise.all(a).then(async()=>{const r=new m(this._loader.babylonScene,null,e.specularImageSize);if(r.name=e.name||"environment",e._babylonTexture=r,e.intensity!=null&&(r.level=e.intensity),e.rotation){let i=u.FromArray(e.rotation);this._loader.babylonScene.useRightHandedSystem||(i=u.Inverse(i)),g.FromQuaternionToRef(i,r.getReflectionTextureMatrix())}if(!e.irradianceCoefficients)throw new Error(`${t}: Irradiance coefficients are missing`);const s=f.FromArray(e.irradianceCoefficients);s.scaleInPlace(e.intensity),s.convertIrradianceToLambertianRadiance();const o=x.FromHarmonics(s),l=(n.length-1)/Math.log2(e.specularImageSize);return await r.updateRGBDAsync(n,o,l)})}return e._loaded.then(()=>e._babylonTexture)}}b(c);A(c,!0,d=>new G(d));export{G as EXT_lights_image_based};
