A LayeredMaterial blends up to four PBR layers, such as stone, dirt, and moss, so floors, walls, and terrain don’t look like one repeating texture. It is lit exactly like a PBRMaterial.
Pipeline
Add a LayeredMaterialPipelineProvider and render with a PBREnvironment.
val modelBatch =
ModelBatch(device).apply {
addPipelineProvider(LayeredMaterialPipelineProvider())
colorFormat = graphics.preferredFormat
depthFormat = depthTarget.format
}
Layers
Each Layer takes an optional base color, normal, and occlusion-roughness-metallic texture. Without a base color texture it is its tint. tiling is repeats per world unit, or per UV unit with Mapping.UV.
val stone =
LayeredMaterial.Layer(
baseColorTexture = stoneColor,
normalTexture = stoneNormal,
occlusionRoughnessMetallicTexture = stoneOrm,
tiling = 0.25f,
antiTiling = true,
)
val puddle = LayeredMaterial.Layer(tint = Color(0.03f, 0.035f, 0.04f, 1f), roughness = 0.04f)
Every var, including those of each layer, can be changed at any time. Changing a val, such as a texture or the mapping, needs a new material.
Weights
The weight of each layer comes from the RGBA channels of a controlTexture, red being the first layer.
val floorMaterial =
LayeredMaterial(
device,
layers = listOf(stone, dirt, moss, puddle),
controlTexture = controlTexture,
controlMapping = LayeredMaterial.ControlMapping.WORLD_XZ,
mapping = LayeredMaterial.Mapping.WORLD_XZ,
)
floorMaterial.controlSize.set(24f, 24f)
floorMaterial.controlOffset.set(-12f, -12f)
ControlMapping.UV and UV2 read the first or second UV set instead. Without a control texture the weights come from the vertex colors.
val wallMaterial = LayeredMaterial(device, layers = listOf(bricks, moss))
Height Blending
A layer’s height makes it cover the layers around it with natural edges, such as moss filling the cracks between stones. heightSource reads it from the base color alpha or luminance, or the ORM alpha. heightOffset raises a whole layer.
stone.heightSource = LayeredMaterial.HeightSource.ORM_ALPHA
moss.heightOffset = 0.15f
floorMaterial.heightBlendSharpness = 0.8f // 0 soft crossfade, 1 hard edges
blendWeights blends weights the same way on the CPU, such as to find the layer under a character for footstep sounds.
Mapping
mapping sets how the layer textures are projected.
Mapping.UVuses the first UV set, the default.Mapping.WORLD_XZprojects from above in world space, for floors spanning many meshes.Mapping.TRIPLANARprojects along each axis and blends by the normal, so walls and slopes don’t stretch.triplanarSharpnessshortens the transitions.
val wallMaterial =
LayeredMaterial(
device,
layers = listOf(bricks, moss),
weightSource = LayeredMaterial.WeightSource.VERTEX_COLOR,
mapping = LayeredMaterial.Mapping.TRIPLANAR,
)
Hiding Repetition
antiTiling samples a layer twice with random offsets across the surface, scaled by antiTilingStrength. macroVariation adds a low frequency brightness change over every layer, at macroVariationScale; 0 disables it.
stone.antiTilingStrength = 0.5f
floorMaterial.macroVariation = 0.15f
floorMaterial.macroVariationScale = 0.08f
Texture Limit
Only the textures that are set are bound: up to three per layer plus the control texture. The PBREnvironment binds up to five more: shadows 1, image based lighting 3, and spot shadows 1. Together they must fit maxSampledTexturesPerShaderStage, 16 by default, or an error is thrown when its pipeline is created. Drop a layer’s ORM texture to make room.
Example
See the LayeredMaterialExample (-layeredMaterial) for a dungeon floor blending four layers from a world space control texture, and walls blending bricks and moss from vertex colors with triplanar mapping.