Tutorial: Star Layers


Star layers spawn a user-specified number of stars at random locations onscreen. They have a simple set of parameters to give the user some control over this generation:

  • STAR QUANTITY
    The total number of stars generated by the layer
  • STAR RATIO
    The relative frequency of small, medium and large stars. Separate sliders are provided for each type.
  • FLICKER RATE
    The speed at which stars flicker
  • FLICKER DEPTH
    The range between maximum and minimum brightness
  • SCROLL SPEED
    The speed at which the layer scrolls across the screen.
  • NEW SEED
    Randomises the position of all stars in the layer



NOTES

A subtle twinkling effect is applied to star layers when displayed within Space Generator - this is a very simple shader applied to the whole layer:

shader_type canvas_item;
uniform sampler2D noise_texture;
uniform float rate;
uniform float depth;
float get_lum(vec4 color)
{
    return (color.r * 0.299) + (color.g * 0.587) + (color.b * 0.114);
}
void fragment()
{
    float offset = get_lum(texture(noise_texture, UV)) * 2.0;
    float color_mod = 0.5 *
            (1.0 + sin(2.0 * PI * rate * (TIME + offset)));
    COLOR *= (1.0 - (color_mod * depth));
}

Note: the language used here is .gdshader, but this is very similar in use to GLSL ES 3.0 - if you're familiar it shouldn't be too challenging to port this over. A useful reference for migrating between GLSL ES 3.0 and .gdshader can be found here

This shader allows the user to specify a handful of variables:

  • Noise Texture
    A random noise texture that will be sampled per-pixel to ensure fluctuations in brightness are not identical across the screen
  • Rate
    The speed of the flickering
  • Depth
    How strong the fluctation in brightness should be

With these parameters set, the shader will then adjust the brightness of each pixel at the same rate, but offset according to the brightness of the noise texture at that pixel's coordinates. These variables are exposed via the layer settings for the user to experiment with.

This shader is under CC0 terms - feel free to copy this over to your own projects and adjust the variables to your taste, or rewrite it to better suit your needs!

Get Space Generator

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.