This blog describes my process for creating a painterly shader in UE5, answering the question:
To begin my experiments, I looked to p5.js, a in browser JavaScript library focused on audio-visual coding. However, before I even touched p5.js, I needed to develop a goal.
Inspiration Board
Lighting Inspiration
I was inspired by the works of Jean Michel Basquiat and some pre-existing Unreal shaders in my mockups, wanting to bridge the line between textural and graphic. I used a classic sphere lighting mockup to express these ideas so I could just focus on value before anything else.
Edited p5 scribble demo
Then I threw myself in the deep end! I started with shfitz's p5 scribble demo, removing every detail except the scribble filling and making it so the seed updated every few frames to give it a 'wiggling' effect. There was, however, a large issue with this method. I gained no understanding of how to create this effect and could not take it out of p5.js and into another program like UE5.
My shader must be able to:
Mask an area
Draw a scribble within that masked area
...which sounds much easier than it is.
I took my reference image from before, a sphere lighting reference, and uploaded it to p5.js. Then, using the tutorial above, I created an algorithm that searched every few pixels of the image and drew rectangles with the colour of the pixel, resulting in the image below.
This served as the basis of my mask. From here I could tell the algorithm to single out contrasting pixels, such as below a certain value threshold.
The scribble was more complicated. The scribble fill uses simple shapes like circles and squares, however my mask is organic and changing depending on the object, image or threshold I pick. Below are examples of my first attempt at doing this.
Created a horizontal scribble
Animated the horizontal scribble
Applying this effect to the entire canvas
Scribble with two colours in selective areas.
I have not edited the tones, these are issues with gif compression.
These scribbles were made using custom shapes in p5.js and have some drawbacks. First of all, the lines are pretty limited to the x or y axis due to how the algorithm looks for the next pixel in the mask. This leaves the scribble feeling quite rigid. For the above examples I opted for a horizontal scribble and although vertical noise is possible, the lines were too wobbly and looked like someone slowly drawing with a shaky hand instead of a loose, free scribble like the scribble demo above.
With this new information, I created a new test, attempting to make a new scribble that draws around a point as a continuous curved line.
This first version that I created randomly places vertices within a box defined by scribbleRadius which join together to make a large curved line. There were some major issues with this version, the largest being that the scribble was not circulating around the centre and was square in shape. The line simply meandered around the box I defined.
From there, I separated the scribble into it's own function and got to work. The solution I found was creating a random distance and angle from the center every time a new vertex is placed, with this I could use trigonometry to calculate the x and y coordinates of the new vertex and control the angle of the next vertex. The first vertex has a completely random angle to the center, however every vertex after that is between 120 degrees and 240 degrees more than the previous vertex. This forces the line to double up over the centre more and fill in that space, while still giving room for stray lines that circle around the outside.
I then edited this so it is possible to give the code multiple coordinates to scribble around. This is done using two arrays of coordinates, one for the x and one for the y axis, that the code switches between depending on how many vertices have been drawn, spreading them equally across each entry of the array.
From there I could apply the scribble to the mask I made before except instead of drawing a square or a line it drew a circular scribble that is the same colour as the pixel below it. I also adapted the scribble function to include x and y coordinates, meaning that as the mask function searches the image for pixels of a certain value, it places a scribble at the coordinates it just checked rather than adding each coordinate to an array and connecting them all which looked too messy.
The result is a subtle yet effective procedural shader that is customisable, draws your eye and does not overpower the image below. The next step is to apply this to Unreal Engine which will allow me more customisation in terms of textures, maps and masks.
Now it was time to apply my learning to UE5.
As a basis, I used a simple toon shader from another project that I made and set off to make the edges of said toon shader more organic using noise.
Along the way I found an interesting ripple effect that I could be used for another project.
However I was beginning to notice a big issue. The noise that looked the best was a texture that overlayed over the camera, instead of projecting in 3 dimensional space. This caused a very disorienting effect when the camera moves.
The solution to this was a 3D Noise such as the noise or vector noise node, however I did not like this effect as much.
Despite this I continued to make the noise animate on certain objects to draw attention to objects that can be interacted with.
From here I attempted to apply a scribble effect to the darkest areas only. However, due to post processing materials being aligned to the camera, my options were very limited. I attempted to create a scribble effect using noise which was largely unsuccessful and discarded.
In the end I made a successful painterly shader, but an unsuccessful Basquiat inspired painterly shader. I found that stretching one coordinate value of my larger noise made appealing brushstroke shaped noise, meanwhile multiplying that with another, smaller noise created small splotches that really sold the effect. In retrospect, I would have had more success if I stuck to either p5js or Unreal Engine, instead of switching between the two half way. I did, however, learn a lot from the process and I feel as if I understand the boundaries of Unreal Engine’s post processing effects.
These were the tutorials I used along the way