Albedo Terrain Mix Shader

A four textures mixer shader for Albedo, inteeded for terrain texture splatting using a RGB mask texture, having each texture for each of the RGB channels and a fourth one for the absence of textures, the black pixels.

Check the tutorial if you are interested in how to use it.

Shader code
shader_type spatial;

uniform sampler2D source_texture_mask : source_color;
uniform sampler2D source_texture_black : source_color;
uniform sampler2D source_texture_red : source_color;
uniform sampler2D source_texture_green : source_color;
uniform sampler2D source_texture_blue : source_color;

uniform float uv_size : hint_range(0.01, 10.0, 0.01) = 1.0;

void fragment() {

vec2 UV_Scaled = UV * uv_size;

// texture_rgbmask UV is not scaled.
vec3 texture_rgbmask= texture(source_texture_mask, UV).rgb; 
vec3 texture_black 	= texture(source_texture_black, UV_Scaled).rgb;
vec3 texture_red 	= texture(source_texture_red, UV_Scaled).rgb;
vec3 texture_green 	= texture(source_texture_green, UV_Scaled).rgb;
vec3 texture_blue 	= texture(source_texture_blue, UV_Scaled).rgb;

float summed_texture_channels = (
	texture_rgbmask.r + 
	texture_rgbmask.g + 
	texture_rgbmask.b);

vec3 mixed_terrain = clamp(
		(	texture_rgbmask.r * texture_red +
			texture_rgbmask.g * texture_green +
			texture_rgbmask.b * texture_blue) /
			summed_texture_channels,
			vec3(0.0), vec3(1.0) // Clamp min, max values.
			);

ALBEDO = mix(mixed_terrain,texture_black,1.0 - summed_texture_channels);

} // Fragment end
Tags
mixing textures, rgb mask, splatting, terrain, terrain material, terrain shader
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from Nanotech Gamedev

3D HP Bar of 2 Blended Colors

Mech Gunfire Effect – Muzzleflash Shader

Mech Gunfire Effect – Smoke Shader

Related shaders

Advanced 7 Texture Albedo Terrain Shader

Triplanar Stochastic Terrain Shader

Ultimate Terrain Maker

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Unhinged Dev
1 year ago

thanks! i edited a bit so it uses more textures