JavaScript EditorFree JavaScript Editor     Ajax Editor 



Main Page
Previous Page
Next Page

16.1. On/Off

Suppose you have an object in the scene that must be drawn two different ways, such as a neon restaurant sign saying "OPEN" that repeatedly flashes on and off. It would certainly be possible to write two different shaders for rendering this object, one for when the sign is "on" and one for when the sign is "off." However, in a situation like this, it may actually be easier to write a single shader that takes a control variable specifying whether the object is to be drawn as on or off. The shader can be written to render one way if the control variable is on and another way if it is off.

To do this, the application would set up a uniform variable that indicates the on/off state. The application would update this variable as needed. To achieve a sequence of 3 seconds on and 1 second off, the application would write the variable with the value for on, update the value to off 3 seconds later, update the value to on 1 second later, and repeat this sequence. In the interim, the application just continually redraws the geometry every frame. The decoupling of the rendering from the animation effect might make the application a little simpler and a little more maintainable as a result.


Previous Page
Next Page




JavaScript EditorAjax Editor     JavaScript Editor