This project is a small example on how to do a FMaterialShader in Unreal Engine.
It also shows how to hook into the Nanite visibility buffer to replace shading bins with custom ones.
A quick overview of this plugin is available here: https://victorcareil.com/making-material-shaders
All the relevant C++ code is here: https://github.com/Phyronnaz/MaterialShaderExample/tree/master/Plugins/MaterialShaderPlugin/Source/MaterialShaderPlugin/Private
The only shader in this project is here: https://github.com/Phyronnaz/MaterialShaderExample/blob/master/Plugins/MaterialShaderPlugin/Shaders/MaterialShaderExample.usf
This example plugin adds a new actor class, MaterialShaderExampleActor. There should only be one placed per level.
This actor has a few settings:
-
MaterialToReplace: this is the material to replace in the Nanite visibility buffer -
MaterialSelector: this is the material handling material selection. It will be called by our custom material shader. The BaseColor.r output will be used as index into theNewMaterialsarray. -
NewMaterials: the materials to replaceMaterialToReplacewith, selected byMaterialSelector
The floor material here is replaced by red & blue materials:
Red and blue are two fully different materials:
The material selector is using noise to pick which material to render:
The actor settings, note the material indices: