PerlinNoiseMap

From Minetest Developer Wiki
Revision as of 22:03, 5 July 2014 by Prestidigitator (talk | contribs) (Caveat on when function can be called, as per https://forum.minetest.net/viewtopic.php?f=6&t=8157)
Jump to navigation Jump to search

A fast, bulk perlin noise generator Can be created via

  • PerlinNoiseMap(noiseparams, size)
  • minetest.get_perlin_map(noiseparams, size)

The minetest.get_perlin_map() function must not be called until after the world is finished initializing, so it must be called during some kind of callback rather than at the top level mod initialization code. The PerlinNoiseMap constructor, on the other hand, may be called at any time since it does not depend on the world's seed.

Methods

get2dMap(pos) size.x x size.y 2d array of 2d noise values starting at pos={x=,y=}
get3dMap(pos) size.x x size.y x size.z 3d array of 3d noise values starting at pos={x=,y=,z=}
get2dMap_flat(pos) Flat size.x * size.y element array of 2d noise values starting at pos={x=,y=}
get3dMap_flat(pos) Same as get2dMap_flat, but 3d noise

Noise Params

Here's an example of the noiseparams:

local testparam = {
   offset = 0,
   scale = 1,
   spread = {x=2048, y=2048, z=2048},
   seed = 1337,
   octaves = 6,
   persist = 0.6
}
  • spread: defines how fast the (ex.) terrain high changes (frequency)
  • seed: every seed generates an other perlin map
  • octaves: higher value, higher details

Examples

minetest.register_on_generated(function(minp, maxp, seed)
   local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
   local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
   local data = vm:get_data()
   
   --get side length of the current mapgen block
   local side_length = maxp.x - minp.x + 1
   local map_lengths_xyz = {x=side_length, y=side_length, z=side_length}
   local perlin_map = minetest.get_perlin_map(testparam, map_lengths_xyz):get2dMap_flat(minp)

   --loop
   local perlin_index = 1
   for z = minp.z, maxp.z do
      for y = minp.y, maxp.y do
         local vi = area:index(minp.x, y, z)
         for x = minp.x, maxp.x do
             print("Some spam value: "..tostring(perlin_map[vi])
             --more efficient coding - x++
             perlin_index = perlin_index + 1
             vi = vi + 1
         end
         --go back, one side_length
         perlin_index = perlin_index - side_length
      end
      --go forward, one side_length
      perlin_index = perlin_index + side_length
   end
end)
Mbox important.png This article is incomplete.
Please help expand this article to include more useful information.