How to make a 2d shader working with ParallaxBackground node in Godot?

Viewed 29

In my game I want to make a scrolling background with moving stars. I am using ParallaxBackground node with ParallaxLayer as a child, and the later has TextureRect child that display a 2d shader for the stars.

Nodes hierarchy: ParallaxBackground -> StarsLayer -> Stars

Stars is the TextureRect and its rect_size equals the project window size. Here is the 2d shader that it uses:

shader_type canvas_item;

uniform vec4 bg_color: hint_color;

float rand(vec2 st) {
    return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123);
}

void fragment() {
    float size = 100.0;
    float prob = 0.9;
    vec2 pos = floor(1.0 / size * FRAGCOORD.xy);
    float color = 0.0;
    float starValue = rand(pos);

    if (starValue > prob)
    {
        vec2 center = size * pos + vec2(size, size) * 0.5;
        float t = 0.9 + 0.2 * sin(TIME * 8.0 + (starValue - prob) / (1.0 - prob) * 45.0);
        color = 1.0 - distance(FRAGCOORD.xy, center) / (0.5 * size);
        color = color * t / (abs(FRAGCOORD.y - center.y)) * t / (abs(FRAGCOORD.x - center.x));
    }
    else if (rand(SCREEN_UV.xy / 20.0) > 0.996)
    {
        float r = rand(SCREEN_UV.xy);
        color = r * (0.85 * sin(TIME * (r * 5.0) + 720.0 * r) + 0.95);
    }
    COLOR = vec4(vec3(color),1.0) + bg_color;
}

Here is ParallaxBackground script:

extends ParallaxBackground

onready var stars_layer = $StarsLayer
var bg_offset = 0.0

func _ready():
    stars_layer.motion_mirroring = Vector2(0, Helpers.WINDOW_SIZE.y)

func _process(delta):
    bg_offset += 30 * delta
    scroll_offset = Vector2(0, bg_offset)

The problem is that the stars are being showed but not moving at all.

1 Answers

Use motion_offset instead of scroll_offset

func _process(delta):
    motion_offset += 30 * delta
Related