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.5 kB
import{T as e,p as t,o as r,ak as n,aZ as s,q as o,ap as a,aq as c,ar as u,F as i}from"./index-B8IgtevW.esm.min.js";import{r as l}from"./gltf-uri-BbzbM-yC.esm.min.js";let m=null;function b(e){return function(){if(m)return m;const e=new Map;function t(t,r,n,s,o,a){e.set(t,{gpuFormat:r,feature:n,blockW:s,blockH:o,blockBytes:a})}m=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 o=[[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<o.length;e++){const[r,n]=o[e],a=`${r}x${n}`;t(37808+e,`astc-${a}-unorm`,s,r,n,16),t(37840+e,`astc-${a}-unorm-srgb`,s,r,n,16)}return e}().get(e)}let g=null;const f={astc:!1,bptc:!1,s3tc:!1,pvrtc:!1,etc2:!1,etc1:!1};function x(){return g||(g=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)}),g.catch(()=>{g=null}),g)}function d(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 h(t){t.errors&&e(454,t.errors),t.mipmaps.length||e(455);for(let r=0;r<t.mipmaps.length;r++)t.mipmaps[r]?.data||e(456,r);return t.mipmaps}function p(e,t){return o(e,{addressModeU:"repeat",addressModeV:"repeat",minFilter:"linear",magFilter:"linear",mipmapFilter:t>1?"linear":"nearest",maxAnisotropy:t>1?4:1})}async function w(s,o,a){const c=await async function(e,r){const n=await x(),s=await n.decode(new t(r),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}}(e));return h(s),s}(s,o),u=c.mipmaps,i=b(c.transcodedFormat);if(i)return function(t,s,o,a){t._device.features.has(o.feature)||e(457,o.feature);const c=s[0].width,u=s[0].height,i=t._device.createTexture({size:{width:c,height:u},format:a?d(o.gpuFormat):o.gpuFormat,mipLevelCount:s.length,usage:r.TEXTURE_BINDING|r.COPY_DST});for(let e=0;e<s.length;e++){const r=s[e],n=Math.ceil(r.width/o.blockW),a=n*o.blockBytes,c=n*o.blockW,u=Math.ceil(r.height/o.blockH)*o.blockH;t._device.queue.writeTexture({texture:i,mipLevel:e},r.data,{bytesPerRow:a},{width:c,height:u})}const l={texture:i,view:i.createView(),sampler:p(t,s.length),width:c,height:u,invertY:!0};return n(l),l}(s,u,i,a);const l=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}}(c.transcodedFormat);if(l)return function(t,s,o,a){const c=s[0].width,u=s[0].height,i=t._device.createTexture({size:{width:c,height:u},format:a?d(o.format):o.format,mipLevelCount:s.length,usage:r.TEXTURE_BINDING|r.COPY_DST});for(let r=0;r<s.length;r++){const n=s[r],a=n.width*n.height*o.bytesPerPixel;n.data.length!==a&&e(458,r,n.data.length,a),t._device.queue.writeTexture({texture:i,mipLevel:r},n.data,{bytesPerRow:n.width*o.bytesPerPixel},{width:n.width,height:n.height})}const l={texture:i,view:i.createView(),sampler:p(t,s.length),width:c,height:u,invertY:!0};return n(l),l}(s,u,l,a);e(459,c.transcodedFormat.toString(16))}async function T(r){const n=await x(),o=h(await n.decode(new t(r),f,{forceRGBA:!0}))[0];o.data.length!==o.width*o.height*4&&e(461);const a=new s(o.data.length);return a.set(o.data),createImageBitmap(new ImageData(a,o.width,o.height))}const y="KHR_texture_basisu",v={5120:1,5121:1,5122:2,5123:2,5125:4,5126:4},C={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16},_="__basisuMaterialData";function M(e){const t=e?.extensions?.KHR_texture_basisu?.source;return"number"==typeof t?t:null}function P(e){const t=e?.index;return"number"==typeof t?t:null}function F(e,t,r,n){return!!function(e,t){const r=P(t);return null!==r&&null!==M(e.textures?.[r])}(e,t?.[r])&&(n[r]=t[r],delete t[r],!0)}async function D(r,n){const s=r.json.images?.[n];if(s||e(86,y,n),void 0!==s.bufferView){const n=r.json.bufferViews?.[s.bufferView];n||e(87,y,s.bufferView);const o=r.binChunk.byteOffset+(n.byteOffset??0),a=new t(n.byteLength);return a.set(new t(r.binChunk.buffer,o,n.byteLength)),a.buffer}if(s.uri){const t=l(s.uri,r.baseUrl),n=await fetch(t);return n.ok||e(88,y,n.status,n.statusText),n.arrayBuffer()}e(89,y)}async function O(e,t){const r=P(t);if(null===r)return null;const n=M(e.json.textures?.[r]);if(null===n)return null;e.bitmaps??=new Map;let s=e.bitmaps.get(r);return s||(s=D(e,n).then(T),e.bitmaps.set(r,s)),s}async function k(e,t,r,n){const s=P(r);if(null===s)return;e.textures??=new Map;const o=`${s}:${n?1:0}`;let a=e.textures.get(o);if(!a){const r=M(e.json.textures?.[s]);if(null===r)return;a=await w(t._engine,await D(e,r),n),e.textures.set(o,a)}return a}async function E(e,t){const r=e.metallicRoughnessTexture,n=e.occlusionTexture,s=P(r),o=P(n);if(null===s&&null===o)return;if(null===s||null===o||s===o)return k(e,t,r??n,!1);e.textures??=new Map;const a=`orm:${s}:${o}`;let c=e.textures.get(a);if(!c){const[s,o]=await Promise.all([O(e,r),O(e,n)]);if(!s||!o)return;c=t._uploadImage(await async function(e,t){const r=e.width,n=e.height,s=new OffscreenCanvas(r,n),o=s.getContext("2d");o.drawImage(e,0,0,r,n);const a=o.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<a.data.length;e+=4)a.data[e]=u.data[e];return o.putImageData(a,0,0),createImageBitmap(s)}(s,o),!1),e.textures.set(a,c)}return c}function I(t,r,n,s){switch(n){case 5126:return t.getFloat32(r,!0);case 5125:{const e=t.getUint32(r,!0);return s?e/4294967295:e}case 5123:{const e=t.getUint16(r,!0);return s?e/65535:e}case 5122:{const e=t.getInt16(r,!0);return s?Math.max(e/32767,-1):e}case 5121:{const e=t.getUint8(r);return s?e/255:e}case 5120:{const e=t.getInt8(r);return s?Math.max(e/127,-1):e}default:e(90,y,n)}}function R(t,r,n){const s=t.accessors[n],o=t.bufferViews[s.bufferView],a=s.componentType,c=v[a];c||e(91,y,n,a);const l=C[s.type]??1,m=l*c,b=o.byteStride??m;b<m&&e(92,y,b,n);const g=!0===s.normalized,f=r.byteOffset+(o.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]=I(x,r+e*c,a,g)}return d}const V={id:y,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},o=n.pbrMetallicRoughness??{};let a=F(e,o,"baseColorTexture",s);a=F(e,o,"metallicRoughnessTexture",s)||a,a=F(e,n,"normalTexture",s)||a,a=F(e,n,"occlusionTexture",s)||a,a=F(e,n,"emissiveTexture",s)||a;const c=n.extensions?.KHR_materials_specular;c&&(a=F(e,c,"specularTexture",s)||a,a=F(e,c,"specularColorTexture",s)||a),a&&Object.defineProperty(n,_,{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 o=new Map;for(const r of Object.keys(e)){const s=e[r],a=n.accessors[s];void 0!==n.bufferViews?.[a.bufferView]?.byteStride&&o.set(r,R(n,t,s))}const u=n.accessors[e.POSITION],i=void 0===r.indices?new a(0):new a(c(n,t,r.indices)._data);s.set(r,{_attributes:o,_indices:i,_vertexCount:u.count,_indexCount:i.length})}return s},async applyMaterial(e,t){const r=e._rawMatDef?.[_];if(!r)return null;const[n,s,o,a,c,u,i]=await Promise.all([k(r,t,r.baseColorTexture,!0),E(r,t),k(r,t,r.normalTexture,!1),k(r,t,r.emissiveTexture,!0),k(r,t,r.specularTexture,!1),k(r,t,r.specularColorTexture,!1),r.specularTexture||r.specularColorTexture?import("./set-metallic-reflectance-CdBVQVJt.esm.min.js"):void 0]),l={...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,...o?{normalTexture:o,normalTextureScale:r.normalTexture?.scale??1}:void 0,...a?{emissiveTexture:a}:void 0};return i&&(c||u)&&i.setPbrMetallicReflectance(l,{...c?{texture:c,useOnlyMetallicFromTexture:!0}:void 0,...u?{reflectanceTexture:u}:void 0}),l.baseColorTexture||l.ormTexture||l.normalTexture||l.emissiveTexture||c||u?l:null}};export{V as default}; //# sourceMappingURL=gltf-ext-basisu-BdyN-_c_.esm.min.js.map