26. Rusted Stairs and Shader Meta-Programming

Kaveh Kardan
3 min readMay 31, 2022

At Taarna we did all our rendering using Pixar’s prman RenderMan software. Though I had written several renderers, this was my first experience with programmable shaders.

Since I was developing hair and cloth simulation code, it sort of made sense for me to also write shaders for them. Taarna was a small operation and we had the flexibility to do research, explore areas of interest, and try new ideas having to do to with all aspects of 3D character animation.

I wrote a variety of shaders, including several fabric ones (my favorite was the “burlap” shader). I wrote shaders for “weathering” materials with rust, dirt, and scratches. Also several wood plank shaders with different amounts of “aging”.

I have memories of crawling out the window to the courtyard to explore and squint at the metal back stairs, which had peeling red paint worn through by spots of rust. Then crawl back in and reproduce the look in my shader code.

Like any self-respecting Lisp programmer, I eventually had the idea of writing a system which would write shaders for me. Shader meta-programming. I had written enough shaders that the underlying pattern became clear to me.

I wrote a shader generator which I could feed a string such as “dirty scratched rusted painted metal”, and it would generate the code for each feature, passing alpha-modulated values from one feature to the next. We had an automated UI-generator for our shaders, so experimenting with this code and tweaking the generated shaders was a lot of fun.

There is something magical about writing code-writing code, whether as lisp macros or simple compiler-type tools. I have always wished I knew more about compiler design.

Looking back, my time at Taarna was a wonderful experience. I would even compare it to a mini graphics Xerox PARC setup. Some time later (I think 1996), when Taarna showed bits of “The Boxer” at Siggraph, I was told that Steve Jobs and Ed Catmull of Pixar came to the booth and expressed an interest in working together.

Years later I would take a number of very talented Taarna developers and artists with me to Square to work on the “Final Fantasy: The Spirits Within” feature film.

Design & Code

While I was reorganizing some code, I decided to refactor the uv-mesh class to inherit from thepolyhedron class. Despite what the Clojure people say, I find inheritance to be a very powerful feature of object-oriented design.

The resulting code is much simpler and cleaner, with the rendering mostly confined to the polyhedron class. Though OpenGL rendering of general polyhedra is less optimal that rendering triangle strips, I had to remind myself that simplicity and elegance of code are much more important at this stage of my software journey than efficiency. After all, I’m using defclass to represent 3D points, and using older deprecated OpenGL calls!

If this code ever gets to point of needing to be production ready, I imagine interfacing it to some GPU-based rendering library. In the meantime, following one of Alan Kay’s maxims, I will keep the software simple and malleable. And maybe completely rewrite it once I have a better understanding of what the design should be.

Next Episode

--

--

Kaveh Kardan

MIT mathematics degree • wrote animation software used in “Jurassic Park” • software R&D lead on “Final Fantasy” movie • software dev on “The Hobbit” films