Archive for October, 2009

Animated Textures – Tutorial 3

Saturday, October 24th, 2009

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.

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);
    }
}

That concludes this short series of articles on animated textures.

Steampunk Textures – Set 7

Saturday, October 17th, 2009

Now available from the main store and from XstreetSL, this new texture set is mainly for walls and floors, and consists of 20 seamlessly tiling textures.

A few examples:

Sample 1

Sample 2

View the set on XstreetSL

Visit the Templar Creations Main Store

Animated Textures – Tutorial 2

Thursday, October 1st, 2009

In the previous blog you saw how to use the SMOOTH option for simple animation. Although this is useful, it is also rather limited. The usual way of creating animated textures is by way of a texture which contains a set of ‘frames’, in a way very similar to that used in traditional animation.

Note: the examples from this blog are available as full-perm pre-built prims at the Templar Creations sandbox (behind the main store), for you to copy and experiment with, and use in your own builds if you wish.

Let’s have an example. Here is a texture which has three frames, to create a rotating gear:

Gears example

Gears example

Gears example (animated)

Gears example (animated)

Let’s assume you are starting from scratch. When you apply the texture to the prim, all three frames will be displayed (and will be ‘squashed’ horizontally):

Gears image applied as texture

Gears image applied as texture

This will be corrected when the animation starts running, but it is often useful to have a single frame displayed when the animation is not active. To do this for this particular texture (and any texture which has just three horizontal frames), you need to set the horizontal repeat to 0.333, so that just 1/3 of the texture is displayed. Set the horizontal offset to 0.666 to display the first frame (understanding the offsets, and how to calculate the offsets you need in order to display specific parts of a texture is an entire future blog article in itself!).

When the texture is not being animated, it will revert to this display.

To start the animation, you need a script. On the Contents tab of the Build dialog, click the New Script button, and double-click the script that appears, to edit it.

For this demonstration, the script will switch the animation on and off when the prim is clicked on, so delete the contents of the script, and replace it with the following:

integer isRunning = FALSE;
default
{
    touch_start(integer count)
    {
        if (isRunning)
        {
            llSetTextureAnim(0, 0, 1, 1, 1, 1, 5.0);
            isRunning = FALSE;
        }
        else
        {
            llSetTextureAnim(ANIM_ON | LOOP, 0, 3, 1, 0, 3, 5.0);
            isRunning = TRUE;
        }
    }
}

The script uses the isRunning variable to keep track of whether the animation is on or off. When someone touches the prim, if this is FALSE (the animation is not running) it starts the animation, otherwise it stops the animation.

So, what are the parameters?

The first parameter is a set of flags, which in this case switch the animation on (ANIM_ON), and set it to play continuously (LOOP). The next parameter is the side of the prim which is to be animated. Use ALL_SIDES to animate all the sides of the prim. In our example, only side 0 is animated.

The next two parameters describe the layout of the frames — how many there are horizontally (3 in this case), and how many there are vertically (1 in this case).

Suppose we had a texture which contained two vertical rows of three frames each. The frames in this texture would get displayed in the following order:

Animated frames example

Frame order example

The next two parameters are the first frame, and the number of frames. This means that you can display only part of an animation if you want to. Some of the animations in the Animated Texture pack make use of this by having the very first frame as the ‘off’ frame, and displaying frames 2 to the end for the actual animation.

Note that the frames are numbered from 0, so in our three-frame example the frames are 0, 1, and 2.

The last parameter is the frame-rate — the number of frames per second.

In the previous blog post, the animation used the SMOOTH flag to scroll a single frame smoothly across the prim surface. Let’s take another look at this, because there is a little more to the SMOOTH option.

To demonstrate this, let’s have an image of 6 numbers:

Scrolling frames example

Scrolling frames example

Apply this texture in place of the original and replace the second llSetTextureAnim call in the script with the following:

llSetTextureAnim(ANIM_ON | SMOOTH | LOOP, 0, 6, 1, 0, 6, 0.5);

Save it, and touch the prim again to restart the animation. This time, you will see each number smoothly scroll into view.

In the next tutorial blog, I’ll show a third way of animating textures, which doesn’t make use of llSetTextureAnim() at all.