While working on my game project, Triptych, I happened to need some godrays (a must for any cathedral and cave scene, if you ask me), and couldn’t find any tutorials on google. So I dug deeper and guess got the exact effect I wanted - it’s a bit subtle in my small church scene, but you could change the parameters to whatever suits you better. Also, the same tech could be used to get pseudo-volumetric light shafts on street lights, spotlights, cloud clearings etc., the method is basically universal.
First, remember that UE4 does have support for crepuscular rays via light occlusion - if that’s the effect you want, you don’t need this tutorial. Check out the corresponding UE documentation here. They wouldn’t do all the work in my case because of complex lighting (they work with directional light only, can be seen only when looking at the light source and are hard to control artistically).
So how would I build a godray? to avoid using much geometry and make it as little resource-hungry as possible, I used a rectangle-shaped plane (2 tris), a simple texture made in Photoshop for the shape of the ray, and a material-based solution for the effect.
Note: I tried a trapezoid shape for the plane to deform the texture, but got really weird, non-uniform texture distortions. You’d rather shape your ray in Photoshop than in your modelling program, turns out.
Here’s the texture I used, you’re free to use it as you see fit:
Or you could make your own, it’s just a couple of brush strokes and a lot of Gaussian blur. Once we have the texture (to control the opacity shape), we’re ready to start making the material. The Material Domain we’re using is Surface, Blend Mode - Translucent, Shading Model - Unlit (we don’t need the surface of the godray to receive shadows and reflections), and we need it to be a two-sided material:
-
What do we need of it? First, we need the plane to always face the camera, to give it a “volumetric” look. UE4 documentation has a pretty nice tutorial on how to achieve that inside your material - as I duplicated their process, I won’t go into details there, and will just give you the link - it’s here. Just go to the “Foliage material” section, build the shader network and connect it to your material’s “World position offset” input.
-
Next, we need to make the ray disappear as we approach it. I’ll show you the shader network and then explain the hows and whys, because this is one of the points you can use to tweak the godrays to your liking:
As you can see, I used a PixelDepth node to get the distance to the camera. Then I divided the distance to the camera by 5000 (you can try your values) and clamped the result. Here’s what it does: as opacity should change between 0 (completely transluscent) and 1 (completely opaque), the division by 5000 means the godray opacity will start changing when the distance between the camera and the mesh is at least 5000 pixels or lower. When the distance is 0 pixels, I will get a value of 0, when it is 5000, I will get 1. The rest of the values are between 0 and 1.
I found out the fade speed wasn’t sufficient so I used a Power node (parametric value of 2, you can use your own values) to make it fade faster as I get closer.
- Now, let’s connect our calculations to the Alpha input of a Linear Interpolate node to lerp our texture and a constant of 0:
- We don’t really want our rays to intersect with opaque objects on our scene, so we’ll add a DepthFade node, and connect the result of our lerp to its Opacity input:
- And one last step to get our opacity values. As you can see on the previous pic, I clamped the result between 0 and a scalar parameter called MaximumOpacity. Turned out, I needed really low values to get the look I wanted, but you can try your own values too. Now connect the clamp’s output to the Opacity input of the material.
The only thing left to do with the material is the base color - we will use the Emissive Color input for that. The shader network is pretty simple:
Just connect the result to the Emissive Color input and you’re set to go.
As you can see, the effect I got was quite subtle. It isn’t easily seen but contributes to the overall atmosphere. I used sets of 12 rays for each window. If I have the time, I’ll add tutorials on how to use blueprints to automatically align godrays to light sources and even how to spawn them procedurally. Also, if there’s enough interest in this tutorial, I’ll do a video version too. If you want to add something I missed or could have done better, I’d love you to leave a message below.
In case you want to download the finished godray assets and use them in your project, leave a message here, I’ll post them on the thread (free to use both commercially and non-commercially). Good luck, have fun.