#how can I make a shader not be affected by transformations?

1 messages · Page 1 of 1 (latest)

feral minnow
#

how can I make a shader not be effected by it's owner's transformations? it's meant to look like image1, but instead looks like image2. (ignore the color)

safe violet
#

Shaders don't have access to transform information, so I think the typical pattern would be to add a vec2 scale uniform to the shader, which you then set via the inspector or script. Within the fragment portion of the shader, you can do something like vec2 uv = (UV - center) * scale + center to adjust your UV's prior to sampling the texture.

feral minnow
safe violet
#

You would plug the scaled uv variable into the UV spot that you have there.

#

Your center might need to be the mouse position, since I'm assuming you're scaling around that point.

feral minnow
#

yeah

#

so my scale variable is actually the size in pixels, (since I'm using panels), would that change anything?

safe violet
#

Yes, you would probably need a separate scale variable for the transform modification. It should be based on the ratio of your vertical and horizontal sizes.

#

So you could have something like vec2(0.5, 1.0) potentially.

#

Whatever gets you back to 1 to 1 or square scaling.

feral minnow
#

how would I make the new variable?

safe violet
#

Add uniform vec2 transform_scale = vec2(1.0, 1.0);, or something similarly named, to the shader.

#

This might get you close to what you want:

shader_type canvas_item;

uniform vec2 mouse_position = vec2(0.5);
uniform float radius = 0.25;
uniform vec2 transform_scale = vec2(1.77, 1.0);
uniform float offset = 0.1;

void fragment() {
    vec2 uv = (UV - mouse_position) * transform_scale;
    COLOR.a = (length(uv) - offset) / max(radius - offset, 1e-8);
}
safe violet
feral minnow
#

ah ok I see