Skip to content

Signed Distance Fields

Video Lecture

Section Video Links
Signed Distance Fields Signed Distance Fields Signed Distance Fields 

 (Pay Per View)

You can use PayPal to purchase a one time viewing of this video for $1.49 USD.

Pay Per View Terms

  • One viewing session of this video will cost the equivalent of $1.49 USD in your currency.
  • After successful purchase, the video will automatically start playing.
  • You can pause, replay and go fullscreen as many times as needed in one single session for up to an hour.
  • Do not refresh the browser since it will invalidate the session.
  • If you want longer-term access to all videos, consider purchasing full access through Udemy or YouTube Memberships instead.
  • This Pay Per View option does not permit downloading this video for later viewing or sharing.
  • All videos are Copyright © 2019-2025 Sean Bradley, all rights reserved.

Description

We have been using SDFs several times throughout the course without really knowing much about them.

In the next few lessons, we will delve much further into SDFs, starting from the basics of using SDFs in 2D scenes and then up to more advanced 3D scenes with camera movement, colouring, shadows and reflections.

What is an SDF?

An SDF is a function that takes a 2D or 3D point as input and returns a scalar distance value.

This distance represents how far the point is from the nearest surface of an object:

  • If the value is positive, the point is outside the object.
  • If the value is zero, the point is on the surface.
  • If the value is negative, the point is inside the object.
<>

In the above example, this is an SDF of a sphere. The radius is .5 and the positional range is between X,Y [-1, -1] to [1, 1]. When we move the mouse pointer, we can see some stats showing,

  • the positional coordinate that the fragment shader would use,
  • the length from 0, 0 to the coordinate,
  • the length minus the radius,
  • a vec3 which contains the an RGB value made up of the resulting calculations.

When an SDF equation returns a number below 0, and that number will be used in a color channel, then it will be clamped to 0. And if the number is greater than 1, then will be clamped to 1.

Start Script

./src/main.ts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import './style.css'
import * as THREE from 'three/webgpu'
import {
  positionLocal,
  length,
  vec2,
  abs,
  Fn,
  vec3,
  min,
  max,
  rotateUV,
  time,
} from 'three/tsl'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  10
)
camera.position.z = 1.5

const renderer = new THREE.WebGPURenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
renderer.setAnimationLoop(animate)

window.addEventListener('resize', function () {
  camera.aspect = window.innerWidth / window.innerHeight
  camera.updateProjectionMatrix()
  renderer.setSize(window.innerWidth, window.innerHeight)
})

const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true

// @ts-ignore
const Circle = Fn(([position, radius]) => {
  return length(position).sub(radius)
})

const radius = 0.5

const main = Fn(() => {
  const p = positionLocal.xy

  const circle = Circle(p, radius)

  const finalColour = vec3(1).mul(circle)

  return finalColour
})

const material = new THREE.NodeMaterial()
material.fragmentNode = main()

const mesh = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), material)
scene.add(mesh)

function animate() {
  controls.update()

  renderer.render(scene, camera)
}

Final Script

./src/main.ts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import './style.css'
import * as THREE from 'three/webgpu'
import {
  positionLocal,
  length,
  vec2,
  abs,
  Fn,
  vec3,
  min,
  max,
  rotateUV,
  time,
  negate,
} from 'three/tsl'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  10
)
camera.position.z = 1.5

const renderer = new THREE.WebGPURenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
renderer.setAnimationLoop(animate)

window.addEventListener('resize', function () {
  camera.aspect = window.innerWidth / window.innerHeight
  camera.updateProjectionMatrix()
  renderer.setSize(window.innerWidth, window.innerHeight)
})

const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true

// @ts-ignore
const Circle = Fn(([position, radius]) => {
  return length(position).sub(radius)
})

// @ts-ignore
const Ellipse = Fn(([position, radius, scale, angle]) => {
  const angledPosition = rotateUV(position, angle, vec2())
  const scaledPosition = angledPosition.mul(scale)
  return length(scaledPosition).sub(radius)
})

// @ts-ignore
const Box = Fn(([position, dimensions, angle]) => {
  const angledPosition = rotateUV(position, angle, vec2())
  const distance = abs(angledPosition).sub(dimensions)
  return length(max(distance, 0.0)).add(min(max(distance.x, distance.y), 0.0))
})

const radius = 0.25

const main = Fn(() => {
  const p = positionLocal.xy

  const circle = Circle(p.sub(vec2(-0.5, 0.5)), radius)

  const ellipse = Ellipse(p.sub(vec2(0.5, 0.5)), radius, vec2(1, 2), time)

  const box = Box(p.sub(vec2(-0.5, -0.5)), vec2(0.5, 0.25), negate(time))

  const torus = Circle(p.sub(vec2(0.5, -0.5)), radius)
    .abs()
    .sub(0.05)
  //const torus = Ellipse(p.sub(vec2(0.5, -0.5)), radius, vec2(1, 2), time).abs().sub(0.05)
  // const torus = Box(p.sub(vec2(0.5, -0.5)), vec2(0.5, 0.25), negate(time))
  //   .abs()
  //   .sub(0.05)

  const finalColour = vec3(1).mul(circle)
  finalColour.assign(min(finalColour, ellipse))
  finalColour.assign(min(finalColour, box))
  finalColour.assign(min(finalColour, torus))

  return finalColour
})

const material = new THREE.NodeMaterial()
material.fragmentNode = main()

const mesh = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), material)
scene.add(mesh)

function animate() {
  controls.update()

  renderer.render(scene, camera)
}

Working Example

<>

Raymarching SDFs

Voronoi Diagram

2D Distance Functions