-
Notifications
You must be signed in to change notification settings - Fork 2
How to use MaterialX shaders
Consider, as example, the following robot model.
This is model from the game. We will recreate from scratch the simple shader, which use textures for this model.
Before we start, some prepare steps. At first, switch shading model to OSL. It's because MaterialX shader nodes translated into osl-shaders and in fact Cycles render this osl-shader.
At second, you need the addon MaterialXSI. It contains all built-in MaterialX nodes.
Ok, we a redy to construct the shader tree. This tree should use only MaterialX nodes, no other are not allowed. It's bacause to convert the shader tree into osl-shader it use built-in MaterialX functionallity. So, it understand only what it understand, nothing more.
Start by adding Surfacematerial node. It's the root node, where the MaterialX shader is start. And connect it to the material port of the root material node. Does not delete DiffuseBSDF node. It will used for fallbacks, if something go wrong and MaterialX will be fail to compile osl-shader.
The shader may start either from this Surfacematerial node or from Lama Surface. But Lama Surface is a part of special Lama family of nodes. Skip it, for simplicity.
Next add Surface node, and connect it to the Surfaceshader port. This node geather different aspect of the surface shader: how the surface looks (Bsdf), how it illuminate the light (Edf) and transparency (Opacity).
Next define the main diffuse layer of the shader. Add Oren Nayar Diffuse Bsdf node and connect it to Bsdf port of the Surface node.
For now it at least works. Not interesting, but wait a time.
Next we should assign an image texture to this diffuse layer. Add Image Color3 node and select the albedo texture.
But before it works, we should define UV-coordinates. And here is a terminolofy problem. In MaterialX there is a node Textcoord Vector2, which get required uv-channel. But it works for non-osl shaders. In OSL u and v channels are not texture coordinates, it,s baricentric coordinates of each triangle. S, for OSL we need another approach. We can get uv-coordinates by using geometry bproperties. So, add Geompropvalue Vector3 node, and define property name geom:uv. We need vector3 version, instead of Vector2, because in Cycles uv-coordinates stored in this form.
Next convert it to Vector2 by using Convert Vector3 Vector2 node. Connect it to Texture Coordinates port of the Image Color3 node.
And now the texture is visible.