开发者

How to implement this rotating spiral in WebGL? [closed]

开发者 https://www.devze.com 2023-02-03 11:46 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and开发者_如何学编程cannot be reasonably answered in its current for
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and开发者_如何学编程 cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

Could somebody try to implement given animation into WebGL shader example? It would be great for people learing WebGL like myself.

How to implement this rotating spiral in WebGL? [closed]

Source: http://dvdp.tumblr.com/post/2664387637/110109


I've implemented your animation at http://www.brainjam.ca/stackoverflow/webglspiral.html. It will give you a "WebGL not supported" message if your browser does not support WebGL. It is adapted from a sandbox created by mrdoob. The basic idea is to show a rectangular surface (consisting of two triangles) and to apply the shader to the surface.

The actual shader code is as follows:

uniform float time;
uniform vec2 resolution;
uniform vec2 aspect;

void main( void ) {

    vec2 position = -aspect.xy + 2.0 * gl_FragCoord.xy / resolution.xy * aspect.xy;
    float angle = 0.0 ;
    float radius = length(position) ;
    if (position.x != 0.0 && position.y != 0.0){
        angle = degrees(atan(position.y,position.x)) ;
    }
    float amod = mod(angle+30.0*time-120.0*log(radius), 30.0) ;
    if (amod<15.0){
        gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 );
    } else{
        gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );                    
    }
}

The spiral resizes with the browser window, but you could easily opt for a fixed size canvas instead.

Update: Just for fun, here's the exact same implementation in a jsfiddle: http://jsfiddle.net/z9EmN/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号