398 console.error(
'MaterialX module is not loaded');
403 console.warn(
'No Physically Based Materials to convert');
407 if (Object.keys(remapKeys).length === 0)
415 let refDoc = this.
mx.createDocument();
416 refDoc.importLibrary(this.
stdlib);
417 const refNode = refDoc.addNode(shaderCategory,
'refShader', this.
mx.SURFACE_SHADER_TYPE_STRING);
419 console.warn(
'MaterialX JS API missing addInputsFromNodeDef()');
422 this.
doc = this.
mx.createDocument();
425 let docString =
'Physically Based Materials from https://api.physicallybased.info.\n'
426 docString +=
' Content Author: Anton Palmqvist, https://antonpalmqvist.com/ \n'
427 docString += ` Content processsed via REST API and mapped to MaterialX V${this.
mx.getVersionString()} \n`
428 docString += ` Target Shading Model: ${shaderCategory} \n`
429 docString +=
' Utility Author: Bernard Kwok. kwokcb@gmail.com '
430 this.
doc.setDocString(docString);
433 for (let i = 0; i < this.
materials.length; i++) {
435 let matName = mat[
'name'];
440 let shaderNode =
null;
443 if (shaderPreFix.length > 0) {
444 matName = shaderPreFix +
'_' + matName;
447 const shaderName = this.
doc.createValidChildName(matName +
'_' + shaderCategory +
'_SPB');
448 this.
addComment(this.
doc,
' Generated shader: ' + matName +
' ');
449 shaderNode = this.
doc.addNode(shaderCategory, shaderName, this.
mx.SURFACE_SHADER_TYPE_STRING);
451 const category = mat[
'category'];
452 const group = mat[
'group'];
454 if (category && category.length > 0) {
455 uifolder = category[0];
457 if (group && group.length > 0) {
458 if (uifolder.length > 0) {
461 uifolder += group[0];
463 if (uifolder.length > 0) {
464 shaderNode.setAttribute(
'uifolder', uifolder);
468 if (mat[
'description'].length > 0) {
469 docString +=
'Description: ' + mat[
'description'];
474 const refString = mat[
'reference'];
475 if (refString.length > 0) {
476 if (docString.length > 0) {
479 docString +=
'Reference: ' + refString[0];
481 let referenceItem = { name: matName, reference: refString[0] };
483 references.push(referenceItem);
486 references.sort((a, b) => a.name.localeCompare(b.name));
491 if (docString.length > 0) {
492 shaderNode.setDocString(docString);
496 const materialName = this.
doc.createValidChildName(matName +
'_' + shaderCategory +
'_MPB');
497 this.
addComment(this.
doc,
' Generated material: ' + matName +
' ');
498 const materialNode = this.
doc.addNode(this.
mx.SURFACE_MATERIAL_NODE_STRING, materialName,
this.mx.MATERIAL_TYPE_STRING);
499 const shaderInput = materialNode.addInput(this.
mx.SURFACE_SHADER_TYPE_STRING,
this.mx.SURFACE_SHADER_TYPE_STRING);
504 const skipKeys = [
'name',
"density",
"category",
"description",
"sources",
"tags",
"reference"];
506 let metallness =
null;
507 let roughness =
null;
508 let transmission_color =
null;
509 let transmission =
null;
510 Object.entries(mat).forEach(([key, value]) => {
512 if (!skipKeys.includes(key)) {
516 if (key ==
'metalness') {
520 if (key ==
'roughness') {
524 if (key ==
'transmission') {
525 transmission = value;
528 if (key ==
'color') {
529 transmission_color = value;
534 if (remapKeys[key]) {
535 key = remapKeys[key];
538 let refInput = refNode.getInput(key);
540 refInput = refNode.addInputFromNodeDef(key);
542 const input = shaderNode.addInput(key);
543 input.copyContentFrom(refInput);
546 if (Array.isArray(value)) {
547 value = value.join(
',');
550 else if (typeof value ===
'number') {
551 value = value.toString();
556 input.setValueString(value, refInput.getType());
565 if (transmission !==
null && metallness !==
null && roughness !==
null && transmission_color !==
null)
567 if (metallness == 0 && roughness == 0)
569 if (remapKeys[
'transmission_color']) {
570 let inputName = remapKeys[
'transmission_color'];
571 let input = shaderNode.addInput(inputName);
573 let value = transmission_color.join(
',');
575 input.setValueString(value,
'color3');