God rays lag problem (need help)

I’ve been investigating 2D god rays and after a couple of days, I’ve gotten them to work on Codea. Granted my iPad isn’t as powerful as my computer, I still can’t get it to go past 5 samples without lag. I’m pretty sure the problem is because the I have an “if” statement inside of the for loop so I’ve been trying to move it out so that it runs only once every call, but I’ve had no luck so far. The if statement is to make sure that only the color yellow (1.0,1.0,0.0) is skewed by turning all of the other colors black.

I was hoping that someone on here would be able to help me instead of me continuing to pull my hair out :wink:

Here is the example code that I threw together


The shader is self contained in the code, so you should just have to copy and paste into a new project

Can you provide a link to an explanation of what “God rays” are?

I’m not sure the conditional is the problem, or at least not the only problem. If it is one, the loop is also one. But you might just have too many texture reads in your fragment shader for a mobile device, and on anything before the A7 CPU (as in iPaid Air/Mini Retina) they will all be slow reads because the coordinates are calculated in the fragment shader (“dependent read”).

@1980geeksquad you could try using the step function:

                 float dist = distance( sample.rgb, vec3(1.0,1.0,0.0) );
                 float mul = 1.0 - step( 0.001, dist );

                 sample.rgb *= mul;

Though I’m not sure if it’s any faster. And there’s probably a better way to eliminate the conditional.

@Andrew_Stacey “god rays” are the name for that type of voluminous light that appears when light enters a dark room and scatters through dust (or when light beams are visible shining through the clouds). Google image search for “god rays” to see an example.

@damny it definitely is the if statement because it gets ran the amount of times that there are samples. Commenting out the if makes the game run at buttery smooth FPS, but with unwanted results


If it can be moved outside of the loop, I think I will get much smoother results

I’ll keep working at it, but thanks for all the help everyone ;:wink:

Never mind… It was smoother because I never turned up the sample count.
The conditional doesn’t impact it as much as I thought, so it is just the case that my iPad is dated (iPad 2) I found another way to achieve this effect that seems a lot less power hungry (granted that it doesn’t look as good) I’ll try that and report back