|
The Grass sample demonstrates the use of vertex and
pixel shaders to create the effect of a grass waving
in the wind.
The availability of programmable vertex and pixel shaders
has allowed programmers to re-examine how they implement
passive background scene geometry. With the ability
to transfer background scene animation over to the GPU,
previously static background objects can now be animated,
adding life and realism to the scene. In this sample,
an approach is shown for realistically and inexpensively
animating grass with a combination of vertex and pixel
shaders.
The waving motion of the grass can be accomplished in
the vertex shader. Using a traditional method the grass
is rendered with randomly placed intersecting quads
stored in a single vertex buffer. The quads are texture
mapped and rendered with an alpha test. In the vertex
shader, the top two vertices of each quad are animated
using a combination of four sinusoidal waves. The waves
are approximated using a Taylor Series approach. This
combination of sine waves using various different frequencies
creates a natural waving that does not look like an
animation or overly repetitious.
When grass blades wave in the wind they also turn and
change their orientation with respect to the sunlight.
Because this method involves using a textured quad to
represent various blades of grass it is impossible to
change the lighting on individual grass blades. To simulate
the change in color that would occur as the blades twist
the wind, the same sine wave that was used to animate
the grass is used to oscillate between two colors. In
this case, the green channel of the color was changed
to make the grass color change from intense green to
a more yellowish-brown. The pixel shader combines this
color with the grass texture to produce the final result
|