Sylvain LC's Blog

A GitHub blog, usefull to me, hope the same for you.

I’am fond of Polyhedra !

In geometry, a polyhedron (plural polyhedra or polyhedrons) is a three-dimensional shape with flat polygonal faces, straight edges and sharp corners or vertices.

Drawning regular polyhedra is quite cool, and these 3D shapes have something facinating. Everybody knows the CUBE that is very commonly seen in our daily life is still very usefull !

First thing interesting is that there is only 5 regular convex polyhedra (Platonic solids). Just try to enumerate all possibilities to assemble triangle, square or pentagonal faces to build a volume and you will get an idea of the demonstration.

References :

Drawing Polyhedra using rasterization

On this page I focus on the Ray Marching method to render images, because this is what is mainly used on Shadertoy. Before that, let’s have a look about how to do it using the more common rasterization method. As these figures have flat faces, this is obviously appropriate.

Using the IQ’s Rasterizer shader, I learnt more about how to declare a Mesh object, that formally declare all the faces of our figure.

References :

Cube

The exact SDF for a cube is simple, and there is an IQ’s video on how to guess it by yourseft. Even more simple but not exact is to just take the MAX of the absolute value of each point coordinates ! (fBoxCheap in Mercury library)

References :

Octahedron

I tried to understand the Cheap IQ function below using Desmos
This is derived from the distance to a plan, that is dot(p,n) + h (on the same page)
Here the plan normal n is vec3(1), to get it normalized there is this multiplication by 0.57735027 that is 1/sqrt(3).
By taking the absolute value of each coordinate, the function need to take care of only 1 plan instead of 8.
The distance is not too much over estimated, the maximum is sqrt(3) times the euclidian distance.

float sdOctahedron( vec3 p, float s)
{
  p = abs(p);
  return (p.x+p.y+p.z-s)*0.57735027;
}

A nice star can be generated by intersecting 4 of these shapes on an Icosahedral symmetry.

All possible combinations of rotations of the Icosahedral symmetry
- [Ico-Disco - dr2](https://www.shadertoy.com/view/Xtf3RB)
- [Closest icosahedron vertices - tdhooper](https://www.shadertoy.com/view/fdXcDl)
- [Compound of five octahedra - Wikipedia](https://en.wikipedia.org/wiki/Compound_of_five_octahedra#:~:text=The%20compound%20of%20five%20octahedra,having%20a%20regular%20convex%20hull.)
- [tiny code star - spiral stepping - flockaroo](https://www.shadertoy.com/ view/dlcGRH) 
Allows to obtain coordinates of textures (uv) by mapping on the different regular polyhedra. Not designed for raymarching but texturing.
- [Spherical polyhedra - nimitz](https://www.shadertoy.com/view/4dBXWD)

References :

Tiling space with Polyhedra

Tiling space is usually done using cubes, but it is also possible using truncated octahedrons and rhombic dodecahedrons.

Here Shadertoy meets the Christalography science, where these lattices are named CC, FCC, BCC.

In “Lattice Framework”, Shane shows some great tricks to short code the map fonction, taking all the advandages of the geomery of the lattice, combining distances to create more shapes. No UV coordinates are used for textures, only triplanar mapping using hit point coordinates and the normal. Powerfull. Beautyfull.

Refereneces

intersecting octahedra and tretrahedra

Creating intersecting polyhedra in Shadertoy is a relatively simple task that can result in stunning star-like figures.