Tiled texture plus offset texture overlay

I made this to explain how to offset and overlay a second texture over another at someone’s request.

Shader code
shader_type canvas_item;

uniform sampler2D white_texture: source_color, hint_default_white;
uniform float white_tile_frequency = 2.0;

uniform sampler2D red_texture: source_color, hint_default_black;
uniform vec2 red_begin = vec2(0.25, 0.25);
uniform vec2 red_end = vec2(0.75, 0.75);

void fragment() {
	vec2 white_uv_tiled = mod(UV * white_tile_frequency, 1.0);
	vec2 red_range = red_end - red_begin;
	vec2 red_uv = (UV - red_begin) / red_range;
	vec4 red_texture_color = texture(red_texture, red_uv);
	
	// rather than using an if statement, do some convoluted math with step
	// red_bound is 1.0 when inside the red's bounding rectangle, and 0.0 outside
	vec2 red_bound_top = step(1.0, red_uv);
	vec2 red_bound_lower = 1.0 - step(0.0, red_uv);
	float red_bound = clamp(1.0-(red_bound_top.x + red_bound_top.y + red_bound_lower.x + red_bound_lower.y), 0.0, 1.0);
	
	COLOR = mix(texture(white_texture, white_uv_tiled), red_texture_color, red_texture_color.a * red_bound);
}
Live Preview
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 XenithStar

Related shaders

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments