Graphics

Stylistic Shaders in Unity

A set of non-photorealistic shaders written in Cg/HLSL, including a dynamic hull-outline technique for multi-step toon shading.

Three Suzanne monkey heads rendered in Unity with painterly toon shading, stippled ink outlines and depth-of-field blur

Why we built it

This was our final project for CS 184, Berkeley’s computer graphics course, where each group picked any graphics topic to explore. Most of the course is about physically realistic rendering: light bouncing around a scene the way it does in the real world. We went the other way, writing non-photorealistic shaders that make 3D scenes look hand-drawn, like a cartoon, a comic book or a newspaper print.

We were inspired by the cel shading of The Legend of Zelda: Breath of the Wild and the comic-book look of Spider-Man: Into the Spider-Verse. Stylized shading isn’t just about looks, either. In games, outlines and glow can make important objects easier to see.

We were a team of four. My parts were the multi-step toon shader, hull outlining (including a two-tone outline that stays visible on any background), the halftone-with-outline shader and the comic-book shader on the Bugatti, plus the monkey lineup test scene and both of our project videos.

How it works

Every shader is written in Cg/HLSL and runs in Unity, mostly as fragment shaders that decide the color of each pixel. For each one, we exposed the important settings as sliders and color pickers in Unity’s material panel, so an artist could tune the look without touching the code.

The work falls into three groups: strokes (outlines), fills (how surfaces are colored in) and halftone (dot-pattern printing).

Strokes

Objects don’t have outlines in real life, but they almost always do in drawings. We tried three ways to draw them.

Fresnel shading adds a glow wherever a surface turns away from the camera, using the angle between the surface’s normal and the view direction. Thresholding that glow gives a hard outline, but its width varies a lot with the shape of the object.

Test scene with a baby, a monkey head, a house, a tree, a dog, a bust and a teapot, all in purple with a glowing Fresnel rim
Fresnel glow: a soft rim wherever surfaces turn away from the camera.

Hull outlining gives much more even strokes. The trick is to draw the object twice:

Hull outlining in three steps: copy the object, push the copy's vertices outward along their normals, and cull its front faces so only a rim shows around the original 1 · Copy the objectA second copy, the hull,drawn in the outline color2 · Push it out along its normalsEvery vertex moves a littleoutward, like a wrapper3 · Hide the copy's front facesOnly the back of the hullshows, peeking out at the edges
Hull outlining, the technique behind our cleanest strokes.

The one thing hulls miss is sharp creases inside an object, like the fold of a door panel. For those we tried a derivative stroke, which draws a line wherever the surface normal changes quickly between neighboring pixels. It found the creases, but the lines came out jagged and picked up the polygons of the mesh, so we left it out of the final looks.

Fills

Toon shading, or cel shading, takes the smooth gradient of real lighting and snaps it to a few flat colors, the way an animator would paint it. The simplest version starts from ordinary diffuse lighting and splits it into two colors at a threshold, including Unity’s shadows.

The test scene in pink two-tone toon shading, with thin derivative strokes on the house's door panels and the teapot's rim
Single-step toon shading with derivative strokes, which pick out the door panels and the teapot's rim.

The hard part is the edge between colors. A hard cut is jagged, and a soft blend of fixed width looks blurry wherever the lighting changes slowly. The fix was to size the blend with fwidth, a GPU built-in that measures how fast a value changes from one pixel to the next. That keeps every color edge about one pixel wide, crisp but smooth.

Three ways to threshold shading into two colors: a hard step gives jagged edges, a fixed-width smoothstep gives blurry edges of uneven width, and a smoothstep sized with fwidth gives a crisp edge about one pixel wide STEPHard cut: jagged,aliased edges SMOOTHSTEP, FIXED WIDTHSmooth, but blurry wherethe shading changes slowly SMOOTHSTEP SIZED BY FWIDTHAlways about one pixelwide: crisp and smooth
Turning smooth shading into flat bands of color, and keeping the edges between them clean.

My multi-step toon shader builds on this with several bands of color and a specular highlight. In real lighting, a highlight is added on top of the diffuse color. In a cel-shaded drawing, the highlight is painted over it instead, so past its threshold the shader blends from the diffuse bands straight into the highlight color.

The test scene in orange multi-step toon shading with flat bands of light and bright highlights
Multi-step toon shading on its own.
The test scene in purple multi-step toon shading with black hull outlines around each object
Retuned in Unity's material panel, with hull outlines.

With a moving light, like the sun crossing the sky, the highlights slide across the bands of color:

Multi-step toon shading under a moving sun.

Halftone

With only a couple of colors, flat thresholds lose the shape of an object. Print solved this long ago: newspapers and comic books use halftone, patterns of tiny dots that read as smooth shading from a distance, using only ink and paper.

Our halftone shader turns each pixel into a single brightness value, adds a pattern of offsets (a mix of dot patterns and noise), and thresholds the result, using the same fwidth trick for clean edges. The pattern’s scale, rotation and mix are all adjustable, including an “n-axis eggbox” dot pattern of our own invention.

The test scene in two-tone halftone, with shading built from tiny dots
The halftone shader.
The test scene in a grainy black-and-white style like a photocopy, with outlines that flip between black and white
A grainier, photocopy-like pattern, with two-tone outlines.

Halftone makes object edges hard to see, so I added a two-tone hull outline: the outline turns white where the object behind it is dark, and black where it’s light, so it always stands out. Combined with thresholded lighting, it gave us a black-and-white comic-book style:

My comic-book shader on a Bugatti.

Bonus shaders

We also made two extras. A dissolve effect jitters where each pixel samples the image, so the whole frame looks like it’s breaking apart; it’s the image at the top of this page. And a fragment-only shader draws an entire scene with no mesh at all: the shapes are defined by a math function, found by ray marching, and shaded with three lights.

What I learned

Most of the course came with starter code and step-by-step instructions. This project didn’t, which made it the best way to really understand lighting, rasterization and aliasing: we had to take ideas from a blank page to a working look on our own. I especially enjoyed the mix of learning new tools (Unity and Cg/HLSL), digging into the algorithms, and the creative side of inventing looks like the comic-book Bugatti.

Next projectAI/ML Bootcamp →