UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 2.6 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import e from"../../../core/Error.js";import{whenOrAbort as t,throwIfAborted as s,allSettledValues as n,isAborted as r,createAbortError as o}from"../../../core/promiseUtils.js";import{removeFile as i,makeRelative as l}from"../../../core/urlUtils.js";import a from"../../Point.js";async function c(e,n,r){const{source:o}=n,{loadGLTFMesh:i}=await t(import("./loadGLTFMesh.js"),r),l=await f(o,r);s(r);const c=i(new a({x:0,y:0,z:0,spatialReference:e.spatialReference}),l.url,{resolveFile:u(l),signal:r?.signal,expectedType:l.type,unitConversionDisabled:n.unitConversionDisabled});c.then((()=>l.dispose()),(()=>l.dispose()));const{vertexAttributes:m,components:p}=await c;e.vertexAttributes=m,e.components=p}function u(e){const t=i(e.url);return s=>{const n=l(s,t,t),r=n?n.replace(/^ *\.\//,""):null;return(r?e.files.get(r):null)??s}}async function f(t,s){if(Array.isArray(t)){if(!t.length)throw new e("mesh-load-external:missing-assets","There must be at least one file to load");return t[0]instanceof File?y(t):h(t,s)}return p(t)}async function m(e,t){const{parts:n,assetMimeType:r,assetName:o}=e;if(1===n.length)return new v(n[0].partUrl);const i=await e.toBlob(t);return s(t),v.fromBlob(i,x(o,r))}function p(e){return v.fromBlob(e,x(e.name,e.type))}function y(e){return j(e.map((e=>({name:e.name,mimeType:e.type,source:p(e)}))))}async function h(e,t){const i=await n(e.map((async e=>{const n=await m(e);return s(t),{name:e.assetName,mimeType:e.assetMimeType,source:n}})));if(r(t))throw i.forEach((e=>e.source.dispose())),o();return j(i)}const d=/^model\/gltf\+json$/,g=/^model\/gltf-binary$/,w=/\.gltf$/i,b=/\.glb$/i;function T({mimeType:e,source:t,name:s}){return d.test(e)||w.test(s)?{url:t.url,type:"gltf"}:g.test(e)||b.test(s)?{url:t.url,type:"glb"}:null}function j(t){const s=new Map;let n=null,r=null;for(const e of t){const{source:t,name:o}=e;n??=T(e),"ESRI3DO_NORM.glb"===o&&(r=T(e)),s.set(o,t.url),t.files.forEach(((e,t)=>s.set(t,e)))}const o=r??n;if(null==o)throw new e("mesh-load-external:missing-files","Missing files to load external mesh source");return new v(o.url,(()=>t.forEach((({source:e})=>e.dispose()))),s,o.type)}class v{constructor(e,t=()=>{},s=new Map,n){this.url=e,this.dispose=t,this.files=s,this.type=n}static fromBlob(e,t){const s=URL.createObjectURL(e);return new v(s,(()=>URL.revokeObjectURL(s)),void 0,t)}}function x(e,t){return d.test(t)||w.test(e)?"gltf":g.test(t)||w.test(e)?"glb":void 0}export{c as loadExternal};