317 console.error(
'MaterialX module is not loaded');
322 console.warn(
'No Physically Based Materials to convert');
326 if (remapKeys.length == 0) {
332 let refDoc = this.
mx.createDocument();
333 refDoc.importLibrary(this.
stdlib);
334 const refNode = refDoc.addNode(shaderCategory,
'refShader', this.
mx.SURFACE_SHADER_TYPE_STRING);
336 this.
doc = this.
mx.createDocument();
339 this.
addComment(this.
doc,
'Physically Based Materials from https://api.physicallybased.info ');
340 this.
addComment(this.
doc,
' Processed via API and converted to MaterialX ');
341 this.
addComment(this.
doc,
' Target Shading Model: ' + shaderCategory);
342 this.
addComment(this.
doc,
' Utility Author: Bernard Kwok. kwokcb@gmail.com ');
345 for (let i = 0; i < this.
materials.length; i++) {
347 let matName = mat[
'name'];
352 console.log(
'Skipping material:', matName);
357 if (shaderPreFix.length > 0) {
358 matName = shaderPreFix +
'_' + matName;
361 const shaderName = this.
doc.createValidChildName(
'SPB_' + matName);
362 this.
addComment(this.
doc,
' Generated shader: ' + shaderName +
' ');
363 const shaderNode = this.
doc.addNode(shaderCategory, shaderName, this.
mx.SURFACE_SHADER_TYPE_STRING);
364 let docString = mat[
'description'];
365 const refString = mat[
'reference'];
366 if (refString.length > 0) {
367 if (docString.length > 0) {
370 docString +=
'Reference: ' + refString[0];
372 if (docString.length > 0) {
373 shaderNode.setDocString(docString);
377 const materialName = this.
doc.createValidChildName(
'MPB_' + matName);
378 this.
addComment(this.
doc,
' Generated material: ' + materialName +
' ');
379 const materialNode = this.
doc.addNode(this.
mx.SURFACE_MATERIAL_NODE_STRING, materialName,
this.mx.MATERIAL_TYPE_STRING);
380 const shaderInput = materialNode.addInput(this.
mx.SURFACE_SHADER_TYPE_STRING,
this.mx.SURFACE_SHADER_TYPE_STRING);
385 const skipKeys = [
'name',
"density",
"category",
"description",
"sources",
"tags",
"reference"];
387 let metallness =
null;
388 let roughness =
null;
389 let transmission_color =
null;
390 let transmission =
null;
391 Object.entries(mat).forEach(([key, value]) => {
393 if (!skipKeys.includes(key)) {
395 if (key ==
'metalness') {
399 if (key ==
'roughness') {
403 if (key ==
'transmission') {
404 transmission = value;
407 if (key ==
'color') {
408 transmission_color = value;
412 if (remapKeys[key]) {
413 key = remapKeys[key];
416 let refInput = refNode.getInput(key);
418 refInput = refNode.addInputFromNodeDef(key);
420 const input = shaderNode.addInput(key);
421 input.copyContentFrom(refInput);
424 if (Array.isArray(value)) {
425 value = value.join(
',');
428 else if (typeof value ===
'number') {
429 value = value.toString();
434 input.setValueString(value, refInput.getType());
443 if (transmission !==
null && metallness !==
null && roughness !==
null && transmission_color !==
null)
445 if (metallness == 0 && roughness == 0)
447 if (remapKeys[
'transmission_color']) {
448 let inputName = remapKeys[
'transmission_color'];
449 let input = shaderNode.addInput(inputName);
451 let value = transmission_color.join(
',');
452 console.log(`Add
"${inputName}":
"${value}"`);
453 input.setValueString(value,
'color3');