|
| 1 | +const fs = require("node:fs"); |
| 2 | +const AdmZip = require("adm-zip"); |
| 3 | + |
| 4 | +const materialxVersion = "1.39"; |
| 5 | +const outputFile = "MaterialX-v" + materialxVersion + "_OpenPBR.zip"; |
| 6 | +const tempFolder = "./tmp/"; |
| 7 | + |
| 8 | +function createFiles() { |
| 9 | + return new Promise((resolve, reject) => { |
| 10 | + fs.readFile("./deploy/v2/materials.json", "utf8", (err, data) => { |
| 11 | + if (err) { |
| 12 | + console.error(err); |
| 13 | + reject(err); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + // prettier-ignore |
| 18 | + function makeMaterialX( |
| 19 | + hit |
| 20 | + ) { |
| 21 | + const baseColor = JSON.stringify(hit.color[0].color) !== JSON.stringify([0.8, 0.8, 0.8]) && !hit.transmission && !hit.subsurfaceRadius ? ' <input name="base_color" type="color3" value="'+ hit.color[0].color[0].toFixed(3)+', '+ hit.color[0].color[1].toFixed(3)+', '+ hit.color[0].color[2].toFixed(3) +'" />\n' : ""; |
| 22 | + const metalness = hit.metalness > 0 ? ' <input name="base_metalness" type="float" value="'+ hit.metalness.toFixed(1) +'" />\n' : ""; |
| 23 | + const specularColor = hit.specularColor ? ' <input name="specular_color" type="color3" value="'+ hit.specularColor[0].color[0].toFixed(3)+', '+ hit.specularColor[0].color[1].toFixed(3)+', '+ hit.specularColor[0].color[2].toFixed(3) +'" />\n' : ""; |
| 24 | + const roughness = hit.roughness != 0.3 ? ' <input name="specular_roughness" type="float" value="'+ hit.roughness.toFixed(1) +'" />\n' : ""; |
| 25 | + const specularIor = hit.ior && hit.metalness < 1 && hit.ior != 1.5 ? ' <input name="specular_ior" type="float" value="'+ hit.ior.toFixed(2) +'" />\n' : ""; |
| 26 | + const transmission = hit.transmission ? ' <input name="transmission_weight" type="float" value="'+ hit.transmission.toFixed(1) +'" />\n' : ""; |
| 27 | + const transmissionColor = hit.transmission && JSON.stringify(hit.color[0].color) !== JSON.stringify([1, 1, 1]) ? ' <input name="transmission_color" type="color3" value="'+ hit.color[0].color[0].toFixed(3)+', '+ hit.color[0].color[1].toFixed(3)+', '+ hit.color[0].color[2].toFixed(3) +'" />\n' : ""; |
| 28 | + const transmissionDispersion = hit.transmissionDispersion ? ' <input name="transmission_dispersion_scale" type="float" value="1.0" />\n' : ""; |
| 29 | + const transmissionDispersionAbbeNumber = hit.transmissionDispersion ? ' <input name="transmission_dispersion_abbe_number" type="float" value="'+ hit.transmissionDispersion +'" />\n' : ""; |
| 30 | + const subsurface = hit.subsurfaceRadius ? ' <input name="subsurface_weight" type="float" value="1.0" />\n' : ""; |
| 31 | + const subsurfaceColor = hit.subsurfaceRadius ? ' <input name="subsurface_color" type="color3" value="'+ hit.color[0].color[0].toFixed(3)+', '+ hit.color[0].color[1].toFixed(3)+', '+ hit.color[0].color[2].toFixed(3) +'" />\n' : ""; |
| 32 | + const subsurfaceRadiusScale = hit.subsurfaceRadius ? ' <input name="subsurface_radius_scale" type="color3" value="'+ hit.subsurfaceRadius[0]+', '+ hit.subsurfaceRadius[1]+', '+ hit.subsurfaceRadius[2] +'" />\n' : ""; |
| 33 | + const thinFilmWeight = hit.thinFilmThickness ? ' <input name="thin_film_weight" type="float" value="1.0" />\n' : ""; |
| 34 | + const thinFilmThickness = hit.thinFilmThickness && hit.thinFilmThickness[2] ? ' <input name="thin_film_thickness" type="float" value="'+ hit.thinFilmThickness[2]/1000 +'" />\n' : hit.thinFilmThickness && hit.thinFilmThickness[0] ? ' <input name="thin_film_thickness" type="float" value="'+ hit.thinFilmThickness[0]/1000 +'" />\n' : ""; |
| 35 | + const thinFilmIor = hit.thinFilmIor ? ' <input name="thin_film_ior" type="float" value="'+ hit.thinFilmIor.toFixed(2) +'" />\n' : ""; |
| 36 | + const thinWalled = hit.thinFilmThickness && hit.transmission ? ' <input name="geometry_thin_walled" type="boolean" value="true" />\n' : ""; |
| 37 | + let xml = |
| 38 | + // Commented lines are values that are not used and therefore removed to follow best practices for MaterialX "preset" functionality https://academysoftwarefdn.slack.com/archives/C0230LWBE2X/p1660682953141679?thread_ts=1660168970.997769&cid=C0230LWBE2X |
| 39 | + '<?xml version="1.0"?>\n' + |
| 40 | + '<materialx version="'+ materialxVersion +'" colorspace="lin_rec709">\n' + |
| 41 | + ' <surfacematerial name="'+ hit.name.replace(/ |-|\./g, "_").replace(/[()]/g, "") +'" type="material">\n' + |
| 42 | + ' <input name="surfaceshader" type="surfaceshader" nodename="open_pbr_surface_surfaceshader" />\n' + |
| 43 | + ' </surfacematerial>\n' + |
| 44 | + ' <open_pbr_surface name="open_pbr_surface_surfaceshader" type="surfaceshader">\n' + |
| 45 | + baseColor + |
| 46 | + //' <input name="base_diffuse_roughness" type="float" value="0.0" />\n' + |
| 47 | + metalness + |
| 48 | + //' <input name="specular_weight" type="float" value="1" />\n' + |
| 49 | + specularColor + |
| 50 | + roughness + |
| 51 | + specularIor + |
| 52 | + // ' <input name="specular_roughness_anisotropy" type="float" value="0" />\n' + |
| 53 | + transmission + |
| 54 | + transmissionColor + |
| 55 | + //' <input name="transmission_depth" type="float" value="0.0" />\n' + |
| 56 | + //' <input name="transmission_scatter" type="color3" value="0, 0, 0" />\n' + |
| 57 | + //' <input name="transmission_scatter_anisotropy" type="float" value="0" />\n' + |
| 58 | + transmissionDispersion + |
| 59 | + transmissionDispersionAbbeNumber + |
| 60 | + subsurface + |
| 61 | + subsurfaceColor + |
| 62 | + //' <input name="subsurface_radius" type="float" value="1.0" />\n' + |
| 63 | + subsurfaceRadiusScale + |
| 64 | + //' <input name="subsurface_scatter_anisotropy" type="float" value="0.0" />\n' + |
| 65 | + //' <input name="coat_color" type="color3" value="1, 1, 1" />\n' + |
| 66 | + //' <input name="coat_roughness_anisotropy" type="float" value="0.0" />\n' + |
| 67 | + //' <input name="coat_ior" type="float" value="1.6" />\n' + |
| 68 | + //' <input name="coat_darkening" type="float" value="1.0" />\n' + |
| 69 | + thinFilmWeight + |
| 70 | + thinFilmThickness + |
| 71 | + thinFilmIor + |
| 72 | + //' <input name="geometry_opacity" type="float" value="1" />\n' + |
| 73 | + thinWalled + |
| 74 | + ' </open_pbr_surface>\n' + |
| 75 | + '</materialx>'; |
| 76 | + |
| 77 | + return xml; |
| 78 | + } |
| 79 | + |
| 80 | + JSON.parse(data).forEach((element) => { |
| 81 | + if (!fs.existsSync(tempFolder)) { |
| 82 | + fs.mkdirSync(tempFolder, { recursive: true }); |
| 83 | + } |
| 84 | + const fileName = |
| 85 | + tempFolder + |
| 86 | + element.name.replace(/ |-|\./g, "_").replace(/[()]/g, "") + |
| 87 | + ".mtlx"; |
| 88 | + fs.writeFile(fileName, makeMaterialX(element), (err) => { |
| 89 | + if (err) { |
| 90 | + console.error(err); |
| 91 | + reject(err); |
| 92 | + } else { |
| 93 | + console.log(fileName + " created"); |
| 94 | + resolve(); |
| 95 | + } |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +function zipFiles() { |
| 103 | + const zip = new AdmZip(); |
| 104 | + zip.addLocalFolder(tempFolder); |
| 105 | + zip.writeZip(outputFile); |
| 106 | +} |
| 107 | + |
| 108 | +async function main() { |
| 109 | + try { |
| 110 | + await createFiles(); |
| 111 | + zipFiles(); |
| 112 | + console.log("Created " + outputFile + " successfully"); |
| 113 | + |
| 114 | + if (fs.existsSync(tempFolder)) { |
| 115 | + fs.rmSync(tempFolder, { recursive: true, force: true }); |
| 116 | + console.log("Deleted " + tempFolder + " folder successfully"); |
| 117 | + } |
| 118 | + } catch (err) { |
| 119 | + console.error("An error occurred:", err); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +main(); |
0 commit comments