Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. public void Generate() {
  2.         // Setup noise for mountainous terrain
  3.         RidgedMultifractal mountainTerrain = new RidgedMultifractal ();
  4.         mountainTerrain.Frequency = 0.5;
  5.  
  6.         // Setup noise for rolling hills
  7.         Billow baseFlatTerrain = new Billow();
  8.         baseFlatTerrain.Frequency = 1f;
  9.  
  10.         // Create a scale bias to adjust the scaling between the two
  11.         ScaleBias flatTerrain = new ScaleBias(0.1, 0, baseFlatTerrain);
  12.  
  13.         // Setup a terrain type using perlin noise for smooth blending
  14.         Perlin terrainType = new Perlin();
  15.         terrainType.Frequency = 0.1f;
  16.         terrainType.Persistence = 0.1;
  17.  
  18.         // Merge the modules
  19.         Select finalTerrain = new Select (flatTerrain, mountainTerrain, terrainType);
  20.         finalTerrain.SetBounds (0.0f, 1000.0f);
  21.         finalTerrain.FallOff = 1f;
  22.  
  23.         // Setup noise for tree distribution
  24.         Perlin treeMap = new Perlin ();
  25.         treeMap.Frequency = 0.5f;
  26.         treeMap.Persistence = 0.5f;
  27.  
  28.         // Setup noise for rock and debris distribution
  29.         Perlin rockMap = new Perlin ();
  30.         rockMap.Frequency = 0.1f;
  31.         rockMap.Persistence = 0.1f;
  32.  
  33.         // Initialize the distribution map to handle NPC pathing
  34.         distributionMap = new Perlin ();
  35.         distributionMap.Frequency = 0.01f;
  36.         distributionMap.Persistence = 0.1f;
  37.  
  38.         // Generate terrain chunks
  39.         for(int x = 0; x < chunkCount; x++) {
  40.             for(int y = 0; y < chunkCount; y++) {
  41.                 // Create a heightmap based on the final terrain
  42.                 Noise2D heightMap = new Noise2D( resolution + 1, resolution + 1, finalTerrain );
  43.  
  44.                 heightMap.GeneratePlanar(
  45.                     y * 1,
  46.                     y * 1 + 1,
  47.                     x * 1,
  48.                     x * 1 + 1
  49.                 );
  50.  
  51.                 // Get the normalized heights
  52.                 float[,] heights = heightMap.GetNormalizedData( true, 0, 0 );
  53.  
  54.                 // Create an empty game object to use as a terrain chunk
  55.                 GameObject t = new GameObject ();
  56.  
  57.                 // Set the game object to the appropriate position
  58.                 t.transform.position = new Vector3 (x * resolution, 0f, y * resolution);
  59.  
  60.                 // Add a terrain component to the object
  61.                 Terrain terrainComponent = t.AddComponent<Terrain> ();
  62.  
  63.                 // Add a terrain collider component to the object
  64.                 TerrainCollider terrainCollider = t.AddComponent<TerrainCollider> ();
  65.  
  66.                 // Create new terrain data
  67.                 TerrainData terrainData = new TerrainData();
  68.  
  69.                 // set the terrain data properties
  70.                 Vector3 size = terrainData.size;
  71.  
  72.                 size.y = 512;
  73.                 terrainData.size = size;
  74.                 terrainData.heightmapResolution = resolution;
  75.                 terrainData.SetHeights( 0, 0, heights );
  76.  
  77.                 SplatPrototype[] tex = new SplatPrototype [TerrainTextures.Length];
  78.  
  79.                 float[,,] alpha = new float[resolution, resolution, tex.Length];
  80.  
  81.                 for (int i = 0; i < TerrainTextures.Length; i++) {
  82.                     tex [i] = new SplatPrototype ();
  83.                     tex [i].texture = TerrainTextures [i];    //Sets the texture
  84.                     tex [i].tileOffset = new Vector2(0, 0);
  85.                     tex [i].tileSize = new Vector2 (1, 1);    //Sets the size of the texture
  86.                     tex [i].texture.Apply(true);
  87.  
  88.                     // Pretty sure i'm doing this wrong
  89.                     for(int xx = 0; xx < resolution; xx++) {
  90.                         for(int yy = 0; yy < resolution; yy++) {
  91.                             alpha [xx, yy, i] = heights [Mathf.FloorToInt(xx), Mathf.FloorToInt(yy)];
  92.                         }
  93.                     }
  94.                 }
  95.                    
  96.                 terrainData.SetAlphamaps (0, 0, alpha);
  97.                 // Finally assign the new splatmap to the terrainData:
  98.                 terrainData.splatPrototypes = tex;
  99.  
  100.                 // Apply the terrain data to the terrain component
  101.                 terrainComponent.terrainData = terrainData;
  102.  
  103.  
  104.                 // Apply the terrain data to the terrain collider component
  105.                 terrainCollider.terrainData = terrainData;;
  106.             }
  107.         }
  108.  
  109.         completedTerrain = finalTerrain;
  110.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement