How to implement this tunnel like animation in WebGL? Source: http://dvdp.tumblr.com/ See also: How
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, this was fun. 🙂
A WebGL demo is available here: http://boblycat.org/~knute/webgl/tunnel/
(EDIT: no longer available, but I created a ShaderToy version: https://www.shadertoy.com/view/XdKfRD)
The main algorithm is in the fragment shader. The basic idea is a for loop iterating over the black rings/circles, from large to small, also offsetting the center to produce a tunnel-like effect.
Given any pixel, we can check if the pixel is close enough to the ring to be a candidate for a black pixel or not. If it is outside the ring, break the loop to avoid seeing smaller rings through the larger ones.
The distance from the previous (outer) circle is used to “squeeze” the pattern together when rings are close, this helps create the illusion of a 3D surface.
The wavy pattern of each ring is of course a sine curve. The angle of the pixel (compared to the circle center) is combined with a uniform time parameter to animate the wavy pattern for each ring.
And finally, there was lots of experimentation with different parameters and transformation functions like pow() to get the result close to the target animation. It’s not perfect, but pretty close.
The fragment shader code: