Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by mamuelSraz · Jan 17, 2021 at 05:26 PM · threadsthreading

How do i convert a 3dimensional array to be usable in a Job?

So i have a three dimensional array: Gridcell[,,] (gridcell is a custom struct with some values)

and i need to use it in a Job, but Unity Jobs do not support normal arrays. Is there another way than converting it into a one-dimensional NativeArray?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Jan 17, 2021 at 06:57 PM

Multidimensional arrays in C# are all one dimensional under the hood. It's just a matter of calculating the index. All you need are the sizes of each dimension. Lets say they are given like this:

 int Dim0 = 10;
 int Dim1 = 5;
 int Dim2 = 6;

Of course there are different ways how you could "flatten" the index. Though just as an example lets say we want the last dimension (Dim2) to be the continuous dimension. That means two elements in this dimension are next to each other in memory. In this case you can calculate the flattened index like this given the 3 indices:

 int index0, index1, index2;
 int flattenedIndex = Dim1 * Dim2 * index0 + Dim2 * index1 + index2;

Of course we can extract Dim2 from the equation to get

 int flattenedIndex =  Dim2 * (Dim1 * index0 + index1) + index2;

In this given example the element [2,3,5] would give you a flattened index of 60 + 18 + 5 == 83


Just think about it this way: The innermost array consists of 6 elements. The middle array consists of 5 innermost arrays. So those arrays are right next to each other. So index 0-5 are the indices [0,0,0] - [0,0,5]. The next 6 indices 6-11 represent [0,1,0] - [0,1,5]. And so on, so 12-17 represent [0,2,0] - [0,2,5]. The innermost and middle arrays combined have a size of 30 elements (Dim2 * Dim1 == 30). So every 30 indices we get into the next "outer most element". So an index of 30 == [1,0,0] while an index of 60 == [2,0,0].


The built-in multidimensional array in C# simply does this for you behind the scenes. Though those multidimensional arrays are really slow because any access has to perform a range check for all indices and calculate the flattened index every time. That's why using a single dimensional array and doing the flattening yourself can be much faster because you can precalculate the offsets when you operate in nested loops:

 for(int i0 = 0; i0 < Dim0; i0++)
 {
     int Dim0Offset = i0 *Dim1;
     for(int i1 = 0; i1 < Dim1; i1++)
     {
         int offset = (Dim0Offset + i1) * Dim2;
         for(int i2 = 0; i2 < Dim2; i2++)
         {
             int index = offset + i2;
             // array[index]

Of course when you just iterate through all indices you can simply carry the flattened index with you

 for(int i0 = 0, index = 0; i0 < Dim0; i0++)
 {
     for(int i1 = 0; i1 < Dim1; i1++)
     {
         for(int i2 = 0; i2 < Dim2; i2++, index++)
         {
             // array[index]

of if you don't need the indices of each dimension you can of course use an ordinary loop:

 for(int i = 0; i < array.Length; i++)
 {
     // array[i]



In a similar way you could also extract the dimension indices from the flattened index by simply reversing the calculations above. So for any given flattened index you can calculate the 3 dimension indices like this:

 i0 = fIndex / (Dim1 * Dim2);
 i1 = (fIndex % (Dim1 * Dim2)) / Dim2;
 i2 = fIndex % Dim2;

Technically the first term would be

 i0 = (fIndex % (Dim0 * Dim1 * Dim2)) / (Dim1 * Dim2);

Though the modulo is not required for the outermost index as long as the flattened index is inside the valid range. So for example a given index if 42 would result in i0==1, i1==2, i2==0

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What does new Thread do, and how many threads are too many? 1 Answer

Performing "transform.Rotate" in it's own thread 1 Answer

Runtime error printing another thread 2 Answers

Getting current time from the worker thread 1 Answer

Job System without using the main thread 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges