Rain

Drops as quads stretched along their velocity, over a streak of a sprite: the faster one falls the longer it is drawn.


    
main.lua
local natiny = require("natiny")

natiny.backend.init(arg and arg[1] or natiny.backend.AUTO)

local win = natiny.window.create("Natiny - Rain", 900, 600)
natiny.window.set_scale_mode(win, natiny.window.SCALE_MODE_STRETCH)

local BG = natiny.color.from_hex("#0E1522")
local drop = natiny.texture.create(natiny.resource.load("common/textures/raindrop.png"))

local WIND = 9   -- degrees the whole downpour leans

-- A line of drops across the top, falling along the emitter's own +Y, which
-- is already down the screen in 2D.  Each drop is a quad stretched along its
-- velocity, so the faster one falls the longer its streak; the sprite is a
-- streak as well, a faded tail behind a bright head, and the stretch is what
-- pulls it out to length.
local rain = natiny.particles.create(2500)
natiny.particles.set_texture(rain, drop)
natiny.particles.set_spread(rain, 2)
natiny.particles.set_rate(rain, 1200)
natiny.particles.set_speed(rain, 900, 1500)
natiny.particles.set_lifetime(rain, 1.4, 1.4)   -- long enough to cross a tall window
natiny.particles.set_size(rain, 2.5, 4.5)
natiny.particles.set_align(rain, natiny.particles.ALIGN_STRETCHED)
natiny.particles.set_stretch(rain, 0.024)
natiny.particles.set_color(rain, natiny.color.from_hex("#93B4E8CC"),
                                 natiny.color.from_hex("#DCE9FFFF"))
natiny.entity.set_rotation(rain, natiny.entity.SPACE_WORLD, 0, 0, WIND)

natiny.backend.loop(function(dt)
    natiny.window.bind(win, function()
        local w = natiny.window.get_width(win)

        -- Wider than the window, so the wind never opens a gap along one
        -- edge, and above it, so a drop is already falling when it appears.
        natiny.particles.set_shape(rain, natiny.particles.SHAPE_EDGE, w * 1.4)
        natiny.entity.set_position(rain, natiny.entity.SPACE_WORLD, w / 2, -60, 0)

        natiny.render.clear(BG)
        natiny.render.particles(rain)
    end)
end)

natiny.backend.shutdown()