Posts Tagged ‘animated’

LSL Scripts: Scrolling floating text

Wednesday, August 24th, 2011

This is one of an occasional series of small but (I hope) useful pieces of code.

Floating text above a prim has a lot of uses in Second Life, and here is a script which might add even more possibilities. This takes advantage of the fact that you can display multiple lines of text and uses this to create a scrolling text display, like so:

It works very simply. We have a list which stores the lines to be displayed. To add a new line to the text, and to scroll the text up, we remove the first entry in the list, and add the new entry at the end. To display the text, we just string the entries in the list together, separating them with a new-line, and set the result as the floating-text for the prim.

In this little example program, a line of text is added whenever the prim is touched.

list lines = ["", "", "", "", ""];
addLine(string line)
{
    // Add the new message to the end of the list, losing the first entry
    // from the list at the same time.
    lines = llList2List(lines, 1, llGetListLength(lines) - 1) + [line];
    // Concatenate the lines together, separated by a new-line.
    string output = llDumpList2String(lines, "\n");
    // Set the result as the floating text.
    llSetText(output, , 1.0);
}
default
{
    touch_start(integer count)
    {
        addLine("Touched at " + llGetTimestamp());
    }
}

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.

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.

Animated Textures – Tutorial 1

Saturday, September 19th, 2009

With the recent release of the Animated Steampunk Textures pack, this would be a good time to do some tutorials on animated textures, so here we go…

This is the first of a three-part tutorial about using animated textures in Second Life. There three parts, because there are three different ways of animating textures, each with their own use. Two of them make use of the LSL function, llSetTextureAnim, which is the usual way of animating textures. The third way uses a timer and offsets to manually animate the texture — it is a lot less efficient, but can occasionally be useful.

In this first part, however, I’ll look at the simplest and easiest way of animating a texture, using the ‘smooth scroll’ option. This simply moves the texture across the surface of the prim, and doesn’t need a specially designed texture — any texture can be scrolled, although some will obviously work better than others!

A common use of this is to create a flowing water animation, and that’s the example I’ll use here, especially as suitable textures are available from the built-in Library in every Second Life avatar’s inventory.

So…

Rez a simple cube, and apply a water texture to it — the Water – ripple layer 1 texture in the Library\Textures\Waterfalls folder should work well. Resize it into something fairly long and flat (5 x 2 should be effective).

On the Contents tab of the Build window, click the ‘New Script’, and wait for it to appear in the Contents list (if SL is being laggy this might take a few seconds). Double-click to open it.

Replace the existing code with the following:

default
{
    state_entry()
    {
        llSetTextureAnim(ANIM_ON | SMOOTH | LOOP, 0, 1, 1, 1, 1, 0.12);
    }
}

Save the script, and the texture will start scrolling. Assuming that you haven’t rotated the prim, this will scroll the top surface.

Ok, now you’ve seen it in action, let’s look at the parameters.

The first parameter is a set of flags which specify how the texture should be animated. You can string together several flags (as in the above example) by using the ‘|’ symbol (technically an ‘OR’ operator). Note that some combinations don’t make sense.

You can check the LSL Wiki page for details of what each flag means, but in the example we are switching the animation on (rather important!), and we are saying that we want a ‘SMOOTH’ animation, which means that the texture will simply be scrolled across the surface of the prim (actually, this is not quite an accurate description of what SMOOTH does, but I’ll explain it in more detail in the next article).

Finally we are specifying ‘LOOP’, which means that the animation will keep going continuously.

Instead of LOOP we could have specified PING-PONG, which reverses the animation when it reaches the end. In the case of a smooth animation, it will scroll the texture for the entire length, then reverse direction and scroll it back again.

If you want to stop an animation, simply set this parameter to 0. All the other parameters are irrelevant in this case.

The next parameter — 0 in this example — is the face of the prim that the animation should be applied to. All the other faces will be static. However, if you use ALL_SIDES for this parameter, all the faces of the prim will animate.

Unfortunately, it isn’t possible to animate some but not all of the faces — it’s either 1 face or all faces.

A couple of final notes. You’ll notice that the Repeat and Flip options for textures don’t have any effect on animated textures. Rotate, however, does.

That will do for now. In the next article, I’ll take a look at the traditional animated texture, which uses a sequence of frames like an animated movie.

Putting Things In Motion

Tuesday, July 21st, 2009
Yet another Steampunk texture pack, but this time with a crucial difference: these ones are animated textures. Use them to bring your steampunk machinery to life, but please be careful — yes, I’m looking at you, Dr Frankenstein…
Of course, animated textures need a bit more work than just applying them to a prim. You need a script to set them in motion. This pack comes with a set of sample scripts, all with full permissions so that you can copy and edit them as much as you want. It also comes with a set of pre-built prims, with the scripts already present and active — simply add them to your build, or use them as examples to copy (they are No Transfer, though, so if you want to sell your builds, you will need to create your own versions).
Here’s a sample picture of some of the textures (there are eleven in all, some of them in small and large versions). Of course, this can’t show you what these look like in motion, but you can visit the Steampunk Textures floor of the Templar Creations main store to see them (the prims which are displayed in the store are the ones which are included in the pack).
Buy the pack in-world at the store, or from the XStreetSL page:
Templar Creations Main Store http://slurl.com/secondlife/Ling/183/111/54/?img=http%3A//www.templarcreations.com/graphics/Terrain_Pick_Logo.png&title=Templar%20Creations%20Main%20Store
XStreetSL Page https://www.xstreetsl.com/modules.php?name=Marketplace&MerchantID=24474

Yet another Steampunk texture pack, but this time with a crucial difference: these ones are animated textures. Use them to bring your steampunk machinery to life, but please be careful — yes, I’m looking at you, Dr Frankenstein…

Of course, animated textures need a bit more work than just applying them to a prim. You need a script to set them in motion. This pack comes with a set of sample scripts, all with full permissions so that you can copy and edit them as much as you want. It also comes with a set of pre-built prims, with the scripts already present and active — simply add them to your build, or use them as examples to copy (they are No Transfer, though, so if you want to sell your builds, you will need to create your own versions).

Here’s a sample picture of some of the textures (there are eleven in all, some of them in small and large versions). Of course, this can’t show you what these look like in motion, but you can visit the Steampunk Textures floor of the Templar Creations main store to see them (the prims which are displayed in the store are the ones which are included in the pack).

Animated Steampunk Textures

Animated Steampunk Textures

Buy the pack in-world at the store, or from the XStreetSL page:

SLURL: Templar Creations Main Store

XStreetSL Page