Introduction to Distance Functions

Introduction to Distance Functions

Distance functions are mathematical expressions of a shape in 3D. Distance functions were made popular by the research and demos of Inigo Quilez.

Let's have a look at one of the most simple distance function, that of a sphere:

float sdSphere( vec3 p, float r ) { return length(p)-r; }

This function returns the distance from a given point in space to the sphere (defined by it's radius r).

Sphere rendered in RaySupreme DF

But don't worry, using RaySupreme DF you will not need to handle mathematics yourself, it comes with a wide range of predefined objects for you to work with.

An example of some distance function objects / primitives with operators and deforms in RaySupreme.

Advantages of Distance Functions

So what are the advantages of distance functions and why did we choose distance functions for RaySupreme DF.

  1. Using distance function you can easily create complex objects of the highest quality. As objects are described mathematically, the quality of rendered images is very high.
  2. Easy to use. No complicated mesh editing, just manipulating and blending objects. Easy for everybody. It is somewhere in the middle of voxel (very simple) and mesh editing (quite hard). Just think of building objects by assembling simple building blocks.
  3. Operations like union, blend, intersect and subtract between objects are nearly for free using distance functions, these operations are very complex to deal with when using meshes. More on these operations later.
  4. You have many possibility to transform or deform objects using twist, bend, repeat, lathe or even morph tools.
  5. Fast rendering via the GPU. As the scene can be rendered for each screen pixel separately, it is the perfect use case for GPU based rendering. All rendering of RaySupreme takes place on the GPU.

Why are distance functions not used more in Games ?

This section is a bit technical but we wanted to include it here for the sake of completeness. If this explanation is too technical, just skip it, it does not affect RaySupreme.

As you read above, some operations which are very hard todo with meshes, are very easy todo with distance functions. On the other hand, some things which are quite easy with meshes, are hard with distance functions.

The reason is that with a mesh, you can always track your current surface position to the currently polygon, making calculations of normals very easy (we just have to look at the surrounding polygons), whereas with distance functions you have to parse the whole scene to obtain the normals for a given surface, as there are no references to what your surface position is relative to the object.

This is one of the reasons why distance functions are not used more in games. However it does not affect offline rendering employed by RaySupreme, as we want to achieve best possible rendering quality and not realtime display.

RaySupreme DF will support export of scenes and objects to meshes.

Blending Operations

A closer look at how you can combine objects via distance functions operators. The images show a red cube and a blue sphere.

Union

 

Easily combine objects using the union operator. This is the default operator in RaySupreme.

Blend

 

Combine objects using the blend operator. Using blend, you can define how much the two objects "attract" each other. As you can see the sphere is being "embraced" by the cube. The "Blend Radius" object property defines how much the two are blended. Blend is a very powerful modelling function by itself. You can blend various object primitives together to create complex shapes. Also use it to attach objects smoothly to each other, for example the handle of a teapot to the teapot's body.

Subtract

You can subtract objects from each other using the subtract operator.

Intersect

You can intersect objects with each other using the intersect operator. The result is the area where the two objects overlapped.

Grouping

By grouping objects you can operate on a given set of objects (the children of the group) and join it to the main hierarchy with the operator of the group. The children of a group form their own domain in which they operate. Groups can have children by themselves (and so on).

Transformations and Deforms

Apart from the standard move, rotate and scale transformations, you can also deform your objects in many ways. However to list all possible deformations is too much for this overview, we will add specific tutorials for each deform type.

The above image shows a cube with a twist deformation.

 

I hope you find this introduction to distance functions helpful. Please check our Homepage regularly for new tutorials.

Be the first to comment

All comments are moderated before being published