Sunday, July 12, 2009

texture sprites in second life

seasoned web hands will probably be familiar with the concept of CSS Sprites. the idea behind sprites is that with modern networks tuned for downloading large images, why not improve the performance of a web site by smashing all your visual elements in one image (JPG, GIF, PNG, whatever.) and then use the position features of CSS2 to display just a bit of that big image in a visual element. here's a visual to help grok it:

this image contains 12 distinct visual elements, arrows, circles, checkmarks, etc. (i nabbed these images from the wikimedia commons where they are released under a GPL license, more info at the gnome-look website.) note the box around one of the elements? that's there to remind you that you probably only want to display a portion of this image at any given time. the elements are stuffed into a single image for download to your browser to take advantage of the performance boost you may get by downloading single images instead of multiple individual images.

users of second life might have seen textures seem to not rez properly or rez half way and hang before completing download. there are several reasons why this might occur, some errors, others ramifications of the way progressive downloads on SL textures are handled. progressive downloads are intended to give users a context for the virtual world in as short a time as possible. in other words, it was thought that a world where all/most the textures are filled in but fuzzy and gradually got less fuzzy was better than a grey or transparent world where you had to wait forever to render the whole, non-fuzzy scene.

but this policy is problematic for some objects in the virtual world. objects like signs intended to display arbitrary text will sometimes rez the images representing different letters on a different schedule. this gives a weird appearance where some letters are clearly defined while others are fuzzy. for my money, this is a touch unsettling.

you can avoid this with the use of "texture sprites". SL builders are probably familiar with the fact that textures have a "repeats per face" and "offset" options. you can use these options to create the SL equivalent of CSS sprites. that is, you can apply a large image that contains multiple visual elements and use the repeats per face and offset parameters on a primitive face to only show the visual element you want to see. the benefits here are that all the visual elements will be in the same state of "rezzedness".

here's an example. i rezzed a cube with the dimensions <0.01,0.1,0.1> and applied the blank texture. i then put the following script in it:

// Copyright (c) 2009, Meadhbh Hamrick
// All rights reserved.
// 
// Redistribution and use in source and binary forms,
// with or without modification, are permitted provided
// that the following conditions are met:
// 
// * Redistributions of source code must retain the
//   above copyright notice, this list of conditions and
//   the following disclaimer.
// * Redistributions in binary form must reproduce the
//   above copyright notice, this list of conditions and
//   the following disclaimer in the documentation
//   and/or other materials provided with the
//   distribution.
// * Neither the name of the Project Meadhbh nor the
//   names of its contributors may be used to endorse
//   or promote productsderived from this software
//   without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY MEADHBH HAMRICK
// ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL MEADHBH HAMRICK BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

integer face;

stroke_face ( ) {
    vector offset;
    if ( 0 == face ) {
        offset = <0.0,0.333,0.0>;
    } else if ( 1 == face ) {
        offset = <0.0,0.111,0.0>;
    } else if ( 2 == face ) {
        offset = <0.0,-0.111,0.0>;
    } else {
        offset = <0.0,-.333,0.0>;
    }

    llSetPrimitiveParams( [ PRIM_TEXTURE, 2, "4b2b744a-7203-dcc7-3d8e-db8aa195f103", <1.0,0.111,0.0>, offset, 0  ] );
}

default
{
    state_entry()
    {
        face = 0;
        stroke_face();
    }

    touch_start(integer total_number)
    {
        face = face + 1;
        if( face > 3 ) { 
            face = 0;
        }
        stroke_face();
    }
}

people allergic to copy and paste can download the script from project meadhbh at sprite_test.lsl

the texture "4b2b744a-7203-dcc7-3d8e-db8aa195f103" was generated by combining several small images with the css sprite generator from project fondue. a copy of the png the texture was generated from is available at texturesprite.png. and in the interest of full disclosure, i should mention that the images from which this texture was generated were downloaded from the wikimedia commons site, were apparently made by John Erling Blad and released under a GNU Free Documentation License. Ergo, the PNG linked to above and the "4b2b744a-7203-dcc7-3d8e-db8aa195f103" texture should both be considered to be released under the same license.

Saturday, July 11, 2009

when nostalgia strikes

so the other day as i was trying to figure out why my macbook pro was giving me a command line interface at bootup instead of a screenful of aqua goodness, i realized i longed for the delightful mush of my old commodore 64. readers who weren't around for the heady days of the early home computer explosion probably won't get it, but the old 8-bit systems had some great features. i bought my commodore 64 new for $200 and, after hitting the power switch, you were ready to start typing in about 5 seconds. two features you will not find on a PC class machine.

but in today's world, the 8-bit machines are virtually useless for what i do normally: surf the web, write software and use second life. so even though twittering and rendering second life scene graphs are not out of the question for old 6502 based machines; were i to switch back to the old 8 bit machine i cut my teeth on, i would surely die of youtube withdrawal.

so i reckoned the next best thing was to relive the glory days on my macbook pro. if you're like me, one of the apps you keep open constantly is the Terminal.app. i'm an old software developer and NeXT system administrator; i took the time to learn many useful command line tools, and dang-nabbit i'm going to use them.

instead of pitching my mac for an old 8-bit system, i simply made it look a little more like the old c64. and here's how i did it:

  • first, download the Commodore 64 Pixelated typeface. it's a free download. yay! nostalgia doesn't have to hurt! After downloading the typeface, you should see a TrueType File called commodore.ttf. double click on it to launch the font installer.
  • next you'll want to download the small program that spits out the commodore welcome message. you can extract it from the project meadhbh repository file trunk/nostalgia/c64. once it's downloaded, you'll want to move it to your home directory, add a dot to the beginning of it's name and make it executable with the chmod command. so open a terminal, cd to the directory containing the file (probably ~/Downloads unless you've changed it) and execute the commands:

    mv ./c64 ~/.c64
    chmod 755 ~/.c64

  • now download the terminal settings file from trunk/nostalgia/nostalgia.terminal. you'll want to move this file to your home directory for reasons that will be clear soon.
  • finally, open the Terminal.app preferences and click on the settings tab. you should see something like this. note the location of the settings tools widget (it's the thing circled by the red oval.)
  • click the tools widget. this will give you a menu, select the item "Import..." this will bring up a standard file dialog. for reasons i don't understand, my version of Terminal.app doesn't allow you to navigate through your directories in search of nostalgia.terminal file. fortunately, you moved it to your home directory.
  • finally.. you need to insert a line into your .profile file. using your favorite text editor, add the following line to the file ~/.profile

    test -r ~/.c64 && . ~/.c64

    i put mine right above the other test line near the top.
and that's it. you should now be enjoying the glory of a commodore-64 like experience.