# Unity

## 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!

## Article on 80 Level

https://80.lv/articles/using-hooke-s-law-to-create-vfx-effects/

I wrote an article for the game-and-vfx website 80 Level a while back, and it’s gone live. The topic is Hooke’s law as implemented in Popcorn FX (and Unity’s VFX graph.) It’s brief but has been well received, I appreciate the feedback I’ve gotten. A second article for 80.lv centered around Newton’s universal gravitation as implemented in PopcornFX (and Houdini this time) is in the works.

## Propagation in PKFX

PopcornFX is a great tool for realtime applications in that it brings to developers a ready-to-integrate particle system which can be used to author sophisticated effects in a nodal/visual style akin to Houdini, ICE etc. I find it much more intuitive than Niagara while easily being as powerful, and it’s integration into After Effects brings those users a level of sophistication with particle effects they simply haven’t had access to before. That said, when I push the limits it understandably can’t chew through the millions of particles Houdini can handle.

For example in PKFX this frost-like propagation effect, which is admittedly computationally expensive, tops out in usefulness at about 50,000 particles. The in-editor framerates are still good (relatively speaking) at 220 ms, comfortable enough for cinematography work in After Effects for example. But when you push the number of particles up beyond that times go up exponentially. By 100k particles (for this particular task, which involves getting neighboring particle information) the editor becomes increasingly likely to crash.

But to be fair this kind of propagation effect is waaay beyond what most realtime engines are engineered to handle. Other effects in PopcornFX can get into the realm of millions of particles without too much trouble, which means those After Effects users have a system which lets them perform most particle effects they are likely to need extremely efficiently… and only need to go to a package like Houdini for the really intensive stuff.

And at the moment these computations are only on the CPU as PKFX is still implementing its GPU support. There is a lot of improvement on the near horizon for these kinds of intensive effects… we may see sph fluids and the like running realtime soon enough.

(It’s also worth mentioning that there is a LOT of room to optimize the effect as built, both with regular settings and as one of the PopcornFX devs points out massive increases in efficiency should be possible by only performing spatial queries on the particles changing values and depositing stable values into a static layer.)

It’s significant that those integrating PopcornFX into realtime tools get a system sophisticated enough that a comparison with Houdini is even possible.

In all, I’m pretty impressed with PKFX and as an effects guy would welcome jobs where it’s integrated into custom pipelines dealing with realtime media even over Niagara and certainly over Unity’s particle system. It is elegant to use without sacrificing power, and it’s universal nature means one day artists might be able to share and use particle assets across multiple engines and custom pipelines.