Skip to content

Commit

Permalink
a start on shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
Retrisma committed Jun 4, 2024
1 parent 37664c6 commit bfb8b3d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
15 changes: 14 additions & 1 deletion Tiled/Maps/Exports/slime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ return {
tilewidth = 16,
tileheight = 16,
nextlayerid = 8,
nextobjectid = 73,
nextobjectid = 74,
properties = {},
tilesets = {
{
Expand Down Expand Up @@ -9249,6 +9249,19 @@ return {
rotation = 0,
visible = true,
properties = {}
},
{
id = 73,
name = "light",
type = "",
shape = "rectangle",
x = 608,
y = 2177,
width = 0,
height = 0,
rotation = 0,
visible = true,
properties = {}
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion Tiled/Maps/slime.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="250" height="170" tilewidth="16" tileheight="16" infinite="0" nextlayerid="8" nextobjectid="73">
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="250" height="170" tilewidth="16" tileheight="16" infinite="0" nextlayerid="8" nextobjectid="74">
<editorsettings>
<export target="Exports/slime.lua" format="lua"/>
</editorsettings>
Expand Down Expand Up @@ -359,6 +359,7 @@
</layer>
<objectgroup id="2" name="Object Layer">
<object id="2" name="animation" x="490.75" y="2133.25"/>
<object id="73" name="light" x="608" y="2177"/>
</objectgroup>
<objectgroup id="7" name="Triggers">
<object id="55" name="CamLockY" x="402" y="1942" width="2553.33" height="462"/>
Expand Down
3 changes: 3 additions & 0 deletions camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ function updatecamera(dt)

camera.fx = math.floor(camera.rx)
camera.fy = math.floor(camera.ry)

basic.shader:send("camera_pos", {camera.fx, camera.fy})
basic.shader:send("camera_scale", camera.scale)
end
21 changes: 21 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ require "actor"
require "player"
require "triggers"
require "tools"
require "shaders"
require "drawing"
fmod = require "fmodlove"
sti = require "lib/sti"
moonshine = require "lib/moonshine"

window = { width = 800, height = 460, scale = 1.3 }
canvas = love.graphics.newCanvas(window.width * window.scale, window.height * window.scale)

speed = { target = 1 / 60, multiplier = 1 }

Expand All @@ -34,13 +37,22 @@ function love.load()
love.graphics.setDefaultFilter("nearest", "nearest")

loadassets()

fmod.init(0, 32, 64, 1)

bi1 = fmod.loadBank("FMOD/Desktop/Master.bank", 0)
bi2 = fmod.loadBank("FMOD/Desktop/Master.strings.bank", 0)

fmod.setNumListeners(1)

font = love.graphics.setFont(fonts["ElixiR"])
map = loadmap("slime")

for _,v in pairs(p) do
if v.user == "player" then player = v break end
end

basic.setup()
end

function love.update(dt)
Expand Down Expand Up @@ -70,11 +82,15 @@ function love.update(dt)
end
end

basic.update()

mouse.op = mouse.p
end

function love.draw()
--draw tiled map
love.graphics.setCanvas(canvas)
love.graphics.clear()
map:draw(camera.fx * -1, camera.fy * -1, window.scale, window.scale)

love.graphics.push()
Expand All @@ -94,6 +110,11 @@ function love.draw()
love.graphics.circle("fill", mouse.x, mouse.y, 3)

love.graphics.pop()
love.graphics.setCanvas()

love.graphics.setShader(basic.shader)
love.graphics.draw(canvas)
love.graphics.setShader()

--if showdebug then
love.graphics.print(printdebug(), 0, 0)
Expand Down
5 changes: 4 additions & 1 deletion map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ spawntable = {
table.insert(p, a)
end,


light = function(v)
local data = getobjectdata(v)
table.insert(basic.lights, {data.x, data.y})
end,
CamLockY = function(v)
local data = getobjectdata(v)
CameraLockTrigger:add(data.x, data.y, data.width, data.height, false, data.y)
Expand Down
36 changes: 36 additions & 0 deletions shaders.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
basic = {
lights = {}
}

basic.shader = love.graphics.newShader[[
uniform vec2[1] light_pos;
uniform vec2 camera_pos;
uniform float camera_scale;
uniform vec2 player_pos;
vec2 world_transform(vec2 world_coords) {
return (world_coords - camera_pos) * camera_scale;
}
float light_source(vec2 screen_coords, vec2 light, float falloff) {
float d = distance(screen_coords, world_transform(light));
float intensity = 1.5 - (d / falloff);
return clamp(intensity, 1.0, 0.0);
}
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
vec4 pixel = Texel(texture, texture_coords);
float intensity = clamp(light_source(screen_coords, light_pos[0], 1000) + light_source(screen_coords, player_pos, 150), 0.4, 1.0);
return pixel * intensity;
}
]]

function basic.setup()
basic.shader:send("light_pos", unpack(basic.lights))
end

function basic.update()
basic.shader:send("player_pos", {player.x + player.width / 2, player.y + player.height / 2})
end

0 comments on commit bfb8b3d

Please sign in to comment.