Working on a Custom Shader

We are aiming to create a very unique and identifiable art style for our game. Marco is working hard to develop the style and I have been tasked with writing a custom shader which can efficiently reproduce our art style without us needing to create endless textures and lighting (to increase performance and lower network load).

Shaders in Three.js

first I want to talk a little bit about the environment. In Three.js, there are several useful workflows setup to help with custom shader creation. For the most part, these workflows come in the form of uniforms. When defining a custom material, you can also define custom and pre-existing uniforms. In the code below I’ve defined a custom material with my own shader code and the three.js uniforms for lighting:

This process then allows me to use the lights in the scene in my fragment shader. As mentioned before, the uniform lib provides uniforms and constants to the shader:

Based on the beginnings of our art style, I also knew that I would need to generate some kind of noise in the shader. Unfortunately, GLSL does not natively support any kind of random function, but I did find a function which generates pseudo-random noise. Essentially, this is a function which is used because of its lack of repetition and pattern. The original source of this function is unknown to me, but it works well for our purposes. I also had to modify the function so that it supported 3D noise:

I apply the noise in object space so that if the object is moving around the scene, the texture does not change on its surface. Otherwise the rest of this code is just setup to handle basic lighting from ambient, directional, and point lights.

You may also like...