Skip to content

Instantly share code, notes, and snippets.

@gnalvesteffer
Created March 16, 2021 23:14
Show Gist options
  • Save gnalvesteffer/0aaf6536ec22fad90376d9a0f63c57db to your computer and use it in GitHub Desktop.
Save gnalvesteffer/0aaf6536ec22fad90376d9a0f63c57db to your computer and use it in GitHub Desktop.
Punished Bernadetta's Minimap w/ Positional Color Caching
function init()
grassColors = {
{99 / 255, 146 / 255, 103 / 255},
{84 / 255, 127 / 255, 86 / 255},
{70 / 255, 107 / 255, 69 / 255}
}
map_cache = {}
resolution = 32
squareSize = 10
area = 50
height = 50
uiSize = resolution * squareSize
maxDist = height * 2
end
function draw()
local map_square_x = 0
for x = math.floor(area / 2) * -1, math.floor(area / 2), area / resolution do
map_square_x = map_square_x + 1
local map_square_y = 0
for y = math.floor(area / 2) * -1, math.floor(area / 2), area / resolution do
map_square_y = map_square_y + 1
local player_position = GetPlayerTransform().pos
local color = get_color(player_position[1] + x, player_position[3] + y)
UiPush()
UiColor(color.r, color.g, color.b)
UiTranslate(map_square_x * squareSize, map_square_y * squareSize)
UiAlign("left top")
UiRect(squareSize, squareSize)
UiPop()
end
end
end
function get_color(x, y)
-- try to get the cached color at this position
local cached_color = get_cached_color(x, y)
if cached_color ~= nil then
return cached_color
end
-- if there wasn't a color cached at this position, get the color and cache it
--DebugPrint(GetTime() .. ": computing " .. get_cache_key(x, y))
local color = compute_color_at_position(x, y)
set_cached_color(x, y, color.r, color.g, color.b)
return color
end
function compute_color_at_position(x, y)
local pos = Vec(x, height, y)
local hit, dist, normal, shape = QueryRaycast(pos, Vec(0, -1, 0), maxDist)
if hit then
local hitPoint = VecAdd(pos, VecScale(Vec(0, -1, 0), dist))
local mat, r, g, b = GetShapeMaterialAtPosition(shape, hitPoint)
--Thanks Mathias Elgaard for this code
if r + g + b == 0 then
local randomMat = grassColors[math.floor((hitPoint[1] * hitPoint[2] * hitPoint[3]) % 3) + 1]
r, g, b = randomMat[1], randomMat[2], randomMat[3]
end
local value = 0.75 + math.min(hitPoint[2] / 10, 10) * 0.25
return { r = r * value, g = g * value, b = b * value }
else
return { r = 0, g = 0, b = 0 }
end
end
function get_cache_key(x, y)
return string.format("%.1f", round(x, 0.5)) .. "," .. string.format("%.1f", round(y, 0.5))
end
function get_cached_color(x, y)
local color = map_cache[get_cache_key(x, y)]
if color == nil then
return nil
end
return { r = color.r, g = color.g, b = color.b }
end
function set_cached_color(x, y, r, g, b)
map_cache[get_cache_key(x, y)] = { r = r, g = g, b = b }
end
function round(exact, quantum)
local quant,frac = math.modf(exact/quantum)
return quantum * (quant + (frac > 0.5 and 1 or 0))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment