Skip to content

Instantly share code, notes, and snippets.

@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 March 13, 2024 16:14
Point light shadow mapping
--[[ Point light shaddow mapping
This is obsolete version that requires few modifications to work on post 0.17 lovr
See this updated version: https://github.com/Tackyflea/lovrShadows/
-- usage snippets
local point_light = require('point_light')
point_light.load(1024, vec3(0, 1, 0)) -- depth texture resolution & position
-- in lovr.update(dt), or at start of lovr.draw(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 March 10, 2023 21:12
A testbed for light probe generation from a dynamically rendered skybox
local skybox = {} -- from https://github.com/jmiskovic/lovr-atmo
skybox.__index = skybox
function skybox.new(resolution)
local self = setmetatable({}, skybox)
self.resolution = math.floor(resolution or 256)
assert(self.resolution > 0 and self.resolution < 6000)
self.cubetex = lovr.graphics.newTexture(
self.resolution, self.resolution, 6,
{type='cube', mipmaps=false, usage = {'render', 'sample', 'transfer'}})
@jmiskovic
jmiskovic / glitch-hands.lua
Created January 7, 2023 17:18
Reaction-diffusion model with hands erasing the growth (for lovr framework)
local resolution = 1024
local group_size = 16
local tex1 = lovr.graphics.newTexture(resolution, resolution, {mipmaps = false, usage = {'render', 'sample', 'storage', 'transfer'}, linear = true})
local tex2 = lovr.graphics.newTexture(resolution, resolution, {mipmaps = false, usage = {'render', 'sample', 'storage', 'transfer'}, linear = true})
local tex = lovr.graphics.newTexture(resolution, resolution, {mipmaps = false, usage = {'render', 'sample', 'storage', 'transfer'}, linear = true})
local projection = lovr.math.newMat4():perspective(math.rad(110), 1, .1, 0)
local palette = {0xded4c8, 0xbeaa9c, 0x94837a, 0x645c59, 0x181e28, 0xdbaf88, 0xb8926f, 0x987052, 0x624a30, 0x2f2114, 0xdf8b79, 0xe26560, 0xb0454c, 0x5b3636, 0xe5be3e, 0xbe8e03, 0x916803, 0x644507, 0xf18b49, 0xd46b2b, 0xba5113, 0x7a3412, 0xeb8281, 0xd95b5b, 0xbf2f37, 0x64272c, 0xb58fb6, 0x7e638e, 0x594a66, 0x3c3145, 0x30bab3, 0x1390ac, 0x0b5472, 0x233552, 0xabcf5f, 0x789949, 0x39681d, 0x084739}
@jmiskovic
jmiskovic / directional_shadow.lua
Created December 21, 2022 16:55
directional shadow mapping in lovr
local light_pose = lovr.math.newMat4()
local light_view = lovr.math.newMat4()
local light_projection = lovr.math.newMat4():perspective(math.rad(120), 1, 0.01)
--light_projection:orthographic(-5, 5, -5, 5, 100, -100) -- why is near and far inverted? ¯\_(ツ)_/¯
local depthBufferSize = 1024
local depthtex = lovr.graphics.newTexture(depthBufferSize, depthBufferSize, {format = 'd32f', mipmaps = false, usage = {'render', 'sample'}})
local shadowmapper = lovr.graphics.newShader(
[[