• 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
0
Question by MattG54321 · Apr 17, 2018 at 12:28 AM · meshoptimizationprofilervoxelvoxels

Profiler shows Object.ElementAddr_3_8() called over a million times. What is it? How do I reduce this?

I'm working on a voxel game, and in the profiler, under ChunkBehavior.CreateMeshAndCollision(), I see that Object.ElementAddr_3_8() is called tens of thousand of times per call of CreateMeshAndCollision(). It's creating over 70ms of delay every time I create new chunks, making my game stutter. Any idea why Object.ElementAddr_3_8() is being called so many times?


I appreciate any help. I've dropped my CreateMeshAndCollision() method below, if you'd like to take a look at it. Thanks!


 public void CreateMeshAndCollision() {
     vertices = new List<Vector3>();
     triangles = new List<int>();

     ChunkBehavior topNeighbor = tb.GetChunk(transform.position.x, transform.position.y + SIZE, transform.position.z);
     ChunkBehavior bottomNeightbor = tb.GetChunk(transform.position.x, transform.position.y - SIZE, transform.position.z);
     ChunkBehavior posXNeighbor = tb.GetChunk(transform.position.x + SIZE, transform.position.y, transform.position.z);
     ChunkBehavior negXNeighbor = tb.GetChunk(transform.position.x - SIZE, transform.position.y, transform.position.z);
     ChunkBehavior posZNeighbor = tb.GetChunk(transform.position.x, transform.position.y, transform.position.z + SIZE);
     ChunkBehavior negZNeighbor = tb.GetChunk(transform.position.x, transform.position.y, transform.position.z - SIZE);

     for (int x = 0; x < SIZE; x++) {
         for (int y = 0; y < SIZE; y++) {
             for (int z = 0; z < SIZE; z++) {
                 if (blocks[x, y, z] != null) {

                     bool needsCollision = false;

                     if (y == SIZE - 1) {
                         if (topNeighbor != null) {
                             if (!topNeighbor.BlocksVew(x, 0, z)) {
                                 blocks[x, y, z].CreateTopFace(vertices, triangles);
                                 needsCollision = true;
                             }
                         } else {
                             blocks[x, y, z].CreateTopFace(vertices, triangles);
                             needsCollision = true;
                         }
                     } else if (blocks[x, y + 1, z] == null) {
                         blocks[x, y, z].CreateTopFace(vertices, triangles);
                         needsCollision = true;
                     }
                     
                     if (y == 0) {
                         if (bottomNeightbor != null && !bottomNeightbor.BlocksVew(x, SIZE - 1, z)) {
                             blocks[x, y, z].CreateBottomFace(vertices, triangles);
                             needsCollision = true;
                         }
                     } else if (blocks[x, y - 1, z] == null) {
                         blocks[x, y, z].CreateBottomFace(vertices, triangles);
                         needsCollision = true;
                     }

                     if (x == SIZE - 1) {
                         if (posXNeighbor != null && !posXNeighbor.BlocksVew(0, y, z)) {
                             blocks[x, y, z].CreatePosXFace(vertices, triangles);
                             needsCollision = true;
                         }
                     } else if (blocks[x + 1, y, z] == null) {
                         blocks[x, y, z].CreatePosXFace(vertices, triangles);
                         needsCollision = true;
                     }

                     if (x == 0) {
                         if (negXNeighbor != null && !negXNeighbor.BlocksVew(SIZE - 1, y, z)) {
                             blocks[x, y, z].CreateNegXFace(vertices, triangles);
                             needsCollision = true;
                         }
                     } else if (blocks[x - 1, y, z] == null) {
                         blocks[x, y, z].CreateNegXFace(vertices, triangles);
                         needsCollision = true;
                     }

                     if (z == SIZE - 1) {
                         if (posZNeighbor != null && !posZNeighbor.BlocksVew(x, y, 0)) {
                             blocks[x, y, z].CreatePosZFace(vertices, triangles);
                             needsCollision = true;
                         }
                     } else if (blocks[x, y, z + 1] == null) {
                         blocks[x, y, z].CreatePosZFace(vertices, triangles);
                         needsCollision = true;
                     }

                     if (z == 0) {
                         if (negZNeighbor != null && !negZNeighbor.BlocksVew(x, y, SIZE - 1)) {
                             blocks[x, y, z].CreateNegZFace(vertices, triangles);
                             needsCollision = true;
                         }
                     } else if (blocks[x, y, z - 1] == null) {
                         blocks[x, y, z].CreateNegZFace(vertices, triangles);
                         needsCollision = true;
                     }
                     /* Don't create collision until we've fixed t$$anonymous$$s problem.
                     if (needsCollision) {
                         BoxCollider newBoxCollider = gameObject.AddComponent<BoxCollider>();
                         newBoxCollider.center = new Vector3(x + .5f, y + .5f, z + .5f);
                     }*/
                 }
             }
         }
     }

     mesh = new Mesh();
     mesh.vertices = vertices.ToArray();
     mesh.triangles = triangles.ToArray();
     mesh.RecalculateNormals();
     GetComponent<MeshFilter>().mesh = mesh;
 }


Comment
Add comment · Show 1
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
avatar image bobisgod234 · Apr 17, 2018 at 04:13 AM 0
Share

What value is "SIZE"?

1 Reply

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

Answer by Bunny83 · Apr 17, 2018 at 04:25 AM

ElementAddr_3_8 is the internal method to calculate the flattend array index for your multidimensional array. Since you have a lot array accesses inside the inner most loop you will of course get a lot of these.

You can eliminate some of them by cac$$anonymous$$ng the current block at the start of your innermost loop. However for preformance it would be better to use a one dimensional flattend array yourself. Multidimensional arrays are quite slow. When you manually flatten the array you can calculate the intermediate offsets before each nested loop so the index calculation would get simpler.


Though those pure number crunc$$anonymous$$ng stuff you may want to carry out on a seperate thread. Just prepare all data required on the main thread and then start the generation. Of course the actual final Mesh creation has to be done on the main thread again.

Comment
Add comment · Show 1 · 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
avatar image MattG54321 · Apr 18, 2018 at 02:00 AM 0
Share

Alright. I'll try the array flattening and I'll have to look into the upcoming Unity C# job system or multithreading. Thanks for the quick response! It was killing me to know what ElementAddr_3_8 was.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

96 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

Related Questions

Size of a mesh vs performance 1 Answer

Mesh Collider for Voxels 1 Answer

How do you handle water in voxel games? 0 Answers

Is there a Per-Mesh-Profiler somewhere? 2 Answers

Frame Rate drops when creating mesh in mobile Voxel World? 0 Answers


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