Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Generate() {
- // Setup noise for mountainous terrain
- RidgedMultifractal mountainTerrain = new RidgedMultifractal ();
- mountainTerrain.Frequency = 0.5;
- // Setup noise for rolling hills
- Billow baseFlatTerrain = new Billow();
- baseFlatTerrain.Frequency = 1f;
- // Create a scale bias to adjust the scaling between the two
- ScaleBias flatTerrain = new ScaleBias(0.1, 0, baseFlatTerrain);
- // Setup a terrain type using perlin noise for smooth blending
- Perlin terrainType = new Perlin();
- terrainType.Frequency = 0.1f;
- terrainType.Persistence = 0.1;
- // Merge the modules
- Select finalTerrain = new Select (flatTerrain, mountainTerrain, terrainType);
- finalTerrain.SetBounds (0.0f, 1000.0f);
- finalTerrain.FallOff = 1f;
- // Setup noise for tree distribution
- Perlin treeMap = new Perlin ();
- treeMap.Frequency = 0.5f;
- treeMap.Persistence = 0.5f;
- // Setup noise for rock and debris distribution
- Perlin rockMap = new Perlin ();
- rockMap.Frequency = 0.1f;
- rockMap.Persistence = 0.1f;
- // Initialize the distribution map to handle NPC pathing
- distributionMap = new Perlin ();
- distributionMap.Frequency = 0.01f;
- distributionMap.Persistence = 0.1f;
- // Generate terrain chunks
- for(int x = 0; x < chunkCount; x++) {
- for(int y = 0; y < chunkCount; y++) {
- // Create a heightmap based on the final terrain
- Noise2D heightMap = new Noise2D( resolution + 1, resolution + 1, finalTerrain );
- heightMap.GeneratePlanar(
- y * 1,
- y * 1 + 1,
- x * 1,
- x * 1 + 1
- );
- // Get the normalized heights
- float[,] heights = heightMap.GetNormalizedData( true, 0, 0 );
- // Create an empty game object to use as a terrain chunk
- GameObject t = new GameObject ();
- // Set the game object to the appropriate position
- t.transform.position = new Vector3 (x * resolution, 0f, y * resolution);
- // Add a terrain component to the object
- Terrain terrainComponent = t.AddComponent<Terrain> ();
- // Add a terrain collider component to the object
- TerrainCollider terrainCollider = t.AddComponent<TerrainCollider> ();
- // Create new terrain data
- TerrainData terrainData = new TerrainData();
- // set the terrain data properties
- Vector3 size = terrainData.size;
- size.y = 512;
- terrainData.size = size;
- terrainData.heightmapResolution = resolution;
- terrainData.SetHeights( 0, 0, heights );
- SplatPrototype[] tex = new SplatPrototype [TerrainTextures.Length];
- float[,,] alpha = new float[resolution, resolution, tex.Length];
- for (int i = 0; i < TerrainTextures.Length; i++) {
- tex [i] = new SplatPrototype ();
- tex [i].texture = TerrainTextures [i]; //Sets the texture
- tex [i].tileOffset = new Vector2(0, 0);
- tex [i].tileSize = new Vector2 (1, 1); //Sets the size of the texture
- tex [i].texture.Apply(true);
- // Pretty sure i'm doing this wrong
- for(int xx = 0; xx < resolution; xx++) {
- for(int yy = 0; yy < resolution; yy++) {
- alpha [xx, yy, i] = heights [Mathf.FloorToInt(xx), Mathf.FloorToInt(yy)];
- }
- }
- }
- terrainData.SetAlphamaps (0, 0, alpha);
- // Finally assign the new splatmap to the terrainData:
- terrainData.splatPrototypes = tex;
- // Apply the terrain data to the terrain component
- terrainComponent.terrainData = terrainData;
- // Apply the terrain data to the terrain collider component
- terrainCollider.terrainData = terrainData;;
- }
- }
- completedTerrain = finalTerrain;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement