Search Unity

Seamless tiled infinite terrain: simple script for easy fix of seams and edit

Discussion in 'Scripting' started by Fraconte, Aug 2, 2014.

  1. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Create a c# script named "SwapTerrain.cs" and paste the following code:

    SwapTerrain.cs

    Code (CSharp):
    1. // This script make your terrain good for use with "infiniteTerrain.cs".
    2. // Add this to the Terrain and click the "Swap" button in the Inspector to swap
    3. // the borders of the heightmap so you can edit and smooth as needed.
    4. // Hit "Swap" again and you can test it.
    5. // To only make the infinite terrain seamless, just swap it twice.
    6.  
    7. using UnityEngine;
    8. using System.Collections;
    9.  
    10. [ExecuteInEditMode]
    11. public class SwapTerrain : MonoBehaviour
    12. {
    13.     public bool swap = false;
    14.  
    15.     void Update ()
    16.     {
    17.         if(swap)
    18.         {
    19.             Swap (gameObject.GetComponent<Terrain> ());
    20.             swap = false;
    21.         }
    22.     }
    23.  
    24.     void Swap (Terrain terrain)
    25.     {
    26.         TerrainData data = terrain.terrainData;
    27.  
    28.         // Get the left and right half of the heightmap...
    29.         float[,] h1 = data.GetHeights (0, 0, data.heightmapWidth / 2+1, data.heightmapHeight);
    30.         float[,] h2 = data.GetHeights (data.heightmapWidth / 2, 0, data.heightmapWidth / 2+1, data.heightmapHeight);
    31.  
    32.         // ... and swap them
    33.         data.SetHeights (0, 0, h2);
    34.         data.SetHeights (data.heightmapWidth / 2, 0, h1);
    35.  
    36.         // Get the upper and lower half of the heightmap...
    37.         float[,] v1 = data.GetHeights (0, 0, data.heightmapWidth, data.heightmapHeight / 2+1);
    38.         float[,] v2 = data.GetHeights (0, data.heightmapHeight / 2, data.heightmapWidth, data.heightmapHeight / 2+1);
    39.  
    40.         // ... and swap them
    41.         data.SetHeights (0, 0, v2);
    42.         data.SetHeights (0, data.heightmapWidth / 2, v1);
    43.     }
    44. }
     
  2. Hard target

    Hard target

    Joined:
    Oct 31, 2013
    Posts:
    132
    Is the infinite terrain cs. script from youtube.Because that is the one I use.I only wish it had a way to randomly generate postions for houses, trees,hills,rocks and spawn points for enemies.But it would need to space them.
     
  3. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Yes. It's the first entry when you google "infinite terrain".
    This is the link to the script: http://answers.unity3d.com/storage/attachments/1978-infiniteterrain.zip

    When you use it with non flat terrain it leave holes between edges. Usually you can fix it by exporting, editing the raw heightmap with Photoshop and reimporting it. With my script you simply edit the terrain in the Unity editor.