cropping and expanding using shader?

224 Views Asked by At

I'm trying to create a shader which can crop or expand a texture (along the x axis)
so far this is what I've come up with on the cropping part:

shader_type canvas_item;

uniform float start_x:hint_range(0.0, 1.0, 0.001) = 0.0;
uniform float end_x:hint_range(0.0, 1.0, 0.001) = 1.0;

void fragment()
{
    if(UV.x>start_x && UV.x<end_x)
        COLOR = texture(TEXTURE,UV);
    else
        COLOR = vec4(0.0,0.0,0.0,0.0);
}

But I have no clue how work on the expand part

What do I mean by expanding?


I'm actually trying to apply this shader to the AnimatedSprite node to create a pseudo "Region" effect like in a Sprite node

So by expanding I mean the sprite equivalent of setting the texture on mirrored repeat and increasing it's region_rect

enter image description here

So if I'd give the shader parameters start_x=0.0 & end_x=1.5 it would display the entire animatedsprite + 1/2 (as in the above diagram but with animated sprites instead)

1

There are 1 best solutions below

1
On BEST ANSWER

What you need to do is transform your UVs from their default range (0 to 1) to your desired range (start_x to end_x).

You can do that in the shader with something like the following:

UV.x = (UV.x * (end_x - start_x)) + start_x;