# 2D Graphics

## Procedural Glyphs, another method

I thought I was done with the procedural glyphs project but it occured to me there was another approach I could take and I had to give it a try. The results are a bit closer to feeling handwritten. What would be really interesting would be to develop a continuous cursive-like script with some form of punctuation.

Update: Cursive writing GET.

## Procedural Hatching

I’ve been using Shadershop as a tool to explore useful procedural functions. In this case I needed procedural hatching. A square wave was quick to define:
In this case, all that we need to do is call a couple of floor functions. Frequency, phase and amplitude are easy enough to adjust after the fact just as you would any other periodic function. (A simpler method to create a square wave is to use a rounding function, but I wanted to see where Shadershop’s approach took me.)
I then extrapolated this function into a vector2, skewed it with a 2×2 matrix, and voila. Shadershop makes figuring this kind of thing out much easier by using a visual workflow and graphing your function as you go. Very handy!

You can do something similar with a 1d random function fract(sin(x)*999) to achieve a periodic 2d noise pattern.

A simple crosshatch shader in unity can be made by adding two of the above square wave functions. In this case I used a min and max function to change the “pulse length” of the square wave. You can get a similar look by using the more common approach of establishing a sine wave which you then clip/threshold. I’m not sure which method would be faster… Here’s the basic shader:

``````Shader "Unlit/Test"
{
Properties
{
_Density ("Density", Range(1,400)) = 150
_Rotation ("Rotation", Range(0,360)) = 45.0
_Width ("Width", Range(0,1)) = 0.4
}
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float _Density;
float _Rotation;
float _Width;
v2f vert (float4 pos : POSITION, float2 uv : TEXCOORD0)
{
v2f o;
o.vertex = UnityObjectToClipPos(pos);
o.uv = uv * _Density;
return o;
}

fixed4 frag (v2f i) : SV_Target
{
float sn, cs;
float2x2 mt = float2x2(cs, -sn, sn, cs);
float2 c = i.uv;
c.xy = mul ( c.xy, mt );
float hatch = max(abs(floor((c.x/2 - 0.5)/-1)
+ floor(c.x/2)+1),
abs(floor(((c.x-_Width)/2 - 0.5)/-1)
+ floor((c.x-_Width)/2)+1))
- min(abs(floor((c.y/2 - 0.5)/-1)
+ floor(c.y/2)+1),
abs(floor(((c.y-_Width)/2 - 0.5)/-1)
+ floor((c.y-_Width)/2)+1));
return hatch;
}
ENDCG
}
}
}
``````

The problem with this kind of approach is that it’s overly complex. All of that min/max/floor stuff can be replaced with a much shorter step function. Same look, much more direct.

``````{
float sn, cs;
float2x2 mt = float2x2(cs, -sn, sn, cs);
float2 c = i.uv;
c.xy = mul ( c.xy, mt );
float hatch = abs(step(1-_Width,sin(c.x)))
+ abs(step(1-_Width,sin(c.y)));
return hatch;
}
``````

I added some lighting to the shader and here’s the result. Procedural and written entirely in notepad!

## NPR Final Touches

It’s wrapping up, I’ve added all the bells and whistles – Ambient Occlusion, Depth fading, Image Adjustment, etc. Highlights can now be dialated or eroded. Support for separate outlines of normals. Presets and clean menus. I think this shader is about done.

The shader allows for a pretty broad array of looks, from architectural sketches to classic toon shading. Below is an example of a more painterly rendering.

Here are some presets:

I’m continuing to refine the NonPhotoRealistic (NPR) sketch post-process in Unreal. This is using completely different methods now, I’ve changed the method of convolution edge detection for outlines, and am using world-aligned sketch texturing in the shadow and highlight regions.

The background is also given a custom color and texture. The light source is defined through any number of unreal lights.

There is now support for sketchy highlights and varying line colors, and much better response to light. Ideally the post process would support variation in outline width, although I don’t have a good method for that yet.

There are options to use procedural line, dot and crosshatching for a more comic/newsprint look.

The results are very solid and smooth in motion, with no temporal swimming or distractions. I see this shader being ideal for architectural stylization and general NPR usage.

To get a more chalk-like effect with ink outlines I take this post process and combine it with an NPR painterly process which applies brushstrokes on the image in world space. Here’s the result:

## Unreal Matcap PostProcess

Some more NPR experimentation, this time implementing a Matcap shader in Unreal as a post process.

## Unreal Lithograph Post Process Shader

This is a first test of a simple NPR shader I just put together, used as a post process in Unreal for a lithograph-like effect. Works well temporally but lacks a “handdrawn” feel, being very precise.

## Niagara Game Effects

I did a little job for a studio in Prague recently which consisted of a series of “spell” effects for a RPG. I can’t show any of those effects of course but along the way I made a number of other interesting little Niagara effects, here are a few:

The images below are a mixed bag, design wise. But they were fun to make. Oddly the brief was to make effects which were baked down into spritesheets, for a mobile release. It was strange, but it did allow me to up some particle counts and rely less on billboards in Niagara. I’m not sure I like the look but it was nice to be able to explore Niagara in this way.

## Procedural Blueprint Elements

This houdini file creates an infinite number of shapes to populate blueprints or info screens, with a “pen and ink” rendering look. It was a lot of fun to make.

## Dot Product and Edges in Vex

Getting the dot product of two vectors is infinitely useful, and easy in Vex. In this example I get a vector pointing from scattered particles to the camera and then get their dot product with the surface normal at that point. This gives you a very good idea of whether the surface is facing or perpendicular to the camera at any given point.

In this case, I simply cull any point which isn’t close to a value of zero, indicating the surface is perpendicular to the camera. If instead I culled particles that were less than zero I would be removing all back-facing (occluded) particles, which is perhaps more useful but less visually interesting. Since I am left with only particles near the edges of the geometry it’s easy to connect them with lines and get an “edge detection” kind of look.

Here’s the Vex wrangle I used. Note an earlier polyframe node gave me point normals on the scattered points to work with.

```float threshold = chf("Threshold");
//User input threshold
vector camPos = point(1, "P", 0);
//The position of the imported camera
vector toCam = normalize(camPos - @P);
//A vector from each point to the camera)
//@N = toCam;
//Use to visualize toCam as normals
f@angle = dot(toCam, @N);
//The dot product of the normal and toCam vectors.
if (@angle > threshold || @angle < -1*threshold){
removepoint(0,@ptnum);
}```

I’ve used this edge culling in production a few times, to create anime style speedlines and particle effects like rain strikes and halos. Culling particles behind geometry is of course also handy, though raycast solutions are much cleaner for that kind of thing. Next, we’ll use a simple dot product calculation to cull points and create a gradient effect.