Advertisement
Guest User

PerlinNoiseTerrain

a guest
Nov 20th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 KB | None | 0 0
  1. -- Made by Dev_Ryan (Roblox Dev) - Nov. 20, 2017
  2.  
  3. -- settings:
  4. local xSize = 512 -- ideal: ~500
  5. local ySize = 512 -- ideal: ~500
  6. local scale = 16 -- ideal: ~20 (spacing between dots)
  7. local spread = 32 -- ideal: ~20 (how high dots go)
  8. local speed = 0.05 -- ideal: ~0.05 (while wait(speed) do)
  9. local flySpeed = 0.02 -- ideal: ~0.05 (0.1 is slightly faster)
  10. local offset = 0.2 -- ideal: ~0.2 (spike width)
  11.  
  12. local color = BrickColor.new("Cyan") -- color of dots and lines
  13.  
  14. ---------- Dont really need to change anything below.. ------------------
  15. -- (yes I know I am not using the best methods but its something to work from..)
  16.  
  17. local boolName = "Start/Stop"
  18. if workspace:FindFirstChild(boolName) then workspace[boolName]:Destroy() end
  19. local BV = Instance.new("BoolValue") -- click this in workspace to start/stop the animation.
  20. BV.Name = boolName
  21. BV.Parent = workspace
  22.  
  23. local noiseTbl = {}
  24. local terrainTbl = {}
  25.  
  26. local folderName = "TerrainData"
  27. if workspace:FindFirstChild(folderName) then workspace[folderName]:Destroy() end
  28. local terrainFolder = Instance.new("Folder")
  29. terrainFolder.Name = folderName
  30. terrainFolder.Parent = workspace
  31.  
  32. local cf = CFrame.new
  33.  
  34. local function createRope(p1, p2)
  35.     local a1 = Instance.new("Attachment")
  36.     a1.Parent = p1
  37.     local a2 = Instance.new("Attachment")
  38.     a2.Parent = p2
  39.     local rope = Instance.new("RopeConstraint")
  40.     rope.Color = color
  41.     rope.Thickness = 0.5
  42.     rope.Parent = p1
  43.     rope.Attachment0 = a1
  44.     rope.Attachment1 = a2
  45.     rope.Visible = true
  46. end
  47.  
  48. local rows = 0 -- keep zero
  49. local columns = 0 -- keep zero
  50.  
  51. local function setup()
  52.     columns = xSize / scale
  53.     rows = ySize / scale
  54.     for x = 1, columns do
  55.         noiseTbl[columns] = {}
  56.         for y = 1, rows do
  57.             noiseTbl[columns][rows] = 0
  58.         end
  59.     end
  60.     local p = Instance.new("Part")
  61.     p.BrickColor = color
  62.     p.Material = Enum.Material.SmoothPlastic -- Enum.Material.Neon
  63.     p.Size = Vector3.new(1,1,1)
  64.     p.Shape = Enum.PartType.Ball
  65.     p.TopSurface = Enum.SurfaceType.Smooth
  66.     p.BottomSurface = Enum.SurfaceType.Smooth
  67.     p.Anchored = true
  68.     p.CanCollide = false
  69.     p.Parent = terrainFolder
  70.     for xpos = 1, columns do wait()
  71.         terrainTbl[xpos] = {}
  72.         for ypos = 1, rows do
  73.             local c = p:Clone()
  74.             c.Parent = terrainFolder
  75.             c.CFrame = cf((xpos*scale)-(xSize/2), 0, (ypos*scale)-(ySize/2))
  76.             terrainTbl[xpos][ypos] = c
  77.             if xpos > 1 then
  78.             createRope(c, terrainTbl[xpos-1][ypos])
  79.             end
  80.             if ypos > 1 then
  81.             createRope(c, terrainTbl[xpos][ypos-1])
  82.             end
  83.             if xpos > 1 and ypos > 1 then
  84.             createRope(c, terrainTbl[xpos-1][ypos-1])
  85.             end
  86.         end
  87.     end
  88.     wait()
  89.     p:Destroy()
  90. end
  91. setup()
  92.  
  93.  
  94. local noise = math.noise
  95. local clamp = math.clamp
  96.  
  97. local function getNoise(x,y)
  98.     local n = noise(x,y)
  99.     n = n * spread
  100.     n = clamp(n, -spread, spread)
  101.     return n
  102. end
  103.  
  104. repeat wait(1) until BV.Value
  105. print("Started!")
  106.  
  107. local flying = 0 -- keep zero.
  108. while true do wait(speed)
  109.     if not BV.Value then
  110.         print("Paused!")
  111.         repeat wait(1) until BV.Value
  112.         print("Resumed!")
  113.     end
  114.     flying = flying - flySpeed
  115.     local yOffset = flying
  116.     for y = 1, rows-1 do
  117.         local xOffset = 0
  118.         for x = 1, columns-1 do
  119.             if type(terrainTbl[x][y]) == "userdata" then
  120.                 local getcf = terrainTbl[x][y].CFrame
  121.                 if getcf then
  122.                     terrainTbl[x][y].CFrame = cf(getcf.x, getNoise(xOffset,yOffset), getcf.z)
  123.                 end
  124.             end
  125.             xOffset = xOffset + offset
  126.         end
  127.         yOffset = yOffset + offset
  128.     end
  129. end
  130. print("Stopped!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement