[Tutorial] Godrays!

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:

Godray5.png

  • 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:

Godray1.png

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:

Godray2.png

  • 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:

Godray4.png

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.

Almost forgot: I’m going to add moving dust particles to my rays. When I’m done, and if I like the result, I’ll post a tutorial here.

Kudos to basspixel, who has a free dusty particles 4k texture pack available here.

Thanks man ! if i can setup mine windows i will send here the result.

:slight_smile: My pleasure. If something doesn’t work, post the problem here - I’ll lend a hand.

Awesome! Thx for sharing. :slight_smile:

In case you have problem with the Custom node (in the Epic tutorial, I did have), the solution is - In.x, In.y - In should be declared as the input (when you click on the Custom node, you can declare inputs).

Epic has a blueprint-based solution for the same thing, but the I happen to like the material-based thing better - you can instance it, use it wherever you want, have all the customization you need.

There’s one thing I should warn you about, though. Rendering translucent objects through translucent objects increases shader complexity (and, I think, GPU load). Though I didn’t experience any real framerate drop on my example, the Shader Complexity view looked atrocious:

So using too many godrays where one can be seen through another might not be a good idea after all.

Here’s the whole setup, finally got around to uploading it. Though I’d prefer that you make it yourself, just to learn the process, you’re free to use this one too:

** Click here ]**](https://drive.google/file/d/0B1yO7ZdR9tyzWW01S2c3N0hzQVE/view?usp=sharing)

Just unrar it to the Content folder of your project, you’ll see the mesh under TriptychContent/Effects

Have fun.

Thanks, **** :slight_smile: I’m an admirer of your work on UE, so your praise is really encouraging :slight_smile:

Here’s some ideas I had, I’d like to hear what others think. In case we have these godrays coming through, say, Venetian blinds, we’d need an illusion of the shafts being of constant width and height, which are not equal to each other. I’d need to manipulate the shaft size as I manipulate the rotation. The question is, do you want such a feature?

P.S. when I’m done with it, guess I’ll post it to the Marketplace as a free asset.

Thanks,Rhayader this gona be very usel to the scenarios i works to 2.5D Fight Game

Glad to be of service, mate :slight_smile: if you use it, it would be lovely if you posted the result here, for the rest of us to see :slight_smile:

Hi, I’m currently trying to make your god rays with the the dust texture. But I can’t find a 1 material node in unreal engine, (please see the attached image, the red outline) please help me, thanks in advance!

Thanks for this Rhayader! I got the basic setup working nicely but for some reason I am still getting shadows cast on my floor from the planes I attached the material to, do you know what setting would fix this since the [Cast Shadow] box is grayed out?

https://a.uguu.se/lwyyht.png

The Unreal Engine also provides materials to achieve these effects and are part of their Core Engine assets. They are the UE4 equivalent of the shaders they made for UE3 for which they made a Tutorial (https://udn.epicgames/Three/VolumetricLightingGuide.html). To find these materials, look for the material shaders:

/Engine/EngineVolumetrics/Fogsheet/Materials/M_EV_FogSheet_Master_01
/Engine/EngineVolumetrics/LightBeam/Materials/M_EV_Lightbeam_Master_01.M_EV_Lightbeam_Master_01

In Fact I’ve made a simple drag-and-drop blueprint spotlight from this, which you can find in the Market Place:

https://www.unrealengine/marketplace/volumetric-spotlight-with-shadow

Using my blueprint can easily save you a day trying to figure out how to set up this shader.

That isn’t really the core of the asset, they are only serve as a reference for the main asset: the Volumetric Spotlight! This is a real 3D Volumetric Spotlight. Take a look at a screenshot and a video:

VIDEO!!!

https://vimeo/170536888

In fact Epic provides a free demo scene, just called “blueprint”, which uses exactly this technique. They have blueprint that let’s a plane mesh always face the camera and then apply an emissive shader to it. The scene is a futuristic office room on a top floor. Just look for it in the Unreal Launcher Library section.

Alright I’m confused. So after reading the comments, it appears that you have a plane mesh that is (I think) apart of the camera.
The only thing is, I tried that and nothing seemed to work. I also tried applying the godray material to a window, and of course that did nothing.
I’m not exactly sure how to make it work.

Sorry for the delayed reply. Use a simple plane, align it to the light direction, originating from the window. Apply the material to the plane, and you’re good to go.

The way the material works is it rotates the plane around its center so it always faces the camera + some nice tweaks to stop it from intersecting with other geometry and disappear when you come near it.

Good job, man, this looks amazing!

Hello!
Can you email me this god rays you created so I can import them and start working! I would appreciate,I’m following your tutorial so all I need is this god rays!
You can email me god rays on gamingbip

Thanks!