SDF CAD
SDF's are pretty neat!. A function generated from simple mathematical functions but can make some really complex geometry. If you want to see some stunning examples go no furthur than shadertoy.com. Would you believe the following scene was not made in some complicated software with hundreds and thousands of menus and buttons backed multi billion dollar corporation, but its just some few hundred of lines of C
like code typed into an editor. Its even animated! Inigo Quilez who runs the website, is a well known personality in the graphics space and has some really well put explanations on how SDF's work. This link is a good start.
I wont go into detail how SDF's work. The gist is that its a way to represent the "distance" to a surface of an "object". The function when evaluated at some point in space will tell you how far the nearest surface to the object is. On the surface of the object the sdf value ofcourse is 0. Thats it, its easy and known how to construct SDF's for some primitive objects like spheres, cubes, cones, cylinders, prisms etc. Theres a way to combine them by putting them through another function. You can take intersections and subtract one object from another. Rotate, move, elongate, scale lot of great stuff!. Whats neat is that you can also round the corners and edges very trivially in this representation. When combining two objects you can smoothly combine them too ! (figure below). Rendering them is a matter of doing a thing called raymarching. Its fairly simple to generate shadows and lighting. I think anyone who's even slightly mathematically inclined is mindblown when first coming across SDF's and shadertoy. You really should check it out if you haven't. This project heavily derives of the distfunctions listed on Inigo Quilez's website.
As a 3D printing nerd one of the first things this got me thinking was if this was a feasible way to design objects for 3D printing. I'm not the first to think of this, there's a dozen of implementations of SDF's for generating models out there (sdf-csg , fogleman/sdf to name two). I wrote a tool with 3 key features. The language used construction is Python. It runs in your browser (pyodide) and can be viewed in the browser (sdfx and sdf.py dont have a viewer). You can generate STL's (marching cubes) for 3d printing. Almost all the primitives from Inigo Quilez (link here) are supported. Combining objects with unions, intersections , subtractions and there respetive smoothened versions are there. Scaling, elongate, rotation , translation, even twisting and bending!.
How does this work? You type your python code in the editor. The browser runs the code in pyodide (a python implementation running in WASM) which generates a sort of tree of the objects that you added to the scene. When you hit recompile it recursively goes throught the tree generating an SDF of your scene in glsl. The shader is then raymarched to show you what it looks like. When you want an STL, its first converted to a mesh using the marching-cubes algorithm. Its a shame that one has to first convert to a mesh to feed it to 3d printers, most slicers cannot slice SDF's directly although i think that would be so much easier to do. Mesh generation first evalutates the SDF in glsl at gridpoints and stores them in a framebuffer to be returned to the javascript.
The STL generation needs more work to be done, right now its generating points by evaluating the sdf on a grid and saving to a 3d texture , this is unfeasable for high resolution due to limited VRAM (should be chunked), The default resolution is kept to 128 points on a cube of -5 to +5 in all dimensions.
I hope you have fun with it.