Skip to content

Instantly share code, notes, and snippets.

@jimmyjonezz
Last active April 6, 2020 17:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmyjonezz/7617162f38dd495f3ee6f73780f4d6db to your computer and use it in GitHub Desktop.
Save jimmyjonezz/7617162f38dd495f3ee6f73780f4d6db to your computer and use it in GitHub Desktop.
Alpha mask on shader for Godot
shader_type canvas_item;
uniform sampler2D base_mask: texture;
uniform vec2 size;
uniform vec2 scale;
uniform vec2 position;
uniform vec2 region;
vec2 uv(vec2 uv) {
vec2 s = vec2(size.x / scale.x, size.x / scale.y);
vec2 p = vec2(position.x / (size.x / s.x), position.y / (size.y / s.y));
return vec2(uv.x * s.x - p.x, uv.y * s.y - p.y);
}
void fragment() {
vec4 base = texture(TEXTURE, UV).rgba;
base.a = texture(base_mask, uv(UV)).a;
COLOR = base;
}
@cenullum
Copy link

I needed a shrinked masked image however when I did this UV seems weird so I had to cut. Btw you don't use region uniform
aaaaa
ggg


uniform sampler2D base_mask: texture;
uniform vec2 size;
uniform vec2 scale;
uniform vec2 position;


vec2 uv(vec2 uv) {
    vec2 s = vec2(size.x / scale.x, size.x / scale.y);
    vec2 p = vec2(position.x / (size.x / s.x), position.y / (size.y / s.y));
	vec2 result = vec2(uv.x * s.x - p.x, uv.y * s.y - p.y);
	if (result.x>=1.0 ||result.x<=0.0||result.y<=0.0||result.y>=1.0){
		result.x=0.0;
	}
	return result;
}

void fragment() {
	vec4 base = texture(TEXTURE, UV).rgba;
	vec2 result_uv =uv(UV);
	
	if(result_uv.x==0.0){
		base.a = 0.0;
	}else{
		base.a = texture(base_mask, result_uv).a;
	}
    COLOR = base;
}

@jimmyjonezz
Copy link
Author

I needed a shrinked masked image however when I did this UV seems weird so I had to cut. Btw you don't use region uniform

this is not my code. I only collect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment