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