Campfire
Added flames under painted smoke, sorted back to front and four seconds old when the window opens.
local natiny = require("natiny")
natiny.backend.init(arg and arg[1] or natiny.backend.AUTO)
local win = natiny.window.create("Natiny - Campfire", 960, 640)
natiny.window.set_scale_mode(win, natiny.window.SCALE_MODE_STRETCH)
local BG = natiny.color.from_hex("#151E2C")
local sprite = natiny.texture.create(natiny.resource.load("common/textures/particle.png"))
local platform = natiny.resource.load("common/models/platform.glb")
natiny.model.create(natiny.mesh.load(platform))
natiny.resource.destroy(platform)
-- The flames: born on a small disc, thrown up within a narrow cone, shrinking
-- and cooling as they rise. Added light rather than paint, so where two of
-- them overlap the fire is brighter.
local fire = natiny.particles.create(500)
natiny.particles.set_texture(fire, sprite)
natiny.particles.set_align(fire, natiny.particles.ALIGN_VERTICAL)
natiny.particles.set_shape(fire, natiny.particles.SHAPE_CONE, 12, 0.12)
natiny.particles.set_rate(fire, 70)
natiny.particles.set_speed(fire, 0.45, 0.85)
natiny.particles.set_lifetime(fire, 0.6, 1.0)
natiny.particles.set_size(fire, 0.18, 0.30)
natiny.particles.set_sizes(fire, { 1.0, 0.8, 0.2 })
natiny.particles.set_colors(fire, {
natiny.color.from_hex("#FFC86040"),
natiny.color.from_hex("#FF6A1A30"),
natiny.color.from_hex("#80200A00"),
})
natiny.particles.set_blend(fire, natiny.particles.BLEND_ADD)
natiny.entity.set_position(fire, natiny.entity.SPACE_WORLD, 0, 0.02, 0)
-- The smoke above it: slower, turning, growing, drifting sideways. It is
-- painted rather than added, so overlapping particles have to be drawn far
-- to near to look right.
local smoke = natiny.particles.create(300)
natiny.particles.set_texture(smoke, sprite)
natiny.particles.set_shape(smoke, natiny.particles.SHAPE_CONE, 18, 0.1)
natiny.particles.set_rate(smoke, 18)
natiny.particles.set_speed(smoke, 0.25, 0.45)
natiny.particles.set_lifetime(smoke, 2.5, 3.5)
natiny.particles.set_size(smoke, 0.25, 0.4)
natiny.particles.set_sizes(smoke, { 0.5, 1.3, 2.2 })
natiny.particles.set_rotation(smoke, 0, 360)
natiny.particles.set_spin(smoke, -35, 35)
natiny.particles.set_gravity(smoke, 0.05, 0.08, 0)
natiny.particles.set_damping(smoke, 0.3)
natiny.particles.set_colors(smoke, {
natiny.color.from_hex("#6B707A00"),
natiny.color.from_hex("#6B707A77"),
natiny.color.from_hex("#3A3F4800"),
})
natiny.particles.set_sort(smoke, natiny.particles.SORT_BACK_TO_FRONT)
natiny.entity.set_position(smoke, natiny.entity.SPACE_WORLD, 0, 0.45, 0)
-- Four seconds of it have already happened when the window opens.
natiny.particles.simulate(smoke, 4.0)
local pivot = natiny.pivot.create()
local camera = natiny.camera.create()
natiny.entity.set_parent(camera, pivot)
natiny.entity.set_position(camera, natiny.entity.SPACE_LOCAL, 0, 1.1, 2.7)
natiny.entity.look_at(camera, 0, 0.55, 0)
natiny.camera.set_fov(camera, 50.0)
natiny.camera.set_clip(camera, 0.1, 100.0)
natiny.backend.loop(function(dt)
natiny.entity.rotate(pivot, natiny.entity.SPACE_LOCAL, 0, 12.0 * dt, 0)
natiny.window.bind(win, function()
natiny.render.clear(BG)
natiny.camera.bind(camera, function()
natiny.render.tag("model")
-- A system with no material of its own answers to "particles".
natiny.render.tag("particles", natiny.render.SORT_BACK_TO_FRONT)
end)
end)
end)
natiny.backend.shutdown()