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 · Oct 28, 2014 at 08:02 PM · optimizationnoiseperlinterrain genchunk

Optimize perlin code

My question is if you have any way to make this delay less than 0.1 or make it un-noticable? So for example simplify some of the code to make it run a little faster :) Is there for example a way to multithread it and do the math on another thread then just apply it on the main thread? I think that the SetHeights and SetDetailLayer is taking alot of time to perform, but that is just a theory.

EDIT: That wasn't it, it was the loops that took 0.07 each, which gives that small delay. My problem is I can't make the two loops into one, because I can't get steepness if the terrain height hasn't been set.

I have this code which moves 3 tiles in 0.4 seconds, which gives this small lag everytime I walk onto a new tile:

     var Chunk : Terrain;
     var tmp = GameObject.Find("Chunk "+fromnx+","+fromnz);
     var ocupied = GameObject.Find("Chunk "+nx+","+nz);
     if (ocupied != null) {
         Destroy(tmp);
         return;
     }
     if (tmp == null) {
         var td = new TerrainData();
         td.heightmapResolution = 256;
         td.alphamapResolution = td.heightmapResolution;
         td.SetDetailResolution(td.heightmapResolution, 16);
         
         td.size = new Vector3(terrainSize, terrainHeight, terrainSize);
         
         td.splatPrototypes = splatPrototypes;
         td.treePrototypes = treeProtoTypes;
         td.detailPrototypes = detailProtoTypes;
         tmp = Terrain.CreateTerrainGameObject(td);
     }
     Chunk = tmp.GetComponent(Terrain);
     if (PlayerPrefs.HasKey("GrassDistance")) {
         Chunk.detailObjectDistance = PlayerPrefs.GetInt("GrassDistance");
     }
     var hw : float = Chunk.terrainData.heightmapWidth;
     var ChunkPosX : float = terrainSize * nx;
     var ChunkPosZ : float = terrainSize * nz;
     
     Chunk.transform.position = Vector3(ChunkPosX, 0, ChunkPosZ);
     Chunk.name = "Chunk "+nx+","+nz;
     Chunk.tag = "Chunk";
     
     var startPosX = (hw-1)*(nx-1);
     var startPosZ = (hw-1)*(nz-1);
     
     if (Chunk.transform.Find("Water") == null) {
         var Water = GameObject.Instantiate(WaterPrefab, Vector3(0, WaterLevel, 0), Quaternion.identity);
         Water.name = "Water";
         Water.transform.parent = Chunk.transform;
     }
     
     var a = Chunk.terrainData.alphamapWidth;
     var heightmap = new float[hw,hw];
     var ratio = terrainSize/hw;
     
     for(var x = 0; x < hw; x++) {
         for(var z = 0; z < hw; z++) {
             var worldPosX : float = (x+startPosX)*ratio;
             var worldPosZ : float = (z+startPosZ)*ratio;
             
             var mountains : float = Mathf.Max(0.0f, perlin.FractalNoise2D(worldPosX+250, worldPosZ+250, 5, Frq, 0.8f));
             var plain : float = perlin.FractalNoise2D(worldPosX-250, worldPosZ-250, 1, Frq, 0.5f) + 0.175f;
             
             heightmap[z,x] = plain+mountains;
         }
     }
     
     Chunk.terrainData.SetHeights(0,0,heightmap);
     var map = new float[a, a, Chunk.terrainData.alphamapLayers];
     var detailMap = new int[a,a];
     var aratio = (terrainSize/a);
     var aunit = 1.0f/(a-1);
     for (var ax = 0; ax < a; ax++) {
         for (var az = 0; az < a; az++) {
             var worldPosX1 : float = (ax+startPosX)*aratio;
             var worldPosZ1 : float = (az+startPosZ)*aratio;
             
             var Biome = GetBiome(worldPosX1,worldPosZ1);
             var normX : float = ax*aunit;
             var normZ : float = az*aunit;
                 
             // Get the steepness value at the normalized coordinate.
             var angle = Chunk.terrainData.GetSteepness(normX,normZ);
             var height = Chunk.terrainData.GetInterpolatedHeight(normX,normZ);
             
             if (height < WaterLevel + 2 || Biome == "Desert") {
                 map[az,ax,2] = 1;
             } else if (height > 120 || Biome == "Snow") {
                 map[az,ax,3] = 1;
             } else if (angle > 25) {
                 map[az,ax,1] = 1;
             } else if (Biome == "Plain") {
                 detailMap[az,ax] = 2;
                 map[az,ax,0] = 1;
             } else if (Biome == "Forest") {
                 detailMap[az,ax] = 1;
                 map[az,ax,0] = 1;
             }
         }
     }
     Chunk.terrainData.SetDetailLayer(0,0,0,detailMap);
     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 lamecheesykiwi · Oct 28, 2014 at 10:14 PM

Have you tried .05?

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 · Oct 29, 2014 at 06:23 AM 0
Share

@lamecheesykiwi As the amplitude in the fractal noise?

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

Low performance on multiple runtime generated sprites 1 Answer

Basics of Perlin Noise? 1 Answer

Perlin Noise Plane Manipulation 1 Answer

Using noise as texture 2 Answers

easy c# Simplex Noise script code 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