import{GLTFLoader as S,ArrayItem as L}from"./glTFLoader-Bx351I09.js";import{D as k,aN as C,aO as I}from"./index-6jOCfdig.js";import"./index-Ds2meNS5.js";import"./svelte/svelte_svelte.js";import"./svelte/svelte_animate.js";import"./svelte/svelte_attachments.js";import"./svelte/svelte_easing.js";import"./svelte/svelte_internal_client.js";import"./svelte/svelte_internal_flags_async.js";import"./svelte/svelte_internal_flags_legacy.js";import"./svelte/svelte_internal_flags_tracing.js";import"./svelte/svelte_internal_server.js";import"./svelte/svelte_legacy.js";import"./svelte/svelte_motion.js";import"./svelte/svelte_reactivity.js";import"./svelte/svelte_reactivity_window.js";import"./svelte/svelte_server.js";import"./svelte/svelte_store.js";import"./svelte/svelte_transition.js";import"./svelte/svelte_events.js";import"./bone-DdTyU8i_.js";import"./skeleton-C0c5yg7x.js";import"./rawTexture-CpjOe2U7.js";import"./assetContainer-Drc86vic.js";import"./objectModelMapping-DT0r674A.js";const n="KHR_materials_variants";class s{constructor(t){this.name=n,this._loader=t,this.enabled=this._loader.isExtensionUsed(n)&&!this._loader.parent.skipMaterials}dispose(){this._loader=null}static GetAvailableVariants(t){const e=this._GetExtensionMetadata(t);return e?Object.keys(e.variants):[]}getAvailableVariants(t){return s.GetAvailableVariants(t)}static SelectVariant(t,e){const r=this._GetExtensionMetadata(t);if(!r)throw new Error(`Cannot select variant on a glTF mesh that does not have the ${n} extension`);const m=c=>{const h=r.variants[c];if(h)for(const f of h)f.mesh.material=f.material};if(e instanceof Array)for(const c of e)m(c);else m(e);r.lastSelected=e}selectVariant(t,e){s.SelectVariant(t,e)}static Reset(t){const e=this._GetExtensionMetadata(t);if(!e)throw new Error(`Cannot reset on a glTF mesh that does not have the ${n} extension`);for(const r of e.original)r.mesh.material=r.material;e.lastSelected=null}reset(t){s.Reset(t)}static GetLastSelectedVariant(t){const e=this._GetExtensionMetadata(t);if(!e)throw new Error(`Cannot get the last selected variant on a glTF mesh that does not have the ${n} extension`);return e.lastSelected}getLastSelectedVariant(t){return s.GetLastSelectedVariant(t)}static _GetExtensionMetadata(t){return t?._internalMetadata?.gltf?.[n]||null}onLoading(){const t=this._loader.gltf.extensions;if(t&&t[this.name]){const e=t[this.name];this._variants=e.variants}}onReady(){const t=this._loader.rootBabylonMesh;if(t){const e=this._loader.parent.extensionOptions[n];e?.defaultVariant&&s.SelectVariant(t,e.defaultVariant),e?.onLoaded?.({get variants(){return s.GetAvailableVariants(t)},get selectedVariant(){const r=s.GetLastSelectedVariant(t);return r?Array.isArray(r)?r[0]:r:s.GetAvailableVariants(t)[0]},set selectedVariant(r){s.SelectVariant(t,r)}})}}_loadMeshPrimitiveAsync(t,e,r,m,c,h){return S.LoadExtensionAsync(t,c,this.name,async(f,V)=>{const x=new Array;return x.push(this._loader._loadMeshPrimitiveAsync(t,e,r,m,c,o=>{if(h(o),o instanceof k){const $=S._GetDrawMode(t,c.mode),d=this._loader.rootBabylonMesh,E=d?d._internalMetadata=d._internalMetadata||{}:{},w=E.gltf=E.gltf||{},p=w[n]=w[n]||{lastSelected:null,original:[],variants:{}};p.original.push({mesh:o,material:o.material});for(let g=0;g<V.mappings.length;++g){const u=V.mappings[g],F=L.Get(`${f}/mappings/${g}/material`,this._loader.gltf.materials,u.material);x.push(this._loader._loadMaterialAsync(`#/materials/${u.material}`,F,o,$,O=>{for(let v=0;v<u.variants.length;++v){const y=u.variants[v],M=L.Get(`/extensions/${n}/variants/${y}`,this._variants,y);p.variants[M.name]=p.variants[M.name]||[],p.variants[M.name].push({mesh:o,material:O}),o.onClonedObservable.add(T=>{const G=T;let l=null,i=G;do{if(i=i.parent,!i)return;l=s._GetExtensionMetadata(i)}while(l===null);if(d&&l===s._GetExtensionMetadata(d)){i._internalMetadata={};for(const a in d._internalMetadata)i._internalMetadata[a]=d._internalMetadata[a];i._internalMetadata.gltf=[];for(const a in d._internalMetadata.gltf)i._internalMetadata.gltf[a]=d._internalMetadata.gltf[a];i._internalMetadata.gltf[n]={lastSelected:null,original:[],variants:{}};for(const a of l.original)i._internalMetadata.gltf[n].original.push({mesh:a.mesh,material:a.material});for(const a in l.variants)if(Object.prototype.hasOwnProperty.call(l.variants,a)){i._internalMetadata.gltf[n].variants[a]=[];for(const A of l.variants[a])i._internalMetadata.gltf[n].variants[a].push({mesh:A.mesh,material:A.material})}l=i._internalMetadata.gltf[n]}for(const a of l.original)a.mesh===o&&(a.mesh=G);for(const a of l.variants[M.name])a.mesh===o&&(a.mesh=G)})}}))}}})),await Promise.all(x).then(([o])=>o)})}}C(n);I(n,!0,_=>new s(_));export{s as KHR_materials_variants};
