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).


Ged Larsen said...

That's great, using the binary search! When you "move on to the next piece", do you start with the previously found "delta theta" that worked? My guess is that in large portions of the ellipse, it'd likely be really close to the next "correct" angle change?

My program does the search exactly as yours used to, but with skirts, I was able to get away with increasing the angle by 1 degree at a time (a pretty big bit-by-bit), and I didn't even worry about the "certain amount of tolerance" -- I just increased the angle UNTIL I reached a point greater than or equal to where the next panel was supposed to be, and then moved on to the next panel. Like you, I only did the calculations for half an ellipse. (If you had an even number of objects, you could even use a quarter of an ellipse.)

For my weird PathRez Lissajous thingy, like you, I found LSL very slow, and because I needed tighter tolerances (I think I used a deltaTheta of 0.1 or 0.01 degrees), I offloaded the computation from SL to the desktop. Oh, and because a Lissajous curve isn't an ellipse, there wasn't even an approximation forumula for a circumference, so I had to estimate it via estimated integrals... LSL would not have enjoyed that...

And, yeah, texture tiling... Ugh... I'm glad you are taking on that challenge, especially with cut and tapered cylinders. You'll notice that on mine, I cheated and used blocks, just to avoid that hassle ;)

Someday, perhaps we should collaborate on a project :)

I think I write too much. Sorry...

Johanna Hyacinth said...

Yes, I start from the last found position. I set my default accuracy to 0.00001 meters, so I figure it's close enough.

Alas, I don't think there is a cure for the tapered cylinder texture weirdness, not unless the Lindens decide to give us the ability to directly specify UV coordinates to map the textures onto faces instead of the rather primitive repeat/offset combinations.

It's so funny; you have no problem just whipping up something with "estimated integrals" but balk at the texture tiling... whereas integrals scare the crap out of me and the texture tiling seemed pretty straightforward (the hardest part was figuring out why I was running into fencepost error). I agree, we really ought to collaborate on something; I think we'd complement each other's strengths.

Delfina Vacano said...

Hello... I was just thinking about forging into jewelry-making and was researching how to make it (seriously, I've no clue, I'm doing lots of research - I know how to buy stuff but I have yet to make anything, and I always seem to miss those jewelry-making classes) when I came across your blog - I saw the results of your necklace generator and they look great! Is that script available or are you selling it (like the skirt maker?) I've no access to SL right now so if it is available in your store, let me know and I'll check it out whenever I can get on! Also, I haven't been to your store yet, either - your jewelry looks awesome! I can't wait to check your stuff out.