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