Skip to content

Instantly share code, notes, and snippets.

@Elmuti
Created November 4, 2016 22:57
Show Gist options
  • Save Elmuti/3cb87f47b398bcff5e5408d9626c3ce1 to your computer and use it in GitHub Desktop.
Save Elmuti/3cb87f47b398bcff5e5408d9626c3ce1 to your computer and use it in GitHub Desktop.
local floor = math.floor
local ceil = math.ceil
local max = math.max
local min = math.min
local cos = math.cos
local pi = math.pi
local sqrt = math.sqrt
local terrain = workspace.Terrain
function smoothSphere(centerPoint,radius,material,resolution)
local resolution = resolution or 4
local minBounds = Vector3.new(
floor((centerPoint.x - radius) / resolution) * resolution,
floor((centerPoint.y - radius) / resolution) * resolution,
floor((centerPoint.z - radius) / resolution) * resolution
)
local maxBounds = Vector3.new(
ceil((centerPoint.x + radius) / resolution) * resolution,
ceil((centerPoint.y + radius) / resolution) * resolution,
ceil((centerPoint.z + radius) / resolution) * resolution
)
local region = Region3.new(minBounds, maxBounds)
local materials, occupancies = terrain:ReadVoxels(region, resolution)
for ix, vx in ipairs(occupancies) do
local cellVectorX = minBounds.x + (ix - .5) * resolution - centerPoint.x
for iy, vy in pairs(vx) do
local cellVectorY = minBounds.y + (iy - .5) * resolution - centerPoint.y
for iz, cellOccupancy in pairs(vy) do
local cellVectorZ = minBounds.z + (iz - .5) * resolution - centerPoint.z
local cellMaterial = materials[ix][iy][iz]
local distance = sqrt(cellVectorX * cellVectorX + cellVectorY * cellVectorY + cellVectorZ * cellVectorZ)
local magnitudePercent = cos(min(1, distance / (radius + resolution * .5)) * pi * .5)
local brushOccupancy = max(0, min(1, (radius + .5 * resolution - distance) / resolution))
if brushOccupancy > cellOccupancy then
occupancies[ix][iy][iz] = brushOccupancy
end
if brushOccupancy >= .5 and cellMaterial == Enum.Material.Air then
materials[ix][iy][iz] = material
end
end
end
end
terrain:WriteVoxels(region,4,materials,occupancies)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment