Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Tiled Texture Offsets By Index Utility

Ushuaia Tokugawa
Nobody of Consequence
Join date: 22 Mar 2005
Posts: 268
06-21-2005 09:17
I found myself often doing this by hand (tedious) or doing it on the fly (expensive), so now I use this utility script to generate my texture offsets for tiled textures ahead of time then I hardcode the output list into my script.

Simply change the columns and rows variables to match your tiled texture and save. You can copy the output from your chat history window and paste it into your script.

CODE

////////////////////////////////////////////////////////////////////////////////
// Tiled Texture Offsets By Index
////////////////////////////////////////////////////////////////////////////////
// Generates a list containing the offsets for each index of a tiled image
// Copy and paste the list into your own script to easily offset textures
////////////////////////////////////////////////////////////////////////////////

default {
state_entry() {
// how many columns does your tiled texture have?
integer columns = 4;

// how many rows does your tiled texture have?
integer rows = 5;

// get the horizontal and vertical texture scale
float horizontalScale = 1.0 / columns;

float verticalScale = 1.0 / rows;

// rows and columns with an even number of tiles
// need a spacer equal to half the scale added to their offsets
float horizontalSpacer;

float verticalSpacer;

if (columns % 2 == 0) horizontalSpacer = horizontalScale / 2;

if (rows % 2 == 0) verticalSpacer = verticalScale / 2;

// translate the human readable index to the actual index
// and build a list of texture offsets for each index
// 0 1 2 3 10 11 8 9
// 4 5 6 7 6 7 4 5
// 8 9 10 11 translates to 2 3 0 1
// 12 13 14 15 18 19 16 17
// 16 17 18 19 14 15 12 13
list textureOffsets;

integer column;

integer row;

// set y to the row of the actual 0 index tile
integer y = llCeil(rows / 2.0) - 1;

// roll through each row a column at a time
for (row = 0; row < rows; row++) {
// set x to the column of the actual 0 index tile
integer x = llFloor(columns / 2.0);

for (column = 0; column < columns; column++) {
integer actualIndex = x + (y * columns);

// figure out the horizontal and vertical offsets for the actual index
float horizontalOffset =
((1.0 * actualIndex / columns) + horizontalSpacer) -
(actualIndex / columns);

float verticalOffset =
((actualIndex / columns) * verticalScale) + verticalSpacer;

// put the texture offset in a vector
// for easy use in llSetPrimitiveParams()
textureOffsets += <horizontalOffset, verticalOffset, 0.0>;

x++;

// reposition x to the left column
// if x is higher than the right column
if (x > columns - 1) x = 0;
}

y--;

// reposition y to the bottom row if y is lower than the top row
if (y < 0) y = rows - 1;
}

// output the results

// put the texture scale in a vector for easy use in llSetPrimitiveParams()
vector textureScale = <horizontalScale, verticalScale, 0.0>;

// set the name of the object to an empty string
// so we can get copyable output
llSetObjectName("");

// output the texture scale
llOwnerSay("/me vector textureScale = " + (string)textureScale + ";\n");

// output the texture offset as a list for easy copy and paste
llOwnerSay("/me list textureOffsets = [");

integer i;

for (i = 0; i < (rows * columns); i++) {
// lsl doesn't like a comma after the last element of a list
string comma = ",";

if (i == (rows * columns) - 1) comma = "";

llOwnerSay("/me " + llList2String(textureOffsets, i) + comma);
}

llOwnerSay("/me ];");
}
}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion Thread
06-21-2005 10:54
/54/54/51017/1.html
_____________________
i've got nothing. ;)