Hoy can I make a parallax layer using a shader?

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

For quick prototyping I like to add a checker board parallax layer in order to make it easier to perceive distances and speed, but parallax layers don’t work well when changing the zoom and I also have to manually readjust it whenever I want to change window sizes.

Recently I’m starting to learn about shaders and after watching this video I’m sure that there has to be a way to make what I’m trying to do with that but instead of being an auto-scrolling texture it just adjusts to the player’s movement, that way I can easily dynamically resize the region rect whenever I want to change window size or zoom. Unfortunately I can’t figure out how to make it work because it’s hard to find a lot of information about shaders, I’d love to get some help!

:bust_in_silhouette: Reply From: skysphr

I assume you mean something like this?

shader_type canvas_item;

uniform float offset = 0.0;
uniform float multiplier;

void fragment() {
    float u = offset * multiplier * TEXTURE_PIXEL_SIZE.x;
    COLOR = texture(TEXTURE, vec2(UV.x + u, UV.y));
}

Make sure you enable the “repeat” flag on the texture (or you could also mod(u, 1.0) in the shader) and set the shader parameters with process_material.set_shader_param(). Offset is the “real world” offset and multiplier is the speed of the parallax.

yeah that’s exactly what I was trying to do, I mostly do top-down games instead of side scrollers but I was able to easily figure out what I had to edit in order to make it work top down, thanks!

AsCiiexe | 2021-09-25 23:22