1
\$\begingroup\$

Please bare with me as I explain what I am trying to accomplish :P

Each 'X' below represents a mesh vertex:

enter image description here

For my Unity3d GameObject (G1); the mesh vertices are stored in a C# List object in the following order. *Note the mesh index 'A'. This is a value I know - in this case A=16 (mesh index 16).

enter image description here

I have another GameObject (G2) whose shape is the same (a simple rectangle) but the mesh vertices are ordered differently.*Note the mesh index 'B'. This is a value I dont know and I want to find this value (mesh index) - in this case B=17 (mesh index 17).

enter image description here

As you can see the mesh arrangement is pretty much the same except rotated 90 degrees. If I know the 'mesh vertex index' of A how can I find the 'mesh vertex index' of B?

Whats an algorithm that can calculate the mesh index of B? My attempt is not working at all:

uint A     = 16;
uint nCols = 6;
uint nRows = 4;

// Technically both B and A sit on Row 3, Column 5 so if I can find A's row and column maybe I am there?
uint aCol = 16 / nCols;        // nup not right
uint aRow = (16 % nCols) - 1;  // gives 3

// Once I've calculated aCol and aRow its easy
uint B    = (aRow * nRows) + aCol;  // ie, (3 * 4) + 5 = 17
\$\endgroup\$
1
  • \$\begingroup\$ I think I have the general idea, but it might be better if it was rephrased in terms of inputs and outputs. Would it be something like this? int GetVertexIndex(int row, int column) { /* ... */ } If so, part of the answer is going to include figuring out which is a row and which is a column, since meshes are row/column agnostic. \$\endgroup\$
    – jzx
    Jun 20, 2014 at 17:04

1 Answer 1

1
\$\begingroup\$

I think that you got the most part right except by the fact of aCol and aRow is swapped.

// the mod operation will give a number ranging from 0 to nCols-1 so there's no need to
// subtract 1.
uint aCol = 16 % nCols;
uint aRow = 16 / nCols; // this value must be less than nRows

// The B get swapped as well because of the rotation of the matrix
// uint B    = (aCol * nRows) + aRow;
// but is not quite right yet, since the aRow counts ftom top to bottom therefore
uint B    = (aCol * nRows) + (nRows - aRow - 1);

Hope it helps. If the mesh arrangement is arbitrary you will need a more complex code than this one.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .