Skip to content

Instantly share code, notes, and snippets.

@jmiskovic
jmiskovic / cycles.lua
Created April 19, 2025 20:27
Stabilize the LÖVR physics and state updates by running them at consistent intervals regardless of frame rate
-- require this file to have reduced input lag and game logic that runs consistently
-- inspired by https://gafferongames.com/post/fix_your_timestep/
-- note: this decouples update() from draw(); update() can run multiple times per frame
local m = {}
m.update_delta = 1 / 60
function lovr.run()
if lovr.timer then lovr.timer.step() end
@jmiskovic
jmiskovic / physarum.lua
Last active April 5, 2025 21:33
The LÖVR implementation of physarum model in a compute shader
-- https://cargocollective.com/sagejenson/physarum
-- also [Coding Adventure: Ant and Slime Simulations](https://www.youtube.com/watch?v=X-iSQQgOd1A)
-- "agents" move around and leave behind a trail
-- they also detect others' trail and steer towards it
local m = {}
local agent_count = 1e4
local iterations_per_frame = 3
@jmiskovic
jmiskovic / measure_lag.sh
Last active February 14, 2025 19:20
Measure input lag
#!/bin/bash
# Measures input lag of an application that draws a blue circle on the last known cursor location
# automates cursor motion for consistency; takes a screenshot and measures distance; averages over N runs
CURSOR_COLOR="#7c7c7c" # set this to your system cursor primary color
CIRCLE_COLOR="#0000ff" # the measured app should draw a circle of this color
TESTS_N=50
get_window_info() {
@jmiskovic
jmiskovic / main.lua
Last active April 5, 2024 09:55
Example of 3rd person camera setup
local cam = require'cam' -- https://github.com/jmiskovic/lovr-cam
local player_pos = Vec3()
local player_vel = Vec3(0, 0, -0.01)
local function getWorldFromScreen(pass)
local w, h = pass:getDimensions()
local clip_from_screen = mat4(-1, -1, 0):scale(2 / w, 2 / h, 1)
local view_pose = mat4(pass:getViewPose(1))
@jmiskovic
jmiskovic / dir_light.lua
Last active March 17, 2024 09:25
Directional light shadow mapping for lovr
-- directional light shadow mapping for lovr 0.18 (currenly on post 0.17.1 dev branch)
local m = {}
m.near = 0.01
m.far = 500
m.orthographic_span = 40
m.perspective_fov = 90
m.view_pose = lovr.math.newMat4()
:lookAt(
@jmiskovic
jmiskovic / stagestack.lua
Created February 8, 2024 07:56
Tiny Lua stage management module
-- manages "stages" with a stack interface
-- stage is Lua module with callbacks (load, update, draw, keypress...)
-- only single stage is active, one on top of the stack
-- after push and pop operation another stage takes over the callbacks
-- example stack: os > main_menu > level_selection > game_play > overlay_menu
-- example usage:
-- local stagestack = require'stagestack'
-- stagestack.push(require'main_menu')
local m = {}
@jmiskovic
jmiskovic / mouse_picking.lua
Created October 2, 2023 18:09
Example for dragging physics colliders with mouse in LÖVR
-- Creates a few hundred colliders which can be dragged around with right mouse button
-- Uses phywire.lua from https://github.com/jmiskovic/lovr-phywire
local phywire = require'phywire'
local world = lovr.physics.newWorld(0,0,0, false)
world:setLinearDamping(0.1)
world:setAngularDamping(0.1)
local mouse_collider = world:newSphereCollider(0,0,0, 0.001)
@jmiskovic
jmiskovic / point_light.lua
Last active May 20, 2025 14:25
Point light shadow mapping
--[[ Point light shaddow mapping (VSM Fixed)
-- usage snippets
local point_light = require('point_light')
point_light.load(1024, vec3(0, 1, 0)) -- depth texture resolution & light position
-- in lovr.update(dt), or at start of lovr.draw(pass)
local pass = point_light.getPass()
-- ... draw the scene using this pass ...
lovr.graphics.submit(pass)
-- if using within draw(), submit it together with main pass
@jmiskovic
jmiskovic / main.lua
Last active May 1, 2023 14:27
LOVR collider mouse drag
-- LOVR demo: Creates a few hundred colliders and allows to drag them around using the right mouse button
-- Dependencies:
-- lovr-mouse.lua from https://github.com/bjornbytes/lovr-mouse/
-- mousearm.lua from https://github.com/jmiskovic/lovr-mousearm
-- phywire.lua from https://github.com/jmiskovic/lovr-phywire
local mousearm = require'mousearm'
local phywire = require'phywire'
local world = lovr.physics.newWorld(0,0,0, false)
@jmiskovic
jmiskovic / lightprobe_skybox.lua
Last active May 20, 2025 14:26
A testbed for light probe generation from a dynamically rendered skybox
local shh = require'shh' -- grab from https://github.com/bjornbytes/shh
-- a newer version of skybox might be available https://github.com/jmiskovic/lovr-atmo
local skybox = {}
skybox.__index = skybox
local cubemap_transforms = {
Mat4():lookAt(vec3(0, 0, 0), vec3( 1, 0, 0), vec3(0, 1, 0)),
Mat4():lookAt(vec3(0, 0, 0), vec3(-1, 0, 0), vec3(0, 1, 0)),
Mat4():lookAt(vec3(0, 0, 0), vec3( 0, 1, 0), vec3(0, 0,-1)),