@@ -2,8 +2,11 @@ import fs from "node:fs";
22import AdmZip from "adm-zip" ;
33
44const materialxVersion = "1.39" ;
5- const outputFile = "MaterialX-v" + materialxVersion + "_OpenPBR.zip" ;
6- const tempFolder = "./tmp/" ;
5+ const outputFile = `MaterialX-v"${ materialxVersion } "_OpenPBR.zip` ;
6+ const tempFolder = "./tmp/mtlx/openpbr/" ;
7+ const doc = ` doc="Generated with data from https://api.physicallybased.info on ${
8+ new Date ( ) . toISOString ( ) . split ( "T" ) [ 0 ]
9+ } "`;
710
811function createFiles ( ) {
912 return new Promise ( ( resolve , reject ) => {
@@ -15,66 +18,65 @@ function createFiles() {
1518 }
1619
1720 // 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 [ 1 ] . color [ 0 ] . color [ 0 ] + ', ' + hit . specularColor [ 1 ] . color [ 0 ] . color [ 1 ] + ', ' + hit . specularColor [ 1 ] . color [ 0 ] . color [ 2 ] + '" />\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
21+ function makeMaterialX ( hit ) {
22+ 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` : "" ;
23+ const metalness = hit . metalness > 0 ? ` <input name="base_metalness" type="float" value="${ hit . metalness . toFixed ( 1 ) } " />\n` : "" ;
24+ const specularColor = hit . specularColor ? ` <input name="specular_color" type="color3" value="${ hit . specularColor [ 1 ] . color [ 0 ] . color [ 0 ] } , ${ hit . specularColor [ 1 ] . color [ 0 ] . color [ 1 ] } , ${ hit . specularColor [ 1 ] . color [ 0 ] . color [ 2 ] } " />\n` : "" ;
25+ const roughness = hit . roughness !== 0.3 ? ` <input name="specular_roughness" type="float" value="${ hit . roughness . toFixed ( 1 ) } " />\n` : "" ;
26+ const specularIor = hit . ior && hit . metalness < 1 && hit . ior !== 1.5 ? ` <input name="specular_ior" type="float" value="${ hit . ior . toFixed ( 2 ) } " />\n` : "" ;
27+ const transmission = hit . transmission ? ` <input name="transmission_weight" type="float" value="${ hit . transmission . toFixed ( 1 ) } " />\n` : "" ;
28+ 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` : "" ;
29+ const transmissionDispersion = hit . transmissionDispersion ? ` <input name="transmission_dispersion_scale" type="float" value="1.0" />\n` : "" ;
30+ const transmissionDispersionAbbeNumber = hit . transmissionDispersion ? ` <input name="transmission_dispersion_abbe_number" type="float" value="${ hit . transmissionDispersion } " />\n` : "" ;
31+ const subsurface = hit . subsurfaceRadius ? ` <input name="subsurface_weight" type="float" value="1.0" />\n` : "" ;
32+ 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` : "" ;
33+ const subsurfaceRadiusScale = hit . subsurfaceRadius ? ` <input name="subsurface_radius_scale" type="color3" value="${ hit . subsurfaceRadius [ 0 ] } , ${ hit . subsurfaceRadius [ 1 ] } , ${ hit . subsurfaceRadius [ 2 ] } " />\n` : "" ;
34+ const thinFilmWeight = hit . thinFilmThickness ? ` <input name="thin_film_weight" type="float" value="1.0" />\n` : "" ;
35+ 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` : "" ;
36+ const thinFilmIor = hit . thinFilmIor ? ` <input name="thin_film_ior" type="float" value="${ hit . thinFilmIor . toFixed ( 2 ) } " />\n` : "" ;
37+ const thinWalled = hit . thinFilmThickness && hit . transmission ? ` <input name="geometry_thin_walled" type="boolean" value="true" />\n` : "" ;
38+ const xml =
39+ // 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
40+ ' <surfacematerial name="' + hit . name . replace ( / | - | : | \. / g, "_" ) . replace ( / [ \[ \] ( ) º ] / g, "" ) + '" type="material">\n' +
41+ ' <input name="surfaceshader" type="surfaceshader" nodename="open_pbr_surface_surfaceshader" />\n' +
42+ ' </surfacematerial>\n' +
43+ ' <open_pbr_surface name="open_pbr_surface_surfaceshader" type="surfaceshader">\n' +
44+ baseColor +
45+ //' <input name="base_diffuse_roughness" type="float" value="0.0" />\n' +
46+ metalness +
47+ //' <input name="specular_weight" type="float" value="1" />\n' +
48+ specularColor +
49+ roughness +
50+ specularIor +
51+ // ' <input name="specular_roughness_anisotropy" type="float" value="0" />\n' +
52+ transmission +
53+ transmissionColor +
54+ //' <input name="transmission_depth" type="float" value="0.0" />\n' +
55+ //' <input name="transmission_scatter" type="color3" value="0, 0, 0" />\n' +
56+ //' <input name="transmission_scatter_anisotropy" type="float" value="0" />\n' +
57+ transmissionDispersion +
58+ transmissionDispersionAbbeNumber +
59+ subsurface +
60+ subsurfaceColor +
61+ //' <input name="subsurface_radius" type="float" value="1.0" />\n' +
62+ subsurfaceRadiusScale +
63+ //' <input name="subsurface_scatter_anisotropy" type="float" value="0.0" />\n' +
64+ //' <input name="coat_color" type="color3" value="1, 1, 1" />\n' +
65+ //' <input name="coat_roughness_anisotropy" type="float" value="0.0" />\n' +
66+ //' <input name="coat_ior" type="float" value="1.6" />\n' +
67+ //' <input name="coat_darkening" type="float" value="1.0" />\n' +
68+ thinFilmWeight +
69+ thinFilmThickness +
70+ thinFilmIor +
71+ //' <input name="geometry_opacity" type="float" value="1" />\n' +
72+ thinWalled +
73+ ' </open_pbr_surface>\n' ;
74+ return (
3975 '<?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 ;
76+ '<materialx version="' + materialxVersion + '" colorspace="lin_rec709"' + doc + '>\n' +
77+ xml +
78+ '</materialx>'
79+ ) ;
7880 }
7981
8082 JSON . parse ( data ) . forEach ( ( element ) => {
@@ -83,14 +85,17 @@ function createFiles() {
8385 }
8486 const fileName =
8587 tempFolder +
86- element . name . replace ( / | - | : | \. / g, "_" ) . replace ( / [ \[ \] ( ) º ] / g, "" ) +
88+ element . name
89+ . replace ( / | - | : | \. / g, "_" )
90+ . replace ( / [ \[ \] ( ) º ] / g, "" )
91+ . toLowerCase ( ) +
8792 ".mtlx" ;
8893 fs . writeFile ( fileName , makeMaterialX ( element ) , ( err ) => {
8994 if ( err ) {
9095 console . error ( err ) ;
9196 reject ( err ) ;
9297 } else {
93- console . log ( fileName + " created" ) ;
98+ console . log ( ` ${ fileName } created` ) ;
9499 resolve ( ) ;
95100 }
96101 } ) ;
@@ -108,12 +113,12 @@ function zipFiles() {
108113async function main ( ) {
109114 try {
110115 await createFiles ( ) ;
111- zipFiles ( ) ;
112- console . log ( " Created " + outputFile + " successfully" ) ;
116+ // zipFiles();
117+ // console.log(` Created ${ outputFile} successfully` );
113118
114119 if ( fs . existsSync ( tempFolder ) ) {
115- fs . rmSync ( tempFolder , { recursive : true , force : true } ) ;
116- console . log ( "Deleted " + tempFolder + " folder successfully" ) ;
120+ // fs.rmSync(tempFolder, { recursive: true, force: true });
121+ // console.log("Deleted " + tempFolder + " folder successfully");
117122 }
118123 } catch ( err ) {
119124 console . error ( "An error occurred:" , err ) ;
0 commit comments