Simple Shading in USD
VERIFIED ON USD VERSION 19.05
This tutorial demonstrates how to create a simple textured material and bind it to geometry in USD. In it we:
- Make a Model asset
- Add some simple geometry to the Model
- Make a Material to contain shading data
- Add a surface shader to the Material
- Add texturing to the surface
We create the scene as a simple Model, adding geometry and a Material with a simple shading network into the model, then binding the geometry to the Material.
To fully illustrate these concepts, we walk through a Python script that performs these steps using the Python USD API, as well as showing the resulting text USDA outputs.
The scripts and data files exist in the USD distribution under
USD/extras/usd/tutorials/simpleShading. Run generate_simpleShading.py in that directory to generate all of the snippets for each step shown below.
Making a Model
The first thing we want to do is create a "container" that will hold both the geometry and the shading prims that we will create. We could have created Mesh and Material prims at root scope in the scene, but by putting them both under a common "model prim", we make it possible to reference this asset as a whole into other scenes. In a python shell, execute the following:
Adding a Mesh "Billboard"
In the interests of simplicity and brevity, we stick with a very simple piece of geometry, a quadrilateral "billboard", with a st texture coordinate that maps the corners of the quadrilateral to the unit square in uv texture space. Note that we do not create any normals for our Mesh; this is because by default meshes use catmull-clark subdivision (to change the subdivision scheme, use UsdGeom.Mesh.CreateSubdivisionSchemeAttr()) which provide analytically computed normals to a renderer.
Let's take a look at what we have so far. The last step above saved the current state of the stage into simpleShading.usd (USD files can be edited and saved multiple times). In another command shell, try:
We should see something like:
Make a Material
In USD, we "bind" geometry to Material prims in order to customize how the geometry should be shaded. A Material is a container for networks of shading prims; a Material can contain one network that defines the surface illuminance response, and another that defines surface displacement. It can also contain different networks for different renderers that don't share a common shading language. Complex models will define multiple materials, binding different geometry (Gprims) to different Materials.
In our model, we have only one gprim, and therefore need only a single Material - let us create it!
Add a UsdPreviewSurface
The most important shader in our Material will be the "surface" shader. We create it (and any other shaders in the network) as children of the material prim - this is what it means for the Material to be a container for the shading network, and is an important property to preserve. We set the surface's metallic and roughness properties to get familiar with setting shader input properties. Any input whose value we do not set will be filled in by the renderer with the fallback value defined in the shader specification. After creating the shader, we connect the material's surface output to the
UsdPreviewSurface's surface output - this is what identifies the source of the Material's surface shading.
The last step is to add a texture as the source for the surface's diffuseColor. Texturing requires two nodes in the shading network: a
UsdUVTexture node to read and map the texture, and a
UsdPrimvarReader (more precisely, a
UsdPrimvarReader_float2, the implementation that reads float2Array-valued primvars) to fetch a texture coordinate from each piece of geometry bound to the material, to inform the texture node how to map surface coordinates to texture coordinates.
Note that we have not yet specified what texture coordinate (primvar) the PrimvarReader should read. We could author the name of the primvar directly on its varname input. However, we instead demonstrate how we can connect shader inputs to "public interface attributes" on Materials. Any input in a shading network can connect to an input on its enclosing material, and the renderer will migrate the Material input's authored value (if any) to any shader input connected to it before rendering and shading begins. In effect, this gives us a way to "expose" inputs deep in a shading network for easy overriding by consumers of the Material, since all the inputs on a Material will be exposed and editable without needing to look inside the Material, which may contain many shading prims.
And lastly, bind the Mesh to our Material and save the results!
In usdview, we should now see something like: