Animated Textures – Tutorial 3

In the previous two articles you saw how to use llSetTextureAnim() to activate conventional animated textures. This is the usual way of animating textures in Second Life.

There is another way, which involves using a script and a timer, along with the ‘offset texture’ function. This is far less efficient than using a normal animated texture, but there are occasions when it can be useful.

Let’s have an example. In the first set of animated textures released by Templar Creations you will find an animated gauge, with three frames of animation. If this is animated in the normal way, the effect is like this:

Animation example

Although this works, it’s rather ineffective. It would be better if the needle moved randomly. That can’t be done with a normal animation, but it is possible using the timer system.

The way this works is that the script sets up a timer, and each time the timer event fires, the script selects a random frame and displays it. I can’t easily demonstrate this is on a webpage, but if you visit the main Templar Creations store you will find a running example.

So, how do you display a specific frame?

In the previous article you saw how to display a single frame of an animation, using the ‘repeats’ setting on the texture tab of the Build dialog, and the llOffsetTexture() function.

For this example, let’s set the repeats in the script itself. The texture in question has three frames, so the horizontal repeats must be set to 0.333. The script will put the offset at 0.0, which will display the second frame, with the needle pointing straight up.

Why does an offset of 0.0 display the middle frame? Because the offset is from the middle of the texture. An offset of 0.333 will move the texture to the left so that the third frame is displayed. An offset of -0.333 (or 0.666, because textures wrap around) will move the texture to the right, so that the first frame is displayed.

Imagine that the texture is a long strip extending out from both sides of the visible prim face. Here it is at the default offset of 0.0, so that the middle frame is visible:

Offset 0.0, Frame 1
Offset 0.0, Frame 1

Setting the offset to 0.3333 shifts the strip of frames to the left, displaying the third frame:

Offset 0.3333, Frame 3
Offset 0.3333, Frame 3

Setting the offset to -0.3333 shifts the strip to the right, displaying the first frame:

Offset -0.3333, Frame 1
Offset -0.3333, Frame 1

Naturally, if you have a different number of frames, these numbers will also be different.

The script makes use of the llFrand() function to return a random number between 0 and 2, which it then uses to select the correct frame, and sets the offset for that frame. It also uses this to set the delay between frames to a random amount.

It also sets the texture scale, so that one frame at a time is displayed.

[sourcecode language="cpp"]
list Offsets = [0.666, 0.000, 0.333];
integer Frame = 1;
default
{
    state_entry()
    {
        llScaleTexture(0.333, 1.0, ALL_SIDES);
        llOffsetTexture(llList2Float(Offsets, Frame), 0.0, ALL_SIDES);
        llSetTimerEvent(llFrand(0.5) + 0.1);
    }
    timer()
    {
        Frame = (integer)llFrand(3);
        llOffsetTexture(llList2Float(Offsets, Frame), 0.0, ALL_SIDES);
        llSetTimerEvent(llFrand(0.5) + 0.1);
    }
}
[/sourcecode]

That concludes this short series of articles on animated textures.


Posted

in

,

by

Comments

Leave a Reply