Advertisement
Alakazard12

Random Noise

Jun 5th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.43 KB | None | 0 0
  1. local component = require("component")
  2. local holo = component.hologram
  3.  
  4. local noiseWidth = 48
  5. local noiseHeight = 48
  6. local maxHeight = 32
  7.  
  8. local noise = {}
  9. for i = 0, noiseWidth - 1 do
  10.     noise[i] = {}
  11. end
  12.  
  13. local function generateNoise()
  14.     for x = 0, noiseWidth - 1 do
  15.         for y = 0, noiseHeight - 1 do
  16.             noise[x][y] = math.random()
  17.         end
  18.     end
  19. end
  20.  
  21. local function smoothNoise(x, y)
  22.     local fractX = x - math.floor(x)
  23.     local fractY = y - math.floor(y)
  24.  
  25.     local x1 = (math.floor(x) + noiseWidth) % noiseWidth
  26.     local y1 = (math.floor(y) + noiseHeight) % noiseHeight
  27.  
  28.     local x2 = (x1 + noiseWidth - 1) % noiseWidth
  29.     local y2 = (y1 + noiseHeight - 1) % noiseHeight
  30.  
  31.     local value = 0
  32.     value = value + fractX * fractY * noise[x1][y1]
  33.     value = value + fractX * (1 - fractY) * noise[x1][y2]
  34.     value = value + (1 - fractX) * fractY * noise[x2][y1]
  35.     value = value + (1 - fractX) * (1 - fractY) * noise[x2][y2]
  36.  
  37.     return value
  38. end
  39.  
  40. local function turbulence(x, y, size)
  41.     local value = 0
  42.     local iSize = size
  43.  
  44.     while (size >= 1) do
  45.         value = value + smoothNoise(x / size, y / size) * size
  46.         size = size / 2
  47.     end
  48.  
  49.     return value / iSize / 2
  50. end
  51.  
  52. generateNoise()
  53. holo.clear()
  54.  
  55. local m = 0
  56.  
  57. for x = 0, noiseWidth - 1 do
  58.     for y = 0, noiseHeight - 1 do
  59.         local h = math.floor(maxHeight * turbulence(x, y, 64))
  60.        
  61.         local mask = 0
  62.         for i = 0, h do
  63.             mask = mask + 2^i
  64.         end
  65.  
  66.         holo.set(x + 1, y + 1, mask)
  67.     end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement