The blog post details a creative misuse of DuckDB-WASM, compiling SQL queries within a web browser to generate 3D-like graphics. By leveraging DuckDB's ability to generate large datasets and then encoding coordinate and color information into a custom string format, the author renders these strings as voxels within a JavaScript-based 3D viewer. While not true 3D graphics rendering in the traditional sense, the approach demonstrates the surprising flexibility of DuckDB and its potential for unconventional applications beyond standard data analysis. Essentially, the SQL queries define the shape and colors of the "voxels," which are then assembled and displayed by the JavaScript frontend.
The blog post "Abusing DuckDB-WASM by making SQL draw 3D graphics (Sort Of)" by Frank McSherry details an unconventional and experimental approach to generating 3D graphics using the in-browser version of the DuckDB analytical database, specifically its WebAssembly (WASM) implementation. The author's primary motivation stems from a fascination with pushing the boundaries of what DuckDB can achieve beyond its intended purpose of data analysis. The core idea revolves around exploiting DuckDB's ability to perform relational algebra operations on large datasets to manipulate and render a 3D model.
Instead of traditional graphics APIs and shaders, McSherry leverages SQL queries to process the vertex and face data of a 3D model. The model itself is represented as relational tables within DuckDB, where each vertex and each triangular face are stored as rows with their respective coordinates and connectivity information. The "rendering" process is accomplished by using SQL queries to project the 3D vertices onto a 2D plane, simulating a perspective projection. This projection is achieved through mathematical operations within the SQL query itself, calculating the screen coordinates of each vertex based on a simplified camera model.
The post highlights the limitations of this approach. It's not a true real-time rendering pipeline, nor is it efficient. Rather, it's a proof-of-concept demonstration. The output is not a textured or shaded 3D model in the traditional sense. Instead, the generated 2D projection is visualized by drawing lines connecting the projected vertices, resulting in a wireframe representation of the 3D model. The wireframe is then drawn onto a HTML5 canvas element using JavaScript, with DuckDB providing the calculated 2D coordinates.
McSherry describes the process of importing the 3D model data into DuckDB and constructing the SQL queries necessary for the projection. He also addresses challenges encountered, including the lack of built-in trigonometric functions in the WASM version of DuckDB at the time, which necessitated a workaround involving pre-calculated sine and cosine tables. The author emphasizes the experimental and somewhat "hacky" nature of the project, acknowledging that it's not intended for practical 3D graphics applications. The post concludes with a reflection on the potential of exploring unconventional uses of database systems and the unexpected capabilities that can be unlocked through creative experimentation. The overall tone is one of playful exploration and a desire to push the boundaries of what’s possible with existing tools.
Summary of Comments ( 26 )
https://news.ycombinator.com/item?id=43761998
Hacker News users generally found the DuckDB-WASM 3D rendering hack clever and amusing. Several commenters praised the ingenuity and highlighted the unexpected versatility of DuckDB. Some expressed skepticism about practical applications, questioning performance and limitations compared to dedicated graphics libraries. A few users discussed potential optimizations, including leveraging SIMD and Web Workers. There was also a short thread discussing the broader implications of using databases for unconventional tasks, with some arguing it showcased the power of declarative programming. Finally, some commenters shared similar "hacks" they'd performed with SQL in the past, reinforcing the idea that SQL can be used in surprisingly flexible ways.
The Hacker News post titled "Abusing DuckDB-WASM by making SQL draw 3D graphics (Sort Of)" generated a moderate amount of discussion, with a number of commenters expressing their amusement and fascination with the project.
Several commenters focused on the cleverness and unexpectedness of using SQL for graphical rendering, even if it's not practical. One described it as a "fun hack" and "peak software absurdity," highlighting the enjoyable, albeit unconventional, nature of the project. Another appreciated the technical ingenuity involved, calling it "madness" in a positive light. This sentiment was echoed by others who found the project amusing and appreciated the author's creativity.
Some commenters delved into the technical aspects, discussing the performance implications and limitations of such an approach. One commenter questioned the efficiency and wondered about the practical applications, while acknowledging the project's entertainment value. Another pointed out the inherent limitations of using SQL for graphics processing, suggesting that specialized tools are better suited for such tasks.
The potential educational value of the project was also highlighted. One commenter mentioned that it serves as a good example of how to transfer binary data to and from WebAssembly, a valuable skill for web developers.
A few commenters drew comparisons to other unconventional uses of SQL, such as generating music or solving complex mathematical problems. This reinforces the idea that SQL, while primarily designed for data management, can be used in surprisingly creative ways.
Overall, the comments reflect a general appreciation for the project's novelty and ingenuity, even if its practical applications are limited. The commenters acknowledge the impracticality of using SQL for 3D graphics but celebrate the creative exploration and technical demonstration involved.