Apply shader to sprite region_rect

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By stalker2106

I have a basic mask shader that I use to mask part of my sprite.

I wanted to make the texture repeating to make a larger sprite, but the mask is applied to all the repeated instances of the textures, instead of the global sprite.

shader_type canvas_item;

uniform sampler2D mask_texture;

void fragment() {
  vec4 colour = texture(TEXTURE, UV);
  vec4 mask = texture(mask_texture, UV);
  colour.a = mask.r + mask.g + mask.b;
  COLOR = colour;
}

How to apply shader to region_rect instead of texture ?

EDIT: I tried using light2d in mask mode, but the black part of the mask is not transparent as when using shader alpha.

Paste the shader code please.

magicalogic | 2022-11-14 16:02

I edited the initial question with it.

stalker2106 | 2022-11-14 16:04

:bust_in_silhouette: Reply From: magicalogic

You can tile the texture using shaders instead of using texture region.
Lets say you want the texture to tile 4x4, you just multiply the uv’s by 4 the use fract on the uv’s and you have tilling texture. I mean this way:

vec2 uv = fract(uv * 4);

Use the modified uvs with the colour and the original uvs with the mask and it should give you the effect you are after.
You can replace the 4 with a vec2 that fits your needs.

This pointed me to the correct solution, Thanks!

I used this shader code:

shader_type canvas_item;

uniform sampler2D mask_texture;
uniform vec2 mask_ratio;

void fragment() {
  vec4 colour = texture(TEXTURE, UV);
  vec4 mask = texture(mask_texture, UV * mask_ratio);
  colour.a = mask.r + mask.g + mask.b;
  COLOR = colour;
}

As you can tell i just multiplied mask by the ratio which is a parameter i computed from

 $Sprite.texture.get_size() / region_rect.size

stalker2106 | 2022-11-14 16:20

Glad I could help.

magicalogic | 2022-11-14 18:39