358 console.error(
'MaterialX module is not loaded');
363 console.warn(
'No Physically Based Materials to convert');
367 if (remapKeys.length == 0) {
373 let refDoc = this.
mx.createDocument();
374 refDoc.importLibrary(this.
stdlib);
375 const refNode = refDoc.addNode(shaderCategory,
'refShader', this.
mx.SURFACE_SHADER_TYPE_STRING);
377 this.
doc = this.
mx.createDocument();
380 let docString =
'Physically Based Materials from https://api.physicallybased.info.\n'
381 docString +=
' Content Author: Anton Palmqvist, https://antonpalmqvist.com/ \n'
382 docString += ` Content processsed via REST API and mapped to MaterialX V${this.
mx.getVersionString()} \n`
383 docString += ` Target Shading Model: ${shaderCategory} \n`
384 docString +=
' Utility Author: Bernard Kwok. kwokcb@gmail.com '
385 this.
doc.setDocString(docString);
388 for (let i = 0; i < this.
materials.length; i++) {
390 let matName = mat[
'name'];
395 console.log(
'Skipping material:', matName);
400 if (shaderPreFix.length > 0) {
401 matName = shaderPreFix +
'_' + matName;
404 const shaderName = this.
doc.createValidChildName(
'SPB_' + matName +
'_' + shaderCategory);
405 this.
addComment(this.
doc,
' Generated shader: ' + matName +
' ');
406 const shaderNode = this.
doc.addNode(shaderCategory, shaderName, this.
mx.SURFACE_SHADER_TYPE_STRING);
408 const category = mat[
'category'];
409 const group = mat[
'group'];
411 if (category && category.length > 0) {
412 uifolder = category[0];
414 if (group && group.length > 0) {
415 if (uifolder.length > 0) {
418 uifolder += group[0];
420 if (uifolder.length > 0) {
421 shaderNode.setAttribute(
'uifolder', uifolder);
425 if (mat[
'description'].length > 0) {
426 docString +=
'Description: ' + mat[
'description'];
428 const refString = mat[
'reference'];
429 if (refString.length > 0) {
430 if (docString.length > 0) {
433 docString +=
'Reference: ' + refString[0];
435 let referenceItem = { name: matName, reference: refString[0] };
436 console.log(
'Add Reference:', referenceItem);
437 references.push(referenceItem);
439 if (docString.length > 0) {
440 shaderNode.setDocString(docString);
444 const materialName = this.
doc.createValidChildName(
'MPB_' + matName +
'_' + shaderCategory);
445 this.
addComment(this.
doc,
' Generated material: ' + matName +
' ');
446 const materialNode = this.
doc.addNode(this.
mx.SURFACE_MATERIAL_NODE_STRING, materialName,
this.mx.MATERIAL_TYPE_STRING);
447 const shaderInput = materialNode.addInput(this.
mx.SURFACE_SHADER_TYPE_STRING,
this.mx.SURFACE_SHADER_TYPE_STRING);
448 shaderInput.setAttribute(MTLX_NODE_NAME_ATTRIBUTE, shaderNode.getName());
452 const skipKeys = [
'name',
"density",
"category",
"description",
"sources",
"tags",
"reference"];
454 let metallness =
null;
455 let roughness =
null;
456 let transmission_color =
null;
457 let transmission =
null;
458 Object.entries(mat).forEach(([key, value]) => {
460 if (!skipKeys.includes(key)) {
462 if (key ==
'metalness') {
466 if (key ==
'roughness') {
470 if (key ==
'transmission') {
471 transmission = value;
474 if (key ==
'color') {
475 transmission_color = value;
479 if (remapKeys[key]) {
480 key = remapKeys[key];
483 let refInput = refNode.getInput(key);
485 refInput = refNode.addInputFromNodeDef(key);
487 const input = shaderNode.addInput(key);
488 input.copyContentFrom(refInput);
491 if (Array.isArray(value)) {
492 value = value.join(
',');
495 else if (typeof value ===
'number') {
496 value = value.toString();
501 input.setValueString(value, refInput.getType());
510 if (transmission !==
null && metallness !==
null && roughness !==
null && transmission_color !==
null)
512 if (metallness == 0 && roughness == 0)
514 if (remapKeys[
'transmission_color']) {
515 let inputName = remapKeys[
'transmission_color'];
516 let input = shaderNode.addInput(inputName);
518 let value = transmission_color.join(
',');
520 input.setValueString(value,
'color3');