/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import e from"../../geometry/Mesh.js";import t from"../../geometry/Point.js";import r from"../../geometry/support/MeshMaterialMetallicRoughness.js";import o from"../../geometry/support/MeshTransform.js";import{loadImage as a}from"../../request/loadImage.js";import{meshMakeAbsolute as n}from"./meshUtils.js";import{isEncodedMeshTexture as i}from"../../views/3d/glTF/internal/resourceUtils.js";async function s(r,a,n,i){const s=URL.createObjectURL(new Blob([r]));try{const r=t.fromJSON(a),n=await e.createFromGLTF(r,s,{vertexSpace:r.spatialReference.isGeographic?"local":"georeferenced"}),{rotationAxis:l,rotationAngle:c,translation:u,scale:m}=i;return n.transform=new o({rotationAngle:c??0,rotationAxis:null!=l?[l[0],l[1],l[2]]:[0,0,1],translation:null!=u?[u[0],u[1],u[2]]:[0,0,0],scale:null!=m?[m[0],m[1],m[2]]:[1,1,1]}),n}finally{URL.revokeObjectURL(s)}}async function l(e,t,r,o){const a=await s(e,t,r,o),n=a.extent.toJSON();return a.destroy(),n}async function c(e,t,o,a){const i=await s(e,t,o,a),{components:l}=i;if(null==l)throw new Error("Mesh has no components");const{transform:c}=i;if(null==c)throw new Error("Mesh has no transform");const m=n(i);if(!m)throw new Error("failed to project");const d=l.map(async e=>{const{faces:t,material:o}=e,a=o instanceof r,n={pbrMetallicRoughness:{baseColorFactor:o?.color?.toUnitRGBA()??[1,1,1,1],baseColorTexture:await u(o?.colorTexture),metallicFactor:a?o.metallic:void 0,roughnessFactor:a?o.roughness:void 0,metallicRoughnessTexture:a&&null!=o.metallicRoughnessTexture?await u(o.metallicRoughnessTexture):void 0},normalTexture:null!=o?.normalTexture?await u(o.normalTexture):void 0,occlusionTexture:a&&null!=o.occlusionTexture?await u(o.occlusionTexture):void 0,emissiveTexture:a&&null!=o.emissiveTexture?await u(o.emissiveTexture):void 0,emissiveFactor:a&&o.emissiveColor?.toUnitRGB()||void 0,alphaMode:("auto"===o?.alphaMode?"blend":o?.alphaMode)??"opaque",alphaCutoff:o?.alphaCutoff??.1,doubleSided:o?.doubleSided??!0,cullFace:o?.doubleSided??1?"none":"back"};return{geometry:{position:m.vertexAttributes.position,faces:t,normal:"source"===e.shading?m.vertexAttributes.normal:void 0,uv:m.vertexAttributes.uv,color:m.vertexAttributes.color},material:n}});return Promise.all(d)}async function u(e){if(null==e)return;const{url:t,data:r}=e,o="image/png",a="string"==typeof e.wrap?"clamp"===e.wrap?"none":e.wrap:"repeat";if(null!=r){if(i(r))throw new Error("Cannot bake encoded textures");if(r instanceof HTMLVideoElement)throw new Error("Cannot bake video textures");if(r instanceof HTMLImageElement){const e=await m(await d(r),o),t=await e.arrayBuffer();return{type:o,width:r.width,wrap:a,data:t}}if(r instanceof HTMLCanvasElement){const e=await m(r,o),t=await e.arrayBuffer();return{type:o,width:r.width,wrap:a,data:t}}const e=await m(w(r),o),t=await e.arrayBuffer();return{type:o,width:r.width,wrap:a,data:t}}if(null!=t){const e=await f(t),r=await m(await d(e),o),n=await r.arrayBuffer();return{type:o,width:e.width,wrap:a,data:n}}}async function m(e,t){return new Promise((r,o)=>e.toBlob(e=>null==e?o(new Error("Failed to load image content")):r(e),t))}async function d(e){await e.decode();const t=document.createElement("canvas");t.width=e.width,t.height=e.height;const r=t.getContext("2d");if(null==r)throw new Error("Failed to create 2d context");return r.drawImage(e,0,0),t}function w(e){const t=document.createElement("canvas");t.width=e.width,t.height=e.height;const r=t.getContext("2d");if(null==r)throw new Error("Failed to create 2d context");return r.putImageData(e,0,0),t}function f(e){const t=new Image;return t.src=e,a(t,t.src,!1,void 0)}export{l as extentFromGLB,c as nodeDataFromGLB};
