Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Friday, April 20, 2007

Better Mousetrap

The schedule board at Rockcliffe University, while it looked nice, I'm told it was quite horrible to actually use. It required all sorts of strange configuration via notecards, making the weekly schedule change a nightmare.

I thought there had to be a better way. So I made a similar board - by tapering a box and then mashing it flat, three different letters could be put on a single prim. But instead of arcane configuration, I set it up so you could simply drop a notecard on it, and the text of the notecard would be displayed on the schedule board. Not too bad!

But it was kind of hard to read, and I thought it might be nice if you could change the colors for each line (or groups of lines by day), and added in a #color command which wouldn't be printed, but would set every subsequent line to the specified color, until it was overridden by another #color command. (Shown to the right; click image for a larger view. Classes which had already taken place appear in dim characters.)

And yet I still wasn't satisfied. I was already planning a Web-based schedule database, so the teachers could go in and schedule their classes, which would then appear on the Web site's calendar - so why not also automatically update the board whenever the schedule changed, or a class was over?

This required delving into the XML-RPC functions - which was an interesting learning experience in its own right. But while I was doing that, someone on the Scripters of Second Life group mentioned offhandedly that you could get five letters onto a prim. Well, that sounded like a fun challenge - not to mention cutting the number of prims by 40% - so I set about trying to do so.

Here's a graphic showing the stages of making a 5-letter text segment:


First, we start with a prism. This is the fourth icon on the "Create" pane, between the pyramid and the cylinder. I've colored the front faces red and black just for reference.

Second, set the Taper X and Taper Y both to zero, to get rid of the point on the top and make the edges straight.

Third, set the Path Cut to B=0.2, E=0.8. This cuts a wedge out of the front of the prism, giving us the faces which I've colored green and white. Those particular values leave the red and black faces each taking up one-fifth of the prim's width.

Fourth, set the Hollow to 65. This cuts into the green and white faces, showing us the interior between them (colored blue.

Fifth, set the X size all the way down to 0.010, which flattens out the prim as much as possible.

And sixth, scale the prim so that it gives us a good height-to-width ratio for text. I scaled it to <0.010,0.964,0.250> so that the letter faces were the same size as the ones on my previous version.

Next came setting the texture offsets. If you just tried to put a texture on the faces, they'd look okay on the second and fourth faces, but stretched out somewhat on the first and fifth faces, and REALLY stretched (and backwards!) on the middle face.

So if we were just going to apply a texture with a single letter to each, we'd have to change the texture repeats and offsets. From left to right:

  1. Repeat 2.5 horizontal, 1 vertical; offset -0.25 horizontal, 0 vertical.
  2. Leave at the defaults (repeat 1 horizontal, 1 vertical; offset 0 horizontal, 0 vertical).
  3. Repeat 15.4 horizontal (flipped), 1 vertical; offset -0.2 horizontal, 0 vertical.
  4. Leave at the defaults.
  5. Repeat 2.5 horizontal, 1 vertical; offset 0.25 horizontal, 0 vertical.
With those settings, you would just need to swap the textures for each face in order to display a different letter. (I did it somewhat differently, though - I made a texture with the lower 128 ASCII characters arranged in a grid, which requires a bit more computation every time the letters change.)

I'm still writing the database portion of the system, but to test the XML-RPC communication, I wrote a quick script on my Web site to contact the board in-world, display the version and time, and then include the output of the Linux fortune program, adding #color commands as appropriate:


Now I just need to add in sequence numbers so that if the Web server doesn't receive a response to its communication, it can resend it, and the schedule board won't duplicate the information if it already received it but its response was lost. And then finish writing the database front-end, of course. But still, not bad for a couple days' work!

Monday, February 12, 2007

Textures and Generators

In one of recent posts, Ged Larsen talks about wrapping a texture around a skirt built by his fantastic LoopRez script. He sums up his experiment by saying:

I don't think this script could ever be useful as a releasable tool, though. Essentially, I think for every project, the script would have to be re-coded, which would probably not be worth it.
Of course, to me that sounded like a challenge! I've been meaning to get back to my own necklace generator and finish up the features I want to have in it, so I pulled it out and started to work.

But the first thing I needed to do was make mine faster. Because calculating equal placement on the circumference of an ellipse is extremely complex math, which to be honest I don't understand well enough to translate into LSL. So I did it the ugly, lazy way.

In essence, I started from a particular point on the ellipse and gradually increased the angle, bit by bit, until the position represented by that angle was at the distance I wanted it to be, within a certain amount of tolerance. The lower the tolerance, the longer the process took. For tiny prim jewelry, that required a really low tolerance.

So I ripped out all that code and replaced it with something much faster, based on a binary search algorithm. Basically, it takes two angles: at first, the back of the necklace and the front of the necklace. Then it picks an angle halfway between the two. If the angle is too large, it then recalculates between the starting point and the halfway point and tests again. If the angle is too small, it recalculates between the ending point and the halfway point, repeating until it's gotten within a certain amount of tolerance from the position we actually want. Then it moves on to the next piece, using the previous piece's location as the starting point to calculate from, and so on until it reaches the front of the necklace. (The other side is just a mirror image of the first, which is trivial to calculate.)

It sounds like more work, but even with a narrow tolerance, it's at least four times faster than the old version with a wide tolerance.

As to the image wrapping: while I can place the image on the prims in the proper location, it doesn't quite look right with the usual tapered cylinders that dresses are made out of; the narrow tops have too much image on them, and the wide bottoms too little. That problem will take a little thinking about. Perhaps it won't be as noticeable with a fabric pattern as it is with a snapshot.

Ged asks to see what progress I've made on it; here's a snapshot showing two different "waist sizes", with the same texture wrapped around the "skirt":


(The image I used was a snapshot of me with my friend Rhetorica and some of her schoolmates.) The problem I mentioned above is more visible on the smaller "skirt", up at the top. There's no "poof" to them because I haven't put in code to do so yet.

The process required that I create a skirt panel and drop in a script, then put the panel into my generator's inventory. Then, for each panel, the generator calculated the placement, texture offset and texture repeat values. It then rezzed out all the panels, and then sent the texture information to the script in each one.

It still has quite a ways to go, and I think I'm going to fork off a skirt generator from my necklace generator, just to make the latter faster yet (the fewer calculations it has to do, the better for speed's sake).

Wednesday, February 7, 2007

Conundrum!

I noticed the other day that some of my scripts were able to execute on no-script land.

Through the process of elimination, I was able to narrow it down to exactly what combination of factors permitted this to occur, and now I can reproduce it at will — for example, I modified the "Sexy Walk" attachment so it now continues to function in no-script territory; no more monkey walk for me!

My first thought was, "I should probably report this as a bug."

My second thought was, "But I could make a mint selling AOs that keep working!"

My third thought was, "I wonder if this is an exploit. Do they still offer a bounty on exploits? And if I don't report it, but sell objects which make use of it, would I get banned?"

Further research is needed.

Sunday, January 7, 2007

A Magic Barrier

While I'm getting my little vendor stall ready for its grand opening, I put up a "Coming Soon" barrier across the entrance — I guess I'm a little shy about letting people see my works in progress before I'm happy with the way they look. (Though I got a great compliment on it from someone yesterday, which totally made my day.)

Anyway, since the parcel's "landing zone" overrides the landmark I tried to set in my store, if I wanted to get through the barrier to work on it, I had to edit the barrier, set it to phantom, walk through it, and then set it to non-phantom again. And I'm way too impatient for that (in fact, just about all the programming I do at work is to make my life easier by automating the tedious tasks I have to do). So I wrote a little script for the door to set it to phantom when I merely touch it, and then turn itself back to non-phantom after five seconds.

But I was still too impatient, so I made it do its phantom thing when I walk or fly into it too. As it demonstrates the use of states, timers, and setting prim parameters, I figured it would make a good post for part 3 of my scripting tutorials.

Wednesday, January 3, 2007

Baked Textures, attempt 1

I took the plunge and rented myself a little vendor stall. Of course, I wanted to make it look nice, and I wanted to try out some texture baking, so I've spent the past couple of days working on that.

Aimee Weber's American Apparel build changes textures at sunset, and displays a floodlit look at night. Because I don't own the building my stall is in, I couldn't do much about the exterior — but since my stall has windows on two sides, and is across an aisle from similarly windowed stalls, I decided to change the interior lighting and shadows to match the position of the sun.



Every fifteen minutes (except at "night"), it gets the sun's current position and uses that to determine which texture to display. Fortunately, Linden Lab have been kind enough to provide a function to tell us where the sun is:

vector SunDirection = llGetSunDirection();


For some reason, the sun doesn't go straight east to west; it's tilted over by about 45 degrees, and the center of its path is somewhat above the grid, so it's below the horizon only one hour out of every four.

Anyway, making the textures for the store was fairly easy, but time-consuming. I recreated the entire store, plus some of the surrounding walls, ceiling and floor, using the Persistence of Vision Raytracer (a free 3D renderer). I chose POV-Ray over the other 3D packages I have because, like SL, it works with graphics primitives rather than polygon meshes, so it was easy to exactly re-create the geometry. Also, it can be scripted, so I could just plug in the list of sun positions, and make it light the scene from a different position based on which frame number I was rendering.

I had to render everything which would receive a baked texture: the wall, the floor, the ceiling, and every visible side of the wooden "bench" which would hold three vendor boxes. Then upload and apply! (The uploading was a bit pricey, since I had foolishly chosen to make a new set of textures for every fifteen minutes except nighttime. I managed to squeeze all the "bench" textures onto a single image for each time slot, but the grand total was still L$480 for only four things to texture — ouch! Maybe just day/night textures next time after all.)

Wednesday, December 27, 2006

Quite enough holiday cheer

I feel like a slug for not creating anything in SL or posting here in what seems like forever, but alas, the holidays intruded.

Just getting back into the swing of things (and undertaking my first paying SL job, refurbishing some scripts for someone to cut down on lag and permit multiple instances of the object to coexist), so not really much to show from recent times.

However, I did make this necklace today:



Before the holidays, I also got around to trying to make some movies from within SL, and (once I'd found a codec that actually wrote something to disk!) made a clip showing one of my earlier accessories in motion and uploaded it to YouTube. And I'm just now getting around to putting it on the blog.

My first day in SL, I raided the freebie store on Help Island for everything they had, and among them were some prim wings. "I wonder if I can make wings which flap?" I asked myself. The answer was "yes, but not very well". I'll skip the boring explanations of why my first two versions didn't work, and skip straight to Johanna's Bat Wings, Mark III.



They're flexi prims, scripted to flap in a way which will resynchronize them within a few seconds if they get out of synch, and they appear and disappear based on whether or not the avatar is flying. These were kind of tricky; flexi prims get really weird when you rotate them via a script. When you turn on flexi, the "handles" move from the center of the prim to one edge. You can rotate it manually with the handles — and it rotates from the edge, like you'd expect — but if you try to rotate it from the script, it'll still rotate from the center. But if you cut the prim in half, like you do to make a swinging door open along one edge, it doesn't react the way you'd expect. So I ended up having to do both a rotation and a translation in order to keep their edges "anchored" on the shoulder blades.

And, hey, comments on my other entries! Time to go answer those...

Sunday, December 17, 2006

Oops!

Day 35

I tortured a torus into a spindly, curvy shape and made a simple rotation script in it — really nothing more than llTargetOmega( <0,0,1>, 10, 0.1 ) — and just for the heck of it, turned on the "Physical" checkbox to see what it would do.

It hit the ground, fell over, and started rolling away — and it was so spindly, I couldn't select it to turn off the script! I chased after it, madly right-clicking all the way, and it hit the edge of the sim and disappeared. A few minutes later, I was notified that it had been returned to my Lost and Found folder because it had dropped off the world, two sims away. (I wonder if there are any Wild West sims that could use some tumbleweeds?)

Next time I decide to play around with physical objects, I'm building a corral for them first — and minimizing the script window after I save instead of closing it, so I can uncheck the "running" box if something goes horribly, horribly wrong.

On a side note, I see that I've used exclamation points in just about every post title so far. I'm going to have to watch that, or I'll run out of them.

Saturday, December 16, 2006

Success!

Day 33

Well, Johanna's Necklace Generator is almost finished! After two days of fruitless script-bashing, I swallowed my pride and asked for help on the Scripters of Second Life group. A very polite young man (from Canada, which explains the politeness) spent about an hour and a half with me, ripping out my Euler rotations and replacing them with quaternion math, and then bashing on those until they finally started doing what I needed.

The Necklace Generator is pretty simple, really — to use, at least! It's a script which you put into a cylinder, along with an object representing the chain link you want to use. Edit the script and tell it the name of the link object, how long the link is (which will usually be a little less than the actual length, assuming the links go through each other), whether or not you want it to rotate every other link 90 degrees, how far down you want the chain to come in back, and a couple of other things.

Then you edit your cylinder and stretch it to the dimensions you want. The dimension along the X axis of the cylinder controls how wide the necklace is from front to back (I know, that should be the Y axis, but that would add a whole new layer of math to everything... no thanks!), the Y dimension controls how wide the necklace is from side to side, and the height of the cylinder controls how far down it comes in front. (If you set its Z dimension to 0.010, it suppresses the drop entirely.) How far down it comes in back is controlled by a variable, as mentioned above; it's pretty much a percentage of how far down it comes in front.

Then you just touch the cylinder, and it thinks for a LONG time, and...


...several months later, you have a necklace chain wrapped around the cylinder! (Well, as long as you leave the cylinder with rotation <0,0,0> — it'll ignore the cylinder's rotation and act like it's unrotated regardless.) That slice cut out of the cylinder is to remind me which way is "front". I suppose I'll eventually make a special cylinder texture for the Necklace Generator.


After seeing it, I'm not really sure I like the style of that link — the rings are so thin they disappear when seen edge-on — but you get the idea. And from further away, it doesn't look too bad on the Cheapside Horde cross. As you can see below, the rotations aren't perfect, but it's close enough that only a couple minutes' tweaking are enough to bring everything in line, which is still much better than having to build the whole chain by hand!


The only thing I have left to do is add an option to make it automatically come to a point in front, for pendants and the like.

Also, I'm trying out some new textures for gems. I loaded a test pattern texture (I think it was from Robin Wood's in-world texture tutorial) onto a cylinder tortured into a gemstone shape to see how the faces were actually patterned. After doing a little unrotating, I came up with this texture:



Basically, it's a straight-on view of the gemstone's table and crown, taking up the top 80% of the image (I pasted shrunken copies underneath it as a "bleed" area), with the bottom 20% being the crown "unwrapped" with The GIMP's Polar Coords filter (Filters -> Distorts -> Polar Coords, with "To Polar" unchecked), cropped to the area beneath the points of the table and above the curves of the girdle, and then resized vertically to take up the bottom 20%.

When uploaded to Second Life, and applied to the gemstone, a little massaging the repeats and offsets just a touch, it resulted in this:


It looks a little blurry, because I made the texture at too small a size. But that doesn't matter for this particular gem, because unless people are looking really closely, they probably won't notice.


This is the gem on top of a cube sized to <0.010,0.010,0.010> — the smallest you can make a prim in Second Life without resorting to prim torture:

The gem's prim is actually <0.020,0.020,0.010>, so it's twice as big around as it could be! (I needed to make it bigger to get the proper slope for the crown.) It's a sphere, set to dimple B=0.95, E=1.00, converted into a cylinder, tapered to X=0.95, Y=0.95, and then resized to <0.020,0.020,0.010>.

If you care to try it, feel free to grab the gemstone texture and put it on the prim! The table (the flat part on top) should have its horizontal and vertical repeats per face set to 0.400, the rotation to -90.00 degrees, and the vertical offset at 0.1. The crown (the sloped edges) should be set with a horizontal repeat per face of 1.000, flipped; vertical repeat per face of 4.000, rotation of 0 degrees, a horizontal offset of 0.000, and a vertical offset of 0.300.

(Edit: For some reason, when I followed my original instructions just to make sure I had them right, they didn't produce the results I expected! Above are the corrected instructions.)

Thursday, December 14, 2006

Jewelry

Day 32

So here are some pictures of the jewelry I've made so far. I'm still not finished with the necklace chain generator, but I'm getting closer (I now have placement on the perimeter of the cylinder and one rotation; I just need two more all three rotations).



Above is the first really intricate piece I made, it's an attempt to reproduce my grandmother's locket in-world. The necklace chain was made by hand, and took almost as long as the locket itself. As with all my jewelry, it's scripted: you can change the color of the metal and the gemstone textures, turn bling on and off, and a couple of other minor things. It's "actual size", the locket itself is about 5cm tall and 4cm wide.


This is a matching circlet, bracelet and armband; I was playing around with torturing a torus and the design suggested itself to me. Like with the locket, you can change the colors — either individually or all pieces at once.



This is a pretty simple brooch. I mainly made it to showcase a set of gemstone textures I made. The problem with the usual gemstone textures is that they don't look good when wrapped around a sphere, spheres need to be textured with a Mercator-like projection for minimal distortion. I rendered a spherical faceted gem in POV-Ray with an orthographic camera, then used The GIMP to perform a reverse polar mapping on it. That gave me the top hemisphere, so I just cloned the layer, rotated it 180 degrees and moved it down so that its top edge was touching the bottom edge of the original layer. Flatten, save and upload, et voilĂ ! A spherical gem texture that looks good on a sphere! I also made it in multiple colors:


I made nine gem colors in all: amethyst, blue topaz, citrine, diamond, emerald, garnet, ruby, sapphire and topaz.

This cross, which hopefully will have a necklace chain sometime before the end of the century, is a reproduction of a piece from the Cheapside Hoard, a treasure trove of jewelry from around 1600, dug up in the early 1900s. There are plenty of things I want to make from the Hoard. Above is a rectangular-cut amethyst texture I made; below are garnet and blue topaz (shown with the metal set to gold and silver, respectively). Actually, I created two textures for each color: one for the gem oriented horizontally, and one for vertical orientation. Alas, the distortion caused by tapering the box robbed a lot of the detail from the sides of the gems, but it's still not too bad. There's no bling on this piece, because I found it was just so tiny the bling particles overwhelmed it. But sometimes subtle is much nicer than flashy.

The little finial gemstones are almost too small, as I mentioned in my previous post; I lost quite a few attempts while making them. Create a sphere and set its dimple to B=0.98 and E=1.00, then change it to a cylinder. Taper it to x=0.95 and y=0.95, and then set the size to x=0.010, y=0.010, z=0.010, and you have an incredibly tiny conical frustum. The rectangular and diamond-shaped pieces were made small (or excessively tiny!) in a similar fashion; dimple a sphere (I think I used B=0.75,E=1), change it into a box, then taper and resize to taste.



Just for a little sense of scale, here's a table showing the different pieces of jewelry I've made so far. The sword isn't really jewelry, but I'm proud of it too. Most nights, the Technical User Interfacing (or TUi) school holds challenge build competitions: you show up, and when the competition starts, they tell you what the topic is. You have 45 minutes to create it entirely from scratch (you can use textures and scripts from your inventory, but that's it — all the prims in your build have to be created, textured, and positioned right there). The topic for that night, obviously, was "swords". I decided I wanted something ladylike — no point in making something I couldn't use! — so I Googled for images of swords and found a beautiful swept-hilt rapier.


I was working right up to the time limit. I didn't win (well, I thought mine looked the best!) but it was fun, and challenging, and I made something I probably wouldn't have otherwise. Unfortunately, those contests are usually held at 10PM Pacific time, so if I participate on a work night I only get about four hours' sleep.

Wednesday, December 13, 2006

Math is hard, let's go shopping!

Day 31

I find building jewelry in Second Life to be relaxing, rewarding and aggravating. "Relaxing" because there's a regularity to it; it's precision work, and I can just let the world fade away as I concentrate on moving little tiny prims around. It's what I imagine Zen is like, complete focus on a single thing, where time and the physical world are meaningless.

It's rewarding because there's always the challenge to slice up the prims just a teeny bit smaller than they're supposed to go, without getting them so tiny that the client won't render them anymore and they vanish, invisible and unselectable, not to be seen again until the sandbox returns your them because they've been there too long.

But it's aggravating because if you want to do things perfectly, sometimes you have to do math. And I hate math. I'm not good at it, I look at pages like this and want to run away. (And that was the least technical (and condescending!) answer in the thread!)

See, I was trying to build a better mousetrap. Necklace chains are an enormous pain to do by hand (Zen relaxation is great, but there's only so much of it I can stand). And there are a few scripts out there which will automate building necklaces, but I haven't found any which do quite what I wanted:

  1. Let the user create a cylinder and stretch it to the desired dimensions, so that the x dimension represents the width of the neck, the y dimension represents the front-to-back measurement (including the amount the chain comes forward to lie properly on the chest), and the z dimension represents the vertical curve of the chain (coming around the neck, over the shoulders and down the chest).
  2. Keep the spacing of the links constant, with no clustering or gapping if the path is elliptical.
  3. Calculate the proper number of links necessary, based on the length of the links and the distance traveled around the path.
  4. Rotate every other link 90 degrees around the axis of travel, if desired.
Doesn't sound too hard, right?

Ha! For starters, there's not even a definitive formula for finding the perimeter of an ellipse — just approximations with varying degrees of accuracy! I bashed my head against a wall for about eight hours last night and this morning, trying to get the formula in the above-linked page to work, before I finally gave up and resorted to a brute-force approach. It's not pretty, it's slow as molasses, but at least it works.

Well, what I've got written so far works — they took the grid down for updates before I was able to write the code which rotates the individual links into the proper orientation.

Once I get some decent chains made, I can put up some pictures of my jewelry!


Oh, also, a public service announcement: don't try to play Primtionary (like Pictionary, only with prims) if you've had a drink or two. The results aren't pretty.