Material API
What is the Material API
The Material API is a collection of helper methods & texture inputs to describe the surface of your material. The main purpose of the Material API is to prepare your data and pass it to a ShadingModel.
How does it work?
The Material API is automatically used when you add the common/pixel.hlsl
include to your pixel shader.
Texture Inputs
Material API handles creating & processing texture inputs for everything that PBR shading model needs. When you're including pixel.hlsl
, Material API will automatically populate input slots for most common texture maps by default. It will also create variables for tint color and self-illumination intensity. They will show up in Material Editor when you're creating a new material using your new shader.
If you want to access any of these input maps from material specifically, here are the variable names:
Input Slot | Type | Note |
---|---|---|
Albedo | float3 | |
Metalness | float | |
Roughness | float | |
Emission | float3 | |
Normal | float3 | |
TintMask | float | |
AmbientOcclusion | float | |
Transmission | float3 | does nothing |
Opacity | float | Requires translucency, see custom material inputs section |
Also, if you're not using custom material inputs, you'll be able to access these variables:
Default Variable | Type |
---|---|
g_flTintColor | float3 |
g_flSelfIllumScale | float |
Creating & processing a material
This is an automated variant to handle the material. If you're just starting making shaders, this may be a better option for you. Before you build new material object and pass it into shading model, you need to initialize it inside the MainPs
block first. To do it, simply create a new variable like this, using Material::From( i )
.
After this, it will automatically build a new material object, as well as sample & store into corresponding slots your albedo, normal, and RMA maps. (Roughness + Metalness + AO)
If you have any other texture maps you want to include, you'd need to fill them yourself. For example, if you need a proper emission map for your shader, you'd have to do this:
You can edit any other texture map in material object even after using ::From( i )
the same way. To see all input slots material has, please check out the list above.
Once you finish configuring your material, it is ready to be passed into the shading model and then return the result. Materials are necessary for shading models, as it makes handling big piles of textures much easier.
Custom Material Inputs
Sometimes you don't need automatically generated texture inputs, especially if your shader relies on lots of custom data that is not represented by any of the available PBR input maps. Exactly for these cases, material API allows "disabling" automatic setup and let the shader programmer do it manually.
To disable default texture inputs, add this definition: #define CUSTOM_MATERIAL_INPUTS
in COMMON section, or right before #include "common/pixel.hlsl"
in pixel shader block.
To initialize a material, you'll need to create a new material object using Material::Init()
. Example: Material m = Material::Init()
.
After this, create new texture inputs and Texture2D objects for whatever you need the same way as you'd do it anywhere else:
And this is how they are going to be manually processed in pixel shader itself:
Please pay attention to the way we're storing normal map into the corresponding material slot. Before storing, we are decoding, then transforming this normal map from tangent space to world space. When custom inputs are disabled, material API will do it automatically, but when we are handling all textures by hand, this must be done manually, too.
Transparency
This isn't very related to material API, however if you're curious why Opacity
input doesn't work with the example from above, it's because you need to directly change the render state in your code so shader knows it must be rendering transparency.
Above MainPs
functiom, but below #include "common/pixel.hlsl"
, add RenderState( translucent, true )
and RenderState( BlendEnable, true )
. After that, opacity will start affecting your model.
If you need proper pixel sorting so layered transparent textures are rendering as intended, add RenderState( AlphaToCoverageEnable, true )
.
Lerping
Material API also has a little useful method if you want to blend two texture sets together with a given blend amount. It's called Material::Lerp( Material a, Material b, float amount )
.
amount
is a blend amount value: it can be anything in [0..1] range, (any value going lower or beyond this range will create funky visual errors). It can be a fixed number, or calculated weight of two blend masks - this is defined by user so you can put in there anything you need.