Signed Distance Fields

Talk about things that are not making games here. But you should also make games!

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
lennyhome
Slime Knight
Posts: 115
Joined: Fri Feb 14, 2020 6:07 am

Signed Distance Fields

Post by lennyhome »

This is about making computer graphics, possibly for games. It's essentially 3D CGI without polygons.

For those who don't know what SDFs are, this:
https://www.youtube.com/c/InigoQuilez
should explain everything.

The programming there is hardcore but I've noticed several repeating patterns and, being a long time PovRay user, I wondered if I could invent a language to describe SDFs.

And this is roughly where I'm at.

Image
full size

Code: Select all

camera (0 0 20) (20 45 40) 10
(
	translate (0 0 -1)
	cylinder 100 1 0
)
(
	(
		color yellow
		translate (0 0 20)
		repeat (5 5 5) (2 2 2)
		sphere 5
	)
	intersection 0
	translate (0 0 20)
	color green
	sphere 20
)
This shows something that is especially important to be able to draw organic figures that don't suffer from the typical Metaball limitations. As you can see Shape A if fused to Shape C, Shape B is fused to Shape C but Shape A in not fused to Shape B.

Image
full size

Code: Select all

camera (0 0 0) (80 60 50) 10
(
	translate (0 0 -1)
	cylinder 100 1 0
)
(
	color green
	translate (0 0 10)
	(
		rotate x 45
		box (20 5 5) 1
	)
	translate (0 10 0)
	(
		rotate x 45
		box (20 5 5) 1
	)

	union 5
	color yellow
	(
		rotate y 45
		box (5 20 5) 1
	)
)
And this is where I'm going. To the untrained eye it may resemble somebody's backside but it's not. It's just a group of interpolated ellipsoid primitives.

Image
full size

Code: Select all

camera (0 0 30) (160 10 30) 10
(
	translate (0 0 -1)
	cylinder 100 1 0
)
(
	union 2
	symmetry x

	(
		union 2
		translate (0 0 40)
		ellipsoid (6 3 5)
		(
			translate (2.5 2 -3)
			rotate y -10
			ellipsoid (3 2.5 3.5)
		)
		translate (0 0 8)
		ellipsoid (5 3 8)
		
	)

	(
		union 2
		translate (4.5 0 0)
		translate (0 0 40)
		rotate y 5
		translate (0 0 -10)
		ellipsoid (3 3 10)
		translate (0 0 -8)
		rotate y -5
		translate (0 0 -8)
		ellipsoid (2 2 8)
		translate (0 0 -8)
		rotate x -90
		translate (0 0 -3)
		ellipsoid (1.5 1 4)
	)

)
If you're interested you can get my C source code for these demos from here:
https://sourceforge.net/projects/snes9l ... z/download

Or better yet, visit:
https://www.shadertoy.com/
or some other more official resource and then share your occult knowledge about SDFs with me.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Re: Signed Distance Fields

Post by TMC »

SDFs are fascinating and a lot of fun to play with!

A year ago I finally tried my hand at shaders (though learnt some OpenCL many years ago) and used SDFs to do some procedural rockform generation building on top of some shader code written by the SDF God, iq. Sadly I haven't touched shaders or SDFs since but I know I will.

That DSL's very nice. A DSL to describe geometry seems like a great idea and also obvious... does it already exist? However it does seem like a big loss to not be able to write general shader code. Are you going to keep extending it to make it more flexible? Such as random values or animation?

Although the DSL code is very clean it's none the less very difficult to make sense of all the numbers, and I don't totally get the grammar & semantics (haven't looked at the parser). What are the parameters on operators "intersection 0" and "union 5"?
lennyhome
Slime Knight
Posts: 115
Joined: Fri Feb 14, 2020 6:07 am

Re: Signed Distance Fields

Post by lennyhome »

The only documentation that exists for now is the source code and the example scripts. "intersection 0" means "from now on in this scope go into boolean intersection mode with 0 rounding". "union 5" means "everything that follows is melted with a radius of 5".

"translate" and "rotate" work the same way. When they're encountered anything that follows is either rotated or translated accordingly. They also stack up within a scope, so they can be called multiple times. When a scope begins it's like starting form scratch, when a scope ends its content is merged into the parent one as if it was a primitive.

Hierarchical transformations are supported naturally in this scheme. To do animations it just needs quaternion interpolation for the angles. But the language itself is meant to be produced by some other scripting language. Think of it as a JSON file more than a language.

Each command is meant to replicate some pattern I've seen some SDF guru guy do, and to make it easy.

EDIT:
does it already exist?
Yeah. http://www.povray.org/ but it's so old, crufty, overcomplicated and obtuse...

EDIT: I forgot to mention that my implementation is CPU-based. I'm never going to touch anything OpenGL ever. Because reasons.

EDIT: My favorite feature is that after the rendering phase, you can click on the screen and it'll tell you where in 3D world coordinates that point is, and which line of source code defined the primitive you clicked on. That's going to be the basis for a future UI.
Post Reply