Signed Distance Fields
Video Lecture
Section | Video Links | |
---|---|---|
Signed Distance Fields | ![]() |
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 |
|
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 |
|