Signed distance functions: Using math to make beautiful meshes

Hi! With Roblox’s recent release of EditableMesh, it’s now possible to create stunning meshes using only scripts and math.

I’ve been working on a geometry library that can be used to do this and more cool things. It’s available as both a roblox-ts TypeScript library and in Luau.

Luau library: Voxel’s Geometry (v0.0.1, Luau Export)

TypeScript source: geometry.zip (60.6 KB)

Docs (generated for TypeScript, but still useful for Luau): docs.zip (181.1 KB)

Quickstart: Generate a doughnut

  1. Add the Voxel’s Geometry (v0.0.1, Luau Export) to a game in ReplicatedStorage.

  2. Add a LocalScript to StarterPlayer.StarterPlayerScripts with these contents:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Geometry = ReplicatedStorage:WaitForChild("Geometry")
local Examples = require(Geometry:WaitForChild("Examples"))
Examples.Torus() -- make a torus AKA a doughnut 🍩
  1. Join the game and enjoy!

How does it work?

It works by using the marching cubes algorithm on a signed distance function, creating a real mesh with the resulting data. Before EditableMesh came out, this could be accomplished in Roblox by using WedgeParts as triangles. But now we don’t need this workaround.

A signed distance function is essentially something that takes a 3D point as input and returns the distance from that point to the closest point on some surface. It’s called “signed” because the “distance” is actually negative when we’re inside the surface. In the video below, the surface in question is a sphere.

The definition of the sphere SDF is pretty short:

local function sphere(v: Vector3, radius: number)
	return v.Magnitude - radius
end

With this, we can use the marching cubes algorithm to generate a nice-looking mesh.

6 Likes