0
\$\begingroup\$

I need to send a 2d array to a job system but execute function of IJobParallelFor just accepts one dimension. This is the example:

public  struct CalculateJob : IJobParallelFor
    {
        public NativeArray<Vector3> vertices;


       public int rows;
       public int x, y;

        public void Execute(int i)
        {


        }
    }
\$\endgroup\$
4
\$\begingroup\$

Just flatten the 2d array into a 1d array by concatenating the rows. So a 2d array like this:

11111
22222
33333

becomes a 1d array like this:

111112222233333

Like this for example:

int i = 0;
for (int y = 0; y < height; y++) {  
     for (int x = 0; x < width; x++) {
         nativeArray[i] = 2dArray[x,y];
         i++;
     }
}

To find the index in the 1d array which corresponds to a specific x:y coordinate pair, use the formula i = x + y * width. So to convert it back:

for (int y = 0; y < height; y++) {  
     for (int x = 0; x < width; x++) {
         2dArray[x,y] = nativeArray[x + y * width];
     }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.