S&box Wiki
Home
/
Edit Material API
View
Edit
History
No Category
Developer Overview
The Project System
Publishing To Asset Party
Getting Started With Hammer
Mapping Basics
Mapping Entities
Advanced Mapping Techniques
Getting Started with Modeldoc
Animgraph & Animation
Physics
Modeldoc Nodes
Advanced Modelling
UI Basics
Styles & Stylesheets
Razor Templates
Game Menus
Materials
Built In Shaders
Shaders
Shader Reference
Sounds & Audio
Particles
Getting Started
Making Games
Input
Networking
Physics
Rendering
Editor & Tools
VR
Misc
Playing Guides
Console Commands & Variables
Dedicated Server
Log in to edit
Material API
<cat>Material.ShaderBasic</cat> <title>Material API</title> # 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 <page>ShadingModel</page>. # 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 ); ``` <upload src="653cb/8daa5101c74c705.png" size="77533" name="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.
S&box Wiki
Development
Developer Overview
6
Editor Overview
General FAQ
System Requirements
The s&box wiki
Troubleshooting
Useful Links
The Project System
4
Adding Assets
Creating a Game Project
Project Settings Window - Games
Project Types
Publishing To Asset Party
2
Uploading assets
Uploading projects
Hammer
Getting Started With Hammer
3
Getting Started With Hammer
Making Your First Map
Mapping Resources
Mapping Basics
7
Cordons
Hotspot Materials
Selection Sets
Standard Mapping Dimensions
Tool Materials
Tools Visualisation Modes
Using Entities That Require a Mesh
Mapping Entities
2
Creating a Door
Light Entities
Advanced Mapping Techniques
8
Collaborating With Prefabs and Git
Instances
Prefabs
Quixel Bridge Plugin
Tilesets
Tilesets-Advanced
Tilesets-Proxies
VIS Optimizations
Models & Animation
Getting Started with Modeldoc
7
Automatic Model Setup
Breakpieces
Creating a Model
Guide to Models
Importing Rust Weapons
LODs
ModelDoc FAQ & best practices
Animgraph & Animation
4
Animations without Animgraph
AnimEvents, AnimGraph Tags, Attachments
Animgraph
Delta Animations
Physics
3
Cloth Physics
Collisions, Physics & Surface Types
Jiggle Bones
Modeldoc Nodes
1
Custom ModelDoc nodes
Advanced Modelling
6
Bodygroups
Citizen
First Person
IKChains and Stride Retargeting
Morphs
Vertex Normals
User Interface
UI Basics
6
Custom Fonts
Embedding Websites
Enabling Pointer Events
Events and Input
UI Basics
UI with Components
Styles & Stylesheets
1
Video Backgrounds
Razor Templates
4
A Razor Overview
Aliases and SetProperty Attributes
Generic Components
Templates
Game Menus
1
Making a Custom Pause Screen
Materials & Shaders
Materials
5
Guide to Materials
Material Attributes
Material Resources
Texture Settings
Using Dynamic Expressions
Built In Shaders
2
Foliage Shader
Glass Shader
Shaders
4
Compute Shaders
Constant Buffers
Material API
Shading Model
Shader Reference
5
Anatomy of Shader Files
Getting rid of Tex2D macros
Shader Reference
Shader States
Texture Format Cheat-Sheet
Other Assets
Sounds & Audio
4
Guide to Sounds
Sound Events
Sound Occlusion
Soundscapes
Particles
5
Creating animated sprites
Creating your first particle effect
Understanding Particle Editor
Using custom sprites
Using particle systems from C#
Coding
Getting Started
5
Cheat Sheet
Learning Resources
Setting up Rider
Setting up Visual Studio
Setting up Visual Studio Code
Making Games
2
Components
GameObjects
Input
4
Commands
ConVars
Input System
Speech Recognition
Networking
7
Auth Tokens
Http Requests
Lobby System
Networked Types
Networking Basics
RPCs
WebSockets
Physics
5
Collisions
Hitboxes
Joints
Traces
Triggers
Rendering
3
Render Tags
RenderHooks
Scenes
Editor & Tools
5
Guide to Widgets
Hammer API
Hammer Gizmos
Hotload Performance
Widget Docking
VR
3
Getting Started
VR Input
VR Overlays
Misc
10
Asset Types
Attributes and Component Properties
Backend API
Code Accesslist
CPU Performance Profiling
DisplayInfo
package/find
Setting Up A Navigation Mesh
Threaded Tasks
TypeLibrary
Playing
Playing Guides
3
Default Keybinds
Proton
s&box on macOS (Experimental)
Console Commands & Variables
1
Launch Arguments
Dedicated Server
1
Dedicated Servers