autodesk-forks / MaterialX

MaterialX C++ and Python libraries
http://www.materialx.org/
Apache License 2.0
107 stars 23 forks source link

Update texture baking to support 1.38 material nodes #1013

Closed bernardkwok closed 4 years ago

bernardkwok commented 4 years ago

Update #460 Update #831 Update #920 Update #974

Sample before bake

<?xml version="1.0"?>
<materialx version="1.37">
   <standard_surface name="MappedShader" type="surfaceshader" >
      <input name="base" type="float" value="1" />
      <input name="base_color" type="color3" nodegraph="RedRamp" />
      <input name="metalness" type="float" value="1" />
      <input name="specular" type="float" value="0" />
      <input name="specular_color" type="color3" nodegraph="GreenRamp" />
      <input name="specular_roughness" type="float" nodegraph="UnitGraph" />
      <input name="coat" type="float" value="1" />
   </standard_surface>
   <nodegraph name="RedRamp">
      <ramp4 name="ramp4" type="color3" >
         <input name="valuetl" type="color3" value="1, 0, 0" />
         <input name="valuetr" type="color3" value="0, 0, 0" />
         <input name="valuebl" type="color3" value="1, 0, 0" />
         <input name="valuebr" type="color3" value="0, 0, 0" />
      </ramp4>
      <output name="out" type="color3" nodename="ramp4" />
   </nodegraph>
   <nodegraph name="GreenRamp">
      <ramp4 name="ramp4" type="color3" >
         <input name="valuetl" type="color3" value="0, 1, 0" />
         <input name="valuetr" type="color3" value="0, 0, 0" />
         <input name="valuebl" type="color3" value="0, 1, 0" />
         <input name="valuebr" type="color3" value="0, 0, 0" />
      </ramp4>
      <output name="out" type="color3" nodename="ramp4" />
   </nodegraph>
   <nodegraph name="UnitGraph">
      <constant name="realscale" type="float">
         <parameter name="value" type="float" value="100" unit="centimeter" unittype="distance" />
      </constant>
      <output name="out" type="float" nodename="realscale" />
   </nodegraph>
   <surfacematerial name="MappedShaderMaterial" type="material" >
      <input name="surfaceshader" type="surfaceshader" nodename="MappedShader" />
      <input name="displacementshader" type="displacementshader" value="" />
   </surfacematerial>
</materialx>

Sample after bake

<?xml version="1.0"?>
<materialx version="1.38">
  <nodegraph name="NG_baked" colorspace="srgb_texture">
    <image name="base_color_baked" type="color3">
      <input name="file" type="filename" uniform="true" value="RedRamp_out_MappedShader_baked.png" />
    </image>
    <output name="base_color_output" type="color3" nodename="base_color_baked" />
    <image name="specular_color_baked" type="color3">
      <input name="file" type="filename" uniform="true" value="GreenRamp_out_MappedShader_baked.png" />
    </image>
    <output name="specular_color_output" type="color3" nodename="specular_color_baked" />
    <image name="specular_roughness_baked" type="float">
      <input name="file" type="filename" uniform="true" value="UnitGraph_out_MappedShader_baked.png" />
    </image>
    <output name="specular_roughness_output" type="float" nodename="specular_roughness_baked" />
  </nodegraph>
  <standard_surface name="MappedShader_baked" type="surfaceshader">
    <input name="base" type="float" value="1" />
    <input name="base_color" type="color3" output="base_color_output" nodegraph="NG_baked" />
    <input name="metalness" type="float" value="1" />
    <input name="specular" type="float" value="0" />
    <input name="specular_color" type="color3" output="specular_color_output" nodegraph="NG_baked" />
    <input name="specular_roughness" type="float" output="specular_roughness_output" nodegraph="NG_baked" />
    <input name="coat" type="float" value="1" />
  </standard_surface>
  <surfacematerial name="MappedShaderMaterial_baked" type="material">
    <input name="surfaceshader" type="surfaceshader" nodename="MappedShader_baked" />
  </surfacematerial>
</materialx>

Baked Images: Note that the baked images are sampling upstream graphs after flattening is performed. image

* Original render image

* Baked render image

Normal map example

<?xml version="1.0"?>
<materialx version="1.37">
   <standard_surface name="NormalMappedShader" type="surfaceshader">
      <input name="base" type="float" value="0.6" />
      <input name="metalness" type="float" value="1.0" />
      <input name="specular" type="float" value="0.7" />
      <input name="coat" type="float" value="1" />
      <input name="normal" type="vector3" nodegraph="NormalMapGraph" />
   </standard_surface>
   <surfacematerial name="NormalMappedShaderMaterial" type="material" >
      <input name="surfaceshader" type="surfaceshader" nodename="NormalMappedShader" />
      <input name="displacementshader" type="displacementshader" value="" />
   </surfacematerial>
   <nodegraph name="NormalMapGraph">
      <!--- scale and space are perserved -->
      <normalmap name="normalmap" type="vector3" >
         <input name="in" type="vector3" nodename="tiledimage" />
         <input name="scale" type="float" value="1.1" />
         <input name="space" type="string" value="tangent"/>
      </normalmap>
      <tiledimage name="tiledimage" type="vector3" >
         <parameter name="file" type="filename" uniform="true" value="resources/images/mesh_wire_norm.png" />
         <input name="realworldimagesize" type="vector2" value="1.0, 2.0" unit="centimeter" unittype="distance"/>
         <input name="realworldtilesize" type="vector2" value="3.0, 2.0" unit="centimeter" unittype="distance"/>
         <input name="uvtiling" type="vector2" value="12, 10" />
      </tiledimage>
      <output name="normal" type="vector3" nodename="normalmap" />
   </nodegraph>
</materialx>

*Flattend normalmap graph**

<?xml version="1.0"?>
<materialx version="1.38">
  <nodegraph name="NG_baked" colorspace="srgb_texture">
    <image name="normal_baked" type="vector3">
      <input name="file" type="filename" uniform="true" value="NormalMapGraph_normal_NormalMappedShader_baked.png" />
    </image>
    <normalmap name="normal_baked_map" type="vector3">
      <input name="in" type="vector3" nodename="normal_baked" />
      <input name="scale" type="float" value="1.1" />
      <input name="space" type="string" value="tangent" />
    </normalmap>
    <output name="normal_output" type="vector3" nodename="normal_baked_map" />
  </nodegraph>
  <standard_surface name="NormalMappedShader_baked" type="surfaceshader">
    <input name="base" type="float" value="0.6" />
    <input name="metalness" type="float" value="1.0" />
    <input name="specular" type="float" value="0.7" />
    <input name="coat" type="float" value="1" />
    <input name="normal" type="vector3" output="normal_output" nodegraph="NG_baked" />
    <input name="base_color" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Base Color" uifolder="Base" doc="Color of the diffuse reflection." />
    <input name="diffuse_roughness" type="float" value="0" uimin="0.0" uimax="1.0" uiname="Diffuse Roughness" uifolder="Base" uiadvanced="true" doc="Roughness of the diffuse reflection. Higher values cause the surface to appear flatter and darker." />
    <input name="specular_color" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Specular Color" uifolder="Specular" doc="Color tint on the specular reflection." />
    <input name="specular_roughness" type="float" value="0.2" uimin="0.0" uimax="1.0" uiname="Specular Roughness" uifolder="Specular" doc="The roughness of the specular reflection. Lower numbers produce sharper reflections, higher numbers produce blurrier reflections." />
    <input name="specular_IOR" type="float" value="1.5" uimin="0.0" uisoftmax="3.0" uiname="Index of Refraction" uifolder="Specular" doc="Index of refraction for specular reflection." />
    <input name="specular_anisotropy" type="float" value="0" uimin="0.0" uimax="1.0" uiname="Specular Anisotropy" uifolder="Specular" uiadvanced="true" doc="The directional bias of reflected and transmitted light resulting in materials appearing rougher or glossier in certain directions." />
    <input name="specular_rotation" type="float" value="0" uimin="0.0" uimax="1.0" uiname="Specular Rotation" uifolder="Specular" uiadvanced="true" doc="Rotation of the axis of specular anisotropy around the surface normal." />
    <input name="transmission" type="float" value="0" uimin="0.0" uimax="1.0" uiname="Transmission" uifolder="Transmission" uiadvanced="true" doc="Transmission of light through the surface for materials such as glass or water. The greater the value the more transparent the material." />
    <input name="transmission_color" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Transmission Color" uifolder="Transmission" uiadvanced="true" doc="Color tint on the transmitted light." />
    <input name="transmission_depth" type="float" value="0" uimin="0.0" uisoftmax="100.0" uiname="Transmission Depth" uifolder="Transmission" uiadvanced="true" doc="Specifies the distance light travels inside the material before its becomes exactly the transmission color according to Beer's law." />
    <input name="transmission_scatter" type="color3" value="0, 0, 0" uimin="0,0,0" uimax="1,1,1" uiname="Transmission Scatter" uifolder="Transmission" uiadvanced="true" doc="Scattering coefficient of the interior medium. Suitable for a large body of liquid or one that is fairly thick, such as an ocean, honey, ice, or frosted glass." />
    <input name="transmission_scatter_anisotropy" type="float" value="0" uimin="0.0" uimax="1.0" uiname="Transmission Anisotropy" uifolder="Transmission" uiadvanced="true" doc="The amount of directional bias, or anisotropy, of the scattering." />
    <input name="transmission_dispersion" type="float" value="0" uimin="0.0" uisoftmax="100.0" uiname="Transmission Dispersion" uifolder="Transmission" uiadvanced="true" doc="Dispersion amount, describing how much the index of refraction varies across wavelengths." />
    <input name="transmission_extra_roughness" type="float" value="0" uimin="-1.0" uisoftmin="0.0" uimax="1.0" uiname="Transmission Roughness" uifolder="Transmission" uiadvanced="true" doc="Additional roughness on top of specular roughness. Positive values blur refractions more than reflections, and negative values blur refractions less." />
    <input name="subsurface" type="float" value="0" uimin="0.0" uimax="1.0" uiname="Subsurface" uifolder="Subsurface" uiadvanced="true" doc="The blend between diffuse reflection and subsurface scattering. A value of 1.0 indicates full subsurface scattering and a value 0 for diffuse reflection only." />
    <input name="subsurface_color" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Subsurface Color" uifolder="Subsurface" uiadvanced="true" doc="The color of the subsurface scattering effect." />
    <input name="subsurface_radius" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Subsurface Radius" uifolder="Subsurface" uiadvanced="true" doc="The mean free path. The distance which light can travel before being scattered inside the surface." />
    <input name="subsurface_scale" type="float" value="1" uimin="0.0" uisoftmax="10.0" uiname="Subsurface Scale" uifolder="Subsurface" uiadvanced="true" doc="Scalar weight for the subsurface radius value." />
    <input name="subsurface_anisotropy" type="float" value="0" uimin="0.0" uimax="1.0" uiname="Subsurface Anisotropy" uifolder="Subsurface" uiadvanced="true" doc="The direction of subsurface scattering. 0 scatters light evenly, positive values scatter forward and negative values scatter backward." />
    <input name="sheen" type="float" value="0" uimin="0.0" uimax="1.0" uiname="Sheen" uifolder="Sheen" uiadvanced="true" doc="The weight of a sheen layer that can be used to approximate microfibers or fabrics such as velvet and satin." />
    <input name="sheen_color" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Sheen Color" uifolder="Sheen" uiadvanced="true" doc="The color of the sheen layer." />
    <input name="sheen_roughness" type="float" value="0.3" uimin="0.0" uimax="1.0" uiname="Sheen Roughness" uifolder="Sheen" uiadvanced="true" doc="The roughness of the sheen layer." />
    <input name="coat_color" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Coat Color" uifolder="Coat" doc="The color of the clear-coat layer's transparency." />
    <input name="coat_roughness" type="float" value="0.1" uimin="0.0" uimax="1.0" uiname="Coat Roughness" uifolder="Coat" doc="The roughness of the clear-coat reflections. The lower the value, the sharper the reflection." />
    <input name="coat_anisotropy" type="float" value="0.0" uimin="0.0" uimax="1.0" uiname="Coat Anisotropy" uifolder="Coat" uiadvanced="true" doc="The amount of directional bias, or anisotropy, of the clear-coat layer." />
    <input name="coat_rotation" type="float" value="0.0" uimin="0.0" uimax="1.0" uiname="Coat Rotation" uifolder="Coat" uiadvanced="true" doc="The rotation of the anisotropic effect of the clear-coat layer." />
    <input name="coat_IOR" type="float" value="1.5" uimin="0.0" uisoftmax="3.0" uiname="Coat Index of Refraction" uifolder="Coat" doc="The index of refraction of the clear-coat layer." />
    <input name="coat_affect_color" type="float" value="0" uimin="0" uimax="1" uiname="Coat Affect Color" uifolder="Coat" uiadvanced="true" doc="Controls the saturation of diffuse reflection and subsurface scattering below the clear-coat." />
    <input name="coat_affect_roughness" type="float" value="0" uimin="0" uimax="1" uiname="Coat Affect Roughness" uifolder="Coat" uiadvanced="true" doc="Controls the roughness of the specular reflection in the layers below the clear-coat." />
    <input name="thin_film_thickness" type="float" value="0" uimin="0.0" uisoftmax="2000.0" uiname="Thin Film Thickness" uifolder="Thin Film" uiadvanced="true" doc="The thickness of the thin film layer on a surface. Use for materials such as multitone car paint or soap bubbles." />
    <input name="thin_film_IOR" type="float" value="1.5" uimin="0.0" uisoftmax="3.0" uiname="Thin Film Index of Refraction" uifolder="Thin Film" uiadvanced="true" doc="The index of refraction of the medium surrounding the material." />
    <input name="emission" type="float" value="0" uimin="0.0" uisoftmax="1.0" uiname="Emission" uifolder="Emission" doc="The amount of emitted incandescent light." />
    <input name="emission_color" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Emission Color" uifolder="Emission" doc="The color of the emitted light." />
    <input name="opacity" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Opacity" uifolder="Geometry" doc="The opacity of the entire material." />
    <input name="thin_walled" type="boolean" value="false" uiname="Thin Walled" uifolder="Geometry" uiadvanced="true" doc="If true the surface is double-sided and represents an infinitely thin shell. Suiteable for thin objects such as tree leafs or paper" />
  </standard_surface>
  <surfacematerial name="NormalMappedShaderMaterial_baked" type="material">
    <input name="surfaceshader" type="surfaceshader" nodename="NormalMappedShader_baked" />
  </surfacematerial>
</materialx>

Unbaked normalmap render image

Baked normalmap render image

Baked texture, and original image