Masks of different forms

Godot Version

4.4.1

Question

Hey, How to make mask different from rectangle/ I mean circle mask, hex mask or other forms? I use shaders but all the time have mask as rectangle

A “mask” is a generic, ill-defined term, what do you mean by mask? Why can you make rectangles but not circles?

If by mask you mean a texture build for data, not for display, then you can create any shape you want from a image editing program.

If you mean to use a SDF then you can reference the book of shaders for many shaped SDFs.

On scene, I want to place bground.png on a layer, next layer i want to place other png picture but i want it to be shown partly. at hex frame i want it to be visible and not to be shown part of the picture that is out of hex. that is how i want to use hex as a mask for graphics

So you are not using shaders to achieve this?

A shader like so can use two textures to draw a masked layer.

shader_type canvas_item;

uniform sampler2D mask;

void fragment() {
	vec4 base = texture(TEXTURE, UV);
	float mask_value = texture(mask, UV).r;
	COLOR = vec4(base.rgb, mask_value);
}

I am trying to use shaders but its not working for me:

shader_type canvas_item;

void fragment() {

vec2 uv = (UV - vec2(0.5)) * 4.0;


uv.x *= 0.87;
uv.y *= 0.5;


float ax = abs(uv.x);
float ay = abs(uv.y);
float hex = max(ay, ax * 0.57735 + ay * 0.5);


if (hex > 1.0) {
	discard;
}


COLOR = vec4(1.0, 1.0, 1.0, 1.0);

}
may be i use it in wrong way…i dont know. i want to place png back ground picture under hexmask and want to have bgroung visible inside shaape of the hex

Seems like a mis-match of two hexagon SDFs, first you modify UV to be a strange -2.0 to 2.0 range, then by a hexagon’s half height 0.87 ≈ √3 / 2, then multiply it by 0.57735 which is the inverse of √3.

I’d recommend using this function instead, based on this hexagon SDF blog post

uniform float hexagon_half_width = 0.5;
uniform vec4 color: source_color = vec4(1.0);

float hexagon(vec2 p, float r) {
	p = abs(p);
	const vec2 s = vec2(1.0, sqrt(3.0)) * 0.5;
	return max(dot(p, s),p.x) - r;
}

void fragment() {
	vec2 uv = UV * 2.0 - 1.0;
	float hex = hexagon(uv, hexagon_half_width);
	if (hex > 0.0) {
		discard;
	}
	COLOR = color; 
}