Terrain
Summary
Properties
MaterialColors:BinaryString |
Methods
Inherited Members
106 inherited from BasePart
4 inherited from PVInstance
58 inherited from Instance
6 inherited from Object
API Reference
Properties
Is Smooth
Material Colors
Terrain. Material Colors:BinaryString
Methods
Autowedge Cell
Autowedge Cells
Cell Center To World
Cell Corner To World
Clear
Terrain: Clear():()
Returns
()
Convert To Smooth
Copy Region
Parameters
Returns
Code Samples
Terrain:CopyRegion()
local terrainRegion = workspace.Terrain:CopyRegion(workspace.Terrain.MaxExtents)
workspace.Terrain:Clear()
task.wait(5)
workspace.Terrain:PasteRegion(terrainRegion, workspace.Terrain.MaxExtents.Min, true)Fill Ball
Parameters
Returns
()
Fill Block
Parameters
Returns
()
Fill Cylinder
Parameters
Returns
()
Fill Region
Parameters
Returns
()
Fill Wedge
Parameters
Returns
()
Get Cell
Get Material Color
Parameters
Returns
Get Material Slot
Parameters
Returns
Code Samples
Terrain Material Slots
local MaterialService = game:GetService("MaterialService")
local Terrain = workspace.Terrain
-- Slots 2 through 63 are writable; slots 0 and 1 are reserved for Air and Water
local SLOT_INDEX = 2
local BASE_MATERIAL = Enum.Material.Grass
local VARIANT_NAME = "SwampVariant"
-- Find an existing MaterialVariant for the base material, or create one
-- SetMaterialSlot() expects the variant's Name, not the Material Manager's display name
local variant = MaterialService:FindFirstChild(VARIANT_NAME)
if not variant then
variant = Instance.new("MaterialVariant")
variant.Name = VARIANT_NAME
variant.BaseMaterial = BASE_MATERIAL
variant.Parent = MaterialService
end
-- Configure the slot with the base material, the MaterialVariant name, and a tint
-- Pass an empty string instead of a variant name to use the base material's default appearance tinted by the given color
Terrain:SetMaterialSlot(SLOT_INDEX, BASE_MATERIAL, variant.Name, Color3.fromRGB(50, 60, 30))
local baseMaterial, materialVariant, color = Terrain:GetMaterialSlot(SLOT_INDEX)
print(baseMaterial, materialVariant, color)
-- Write a region of voxels that reference the slot by index, instead of by Enum.Material, using the SolidMaterialIndex channel
local region = Region3.new(Vector3.new(0, 0, 0), Vector3.new(16, 16, 16)):ExpandToGrid(4)
local size = region.Size / 4
local materialIndices = {}
local occupancies = {}
for x = 1, size.X do
materialIndices[x] = {}
occupancies[x] = {}
for y = 1, size.Y do
materialIndices[x][y] = {}
occupancies[x][y] = {}
for z = 1, size.Z do
materialIndices[x][y][z] = SLOT_INDEX
occupancies[x][y][z] = 1
end
end
end
Terrain:WriteVoxelChannels(region, 4, {
SolidMaterialIndex = materialIndices,
SolidOccupancy = occupancies,
})
-- Clearing the slot leaves voxels that reference it rendering as invalid until they're repainted with a valid slot index
Terrain:ResetMaterialSlot(SLOT_INDEX)Get Water Cell
Paste Region
Parameters
Returns
()
Code Samples
Create, Copy and Paste Terrain
local Terrain = workspace.Terrain
-- Create a simple terrain region
local initialRegion = Region3.new(Vector3.zero, Vector3.one * 10)
Terrain:FillRegion(initialRegion, 4, Enum.Material.Grass)
-- Copy the region
local copyRegion = Region3int16.new(Vector3int16.new(0, 0, 0), Vector3int16.new(10, 10, 10))
local copiedRegion = Terrain:CopyRegion(copyRegion)
-- Define where to paste the region
local newRegionCorner = Vector3int16.new(5, 0, 0)
-- Paste the region
Terrain:PasteRegion(copiedRegion, newRegionCorner, true)Read Voxel Channels
Returns
Code Samples
Terrain:ReadVoxelChannels()
local REGION_START = Vector3.new(-20, -20, -20)
local REGION_END = Vector3.new(20, 20, 20)
local function printRegion(terrain, region)
local channelOutput = terrain:ReadVoxelChannels(region, 4, { "SolidOccupancy", "SolidMaterial", "LiquidOccupancy" })
local size = channelOutput.Size
for x = 1, size.X do
for y = 1, size.Y do
for z = 1, size.Z do
print(
("(%2i, %2i, %2i): %.2f %s %.2f"):format(
x,
y,
z,
channelOutput.SolidOccupancy[x][y][z],
channelOutput.SolidMaterial[x][y][z].Name,
channelOutput.LiquidOccupancy[x][y][z]
)
)
end
end
end
end
local region = Region3.new(REGION_START, REGION_END)
printRegion(workspace.Terrain, region)Read Voxels
Returns
Code Samples
Terrain:ReadVoxels()
local REGION_START = Vector3.new(-20, -20, -20)
local REGION_END = Vector3.new(20, 20, 20)
local function printRegion(terrain, region)
local materials, occupancies = terrain:ReadVoxels(region, 4)
local size = materials.Size -- Same as occupancies.Size
for x = 1, size.X, 1 do
for y = 1, size.Y, 1 do
for z = 1, size.Z, 1 do
print(("(%2i, %2i, %2i): %.2f %s"):format(x, y, z, occupancies[x][y][z], materials[x][y][z].Name))
end
end
end
end
local region = Region3.new(REGION_START, REGION_END)
printRegion(workspace.Terrain, region)Replace Material
Terrain: Replace Material(
):()
Parameters
Returns
()
Code Samples
Terrain:ReplaceMaterial
local terrain = workspace.Terrain
local region = Region3.new(Vector3.new(-20, -20, -20), Vector3.new(20, 20, 20))
local resolution = 4
local materialToReplace = Enum.Material.Grass
local replacementMaterial = Enum.Material.Asphalt
terrain:ReplaceMaterial(region, resolution, materialToReplace, replacementMaterial)Reset Material Slot
Parameters
Returns
()
Code Samples
Terrain Material Slots
local MaterialService = game:GetService("MaterialService")
local Terrain = workspace.Terrain
-- Slots 2 through 63 are writable; slots 0 and 1 are reserved for Air and Water
local SLOT_INDEX = 2
local BASE_MATERIAL = Enum.Material.Grass
local VARIANT_NAME = "SwampVariant"
-- Find an existing MaterialVariant for the base material, or create one
-- SetMaterialSlot() expects the variant's Name, not the Material Manager's display name
local variant = MaterialService:FindFirstChild(VARIANT_NAME)
if not variant then
variant = Instance.new("MaterialVariant")
variant.Name = VARIANT_NAME
variant.BaseMaterial = BASE_MATERIAL
variant.Parent = MaterialService
end
-- Configure the slot with the base material, the MaterialVariant name, and a tint
-- Pass an empty string instead of a variant name to use the base material's default appearance tinted by the given color
Terrain:SetMaterialSlot(SLOT_INDEX, BASE_MATERIAL, variant.Name, Color3.fromRGB(50, 60, 30))
local baseMaterial, materialVariant, color = Terrain:GetMaterialSlot(SLOT_INDEX)
print(baseMaterial, materialVariant, color)
-- Write a region of voxels that reference the slot by index, instead of by Enum.Material, using the SolidMaterialIndex channel
local region = Region3.new(Vector3.new(0, 0, 0), Vector3.new(16, 16, 16)):ExpandToGrid(4)
local size = region.Size / 4
local materialIndices = {}
local occupancies = {}
for x = 1, size.X do
materialIndices[x] = {}
occupancies[x] = {}
for y = 1, size.Y do
materialIndices[x][y] = {}
occupancies[x][y] = {}
for z = 1, size.Z do
materialIndices[x][y][z] = SLOT_INDEX
occupancies[x][y][z] = 1
end
end
end
Terrain:WriteVoxelChannels(region, 4, {
SolidMaterialIndex = materialIndices,
SolidOccupancy = occupancies,
})
-- Clearing the slot leaves voxels that reference it rendering as invalid until they're repainted with a valid slot index
Terrain:ResetMaterialSlot(SLOT_INDEX)Set Cell
Set Cells
Set Material Color
Parameters
Returns
()
Set Material Slot
Terrain: Set Material Slot(
):()
Parameters
Returns
()
Code Samples
Terrain Material Slots
local MaterialService = game:GetService("MaterialService")
local Terrain = workspace.Terrain
-- Slots 2 through 63 are writable; slots 0 and 1 are reserved for Air and Water
local SLOT_INDEX = 2
local BASE_MATERIAL = Enum.Material.Grass
local VARIANT_NAME = "SwampVariant"
-- Find an existing MaterialVariant for the base material, or create one
-- SetMaterialSlot() expects the variant's Name, not the Material Manager's display name
local variant = MaterialService:FindFirstChild(VARIANT_NAME)
if not variant then
variant = Instance.new("MaterialVariant")
variant.Name = VARIANT_NAME
variant.BaseMaterial = BASE_MATERIAL
variant.Parent = MaterialService
end
-- Configure the slot with the base material, the MaterialVariant name, and a tint
-- Pass an empty string instead of a variant name to use the base material's default appearance tinted by the given color
Terrain:SetMaterialSlot(SLOT_INDEX, BASE_MATERIAL, variant.Name, Color3.fromRGB(50, 60, 30))
local baseMaterial, materialVariant, color = Terrain:GetMaterialSlot(SLOT_INDEX)
print(baseMaterial, materialVariant, color)
-- Write a region of voxels that reference the slot by index, instead of by Enum.Material, using the SolidMaterialIndex channel
local region = Region3.new(Vector3.new(0, 0, 0), Vector3.new(16, 16, 16)):ExpandToGrid(4)
local size = region.Size / 4
local materialIndices = {}
local occupancies = {}
for x = 1, size.X do
materialIndices[x] = {}
occupancies[x] = {}
for y = 1, size.Y do
materialIndices[x][y] = {}
occupancies[x][y] = {}
for z = 1, size.Z do
materialIndices[x][y][z] = SLOT_INDEX
occupancies[x][y][z] = 1
end
end
end
Terrain:WriteVoxelChannels(region, 4, {
SolidMaterialIndex = materialIndices,
SolidOccupancy = occupancies,
})
-- Clearing the slot leaves voxels that reference it rendering as invalid until they're repainted with a valid slot index
Terrain:ResetMaterialSlot(SLOT_INDEX)Set Water Cell
World To Cell Prefer Empty
World To Cell Prefer Solid
Write Voxel Channels
Parameters
Returns
()
Code Samples
Terrain:WriteVoxelChannels()
local region = Region3.new(Vector3.new(0, 0, 0), Vector3.new(64, 32, 64))
local RESOLUTION = 4
local OCC_EPSILON = 1 / 256
local function generateRandomTerrainInRegion(regionInput)
local region = regionInput:ExpandToGrid(RESOLUTION)
local size = region.Size / RESOLUTION
local solidMaterials = {}
local solidOccupancies = {}
local waterOcc = {}
for x = 1, size.X do
table.insert(solidMaterials, {})
table.insert(solidOccupancies, {})
table.insert(waterOcc, {})
for y = 1, size.Y do
table.insert(solidMaterials[x], {})
table.insert(solidOccupancies[x], {})
table.insert(waterOcc[x], {})
for z = 1, size.Z do
local mat = if math.random() < 0.5 then Enum.Material.Air else Enum.Material.Sand
local occ = 0
local water = math.random()
if mat == Enum.Material.Sand then
occ = math.random() / 2 + 0.5
if occ > 1 - OCC_EPSILON then
water = 0 -- Solids cannot contain water
end
else
occ = 0
end
table.insert(solidMaterials[x][y], mat)
table.insert(solidOccupancies[x][y], occ)
table.insert(waterOcc[x][y], water)
end
end
end
return { SolidMaterial = solidMaterials, SolidOccupancy = solidOccupancies, LiquidOccupancy = waterOcc }
end
local regionContent = generateRandomTerrainInRegion(region)
workspace.Terrain:WriteVoxelChannels(region, RESOLUTION, regionContent)Write Voxels
Returns
()
Code Samples
Terrain:WriteVoxels()
local terrain = workspace.Terrain
local resolution = 4
local region = Region3.new(Vector3.new(0, 0, 0), Vector3.new(16, 28, 20)):ExpandToGrid(resolution)
local materials = {
{
{
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
},
{ Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock },
{ Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock },
{ Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand },
{ Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand },
{ Enum.Material.Mud, Enum.Material.Mud, Enum.Material.Mud, Enum.Material.Mud, Enum.Material.Mud },
{ Enum.Material.Air, Enum.Material.Air, Enum.Material.Air, Enum.Material.Air, Enum.Material.Air },
},
{
{
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
},
{ Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock },
{ Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock },
{ Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand },
{ Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand },
{ Enum.Material.Mud, Enum.Material.Snow, Enum.Material.Snow, Enum.Material.Snow, Enum.Material.Mud },
{ Enum.Material.Air, Enum.Material.Snow, Enum.Material.Snow, Enum.Material.Snow, Enum.Material.Air },
},
{
{
Enum.Material.CrackedLava,
Enum.Material.Sand,
Enum.Material.Sand,
Enum.Material.Sand,
Enum.Material.CrackedLava,
},
{ Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock },
{ Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock },
{ Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand },
{ Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand },
{ Enum.Material.Mud, Enum.Material.Snow, Enum.Material.Snow, Enum.Material.Snow, Enum.Material.Mud },
{ Enum.Material.Air, Enum.Material.Snow, Enum.Material.Snow, Enum.Material.Snow, Enum.Material.Air },
},
{
{
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
Enum.Material.CrackedLava,
},
{ Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock },
{ Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock, Enum.Material.Rock },
{ Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand },
{ Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand, Enum.Material.Sand },
{ Enum.Material.Mud, Enum.Material.Mud, Enum.Material.Mud, Enum.Material.Mud, Enum.Material.Mud },
{ Enum.Material.Air, Enum.Material.Air, Enum.Material.Air, Enum.Material.Air, Enum.Material.Air },
},
}
local occupancies = {
{
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 0.5, 0.5, 0.5, 0.5, 0.5 },
{ 0, 0, 0, 0, 0 },
},
{
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 0.5, 1, 1, 1, 0.5 },
{ 0, 1, 1, 1, 0 },
},
{
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 0.5, 1, 1, 1, 0.5 },
{ 0, 1, 1, 1, 0 },
},
{
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 0.5, 0.5, 0.5, 0.5, 0.5 },
{ 0, 0, 0, 0, 0 },
},
}
terrain:WriteVoxels(region, resolution, materials, occupancies)