Skip to content

Instantly share code, notes, and snippets.

@DataKinds
Last active August 29, 2015 14:23
Show Gist options
  • Save DataKinds/964ff2c8951484687ffe to your computer and use it in GitHub Desktop.
Save DataKinds/964ff2c8951484687ffe to your computer and use it in GitHub Desktop.
Roblox terrain generator
local areaSize = 700 --128
local brickSize = 4 --4
local noiseScale = 64 --32
local noiseMagnitude = 50 --50
local aboutHeight = 0.65 --0.65
local playerGenRadius = 128 --64
--the level indicates the bottom of the region
local snowLevel = 128
local snowBottom = 40 --snow is generated differently
local rockLevel = 64
local grassLevel = 12
local beachLevel = 8
local waterLevel = 7
local topSeedX = math.random() --screw DRY
local topSeedY = math.random()
local midSeedX = math.random()
local midSeedY = math.random()
local lowSeedX = math.random()
local lowSeedY = math.random()
local biomeSeedX = math.random()
local biomeSeedY = math.random()
--local g = Instance.new("Model", game.Workspace)
function genTerrainBlock(i, j)
--[[
local b = Instance.new("Part", g)
b.Size = Vector3.new(brickSize,height,brickSize)
b.Position = Vector3.new(i*brickSize,height/2,j*brickSize)
b.Anchored = true
local brickColor = BrickColor.new("Deep blue") --fill everything unchanged with water
]]--
local noiseLayers = {(math.noise(i/noiseScale+topSeedX, j/noiseScale+topSeedY)+0.5)*noiseMagnitude,
(math.noise(i/(noiseScale/2)+midSeedX, j/(noiseScale/2)+midSeedY)+0.5)*noiseMagnitude*0.7,
(math.noise(i/(noiseScale/5)+lowSeedX, j/(noiseScale/5)+lowSeedY)+0.5)*noiseMagnitude*0.4
}
local height = 0.0
for k,v in pairs(noiseLayers) do
height = height + v
end
if height < 1 then
height = 2
end
height = math.ceil(math.pow(height, (math.noise(i/(noiseScale*4)+biomeSeedX, j/(noiseScale*4)+biomeSeedY)+aboutHeight)))
--print(tostring(height))
if height < 7 then
height = 6
end
local terrainColor = Enum.Material.Water
if height >= beachLevel and height < grassLevel then
--brickColor = BrickColor.new("Pastel yellow")
terrainColor = Enum.Material.Sand
elseif height >= grassLevel and height < rockLevel then
--brickColor = BrickColor.new("Camo")
if math.noise(i/noiseScale+biomeSeedX, j/noiseScale+biomeSeedY) > 0 then
terrainColor = Enum.Material.Grass
else
terrainColor = Enum.Material.Ground
end
elseif height >= rockLevel and height < snowLevel then
--brickColor = BrickColor.new("Dark stone grey")
terrainColor = 896 --Enum.Material.Rock isn't a thing apparently
elseif height >= snowLevel then
--brickColor = BrickColor.new("Institutional white")
terrainColor = Enum.Material.Snow
end
--add snow
if height >= snowBottom then
if math.random() < (height-snowBottom)/(snowLevel-snowBottom) then
terrainColor = Enum.Material.Snow
end
end
game.Workspace.Terrain:FillRegion(Region3.new(Vector3.new(i*brickSize,0,j*brickSize),
Vector3.new(i*brickSize+brickSize,height,j*brickSize+brickSize)):ExpandToGrid(4),
4, terrainColor)
--b.BrickColor = brickColor
--game.Workspace.Terrain:FillBlock(b.CFrame, b.Size, terrainColor)
--b:Remove()
end
for i=-areaSize,areaSize do
for j=-areaSize,areaSize do
genTerrainBlock(i,j)
end
end
--[[
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
while player ~= nil do
local cPos = character.Head.Position
for i=cPos.X-playerGenRadius,cPos.X+playerGenRadius do
for j=cPos.Z-playerGenRadius,cPos.Z+playerGenRadius do
local material, occupancy = game.Workspace.Terrain:ReadVoxels(
Region3.new(
Vector3.new(i, 0, j),
Vector3.new(i+0.1, 0.1, j+0.1) --epsilon
):ExpandToGrid(4), 4)
if material[1][1][1] == Enum.Material.Air then
genTerrainBlock(i/brickSize,j/brickSize)
end
end
end
wait()
end
end)
end)
--]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment