Source linked

ClickHouse SQL Renders a Ray-Traced Scene with CSG, Terrain, and Shadows in One Query

A single ClickHouse SQL SELECT path-traces a scene with glassy lettering over fBm terrain, using arrayFold for bounces and Tuple(Float64) for vectors-no UDFs.

clickhousesql ray tracerpath tracingcsg graphicsprocedural terrain

One SELECT statement. A path tracer that renders glassy, chrome lettering over a Perlin-noise terrain, with shadows and reflections from the letters onto the hills. No UDFs, no external code, no GPU—just ClickHouse SQL running on multiple CPU cores.

Andrew Kensler’s famous Pixar business-card ray tracer inspired the lettering, but this implementation swaps the C++ for a single ClickHouse query. The repository from ClickHouse (the open-source columnar database) demonstrates a complete path tracer built entirely from SQL primitives.

How It Works: Pixels as Parallel Rows

numbers_mt(width * height * samples) generates one row per (pixel, sample). Each row carries a 3D vector as Tuple(Float64, Float64, Float64) and uses lambda aliases like dotProduct, L2Normalize, tuplePlus, and tupleMultiplyByNumber for linear algebra. The bounce loop runs inside each row via arrayFold(range(maxDepth))—one mirror bounce per fold step. Because rows stay independent, the render parallelizes across all CPU cores.

Intermediates are bound with a one-element-array trick: arrayMap(x -> body, [expr])[1] works as a "let" binding, avoiding the expression blow-up that call-by-name WITH lambdas cause.

The explicit x, y coordinates for each pixel let the ClickHouse PNG writer place every sample directly. No ORDER BY needed.

Geometry from CSG Primitives and Ray-Marxched Tori

The scene exercises constructive solid geometry: cylinders for straight strokes (letters l, i, k, H, u, the bar of e), tori for round letters (C, c, o, u, s, e) ray-marched through their signed-distance field, and spheres for the dot on i and a hollow "planet" (sphere minus sphere). Oriented boxes handle flat faces. The letter openings in C, c, s, e come from subtracting a box from the ring.

Terrain with fBm and Shadows

A height field using amp * fBm(x, y) sums several octaves of lattice value-noise. Camera rays ray-march against it, skipping empty air by starting at the maximum terrain height, then linearly interpolate the surface crossing—no step-banding. Shading uses a height color ramp (water → sand → grass → rock → snow), a warm sun plus cool sky-ambient model, marched shadows (terrain self-shadow and cast shadows from letters), and distance fog.

Performance and Reproducibility

Every file in queries/ is self-contained and parameterized. Image dimensions come from ClickHouse’s image-output settings (e.g., --output_format_image_width 2560 --output_format_image_height 1200), and samples per pixel from a {SAMPLES:UInt32} parameter. The hero image (ClickHouse over terrain) renders at 2560×1200 with 8 samples per pixel. A Python generator blends the scene and bounce depth at generation time; image size and samples remain runtime parameters.

This isn’t a demo—it’s a production-ready pattern for in-database rendering. If you can express a path tracer in a single SQL query, you can embed procedural visualization directly into your analytics pipeline without exporting data to a separate renderer.


Source: Ray Tracer in SQL
Domain: github.com

Read original source ->

External source stays available while the OJO article and comment thread stay local.

Comments load interactively on the live page.