Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Generating in Chunks

Discussion in 'Scripting' started by afakih629, Jan 23, 2015.

Thread Status:
Not open for further replies.
  1. afakih629

    afakih629

    Joined:
    Jun 20, 2014
    Posts:
    4
    I currently have this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Terrain : MonoBehaviour {
    5.     private Vector3 pos;
    6.     public GameObject terraincube;
    7.     public int x_len;
    8.     public int height_factor;
    9.     public int y_len;
    10.     public float min_range;
    11.     public int perlin;
    12.     public float max_range;
    13.  
    14.     void Start () {
    15.         float rand = Random.Range(min_range, max_range);
    16.         for (float px = 0; px < x_len; px ++) {
    17.            
    18.      
    19.             for (float py = 0; py< y_len; py ++) {
    20.        
    21.                 float Perlin1 = Mathf.PerlinNoise(px/perlin, rand);
    22.                 float Perlin2 = Mathf.PerlinNoise(py/perlin, rand);
    23.                 pos = new Vector3(py, Perlin1*height_factor*Perlin2, px);
    24.                 GameObject Block = (GameObject)Instantiate(terraincube, pos, Quaternion.identity);
    25.                 Block.transform.parent = transform;
    26.                 Block.name = "Block";
    27.                
    28.             }
    29.         }
    30.     }
    31.    
    32.    
    33.     void Update () {
    34.    
    35.     }
    36. }
    37.  
    How can I generate one chunk at a time and/or generate this terrain so the game doesn't freeze for the first 20 seconds?

    Second, how can I make this procedural and infinite?


     
    Last edited: Jan 23, 2015
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I wrote a chunk tracker class, for exactly this purpose:

    Code (CSharp):
    1. using System.Diagnostics;
    2.  
    3. namespace UnityEngine.Extensions {
    4.  
    5.     public static class ChunkTracker {
    6.  
    7.         public const int MinimumFramesPerSecond = 35;
    8.  
    9.         //If we want at least 35 frames per second, we'll allow a frame calculation to take 28 milliseconds.
    10.         //Longer than that, and we advocate a chunk.
    11.         const float allowableMillisecondsPerFrame = (1f / (float) MinimumFramesPerSecond) * 1000f;
    12.  
    13.         static Stopwatch intraFrameStopWatch;
    14.  
    15.         static float lastConsideredTime = 0f;
    16.  
    17.         static public bool AdvocateChunk(){
    18.  
    19.             //if computational demand is low, Time.time will be higher than lastConsideredTime, because
    20.             //the frame will have been updated without having to advocate a chunk.
    21.             if (lastConsideredTime < Time.time) {
    22.                 lastConsideredTime = Time.time;
    23.                 return false;
    24.             }
    25.  
    26.             //If we're doing something computationally intense, and it's taken too long, we'll reset
    27.             //the tracker and suggest a chunk. It's imporant to note that the first time per frame
    28.             //we get here, we'll always suggest a chunk, because the stopwatch will have been running
    29.             //since last reset. Which is fine, because we'll only get here if an entire frame has elapsed,
    30.             //so a chunk will be necessary anyway.
    31.             if (intraFrameStopWatch.ElapsedMilliseconds >= allowableMillisecondsPerFrame) {
    32.                 intraFrameStopWatch.Reset();
    33.                 intraFrameStopWatch.Start();
    34.                 lastConsideredTime = Time.time;
    35.  
    36.                 return true;
    37.             }
    38.  
    39.             //If we've gotten here, it means we're doing something computationally intense and we've already
    40.             //advocated at least one chunk, but we haven't blown our frame calculation budget for the next one.
    41.             return false;
    42.         }
    43.  
    44.         static ChunkTracker() {
    45.             intraFrameStopWatch = new Stopwatch();
    46.             intraFrameStopWatch.Start();
    47.         }
    48.     }
    49. }
    50.  
    Usage, in your case, would look like this:
    (I didn't test, obviously. Let me know)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Terrain : MonoBehaviour {
    4.     private Vector3 pos;
    5.     public GameObject terraincube;
    6.     public int x_len;
    7.     public int height_factor;
    8.     public int y_len;
    9.     public float min_range;
    10.     public int perlin;
    11.     public float max_range;
    12.  
    13.     IEnumerator Start () {
    14.         float rand = Random.Range(min_range, max_range);
    15.         for (float px = 0; px < x_len; px ++) {
    16.          
    17.    
    18.             for (float py = 0; py< y_len; py ++) {
    19.      
    20.                 float Perlin1 = Mathf.PerlinNoise(px/perlin, rand);
    21.                 float Perlin2 = Mathf.PerlinNoise(py/perlin, rand);
    22.                 pos = new Vector3(py, Perlin1*height_factor*Perlin2, px);
    23.                 GameObject Block = (GameObject)Instantiate(terraincube, pos, Quaternion.identity);
    24.                 Block.transform.parent = transform;
    25.                 Block.name = "Block";
    26.              
    27.                 if (ChunkTracker.AdvocateChunk())
    28.                      yield return new WaitForEndOfFrame();
    29.             }
    30.         }
    31.     }
    32.  
    33. }
    The answer to how to make it procedural and "infinite" depends entirely on your use case, and would be a lot more involved of an answer.
     
  3. nitosiak

    nitosiak

    Joined:
    Jun 2, 2022
    Posts:
    1
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    You just necroed a 7 year old thread to reference a line of code.

    Please create your own threads rather than necroing ones like this.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.