Last active
February 3, 2022 00:26
-
-
Save BomberFish/b4be64291c5553f4d165ebd433a7e44d to your computer and use it in GitHub Desktop.
Perlin Noise Terrain Generation in Roblox! See it in action at https://www.roblox.com/games/8713990328/Perlin-Noise-Terrain-Generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
PERLIN NOISE TERRAIN GENERATION | |
This script generates terrain using Roblox's built-in math.noise() function. | |
It creates small parts, which will act as terrain. | |
TO DO: | |
-Render around the player instead of rendering everything! | |
-Infinite worlds | |
]]-- | |
--Get seed by using os.clock() | |
math.randomseed(os.clock()) | |
--Init variables | |
--Players service | |
local Players = game:GetService("Players") | |
--Local copies of math funcs for L E S P E E D | |
local floor = math.floor | |
local ceil = math.ceil | |
local sqrt = math.sqrt | |
local sin = math.sin | |
local cos = math.cos | |
local max = math.max | |
local min = math.min | |
local abs = math.abs | |
local random = math.random | |
--Width,Depth | |
local width = 512 | |
local depth = 512 | |
local widthstep1 = 362 | |
local depthstep1 = 362 | |
local widthstep2 = 256 | |
local depthstep2 = 256 | |
local widthstep3 = 192 | |
local depthstep3 = 192 | |
local widthpreload = 128 | |
local depthpreload = 128 | |
local widthpreload2 = 64 | |
local depthpreload2 = 64 | |
local widthpreload3 = 16 | |
local depthpreload3 = 16 | |
--Noise stuff | |
local scale = 200 | |
local amplitude = 2500 | |
--Size of each part that make up the terrain | |
--Make note that the size is calculated as Vector3(partSize, partSize, partSize) | |
local partSize = 3 | |
--Terrain Folder (under workspace) | |
local terrainFolder = workspace:FindFirstChild("GeneratedTerrain") | |
terrainFolder:ClearAllChildren() | |
--Let's get generating! | |
--Seed | |
local seed = random() | |
print(seed) | |
for w=widthpreload3 * -1,widthpreload3 do | |
for d=depthpreload3 * -1,depthpreload3 do | |
local height = abs(math.noise(((w / scale) + floor(ceil(sqrt(scale * 2)))), ((d / scale) + floor(ceil(sqrt(scale * 2))))) * scale) * 1.25 | |
local part = Instance.new("Part", terrainFolder) | |
part.Anchored = true | |
part.Size = Vector3.new(partSize, partSize, partSize) | |
part.Position = Vector3.new(w*partSize, height, d*partSize) | |
part.BrickColor = BrickColor.new("Dark green") | |
part.Material = "Grass" | |
end | |
wait() | |
end | |
for w=widthpreload2 * -1,widthpreload2 do | |
for d=depthpreload2 * -1,depthpreload2 do | |
local height = abs(math.noise(((w / scale) + floor(ceil(sqrt(scale * 2)))), ((d / scale) + floor(ceil(sqrt(scale * 2))))) * scale) * 1.25 | |
local part = Instance.new("Part", terrainFolder) | |
part.Anchored = true | |
part.Size = Vector3.new(partSize, partSize, partSize) | |
part.Position = Vector3.new(w*partSize, height, d*partSize) | |
part.BrickColor = BrickColor.new("Dark green") | |
part.Material = "Grass" | |
end | |
wait() | |
end | |
for w=widthpreload * -1,widthpreload do | |
for d=depthpreload * -1,depthpreload do | |
local height = abs(math.noise(((w / scale) + floor(ceil(sqrt(scale * 2)))), ((d / scale) + floor(ceil(sqrt(scale * 2))))) * scale) * 1.25 | |
local part = Instance.new("Part", terrainFolder) | |
part.Anchored = true | |
part.Size = Vector3.new(partSize, partSize, partSize) | |
part.Position = Vector3.new(w*partSize, height, d*partSize) | |
part.BrickColor = BrickColor.new("Dark green") | |
part.Material = "Grass" | |
end | |
wait() | |
end | |
for w=width * -1,width do | |
for d=depth * -1,depth do | |
local height = abs(math.noise(((w / scale) + floor(ceil(sqrt(scale * 2)))), ((d / scale) + floor(ceil(sqrt(scale * 2))))) * scale) * 1.25 | |
local part = Instance.new("Part", terrainFolder) | |
part.Anchored = true | |
part.Size = Vector3.new(partSize, partSize, partSize) | |
part.Position = Vector3.new(w*partSize, height, d*partSize) | |
part.BrickColor = BrickColor.new("Dark green") | |
part.Material = "Grass" | |
end | |
wait() | |
end | |
print("Done!")--[[ | |
PERLIN NOISE TERRAIN GENERATION | |
This script generates terrain using Roblox's built-in math.noise() function. | |
It creates small parts, which will act as terrain. | |
TO DO: | |
-Render around the player instead of rendering everything! | |
-Infinite worlds | |
]]-- | |
--Get seed by using os.clock() | |
math.randomseed(os.clock()) | |
--Init variables | |
--Players service | |
local Players = game:GetService("Players") | |
--Local copies of math funcs for L E S P E E D | |
local floor = math.floor | |
local ceil = math.ceil | |
local sqrt = math.sqrt | |
local sin = math.sin | |
local cos = math.cos | |
local max = math.max | |
local min = math.min | |
local abs = math.abs | |
local random = math.random | |
--Width,Depth | |
local width = 512 | |
local depth = 512 | |
local widthstep1 = 362 | |
local depthstep1 = 362 | |
local widthstep2 = 256 | |
local depthstep2 = 256 | |
local widthstep3 = 192 | |
local depthstep3 = 192 | |
local widthpreload = 128 | |
local depthpreload = 128 | |
local widthpreload2 = 64 | |
local depthpreload2 = 64 | |
local widthpreload3 = 16 | |
local depthpreload3 = 16 | |
--Noise stuff | |
local scale = 200 | |
local amplitude = 2500 | |
--Size of each part that make up the terrain | |
--Make note that the size is calculated as Vector3(partSize, partSize, partSize) | |
local partSize = 3 | |
--Terrain Folder (under workspace) | |
local terrainFolder = workspace:FindFirstChild("GeneratedTerrain") | |
terrainFolder:ClearAllChildren() | |
--Let's get generating! | |
--Seed | |
local seed = random() | |
print(seed) | |
for w=widthpreload3 * -1,widthpreload3 do | |
for d=depthpreload3 * -1,depthpreload3 do | |
local height = abs(math.noise(((w / scale) + floor(ceil(sqrt(scale * 2)))), ((d / scale) + floor(ceil(sqrt(scale * 2))))) * scale) * 1.25 | |
local part = Instance.new("Part", terrainFolder) | |
part.Anchored = true | |
part.Size = Vector3.new(partSize, partSize, partSize) | |
part.Position = Vector3.new(w*partSize, height, d*partSize) | |
part.BrickColor = BrickColor.new("Dark green") | |
part.Material = "Grass" | |
end | |
wait() | |
end | |
for w=widthpreload2 * -1,widthpreload2 do | |
for d=depthpreload2 * -1,depthpreload2 do | |
local height = abs(math.noise(((w / scale) + floor(ceil(sqrt(scale * 2)))), ((d / scale) + floor(ceil(sqrt(scale * 2))))) * scale) * 1.25 | |
local part = Instance.new("Part", terrainFolder) | |
part.Anchored = true | |
part.Size = Vector3.new(partSize, partSize, partSize) | |
part.Position = Vector3.new(w*partSize, height, d*partSize) | |
part.BrickColor = BrickColor.new("Dark green") | |
part.Material = "Grass" | |
end | |
wait() | |
end | |
for w=widthpreload * -1,widthpreload do | |
for d=depthpreload * -1,depthpreload do | |
local height = abs(math.noise(((w / scale) + floor(ceil(sqrt(scale * 2)))), ((d / scale) + floor(ceil(sqrt(scale * 2))))) * scale) * 1.25 | |
local part = Instance.new("Part", terrainFolder) | |
part.Anchored = true | |
part.Size = Vector3.new(partSize, partSize, partSize) | |
part.Position = Vector3.new(w*partSize, height, d*partSize) | |
part.BrickColor = BrickColor.new("Dark green") | |
part.Material = "Grass" | |
end | |
wait() | |
end | |
for w=width * -1,width do | |
for d=depth * -1,depth do | |
local height = abs(math.noise(((w / scale) + floor(ceil(sqrt(scale * 2)))), ((d / scale) + floor(ceil(sqrt(scale * 2))))) * scale) * 1.25 | |
local part = Instance.new("Part", terrainFolder) | |
part.Anchored = true | |
part.Size = Vector3.new(partSize, partSize, partSize) | |
part.Position = Vector3.new(w*partSize, height, d*partSize) | |
part.BrickColor = BrickColor.new("Dark green") | |
part.Material = "Grass" | |
end | |
wait() | |
end | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment