The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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 ?
enter image description here

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

Godot version 3.5.1
in Engine by (73 points)
edited by

Paste the shader code please.

I edited the initial question with it.

1 Answer

+1 vote
Best answer

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.

by (2,018 points)
selected by

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

Glad I could help.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.