So there's this point that starts in the center. If the user drags it up towards the top of the texture, the top squishes and the bottom stretches. If the user drags it down, the top stretches on the bottom squishes. The same goes for left and right. How do I do this?
\$\begingroup\$
\$\endgroup\$
2
-
\$\begingroup\$ Sounds like a job for UV mapping with your point in the niddle, changing position on UV map as it gets moved. \$\endgroup\$– KromsterCommented Mar 3, 2023 at 5:03
-
\$\begingroup\$ I looked up uv mapping it's used for making 3d models flat? But the thing I'm working with is 2d already. \$\endgroup\$– EmilyCommented Mar 3, 2023 at 5:19
Add a comment
|
1 Answer
\$\begingroup\$
\$\endgroup\$
okay i figured it out
shader_type canvas_item;
const vec2 center_original = vec2(0.5, 0.5);
uniform vec2 center_actual = vec2(0.5, 0.5);
void fragment() {
vec2 adjusted_uv = vec2(0.0, 0.0);
if (UV.x < center_actual.x){
adjusted_uv.x = UV.x * center_original.x / center_actual.x;
} else {
adjusted_uv.x = UV.x * (1.0 - center_original.x) / (1.0 - center_actual.x) + 1.0 - (1.0 - center_original.x) / (1.0 - center_actual.x);
}
if (UV.y < center_actual.y){
adjusted_uv.y = UV.y * center_original.y / center_actual.y;
} else {
adjusted_uv.y = UV.y * (1.0 - center_original.y) / (1.0 - center_actual.y) + 1.0 - (1.0 - center_original.y) / (1.0 - center_actual.y);
}
COLOR = texture(TEXTURE, adjusted_uv);
}