Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Constants
- local CHUNK_SIZE = 32
- local WORLD_SIZE = 8
- local BLOCK_SIZE = 2
- local HEIGHT_SCALE = 10 -- Controls the height of the terrain
- local NOISE_SCALE = 0.05 -- Adjust for more or less noise detail
- local NOISE_OCTAVES = 3 -- Controls the complexity of the noise
- local START_POSITION = Vector3.new(0, 50, 0) -- Starting position for the terrain generation
- -- Function to generate terrain using Perlin noise
- local function generateTerrain()
- for chunkX = 0, WORLD_SIZE - 1 do
- for chunkY = 0, WORLD_SIZE - 1 do
- local startX = chunkX * CHUNK_SIZE
- local startY = chunkY * CHUNK_SIZE
- for x = 0, CHUNK_SIZE - 1 do
- for y = 0, CHUNK_SIZE - 1 do
- local worldX = startX + x
- local worldY = startY + y
- -- Generate height using Perlin noise
- local noiseValue = 0
- local amplitude = 1
- local frequency = NOISE_SCALE
- -- Combine octaves for more complex terrain
- for octave = 1, NOISE_OCTAVES do
- noiseValue = noiseValue + amplitude * math.noise(worldX * frequency, worldY * frequency)
- amplitude = amplitude * 0.5
- frequency = frequency * 2
- end
- -- Normalize noise to [0, 1] and scale
- noiseValue = (noiseValue + 1) / 2
- local height = noiseValue * HEIGHT_SCALE
- -- Create blocks based on calculated height
- for h = 0, height - 1 do
- local block = Instance.new("Part")
- block.Size = Vector3.new(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
- block.Position = START_POSITION + Vector3.new(worldX * BLOCK_SIZE, h * BLOCK_SIZE, worldY * BLOCK_SIZE)
- block.Anchored = true
- block.BrickColor = BrickColor.new("Light green") -- Set to Light Green
- block.Material = Enum.Material.Mud
- block.Parent = workspace
- end
- -- Create grass block at the top
- local grassBlock = Instance.new("Part")
- grassBlock.Size = Vector3.new(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
- grassBlock.Position = START_POSITION + Vector3.new(worldX * BLOCK_SIZE, height * BLOCK_SIZE, worldY * BLOCK_SIZE)
- grassBlock.Anchored = true
- grassBlock.BrickColor = BrickColor.Green() -- Change to match your grass block color
- grassBlock.Material = Enum.Material.Grass
- grassBlock.Parent = workspace
- end
- end
- end
- end
- end
- -- Generate the entire world
- generateTerrain()
Advertisement
Add Comment
Please, Sign In to add comment