Texture Masking

What it does

This shader maps a texture into other texture/sprite depending on position within the target texture, its size and the zoom pretended.


hidden_texture – The texture to be revealed

position – position of our object within the given hidden_texture

zoom – basically how modified the size of the mask’s texture is (could be used to simulate a magnifying lense, for example). If you want a one per one representation of the hidden_texture make this param the scale/size of the corresponsing texture/sprite

size – how modified do we want the target hidden_texture to be. As a point of comparison, use the scale of a sprite with the hidden_texture assigned to it

Use Cases

– Revealing only part of an image (https://reallyokeyfruit.itch.io/bye-bye-birdy)

– Simulating Magnifying glass

How it works

It maps each UV coordinate of the main texture taking into account all parameters, using the following equation: (position.x + (UV.x - 0.5) * zoom * float(textureSize(TEXTURE, 0).x))) / (float(textureSize(hidden_texture, 0).x) * size) + 0.5;

Then it changes that UV coordinate’s color, if the alpha in that specific coordinate is bigger then 0, to the corresponding color in the hidden_texture

Godot Version: 4.1

Shader code
shader_type canvas_item;

uniform sampler2D hidden_texture;
uniform vec2 position = vec2(0, 0);
uniform float zoom = 1.0;
uniform float size = 10.0;

void fragment() {
	vec4 main_color = texture(TEXTURE, UV);
	float UV_target_x = 
		(position.x + 
		((UV.x - 0.5) * zoom * float(textureSize(TEXTURE, 0).x)))
		/ (float(textureSize(hidden_texture, 0).x) * size)
		+ 0.5;
	float UV_target_y = 
		(position.y + 
		((UV.y - 0.5) * zoom * float(textureSize(TEXTURE, 0).y)))
		/ (float(textureSize(hidden_texture, 0).y) * size)
		+ 0.5;
	if (main_color.a != 0.0) {
		COLOR.rgba = texture(hidden_texture, vec2(UV_target_x, UV_target_y)).rgba;
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 ReallyOkeyFruit

Rotating Quadrant

Topdown windy plants

Related shaders

Texture population using Texture

masking shader

slice texture

Notify of

Inline Feedbacks
View all comments