Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Name Generation #25

Merged
merged 5 commits into from
Nov 14, 2024
Merged

Conversation

kwokcb
Copy link
Contributor

@kwokcb kwokcb commented Nov 14, 2024

Feature

Add support to allow for proper name identifier generation for nodes, nodegraphs, nodegraph inputs, nodegraph outputs, shaders and materials when converting to MaterialX from glTF.

This is done as a pre-process as this allows for proper string based connections to be created during conversion. If done incrementally the target of the connection and the connection string will become mismatched.

Implementation

  • Add in a new method glTF_graph_create_names to perform the name generation as a pre-pass.
  • Note that this will handle converting invalid names to valid MaterialX ones and will also handle duplicates and empty string names by using the MaterialX createValidChildName() API from within the current parent scope.

Documentation

  • Updated notebook to demonstrate clearing and generating glTF names and conversion to MaterialX.

Tests

  • Added a new glTF test file to unit tests which has the properties mentioned: no name, empty name, duplicate names.
{
  "images": [
    {
      "uri": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4z/AfAAQAAf/zKSWvAAAAAElFTkSuQmCC",
      "name": "KHR_texture_procedural_fallback"
    }
  ],
  "textures": [
    {
      "source": 0
    }
  ],
  "extensions": {
    "KHR_texture_procedurals": {
      "procedurals": [
        {
          "name": "",
          "nodetype": "nodegraph",
          "type": "color3",
          "inputs": [
            {
              "name": "mynode1",
              "nodetype": "input",
              "type": "vector2",
              "value": [
                8.0,
                8.0
              ]
            },
            {
              "name": "mynode1",
              "nodetype": "input",
              "type": "color3",
              "value": [
                1.0,
                0.094118,
                0.031373
              ]
            },
            {
              "nodetype": "input",
              "type": "color3",
              "value": [
                0.035294,
                0.090196,
                0.878431
              ]
            }
          ],
          "outputs": [
            {
              "name": "",
              "nodetype": "output",
              "type": "color3",
              "node": 0
            }
          ],
          "nodes": [
            {
              "nodetype": "mix",
              "type": "color3",
              "inputs": [
                {
                  "name": "fg",
                  "nodetype": "input",
                  "type": "color3",
                  "input": 1
                },
                {
                  "name": "bg",
                  "nodetype": "input",
                  "type": "color3",
                  "input": 2
                },
                {
                  "name": "mix",
                  "nodetype": "input",
                  "type": "float",
                  "node": 5
                }
              ],
              "outputs": [
                {
                  "nodetype": "output",
                  "name": "out",
                  "type": "color3"
                }
              ]
            },
            {
              "nodetype": "dotproduct",
              "type": "float",
              "inputs": [
                {
                  "name": "in1",
                  "nodetype": "input",
                  "type": "vector2",
                  "node": 4
                },
                {
                  "name": "in2",
                  "nodetype": "input",
                  "type": "vector2",
                  "value": [
                    1.0,
                    1.0
                  ]
                }
              ],
              "outputs": [
                {
                  "nodetype": "output",
                  "name": "out",
                  "type": "float"
                }
              ]
            },
            {
              "nodetype": "multiply",
              "type": "vector2",
              "inputs": [
                {
                  "name": "in1",
                  "nodetype": "input",
                  "type": "vector2",
                  "node": 6
                },
                {
                  "name": "in2",
                  "nodetype": "input",
                  "type": "vector2",
                  "input": 0
                }
              ],
              "outputs": [
                {
                  "nodetype": "output",
                  "name": "out",
                  "type": "vector2"
                }
              ]
            },
            {
              "nodetype": "subtract",
              "type": "vector2",
              "inputs": [
                {
                  "name": "in1",
                  "nodetype": "input",
                  "type": "vector2",
                  "node": 2
                },
                {
                  "name": "in2",
                  "nodetype": "input",
                  "type": "vector2",
                  "value": [
                    0.0,
                    0.0
                  ]
                }
              ],
              "outputs": [
                {
                  "nodetype": "output",
                  "name": "out",
                  "type": "vector2"
                }
              ]
            },
            {
              "nodetype": "floor",
              "type": "vector2",
              "inputs": [
                {
                  "name": "in",
                  "nodetype": "input",
                  "type": "vector2",
                  "node": 3
                }
              ],
              "outputs": [
                {
                  "nodetype": "output",
                  "name": "out",
                  "type": "vector2"
                }
              ]
            },
            {
              "nodetype": "modulo",
              "type": "float",
              "inputs": [
                {
                  "name": "in1",
                  "nodetype": "input",
                  "type": "float",
                  "node": 1
                },
                {
                  "name": "in2",
                  "nodetype": "input",
                  "type": "float",
                  "value": 2.0
                }
              ],
              "outputs": [
                {
                  "nodetype": "output",
                  "name": "out",
                  "type": "float"
                }
              ]
            },
            {
              "nodetype": "texcoord",
              "type": "vector2",
              "inputs": [
                {
                  "name": "index",
                  "nodetype": "input",
                  "type": "integer",
                  "value": 0
                }
              ],
              "outputs": [
                {
                  "nodetype": "output",
                  "name": "out",
                  "type": "vector2"
                }
              ]
            }
          ]
        }
      ]
    }
  },
  "materials": [
    {
      "pbrMetallicRoughness": {
        "baseColorTexture": {
          "index": 0,
          "extensions": {
            "KHR_texture_procedurals": {
              "index": 0,
              "output": 0
            }
          }
        }
      }
    }
  ],
  "asset": {
    "version": "2.0",
    "generator": "MaterialX 1.39 / glTF 2.0 Texture Procedural Converter"
  },
  "extensionsUsed": [
    "KHR_texture_procedurals",
    "EXT_texture_procedurals_mx_1_39"
  ]
}

Result:

noname_checkerboard_graph_fromgltf

<?xml version="1.0"?>
<materialx version="1.39" colorspace="lin_rec709" doc="glTF version: 2.0. glTF generator: MaterialX 1.39 / glTF 2.0 Texture Procedural Converter. ">
  <nodegraph name="GRAPH_0">
    <input name="mynode1" type="vector2" value="8, 8" />
    <input name="mynode2" type="color3" value="1, 0.094118, 0.031373" />
    <input name="INPUT_0" type="color3" value="0.035294, 0.090196, 0.878431" />
    <mix name="NODE_0" type="color3">
      <input name="fg" type="color3" interfacename="mynode2" />
      <input name="bg" type="color3" interfacename="INPUT_0" />
      <input name="mix" type="float" nodename="NODE_5" />
    </mix>
    <dotproduct name="NODE_1" type="float">
      <input name="in1" type="vector2" nodename="NODE_4" />
      <input name="in2" type="vector2" value="1, 1" />
    </dotproduct>
    <multiply name="NODE_2" type="vector2">
      <input name="in1" type="vector2" nodename="NODE_6" />
      <input name="in2" type="vector2" interfacename="mynode1" />
    </multiply>
    <subtract name="NODE_3" type="vector2">
      <input name="in1" type="vector2" nodename="NODE_2" />
      <input name="in2" type="vector2" value="0, 0" />
    </subtract>
    <floor name="NODE_4" type="vector2">
      <input name="in" type="vector2" nodename="NODE_3" />
    </floor>
    <modulo name="NODE_5" type="float">
      <input name="in1" type="float" nodename="NODE_1" />
      <input name="in2" type="float" value="2" />
    </modulo>
    <texcoord name="NODE_6" type="vector2">
      <input name="index" type="integer" value="0" />
    </texcoord>
    <output name="OUTPUT_0" type="color3" nodename="NODE_0" />
  </nodegraph>
  <gltf_pbr name="SHADER_0" type="surfaceshader">
    <input name="base_color" type="color3" nodegraph="GRAPH_0" />
  </gltf_pbr>
  <surfacematerial name="MATERIAL_SHADER_0" type="material">
    <input name="surfaceshader" type="surfaceshader" nodename="SHADER_0" />
  </surfacematerial>
</materialx>

@kwokcb kwokcb merged commit 60aa099 into KhronosGroup:main Nov 14, 2024
1 check passed
@kwokcb kwokcb deleted the name_generation branch November 14, 2024 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant