UNPKG

@babylonjs/viewer

Version:

The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.

3 lines (2 loc) 10.3 kB
import{j as e,T as t,i as r,ad as n,aJ as s,k as a,ai as o,aj as c,ak as u,F as i}from"./index-DFZFpUG-.esm.min.js";let l=null;function m(e){return function(){if(l)return l;const e=new Map;function t(t,r,n,s,a,o){e.set(t,{gpuFormat:r,feature:n,blockW:s,blockH:a,blockBytes:o})}l=e;const r="texture-compression-bc",n="texture-compression-etc2",s="texture-compression-astc";t(33776,"bc1-rgba-unorm",r,4,4,8),t(33777,"bc1-rgba-unorm",r,4,4,8),t(33778,"bc2-rgba-unorm",r,4,4,16),t(33779,"bc3-rgba-unorm",r,4,4,16),t(36283,"bc4-r-unorm",r,4,4,8),t(36284,"bc4-r-snorm",r,4,4,8),t(36285,"bc5-rg-unorm",r,4,4,16),t(36286,"bc5-rg-snorm",r,4,4,16),t(36495,"bc6h-rgb-ufloat",r,4,4,16),t(36494,"bc6h-rgb-float",r,4,4,16),t(36492,"bc7-rgba-unorm",r,4,4,16),t(36493,"bc7-rgba-unorm-srgb",r,4,4,16),t(37488,"eac-r11unorm",n,4,4,8),t(37489,"eac-r11snorm",n,4,4,8),t(37490,"eac-rg11unorm",n,4,4,16),t(37491,"eac-rg11snorm",n,4,4,16),t(37492,"etc2-rgb8unorm",n,4,4,8),t(37493,"etc2-rgb8unorm-srgb",n,4,4,8),t(37494,"etc2-rgb8a1unorm",n,4,4,8),t(37495,"etc2-rgb8a1unorm-srgb",n,4,4,8),t(37496,"etc2-rgba8unorm",n,4,4,16),t(37497,"etc2-rgba8unorm-srgb",n,4,4,16);const a=[[4,4],[5,4],[5,5],[6,5],[6,6],[8,5],[8,6],[8,8],[10,5],[10,6],[10,8],[10,10],[12,10],[12,12]];for(let e=0;e<a.length;e++){const[r,n]=a[e],o=`${r}x${n}`;t(37808+e,`astc-${o}-unorm`,s,r,n,16),t(37840+e,`astc-${o}-unorm-srgb`,s,r,n,16)}return e}().get(e)}let b=null;const g={astc:!1,bptc:!1,s3tc:!1,pvrtc:!1,etc2:!1,etc1:!1};function f(){return b||(b=new Promise((e,t)=>{const r=globalThis,n=()=>{const n=r.KTX2DECODER;n?(n.MSCTranscoder.UseFromWorkerThread=!1,n.WASMMemoryManager.LoadBinariesFromCurrentThread=!0,e(new n.KTX2Decoder)):t(new Error("KTX2: decoder global KTX2DECODER not found after script load"))};if(r.KTX2DECODER)return void n();const s=document.createElement("script");s.src="https://cdn.babylonjs.com/babylon.ktx2Decoder.js",s.async=!0,s.onload=n,s.onerror=()=>t(new Error(`KTX2: failed to load ${s.src}`)),document.head.appendChild(s)}),b.catch(()=>{b=null}),b)}function x(e){switch(e){case"rgba8unorm":return"rgba8unorm-srgb";case"bc1-rgba-unorm":return"bc1-rgba-unorm-srgb";case"bc2-rgba-unorm":return"bc2-rgba-unorm-srgb";case"bc3-rgba-unorm":return"bc3-rgba-unorm-srgb";case"bc7-rgba-unorm":return"bc7-rgba-unorm-srgb";case"etc2-rgb8unorm":return"etc2-rgb8unorm-srgb";case"etc2-rgb8a1unorm":return"etc2-rgb8a1unorm-srgb";case"etc2-rgba8unorm":return"etc2-rgba8unorm-srgb";case"astc-4x4-unorm":return"astc-4x4-unorm-srgb";case"astc-5x4-unorm":return"astc-5x4-unorm-srgb";case"astc-5x5-unorm":return"astc-5x5-unorm-srgb";case"astc-6x5-unorm":return"astc-6x5-unorm-srgb";case"astc-6x6-unorm":return"astc-6x6-unorm-srgb";case"astc-8x5-unorm":return"astc-8x5-unorm-srgb";case"astc-8x6-unorm":return"astc-8x6-unorm-srgb";case"astc-8x8-unorm":return"astc-8x8-unorm-srgb";case"astc-10x5-unorm":return"astc-10x5-unorm-srgb";case"astc-10x6-unorm":return"astc-10x6-unorm-srgb";case"astc-10x8-unorm":return"astc-10x8-unorm-srgb";case"astc-10x10-unorm":return"astc-10x10-unorm-srgb";case"astc-12x10-unorm":return"astc-12x10-unorm-srgb";case"astc-12x12-unorm":return"astc-12x12-unorm-srgb";default:return e}}function d(e){e.errors&&t(404,e.errors),e.mipmaps.length||t(405);for(let r=0;r<e.mipmaps.length;r++)e.mipmaps[r]?.data||t(406,r);return e.mipmaps}function h(e,t){return a(e,{addressModeU:"repeat",addressModeV:"repeat",minFilter:"linear",magFilter:"linear",mipmapFilter:t>1?"linear":"nearest",maxAnisotropy:t>1?4:1})}async function w(s,a,o){const c=await f(),u=await c.decode(new e(a),function(e){const t=e._device.features,r=t.has("texture-compression-bc"),n=t.has("texture-compression-etc2");return{astc:t.has("texture-compression-astc"),bptc:r,s3tc:r,pvrtc:!1,etc2:n,etc1:n}}(s)),i=d(u),l=m(u.transcodedFormat);if(l)return function(e,s,a,o){e._device.features.has(a.feature)||t(407,a.feature);const c=s[0].width,u=s[0].height,i=e._device.createTexture({size:{width:c,height:u},format:o?x(a.gpuFormat):a.gpuFormat,mipLevelCount:s.length,usage:r.TEXTURE_BINDING|r.COPY_DST});for(let t=0;t<s.length;t++){const r=s[t],n=Math.ceil(r.width/a.blockW),o=n*a.blockBytes,c=n*a.blockW,u=Math.ceil(r.height/a.blockH)*a.blockH;e._device.queue.writeTexture({texture:i,mipLevel:t},r.data,{bytesPerRow:o},{width:c,height:u})}const l={texture:i,view:i.createView(),sampler:h(e,s.length),width:c,height:u,invertY:!0};return n(l),l}(s,i,l,o);const b=function(e){switch(e){case 32856:return{format:"rgba8unorm",bytesPerPixel:4};case 33321:return{format:"r8unorm",bytesPerPixel:1};case 33323:return{format:"rg8unorm",bytesPerPixel:2};default:return null}}(u.transcodedFormat);if(b)return function(e,s,a,o){const c=s[0].width,u=s[0].height,i=e._device.createTexture({size:{width:c,height:u},format:o?x(a.format):a.format,mipLevelCount:s.length,usage:r.TEXTURE_BINDING|r.COPY_DST});for(let r=0;r<s.length;r++){const n=s[r],o=n.width*n.height*a.bytesPerPixel;n.data.length!==o&&t(408,r,n.data.length,o),e._device.queue.writeTexture({texture:i,mipLevel:r},n.data,{bytesPerRow:n.width*a.bytesPerPixel},{width:n.width,height:n.height})}const l={texture:i,view:i.createView(),sampler:h(e,s.length),width:c,height:u,invertY:!0};return n(l),l}(s,i,b,o);t(409,u.transcodedFormat.toString(16))}async function p(r){const n=await f(),a=d(await n.decode(new e(r),g,{forceRGBA:!0}))[0];a.data.length!==a.width*a.height*4&&t(411);const o=new s(a.data.length);return o.set(a.data),createImageBitmap(new ImageData(o,a.width,a.height))}const T="KHR_texture_basisu",y={5120:1,5121:1,5122:2,5123:2,5125:4,5126:4},v={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16},C="__basisuMaterialData";function _(e){const t=e?.extensions?.KHR_texture_basisu?.source;return"number"==typeof t?t:null}function M(e){const t=e?.index;return"number"==typeof t?t:null}function R(e,t,r,n){return!!function(e,t){const r=M(t);return null!==r&&null!==_(e.textures?.[r])}(e,t?.[r])&&(n[r]=t[r],delete t[r],!0)}async function P(r,n){const s=r.json.images?.[n];if(s||t(78,T,n),void 0!==s.bufferView){const n=r.json.bufferViews?.[s.bufferView];n||t(79,T,s.bufferView);const a=r.binChunk.byteOffset+(n.byteOffset??0),o=new e(n.byteLength);return o.set(new e(r.binChunk.buffer,a,n.byteLength)),o.buffer}if(s.uri){const e=new URL(s.uri,r.baseUrl+"x").href,n=await fetch(e);return n.ok||t(80,T,n.status,n.statusText),n.arrayBuffer()}t(81,T)}async function k(e,t){const r=M(t);if(null===r)return null;const n=_(e.json.textures?.[r]);if(null===n)return null;e.bitmaps??=new Map;let s=e.bitmaps.get(r);return s||(s=P(e,n).then(p),e.bitmaps.set(r,s)),s}async function D(e,t,r,n){const s=M(r);if(null===s)return;e.textures??=new Map;const a=`${s}:${n?1:0}`;let o=e.textures.get(a);if(!o){const r=_(e.json.textures?.[s]);if(null===r)return;o=await w(t._engine,await P(e,r),n),e.textures.set(a,o)}return o}async function F(e,t){const r=e.metallicRoughnessTexture,n=e.occlusionTexture,s=M(r),a=M(n);if(null===s&&null===a)return;if(null===s||null===a||s===a)return D(e,t,r??n,!1);e.textures??=new Map;const o=`orm:${s}:${a}`;let c=e.textures.get(o);if(!c){const[s,a]=await Promise.all([k(e,r),k(e,n)]);if(!s||!a)return;c=t._uploadImage(await async function(e,t){const r=e.width,n=e.height,s=new OffscreenCanvas(r,n),a=s.getContext("2d");a.drawImage(e,0,0,r,n);const o=a.getImageData(0,0,r,n),c=new OffscreenCanvas(r,n).getContext("2d");c.drawImage(t,0,0,r,n);const u=c.getImageData(0,0,r,n);for(let e=0;e<o.data.length;e+=4)o.data[e]=u.data[e];return a.putImageData(o,0,0),createImageBitmap(s)}(s,a),!1),e.textures.set(o,c)}return c}function O(e,r,n,s){switch(n){case 5126:return e.getFloat32(r,!0);case 5125:{const t=e.getUint32(r,!0);return s?t/4294967295:t}case 5123:{const t=e.getUint16(r,!0);return s?t/65535:t}case 5122:{const t=e.getInt16(r,!0);return s?Math.max(t/32767,-1):t}case 5121:{const t=e.getUint8(r);return s?t/255:t}case 5120:{const t=e.getInt8(r);return s?Math.max(t/127,-1):t}default:t(82,T,n)}}function I(e,r,n){const s=e.accessors[n],a=e.bufferViews[s.bufferView],o=s.componentType,c=y[o];c||t(83,T,n,o);const l=v[s.type]??1,m=l*c,b=a.byteStride??m;b<m&&t(84,T,b,n);const g=!0===s.normalized,f=r.byteOffset+(a.byteOffset??0)+(s.byteOffset??0),x=new u(r.buffer),d=new i(s.count*l);for(let e=0,t=0;e<s.count;e++){const r=f+e*b;for(let e=0;e<l;e++,t++)d[t]=O(x,r+e*c,o,g)}return d}const E={id:T,async preMesh(e,t,r){const n=e;!function(e,t,r){for(const n of e.materials??[]){const s={json:e,binChunk:t,baseUrl:r},a=n.pbrMetallicRoughness??{};let o=R(e,a,"baseColorTexture",s);o=R(e,a,"metallicRoughnessTexture",s)||o,o=R(e,n,"normalTexture",s)||o,o=R(e,n,"occlusionTexture",s)||o,o=R(e,n,"emissiveTexture",s)||o;const c=n.extensions?.KHR_materials_specular;c&&(o=R(e,c,"specularTexture",s)||o,o=R(e,c,"specularColorTexture",s)||o),o&&Object.defineProperty(n,C,{value:s})}}(n,t,r);const s=new Map;for(const e of n.meshes??[])for(const r of e.primitives??[]){const e=r.attributes??{};if(!Object.keys(e).some(t=>void 0!==n.bufferViews?.[n.accessors?.[e[t]]?.bufferView]?.byteStride))continue;const a=new Map;for(const r of Object.keys(e)){const s=e[r],o=n.accessors[s];void 0!==n.bufferViews?.[o.bufferView]?.byteStride&&a.set(r,I(n,t,s))}const u=n.accessors[e.POSITION],i=void 0===r.indices?new o(0):new o(c(n,t,r.indices)._data);s.set(r,{_attributes:a,_indices:i,_vertexCount:u.count,_indexCount:i.length})}return s},async applyMaterial(e,t){const r=e._rawMatDef?.[C];if(!r)return null;const[n,s,a,o,c,u]=await Promise.all([D(r,t,r.baseColorTexture,!0),F(r,t),D(r,t,r.normalTexture,!1),D(r,t,r.emissiveTexture,!0),D(r,t,r.specularTexture,!1),D(r,t,r.specularColorTexture,!1)]),i={...n?{baseColorTexture:n}:void 0,...s?{ormTexture:s,...r.metallicRoughnessTexture?{metallicFactor:e._metallicFactor,roughnessFactor:e._roughnessFactor}:void 0,...r.occlusionTexture?{occlusionStrength:1,occlusionTexCoord:r.occlusionTexture.texCoord??0}:void 0}:void 0,...a?{normalTexture:a,normalTextureScale:r.normalTexture?.scale??1}:void 0,...o?{emissiveTexture:o}:void 0,...c?{metallicReflectanceTexture:c,useOnlyMetallicFromMetallicReflectanceTexture:!0}:void 0,...u?{reflectanceTexture:u}:void 0};return i.baseColorTexture||i.ormTexture||i.normalTexture||i.emissiveTexture||i.metallicReflectanceTexture||i.reflectanceTexture?i:null}};export{E as default}; //# sourceMappingURL=gltf-ext-basisu-CKmouyMn.esm.min.js.map