/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../../../core/Logger.js";import{getKey as o}from"../../../core/object.js";import{clone as t}from"../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{makeMaterialParameters as r,makeTextureSource as s}from"./LoaderResult.js";import{GLTFResource as a}from"./internal/Resource.js";import{PrimitiveType as i}from"../../webgl/enums.js";let n=0;async function l(r,s,l={},m=!0,T=!0){const p=await a.load(r,s,l),f="gltf_"+n++,x={lods:[],materials:new Map,textures:new Map,meta:u(p)},g=!(!p.json.asset.extras||"symbolResource"!==p.json.asset.extras.ESRI_type),h=p.json.asset.extras?.ESRI_webstyleSymbol?.webstyle,w=new Map;let v=!1;await c(p,async(r,s,a,n)=>{const u=w.get(a)??0;w.set(a,u+1);const c=void 0!==r.mode?r.mode:i.TRIANGLES,g=c===i.TRIANGLES||c===i.TRIANGLE_STRIP||c===i.TRIANGLE_FAN?c:null;if(null==g)return void e.getLogger("esri.views.3d.glTF").warn("[Unsupported Feature] Unsupported primitive mode ("+o(i,c)+"). Skipping primitive.");if(!p.hasPositions(r))return void e.getLogger("esri.views.3d.glTF").warn("Skipping primitive without POSITION vertex attribute.");const h=await p.decode(r,l);v||=!!h;const S=p.getPositionData(r,l,h),R=p.getMaterial(r,l,m,T),_=p.hasNormals(r)?p.getNormalData(r,l,h):null,F=p.hasTangents(r)?p.getTangentData(r,l,h):null,y=p.hasTextureCoordinates(r)?p.getTextureCoordinates(r,l,h):null,E=p.hasVertexColors(r)?p.getVertexColors(r,l,h):null,I=p.getIndexData(r,l,h),M={name:n,transform:t(s),attributes:{position:await S,normal:_?await _:null,texCoord0:y?await y:null,color:E?await E:null,tangent:F?await F:null},indices:await I,primitiveType:g,material:d(x,await R,f)};let b=null;null!=x.meta?.ESRI_lod&&"screenSpaceRadius"===x.meta.ESRI_lod.metric&&(b=x.meta.ESRI_lod.thresholds[a]),x.lods[a]=x.lods[a]||{parts:[],name:n,lodThreshold:b},x.lods[a].parts[u]=M});for(const e of x.lods)e.parts=e.parts.filter(e=>!!e);const S=await p.getLoadedBuffersSize();return{model:x,meta:{isEsriSymbolResource:g,uri:p.uri,ESRI_webstyle:h,isDracoDecompressed:v},customMeta:{},usedMemory:S}}function u(e){const o=e.json;let t=null;return o.nodes.forEach(e=>{const o=e.extras;null!=o&&(o.ESRI_proxyEllipsoid||o.ESRI_lod)&&(t=o)}),t}async function c(o,t){const r=o.json,s=r.scenes[r.scene||0].nodes,a=s.length>1,i=[];for(const e of s){const o=r.nodes[e];if(i.push(n(e,0)),m(o)&&!a){o.extensions.MSFT_lod.ids.forEach((e,o)=>n(e,o+1))}}async function n(s,a){const l=r.nodes[s],u=o.getNodeTransform(s);if(null!=l.weights&&e.getLogger("esri.views.3d.glTF").warn("[Unsupported Feature] Morph targets are not supported."),null!=l.mesh){const e=r.meshes[l.mesh];for(const o of e.primitives)i.push(t(o,u,a,e.name))}for(const e of l.children||[])i.push(n(e,a))}await Promise.all(i)}function m(e){return e.extensions?.MSFT_lod&&Array.isArray(e.extensions.MSFT_lod.ids)}function d(e,o,t){const a=o=>{const r=`${t}_tex_${o&&o.id}${o?.name?"_"+o.name:""}`;if(o&&!e.textures.has(r)){const t=s(o.data,{wrap:{s:o.wrapS,t:o.wrapT},mipmap:T.has(o.minFilter),noUnpackFlip:!0});e.textures.set(r,t)}return r},i=`${t}_mat_${o.id}_${o.name}`;if(!e.materials.has(i)){const t=r({color:[o.color[0],o.color[1],o.color[2]],opacity:o.color[3],alphaMode:o.alphaMode,alphaCutoff:o.alphaCutoff,doubleSided:o.doubleSided,colorMixMode:o.ESRI_externalColorMixMode,colorTexture:o.colorTexture?a(o.colorTexture):void 0,normalTexture:o.normalTexture?a(o.normalTexture):void 0,occlusionTexture:o.occlusionTexture?a(o.occlusionTexture):void 0,emissiveTexture:o.emissiveTexture?a(o.emissiveTexture):void 0,metallicRoughnessTexture:o.metallicRoughnessTexture?a(o.metallicRoughnessTexture):void 0,emissiveFactor:[o.emissiveFactor[0],o.emissiveFactor[1],o.emissiveFactor[2]],emissiveStrengthKHR:o.emissiveStrengthKHR,colorTextureTransform:o.colorTextureTransform,normalTextureTransform:o.normalTextureTransform,occlusionTextureTransform:o.occlusionTextureTransform,emissiveTextureTransform:o.emissiveTextureTransform,metallicRoughnessTextureTransform:o.metallicRoughnessTextureTransform,metallicFactor:o.metallicFactor,roughnessFactor:o.roughnessFactor,receiveShadows:o.receiveShadows,receiveAmbientOcclusion:o.receiveAmbientOcclusion});e.materials.set(i,t)}return i}const T=new Set([9987,9985]);export{l as loadGLTF};
