S&box Wiki

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 ).

Material m = 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:

PS { // ... CreateInputTexture2D( Emission, Srgb, 8, "", "_em", "Material,10/60", Default3( 0.5, 0.5, 0.5 ) ); Texture2D g_tEmission < Channel( RGB, Box( Emission ), Srgb ); OutputFormat( BC7 ); SrgbRead( true ); >; // ... float4 MainPs( PixelInput i ) : SV_Target0 { // ... Material m = Material::From( i ); m.Emission = g_tEmission.Sample( g_sAniso, i.vTextureCoords.xy ) * g_flSelfIllumScale; // By default, Material API includes a float variable that we can use for changing emission intensity right from the material settings :-) // ... } }

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.

// Always included at the end // ::Shade will return a final, shaded pixel using all textures we've included in material object. return ShadingModelStandard::Shade( i, m );
image.png

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().

PS { // Do not generate texture inputs, let the developer do it themselves #define CUSTOM_MATERIAL_INPUTS #include "common/pixel.hlsl" // <...>

After this, create new texture inputs and Texture2D objects for whatever you need the same way as you'd do it anywhere else:

PS { #define CUSTOM_MATERIAL_INPUTS #include "common/pixel.hlsl" // Albedo map CreateInputTexture2D( Color, Srgb, 8, "", "_color", "Material,10/10", Default3( 1.0, 1.0, 1.0 ) ); Texture2D g_tColor < Channel( RGB, Box( Color ), Srgb ); OutputFormat( BC7 ); SrgbRead( true ); >; // Amazing custom grayscale texture map CreateInputTexture2D( Custom, Linear, 8, "", "_noise", "Material,10/20", Default( 1.0 ) ); // Normal map CreateInputTexture2D( Normal, Linear, 8, "NormalizeNormals", "_normal", "Material,10/20", Default3( 0.5, 0.5, 1.0 ) ); // Normal map has unused alpha channel, lets use it for our custom custom texture so we don't generate redundant compiled textures. Both maps don't need sRGB anyway. Texture2D g_tNormal < Channel( RGB, Box( Normal ), Linear ); Channel( A, Box( Custom ), Linear ); OutputFormat( DXT5 ); SrgbRead( false ); >; // RMA (Roughness, Metalness, AO ) CreateInputTexture2D( Roughness, Linear, 8, "", "_rough", "Material,10/40", Default( 1 ) ); CreateInputTexture2D( Metalness, Linear, 8, "", "_metal", "Material,10/50", Default( 1 ) ); CreateInputTexture2D( AmbientOcclusion, Linear, 8, "", "_rough", "Material,10/60", Default( 1 ) ); Texture2D g_tRMA < Channel( R, Box( Roughness ), Linear ); Channel( G, Box( Metalness ), Linear ); Channel( B, Box( AmbientOcclusion ), Linear ); SrgbRead( false ); OutputFormat( BC7 ); >; // <...>

And this is how they are going to be manually processed in pixel shader itself:

// <...> // Main function for Pixel Shader MainPs( PixelInput i ) : SV_Target0 { // Create a new empty material object Material m = Material::Init(); // Filling out the textures float3 rma = g_tRMA.Sample( g_sAniso, i.vTextureCoords.xy ).rgb; float4 normal = g_tNormal.Sample( g_sAniso, i.vTextureCoords.xy ).rgba; // Lets multiply albedo by noise map that we stored in normal map alpha channel m.Albedo = g_tColor.Sample( g_sAniso, i.vTextureCoords.xy ) * normal.a; // !!! Custom material inputs mean that we must manually transform the normal map from tangent space to world space: m.Normal = TransformNormal( DecodeNormal( normal.rgb ), i.vNormalWs, i.vTangentVWs, i.vTangentUWs ); m.Roughness = rma.r; m.Metalness = rma.g; m.AmbientOcclusion = rma.b; // Shade the pixel & return it the same as anywhere else return ShadingModelStandard::Shade( i, m ); } }

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.