Sample Project To Create A Starlight Effect

429 views
Skip to first unread message

Mathieu

unread,
Dec 17, 2014, 10:20:33 PM12/17/14
to ligh...@googlegroups.com

Here's a sample project file showing how to create a starlight effect with two sources. It works for any grid sizes. Paste these sources in your grid and you'll get the starlight effect :) Now for the explanations.


The red source generates the initial intensities, a bit like generating stars by randomly choosing their distances from earth. Its power formula is: 

if(grid.onactivated, max(0,rand(-200,90)), last) 

When the grid has just been activated (grid.onactivated), we pick a number between -200 and 90 (rand(-200,90)). Then any numbers below 0 will be 0 because of the max function. That means we have more chance to have 0 than 90, thus generating a sparse sky. You can try changing -200 by 0 and you'll get way more bright stars. 

Once we have this number, we always returns it by returning the "last" variable, which is the value returned at the last evaluation round by this formula. 

The trick to get many different stars is to use the source shader mode, meaning, this formula will be evaluated independently for each cell. This way, each cell gets a different value. 


So at this point, we have generated our stars but they don't twinkle. 

Here comes the yellow, generating the pulsations. Its power formula is: 

scale(sin(latch(grid.onactivated, rand(1,10))),0,30) 

Again, keep in mind this formula is evaluated about 80 times per second for each cell. To get a different pulsation speed for each cell, we pick a number once, when the grid has just been activated: latch(grid.onactivated, rand(1,10)). So the pulsation will be between 1 and 10 seconds. The latch function allows remembering one value and updating it only when the 1st parameter is > 0, which in this case is when the grid has just been activated. 

Then we generate a sinus wave using the sinus function with the time period we randomly picked. Finally, since the sin function returns a number between 0 and 100, we rescale it to be between 0 and 30 to limit the pulsation amplitude.

 

The power of the two sources are additive, giving the final result. By the way, we could merge the two formulas and only use one source. However, I prefer working with simple units and combining them to get more complex effects. 

starlight.ljp
Reply all
Reply to author
Forward
0 new messages