Hey there :) So Blender has recently got a new node called Sun Beams, which (as you probably have already guessed) creates light rays/god rays from an image/alpha input. The direction of the sun beams is determined by the x and y values. Sadly the node doesn’t have the option yet to plug in an x/y coordinate, so for now you would have to animate it manually in order for the sun beams to cast in the same direction if the sun beams casting object or the camera moves. Here I show you a quick and easy workaround using a tracking point and a driver with an expression.
So let’s get started:
First you have to render out a new image sequence which just includes a small (red) sphere. The sphere should be placed at the main origin point of the sun beams, so for example if you have a window just place it at the center of the window. The most important part here is that the sphere is at the correct distance from the camera (so the corner of the window would of course work too). When you are finished with putting the sphere in the correct spot, place the sphere on a new layer and create a new Render Layer that just renders that layer (be sure to exclude the sphere layer from the Main Render Layer). I would also advise you to add an emit material to the sphere and changing it to a color of your choice, so that later on the tracking goes along without any troubles. So then you can hit render (make sure your file output is set to OpenEXR MultiLayer).
So the next step is tracking it: After you render your animation open up another version of Blender and go straight to the Compositor (enable Use Nodes / Backdrop). Open up your image sequence (Add>Input>Image Sequence) and go to the Sphere Render Layer. Connect the image output of the layer to you Composite Output and render it out as a .jpg sequence (Click Shift + Ctrl on a node to view the output of that node) (Make sure your render resolution is set correctly). When head back to your project .blend file and go to the Movie Clip Editor and open up your sequence. If you’ve never done tracking in Blender before don’t worry because we aren’t really going to do that right now. Just press Ctrl + LMB to add a tracking marker and rename it to something reasonable in right panel under the “Track” section(press the n key to toggle the display on/off of the panel).I am going to name it “Dot”. Then place your marker on the red sphere by pressing the g key. After that just press Ctrl + T or the track forward button in the left panel under the “Track” section (press the t key to toggle the display on/off of the panel).The tracking shouldn’t fail, but if it does just grab your tracking marker and place it on the sphere and continue tracking.
So now you’re almost done :)
The last bit is to go to the Compositor (enable use nodes / backdrop) and open up your EXR sequence again. Add the Sun Beams node (Add>Filter>Sun Beams) and connect the inverted (Invert Node) alpha channel of your Main Render Layer to the input (Add an Add>Converter>Separate RGBA node and connect it to the Combined Output of the image sequence to get the alpha channel). Then just blend the result with Main Render Layer via a mix node (Add>Color>Mix Add Blending Mode). Now right click on the x location of the Sun Beams node and add a single driver. Also at this point make sure that your Sun Beams node is selected. Divide your window and open up the Graph Editor. Switch the mode from F-Curve to Drivers and select the x location driver in the left panel. Press n to toggle the right hand panel and under scripted expression write this:
Replace the marked spaces accordingly. If you look at your composite you should see light rays (the ray length value has to be greater than 0). If you want to offset the beam just add or subtract a number at the end of the expression. The very last step is to right click on the x location, to copy the driver and to paste it (via right click paste driver) to the y location. Of course you have to go to your graph editor window and replace the “co.x” part of the expression with “co.y”.
And that’s it. I hope this little workaround helped you and if you have any questions regarding this short tutorial comment below or just send me a message via Facebook or YouTube. The video goes into a bit more detail/troubleshooting than this written part of the tutorial, so you might want to watch the video first if any questions arise.