make 3d noise work

This commit is contained in:
MisterE123 2022-12-19 22:52:53 -05:00
parent 5e4f1b58e2
commit 52636b2ed1
2 changed files with 66 additions and 36 deletions

View File

@ -3,6 +3,13 @@
Copyright (C) 2022 MisterE
Copyright (C) 2022 regulus
Copyright (C) 2022 LMD
Copyright (C) 2010 David Ng (voronoi.lua)
MIT LICENSE
Permission is hereby granted, free of charge, to any person obtaining a copy of

View File

@ -1,19 +1,27 @@
luamap = {}
luamap.noises = {}
luamap.noises_2d = {}
luamap.noises_3d = {}
function luamap.remap(val, min_val, max_val, min_map, max_map)
return (val-min_val)/(max_val-min_val) * (max_map-min_map) + min_map
end
function luamap.lerp(var_a, var_b, ratio)
return (1-ratio)*var_a + (ratio*var_b)
function luamap.lerp(var_a, var_b, ratio, power)
power = power or 1
return (1-ratio)*(var_a^power) + (ratio*(var_b^power))
end
function luamap.register_noise(name,data)
luamap.noises[name] = {}
luamap.noises[name].np_vals = data.np_vals
luamap.noises[name].nobj = nil
luamap.noises[name].type = data.type or "2d"
if data.type == "2d" then
luamap.noises_2d[name] = {}
luamap.noises_2d[name].np_vals = data.np_vals
luamap.noises_2d[name].nobj = nil
else -- 3d
luamap.noises_3d[name] = {}
luamap.noises_3d[name].np_vals = data.np_vals
luamap.noises_3d[name].nobj = nil
end
end
local c_air = minetest.get_content_id("air")
@ -36,50 +44,65 @@ function luamap.set_singlenode()
end)
end
local noise_vals = {}
minetest.register_on_generated(function(minp, maxp, seed)
-- localize vars
local logic = luamap.logic
local noises_2d = luamap.noises_2d
local noises_3d = luamap.noises_3d
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
local data = vm:get_data()
local sidelen = maxp.x - minp.x + 1
local chulens3d = {x=sidelen, y=sidelen+17, z=sidelen}
local chulens3d = {x=sidelen, y=sidelen, z=sidelen}
local chulens2d = {x=sidelen, y=sidelen, z=1}
local minpos3d = {x=minp.x, y=minp.y-16, z=minp.z}
local minpos2d = {x=minp.x, y=minp.z}
for name,elements in pairs(luamap.noises) do
if luamap.noises[name].type == "2d" then
luamap.noises[name].nobj = luamap.noises[name].nobj or minetest.get_perlin_map(luamap.noises[name].np_vals, chulens2d)
luamap.noises[name].nvals = luamap.noises[name].nobj:get2dMap_flat(minpos2d)
else -- 3d
luamap.noises[name].nobj = luamap.noises[name].nobj or minetest.get_perlin_map(luamap.noises[name].np_vals, chulens3d)
luamap.noises[name].nvals = luamap.noises[name].nobj:get3dMap_flat(minpos3d)
end
for name,elements in pairs(noises_2d) do
noises_2d[name].nobj = noises_2d[name].nobj or minetest.get_perlin_map(noises_2d[name].np_vals, chulens2d)
noises_2d[name].nvals = noises_2d[name].nobj:get_2d_map_flat(minpos2d)
end
local ni3d = 1
local ni2d = 1
for z = minp.z, maxp.z do
for y = minp.y - 16, maxp.y + 1 do
local vi = area:index(minp.x, y, z)
for x = minp.x, maxp.x do
local noise_vals = {}
for name,elements in pairs(luamap.noises) do
if elements.type == "2d" then
noise_vals[name] = elements.nvals[ni2d]
else -- 3d
noise_vals[name] = elements.nvals[ni3d]
end
end
data[vi] = luamap.logic(noise_vals,x,y,z,seed)
ni3d = ni3d + 1
ni2d = ni2d + 1
vi = vi + 1
for name,elements in pairs(noises_3d) do
noises_3d[name].nobj = noises_3d[name].nobj or minetest.get_perlin_map(noises_3d[name].np_vals, chulens3d)
noises_3d[name].nvals = noises_3d[name].nobj:get_3d_map_flat(minpos3d)
end
local xstride, ystride, zstride = 1,sidelen,sidelen*sidelen
local i2d = 1
local i3dz = 1
for z = minp.z, maxp.z do
local i3dx=i3dz
for x = minp.x, maxp.x do
for name,elements in pairs(noises_2d) do
noise_vals[name] = elements.nvals[i2d]
end
ni2d = ni2d - sidelen
local i3dy=i3dx
for y = minp.y, maxp.y do
local vi = area:index(x, y, z)
for name,elements in pairs(noises_3d) do
noise_vals[name] = elements.nvals[i3dy]
end
data[vi] = logic(noise_vals,x,y,z,seed)
i3dy = i3dy + ystride
end
i3dx = i3dx + xstride
i2d = i2d + 1
end
ni2d = ni2d + sidelen
i3dz=i3dz+zstride
end
luamap.precalc(data, area, vm, minp, maxp, seed)
vm:set_data(data)