What are LayerShaders
Every LayerShader is a gdshader with the additions that connect it to the layer chain above and below.
A LayerShader is a normal gdshader with three additions that turn it from a regular shader to a shader that can connect to the layer below and the layer above it. See the Architecture Overview for where they fit.
LayerShaders are used in LayerMaterials. There are 4 types of LayerShaders, each one for a LayerMaterial type - SurfaceShader, MaskShader, OverlayShader and DataShader. See What are LayerMaterials.
The three parts that make it work:
-
An include at the top:
#include "res://addons/material_layers/shaders/layer_lib.gdshaderinc"- enables the layer variables and macros the rest of the system depends on. ALayerShadercan't work without it. -
A setup macro per function:
SETUP_*_VERTEX;as the first line ofvertex(),SETUP_*_FRAGMENTas the first line offragment(). This enables the layer variables used to output and reference layers.For example in a
SurfaceShader,SETUP_SURFACE_VERTEXat the top ofvertex(),SETUP_SURFACE_FRAGMENTat the top offragment(). -
LayerShader-type specific layer variables: EachLayerShadertype has its own set of layer variables. These are used to output material properties, read previous layers and blend layers together. See Layer Variables.LAYER_OUT_*,LAYER_BELOW_*,LAYER_BLEND_*,LAYER_BASE_*etc.
That's it. Nothing else about writing gdshader changes. You still write triplanar projections, sample textures, use vertex colors - whatever you want. Just make sure to use the correct layer variables when outputting and reading layers and material properties.
See the shader type pages for each type: SurfaceShader, MaskShader, OverlayShader, DataShader.