Update to use increased range of offsets for shadowing

This commit is contained in:
shylie 2025-07-24 03:02:49 -04:00
parent 49ef00c94a
commit 1949623e5d
2 changed files with 67 additions and 63 deletions

128
main.lua
View File

@ -7,7 +7,32 @@ local gx = 0
local gy = 0
local angle = 0
local scale = 0.35
local dist = 5
local dist = 4
local function layered_noise(x, y, layers, persistence)
local freq = 1
local ampl = 1
local maxval = 0
local val = 0
for _ = 1, layers do
val = val + love.math.simplexNoise(x * freq, y * freq) * ampl
maxval = maxval + ampl
ampl = ampl * persistence
freq = freq * 2
end
return val / maxval
end
local function ease(n)
return math.min((1.1 * n ^ 2 + 1 - math.cos(n * math.pi / 2)) / 2, 1)
end
local function heightmap(x, y)
return ease(layered_noise(x * 0.0005, y * 0.0005, 4, 0.8))
end
local atlas = {
water = love.graphics.newQuad(1, 1, 8, 8, tex),
@ -18,32 +43,48 @@ local atlas = {
snow = love.graphics.newQuad(11, 11, 8, 8, tex),
}
local alpha = 0.75
local shadow_alpha = 0.15
local alpha = 0.8
local shadow_alpha
local colors = {}
local time = 0
local function calculate_shadow_alpha(x, y)
local delta = 0.01
local dhdx = (heightmap(x + delta / 2, y) - heightmap(x - delta / 2, y)) / delta
local dhdy = (heightmap(x, y + delta / 2) - heightmap(x, y - delta / 2)) / delta
local value = math.max(math.abs(dhdx), math.abs(dhdy)) * 100
return (1 - 1 / (1 + math.exp(value))) / 8
end
function colors.water(layer)
if layer % 2 == 0 then
return 0, 0, 0, shadow_alpha
end
if layer < 14 then
local r, g, b, a = colors.sand(layer)
return r, g, b, a * 1.2
end
layer = layer / 2
return 0.1, layer / 16, layer / 8, alpha
return layer / 32, layer / 16, layer / 8, alpha
end
function colors.sand(layer)
if layer % 2 == 0 then
return 0, 0, 0, shadow_alpha
end
layer = layer / 2
return layer / 16 + 0.2, layer / 16 + 0.15, 0.4, alpha
return layer / 24 + 0.3, layer / 24 + 0.2, layer / 24 + 0.1, alpha
end
function colors.dirt(layer)
if layer % 2 == 0 then
return 0, 0, 0, shadow_alpha
end
layer = layer / 2
if layer < 16 then
return layer / 40, layer / 80, 0.1, alpha
if layer < 24 then
return layer / 32 + 0.1, layer / 48 + 0.05, layer / 64, alpha
else
return 0.1, layer / 50 + 0.1, 0.1, alpha
return layer / 50 - 0.1, layer / 50 + 0.1, 0.1, alpha
end
end
function colors.stone(layer)
@ -65,67 +106,19 @@ end
local function offset_map(layer)
if layer % 2 == 0 then
return 1, 7
return math.cos(time) / 16, math.sin(time) / 16 + 0.5
else
return 0, 7
return 0, 0.5
end
end
local z = zprite.zchunk.new(tex, 16 * 8, 80, dist * 2 + 1)
local function layered_noise(x, y, layers, persistence)
local freq = 1
local ampl = 1
local maxval = 0
local val = 0
for _ = 1, layers do
val = val + love.math.simplexNoise(x * freq, y * freq) * ampl
maxval = maxval + ampl
ampl = ampl * persistence
freq = freq * 2
end
return val / maxval
end
local function ease(n)
return math.min((1.1 * n + 1 - math.cos(n * math.pi / 2)) / 2, 1)
end
local function heightmap(x, y)
return ease(layered_noise(x * 0.0005, y * 0.0005, 4, 0.8))
end
local function dirtgrass_sprite(layer)
if layer < 20 then
return atlas.dirt
end
return atlas.grass
end
local function pick_from_atlas(height)
if height < 0.25 then
return atlas.water
end
if height < 0.3 then
return atlas.sand
end
if height < 0.7 then
return dirtgrass_sprite
end
if height < 0.9 then
return atlas.stone
end
return atlas.snow
end
local z = zprite.zchunk.new(tex, 256, 80, dist * 2 + 1)
local function pick_color(height)
if height < 0.25 then
return colors.water
end
if height < 0.3 then
if height < 0.4 then
return colors.sand
end
if height < 0.67 then
@ -139,7 +132,8 @@ end
local function generate(x, y)
local height = heightmap(x, y)
z:put(x, y, atlas.stone, math.ceil(height * 80), pick_color(height), offset_map)
shadow_alpha = calculate_shadow_alpha(x, y)
z:put(x, y, atlas.stone, math.ceil(height * 64) + 16, pick_color(height), offset_map)
end
local generated = {}
@ -161,7 +155,17 @@ local function generate_chunk(cx, cy)
end
end
function love.keyreleased(key)
if key == "space" then
generated = {}
end
end
function love.update(dt)
if love.keyboard.isDown("space") then
time = math.fmod(time + dt, 2 * math.pi)
end
local speed = 512
if love.keyboard.isDown("d") then
local dx = speed * math.cos(-angle)
@ -224,7 +228,7 @@ end
local t = love.math.newTransform()
function love.draw()
z._height_scale = scale * 1.4
z._height_scale = scale * 20
t:reset()
t:translate(love.graphics.getWidth() / 2, love.graphics.getHeight() / 2)

2
zprite

@ -1 +1 @@
Subproject commit 0ac6791e752f5887cafe38e74e06ee86c76bec44
Subproject commit 5a14f0da22d2baf1744277c2c3950e57b76aed96