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
1
Question by ExtremePowers · Nov 04, 2014 at 09:11 PM · performancelagnoiseoptimizetimeout

Improve fractal noise function

I am trying to improve this code without having to make the terrain "borring", so I would need some way to optimize this code to make it run faster, because right now it takes 0.9 seconds to generate a heightmap for 3 tiles. Which is way to much when the terrain is infinite and generated on the run. Here is how I run the code:

             if (perlin == null) {
                 perlin = new PerlinNoise(Seed);
             }
             mountains = Mathf.Max(0f, perlin.FractalNoise2D(worldPosX+250, worldPosZ+250, Octaves, mountainFrq, 0.5f) - 0.1f) * 1.5;
             plain = perlin.FractalNoise2D(worldPosX-250, worldPosZ-250, 1, groundFrq, 0.4f) + 0.3f;

Here is the function:

     public float FractalNoise2D(float x, float y, int octNum, float frq, float amp)
     {
         float gain = 1.0f;
         float sum = 0.0f;
         
         for(int i = 0; i < octNum; i++)
         {
             sum += Noise2D(x*gain/frq, y*gain/frq) * amp/gain;
             gain *= 2.0f;
         }
         return sum;
     }

Full code noise code: http://pastebin.com/8VL66dzV

Comment
Add comment · Show 5
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 AlwaysSunny · Nov 04, 2014 at 09:18 PM 0
Share

How many octaves are you using? And how many points are you sampling per terrain? $$anonymous$$y knowledge of writing noise functions is limited at best, but I don't spy any obvious improvements to be made.

avatar image ExtremePowers · Nov 04, 2014 at 09:24 PM 0
Share

I am using 3 Octaves for the mountains, and the terrain is 257*257

avatar image AlwaysSunny · Nov 04, 2014 at 10:11 PM 0
Share

Distribute your calculations over several frames using coroutines, or move the calculations to another thread.

avatar image ExtremePowers · Nov 04, 2014 at 10:50 PM 0
Share

How can I move it to another thread, does unity support multithreading? I have distributed the 3 Chunks moving procces with a second between each of them, but it still seems to lag with a 0.3 second "delay"

avatar image ExtremePowers · Nov 08, 2014 at 02:40 PM 0
Share

@AlwaysSunny, thank you the trick with distributing it over several frames worked :)

1 Reply

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

Answer by AlwaysSunny · Nov 04, 2014 at 11:01 PM

You can create worker threads, yes. The process is similar to using co-routines. Either way, you'll need to insert yields/waits into the nested loop of sampling checks such that only N% of the whole job is done per frame. This means your terrain object will not be "ready" for several frames after requesting it. Your game must accommodate this shortcoming by loading terrains before they'll become visible / necessary.

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

27 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

Related Questions

performance question about particles and instantiating 1 Answer

Unity to android (game very laggy) 1 Answer

Unity performance issues. 0 Answers

How to Reduce the lag? 3 Answers

PlayerController Script Causing Terrible Performance (Profiler says FixedUpdate: LogStringToConsole) 4 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