Voxel Sphere Generation Help

Hello, I found a devforum post about this already, but the poster made the voxel sphere solid. IS it at all possible to make this hollow so only the top layer is generated?

Post referenced: Sphere Generation

That’s my post!

The code there is pretty outdated so I made an updated version that allows you to change the thickness of the sphere.

Sphere Module
-- Sphere
-- Quoteory
-- August 8, 2019 [Updated October 4th, 2021]

local RunService = game:GetService("RunService")
local spherePart = Instance.new("Part")
spherePart.Anchored = true
spherePart.CastShadow = false
spherePart.Material = Enum.Material.Slate

local Sphere = {}

function Sphere:Create(radius, partSize, position, thickness)
	radius = radius or 10 
	partSize = partSize or 5
	position = position or Vector3.new(0, 0, 0)
	thickness = thickness or radius
	
	local sphere = Instance.new("Model")
	sphere.Name = "Sphere"
	
	local templateCube = spherePart:Clone()
	templateCube.Size = Vector3.new(partSize, partSize, partSize)
	
	local thicknessRadiusDifference = radius - thickness
	
	for x = -radius, radius do
		local powX = math.pow(x, 2)
		for y = -radius, radius do	
			local powY = math.pow(y, 2)
			for z = -radius, radius do
				local powZ = math.pow(z, 2)
				local radiusNum = math.sqrt(powX + powY + powZ)
				local isInRadius = radiusNum > thicknessRadiusDifference and radiusNum <= radius
				
				if isInRadius then
					local cube = templateCube:Clone()
					cube.Position = Vector3.new(x*partSize, y*partSize, z*partSize)
					cube.Parent = sphere
				end
			end
		end
		RunService.Heartbeat:Wait()
	end
	
	sphere.Parent = workspace
	sphere:MoveTo(position)
	
	return sphere
end



return Sphere
Script
local Sphere = require(script.Sphere)

local sphere = Sphere:Create(
	3, -- radius
	20, -- part size
	Vector3.new(-72, 41.5, -2), -- position
	1 -- thickness (leave empty if you want the sphere to be completely filled)
)

Example Place: Sphere Generator [Open Source] - Roblox

Sources: Generating Voxel Planet using 3D Simplex Noise | by Hitesh Sahu | Medium (used as reference for the math)

2 Likes

appreciate it, thanks!

3O characters