Reputation: 351
I am in need of help. I've been racking my brain but I just can't seem to find any good code examples of how to programmatically generate shapes similar to these (lightbursts, glimmers, sunrays, etc).
I really need help here. Can anyone off some examples with code that might help me produce something similar?
I'm using a language similar to C++.
Upvotes: 1
Views: 241
Reputation: 13610
These all look like a center of light bright at the center and, on moving out, both fading (growing dimmer) and diffusing (becoming more spread out).
To fade, you will want to scale the light from maximum intensity by a function of r
(distance). Physically this would be something like 1/r
or 1/r^2
but in your case you might have more luck using an exponential e^(-r)
or even a linear fade 1 - a*r
A major aspect of the effect is the diffusion. The easiest way to emulate diffusion short of building a full on raytracer would be to apply a linear gaussian blur (or even a simple averageing blur) along the rotational axis. For example, the diffused point at r = 3, theta = 0.5 might be an average of the undiffused points between r = 3, theta = 0.4 and r = 3, theta = 0.6. You will get best effects if the "window" of the blurring function scales with r
. That is, the larger the r
, the greater the averaging window, or the larger the sigma for the gaussian blur.
Finally, it looks like before the diffusion, the beam rotationally "filtered out" by a theta-varying transmission function. That is, the amount of the beam that "escapes" the filter is a function of the angle. This function appears to be somewhat randomized yet with apparent structure (ie, bands of light and dark, instead of simply being white noise). This is a perfect candidate for Perlin Noise, which generates random data with structure. You can customize how fine this structure is in the perlin noise algorithm; this indeed is what differentiates the three pictures you show.
So in summary:
First, calculate the undispersed distribution:
Start a fading ball of light, f(r,theta) = 1/r (or some other fading)
Multiply the fading ball of light by the noisy filter function p(theta) -- but maybe only to points past a given radius (to have that white ball in the middle)
Then, disperse everything with a radially growing blurring window
Perhaps add a white noise grain to everything.
I think this is just one way to generate these; there might be others, but it could be a good start.
Upvotes: 2
Reputation: 1628
I'd start with defining a function f(theta,distance) that gives you the intensity of light at theta degrees of rotation and at that distance from the center. You can probably play with periodic functions like sin to change how thin the bursts are.
Another, even cooler option, would be to study some physics and figure out how such an effect happens to your eyes in the first place when you look at bright lights... Then replicate the real world math in code.
Upvotes: 0