Advertisement
Guest User

Perlin noise on ROBLOX

a guest
Nov 2nd, 2020
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.60 KB | None | 0 0
  1. local mapSize = 500
  2. local noise = math.noise
  3. local seed = tick()
  4. local heightMap = table.create(mapSize, table.create(mapSize))
  5. local abs = math.abs
  6. local colorTable = {
  7.     [0] = BrickColor.Blue();
  8.     [1] = BrickColor.new("Beige");
  9.     [2] = BrickColor.Green();
  10.     [3] = BrickColor.DarkGray();
  11.     [4] = BrickColor.White();  
  12. }
  13.  
  14. --SETTINGS
  15.  
  16. wait(3)
  17.  
  18. local FREQUENCY =       2;
  19. local REDISTRIBUTION =  1;
  20. local PERSISTANCE =     0.5;
  21. local OCTAVES =         5;
  22. local LACUNARITY =      2;
  23. local AMPLITUDE =       500;
  24.  
  25. local function ridgeNoise(nx, ny)
  26.    
  27.     return noise(nx, ny, seed) --2 * (0.5 - abs(0.5 - noise(nx, ny, seed)));
  28.        
  29. end
  30.  
  31. local function getBiome(hm)
  32.     if hm < 0.05 then
  33.         return 0
  34.     elseif hm < 0.1 then
  35.         return 1
  36.     elseif hm < 0.2 then
  37.         return 2
  38.     elseif hm < 0.3 then
  39.         return 3
  40.     else
  41.         return 4
  42.     end
  43. end
  44.  
  45. for x = 1,mapSize do
  46.     if x % 10 == 0 then
  47.         game:GetService("RunService").Heartbeat:Wait()
  48.     end
  49.     for y = 1,mapSize do
  50.         local nx, ny = x/mapSize - 0.5, y/mapSize - 0.5
  51.         local elevation = 0
  52.        
  53.         for i = 1,OCTAVES do
  54.             local newElev = PERSISTANCE^i * ridgeNoise(nx * FREQUENCY * LACUNARITY^(i-1),ny * FREQUENCY * LACUNARITY^(i-1))
  55.            
  56.             elevation += newElev
  57.                
  58.         end
  59.        
  60.         heightMap[x][y] = math.pow(elevation, REDISTRIBUTION);
  61.        
  62.         local biome = getBiome(heightMap[x][y])
  63.        
  64.         local part = Instance.new("Part")
  65.         part.Anchored = true
  66.         part.Size = Vector3.new(1,50,1)
  67.         part.Position = Vector3.new(x, math.clamp(heightMap[x][y],0.05,10000)*AMPLITUDE, y);
  68.         part.BrickColor = colorTable[biome]
  69.         part.Material = Enum.Material.SmoothPlastic
  70.         part.Parent = workspace
  71.     end
  72. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement