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
0
Question by ExtremePowers · Mar 17, 2015 at 08:49 PM · terrainlagslowsplatmap

Slow for loop

Why does this piece of code run at 300ms? I can't see how it is doing anything expensive:

     IEnumerator PaintTheTerrain() {
         int aw = Chunk.terrainData.alphamapWidth;
         
         for (int x = 0; x < aw; x++) {
             float normalPosX = (x * 1f) / (aw * 1f);
             for (int y = 0; y < aw; y++) {
                 float normalPosY = (y * 1f) / (aw * 1f);
                 
                 float angle = Chunk.terrainData.GetSteepness (normalPosX, normalPosY);
                 float height = Chunk.terrainData.GetInterpolatedHeight (normalPosX, normalPosY);
                 Biome Biome = Biomes.GetPoint (x, y);
 
                 if (height < WaterLevel + 2) {
                     map [y, x, 2] = 1;
                 } else if (angle > 25) {
                     map [y, x, 1] = 1;
                 } else {
                     map [y, x, Biome.SplatIndex] = 1;
                 }
             }
             if (x % 10 == 0) {
                 progress = (x * 1f) / (aw * 1f);
                 yield return null;
             }
         }
         Chunk.terrainData.SetAlphamaps(0,0,map);
     }
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
0

Answer by farhanblu · Mar 18, 2015 at 01:31 PM

Without going deeper into your code, I will guess that the statement "yield return null" is getting called too frequently, which means every iteration is delayed by 'at least' one frame. Normally at 30fps, your loop will run after 0.03 (1/30) seconds every time the boolean expression returns true. Try putting a Debug.Log(x); for the value of x and see if the boolean expression returns true more than what you desire.

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 ExtremePowers · Mar 18, 2015 at 06:39 PM 0
Share

The code without the "yield return null" takes 16 seconds to complete, is there any way to make it quicker?

EDIT: Sorry, forgot the Biome code, which might be what is causing this:

         public Biome GetPoint(int x, int y) {
             Vector2 nearest = GetNearestPoint(x,y);
             int Bne = 0;
             
             for (int i = 0; i < points.Count; i++) {
                 if (points[i] == nearest) {
                     Bne = i;
                 }
             }
 
             return LB[Bne].biome;
         }
 
         private Vector2 GetNearestPoint(int x, int y) {
             Vector2 point = new Vector2 (x, y);
             Vector2 nearest = new Vector2 (0,0);
 
             for (int i = 0; i < points.Count; i++) {
                 if (Vector2.Distance (points[i],point) < Vector2.Distance(nearest,point)) {
                     nearest = points[i];
                 }
             }
             return nearest;
         }

I think it is the GetNearestPoint which is causing the problems, since it is basically runing 100 times every point. Which is 104857600 times over the entire scene. I got this fixed by caching the biome points so it would just be one command ins$$anonymous$$d of a 100.

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

21 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

Related Questions

Update terrain alphamap at runtime makes my game crawling 3 Answers

Terrain Textures (via Splat) 0 Answers

Why scene containing terrain is running slow in android device...? 1 Answer

Why does my program run very slow on MAC OS when built from version 5.3.4up when it runs normal speed like it should when built using 5.2.4f1? 1 Answer

How to get rid of terrain splatmap texture seams? 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