/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../../../core/Error.js";import{scale as t}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{fromArray as o,zeros as r}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{loadImage as a}from"../../../../request/loadImage.js";import{EncodedMeshTexture as s}from"../../glTF/internal/resourceUtils.js";import{useSchematicPBR as i,schematicMRRFactors as n}from"../../webgl-engine/materials/pbrUtils.js";import{alphaCutoff as l}from"../../../../webscene/support/AlphaCutoff.js";async function u(e){const a=[],s=[];if(null==e){return{material:{alphaMode:"opaque",alphaCutoff:l,doubleSided:!0,cullFace:0,normalTextureId:-1,emissiveTextureId:-1,occlusionTextureId:-1,emissiveFactor:[0,0,0],metallicRoughness:{baseColorFactor:[1,1,1,1],baseColorTextureId:-1,metallicRoughnessTextureId:-1,metallicFactor:0,roughnessFactor:.6000000238418579},wrapTextures:!1,hasParametersFromSource:!0},requiredTextures:a,textureData:s}}const u=m(e);"auto"===e.alphaMode&&console.warn('alphaMode "auto" not supported by I3S PBRMaterial - defaulting to "blend".');const d=i({normalTexture:e.normalTexture,emissiveTexture:u?e.emissiveTexture:null,emissiveFactor:u&&e.emissiveColor?.toUnitRGB()||null,occlusionTexture:u?e.occlusionTexture:null,metallicRoughnessTexture:u?e.metallicRoughnessTexture:null,metallicFactor:u?e.metallic:null,roughnessFactor:u?e.roughness:null}),g=d?n[0]:u?e.metallic:0,f=d?n[1]:u?e.roughness:0;let h=r();u&&e.emissiveColor&&(h=o(e.emissiveColor.toUnitRGB()),e.emissiveStrength&&t(h,h,e.emissiveStrength));return{material:{alphaMode:"auto"===e.alphaMode?"blend":e.alphaMode,alphaCutoff:e.alphaCutoff,doubleSided:e.doubleSided,cullFace:e.doubleSided?0:2,normalTextureId:await c(e.normalTexture,a,s,4),emissiveTextureId:u?await c(e.emissiveTexture,a,s,16):-1,occlusionTextureId:u?await c(e.occlusionTexture,a,s,8):-1,emissiveFactor:h,metallicRoughness:{baseColorFactor:null!=e.color?e.color.toUnitRGBA():[1,1,1,1],baseColorTextureId:await c(e.colorTexture,a,s,1),metallicRoughnessTextureId:u?await c(e.metallicRoughnessTexture,a,s,2):-1,metallicFactor:g,roughnessFactor:f},wrapTextures:!0,hasParametersFromSource:d},requiredTextures:a,textureData:s}}async function c(t,o,r,i){if(null==t)return-1;const n=r.length,l=t.data,u=t.url;if(null!=l){if(l instanceof HTMLImageElement||l instanceof HTMLCanvasElement)return r.push({id:n,usage:i,data:l,encoding:8}),o.push({id:n,usage:i,encodings:[{name:void 0,encoding:8}]}),n;if(l instanceof HTMLVideoElement)return-1;if(l instanceof ImageData)throw new e("scenelayer","ImageData textures not supported yet for client side I3S nodes");if(l instanceof s)return r.push({id:n,usage:i,data:l.data.buffer,encoding:1}),o.push({id:n,usage:i,encodings:[{name:void 0,encoding:1}]}),n}else if(null!=u){const e=new Image;e.src=u;const t=await a(e,e.src,!1,void 0);return r.push({id:n,usage:i,data:t,encoding:8}),o.push({id:n,usage:i,encodings:[{name:void 0,encoding:8}]}),n}return-1}function m(e){return e.hasOwnProperty("metallicRoughnessTexture")}export{u as convertMeshMaterialToPBRMaterial};
