This is a Godot 4 (4.5.1) spatial fragment shader that renders a Menger Sponge fractal onto a plane mesh surface.
Instead of using thousands of polygons to model, or implementing expensive raymarching, this uses a fast raycast check for each fractal level, which removes the need for iterative "marching."
menger_sponge.gdshader is the only file of real significance. Everything else is setup for the demo project.
hole_iterations: Number of fractal iterations for the hole pattern. Higher gives more detail, more cost.
cut_iterations: How many layers deep to raycast into interior walls.
surface_color: Pretty much what it says on the tin. Color of the surfaces.
ao_strength: Blending strength of the approximated ambient occlusion.
orthographic: Set to true if rendering with an orthographic projection.
level_of_detail: Fades out holes below this threshold. Little effect on performance, because it's just meant to reduce aliasing and Moiré patterns.
fast_shadows: Uses a simpler render for shadow passes. A bit less accurate for interior shadows, but much faster especially with high res shadow maps.
- This is less isolated from the typical game rendering pipeline, so it plays well with other meshes in the scene, which can pass inside the holes or cast shadows onto it.
- Very fast compared to a raymarch fractal.
- Because this only renders the complex fractal on the surface of the plane meshes, the camera cannot go inside the fractal structure, though other meshes can.
- This method probably would not generalize well to other fractals. A Jerusalem cube or Sierpiński tetrahedron may be doable, but not without significant effort and changes.
Image is rendered on my laptop (Intel Core Ultra 5 226V, integrated Intel Arc Graphics 130V, 1920x1200 resolution). 300+ FPS is achievable when the post processing is disabled.