JavaScript EditorFree JavaScript Editor     Ajax Editor 

Main Page
Previous Page
Next Page

16.6. Vertex Noise

In the previous chapter, we talked about some of the interesting and useful things that can be done with noise. Listing 16.3 shows a vertex shader by Philip Rideout that calls the built-in noise3 function in the vertex shader and uses it to modify the shape of the object over time. The result is that the object changes its shape irregularly.

Listing 16.3. Vertex shader using noise to modify and animate an object's shape

uniform vec3  LightPosition;
uniform vec3  SurfaceColor;
uniform vec3  Offset;
uniform float ScaleIn;
uniform float ScaleOut;
varying vec4  Color;

void main()
    vec3 normal = gl_Normal;
    vec3 vertex = +
                  noise3(Offset + * ScaleIn) * ScaleOut;

    normal = normalize(gl_NormalMatrix * normal);
    vec3 position = vec3(gl_ModelViewMatrix * vec4(vertex,1.0));
    vec3 lightVec = normalize(LightPosition - position);
    float diffuse = max(dot(lightVec, normal), 0.0);

    if (diffuse < 0.125)
         diffuse = 0.125;

    Color = vec4(SurfaceColor * diffuse, 1.0);
    gl_Position = gl_ModelViewProjectionMatrix * vec4(vertex,1.0);

The key to this shader is the call to noise3 with a value (Offset) that changes over time. The vertex itself is also used as input to the noise3 function so that the effect is repeatable. The ScaleIn and ScaleOut factors control the amplitude of the effect. The result of this computation is added to the incoming vertex position to compute a new vertex position. Because of this, the vertex shader must compute gl_Position by performing the transformation explicitly rather than by calling ftransform.

Previous Page
Next Page

JavaScript EditorAjax Editor     JavaScript Editor