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
1
Question by unityrookie14 · Jul 30, 2014 at 05:47 PM · c#instantiateterrainterraindataterraintexture

How to generate multiple Terrain objects with different TerrainData and Splat Texture via C# script?

I'm trying to stream image and elevation data from a remote server for rendering maps in Unity. Since the server is a Tile Map Service (TMS), the images and elevation data are stored in tiles. I created a terrain prefab called mymap_terrain, and instantiate it in my GameController (which is an empty GameObject). Just for a test case, when I hit run, the GameController instantiates two instances of terrain with different textures and elevation data (ideally). Here is my code used for instantiation of terrain objects. The issue is described after:

 function Start () {
     terrain = Instantiate(terrainPrefab, Vector3(0.0, 0.0, 0.0), Quaternion.identity);
     terrain.gameObject.name = "mapL6X124Y16";
     
     yield WaitForSeconds(5.0);
     terrain2 = Instantiate(terrainPrefab, Vector3(15.0, 0.0, 15.0), Quaternion.identity);
     terrain2.gameObject.name = "mapL6X125Y17";
 }
 

Here is my method that is attached to the terrain prefab, that I use to set various parameters of the terrain:

 private IEnumerator terrainDataSet(int layer, int Xtile, int Ytile, float[,] heightData) {
         float tiles = Mathf.Pow (2, 2*layer+1);
         float length = (float)(2048.0/(Mathf.Sqrt(2*tiles)));
             //Get the terrain component of this terrain object
         Terrain terrain = (Terrain)GetComponent("Terrain");
         terrain.terrainData.heightmapResolution = 33;
         terrain.terrainData.SetHeights(0,0,heightData);
         terrain.terrainData.size = new Vector3(length,5,length);
         
         
         WWW www = new WWW(tms_URL + layer + "/" + Xtile + "/" + Ytile + ".jpg");
         while(!www.isDone){
             yield return null;    
         }
         Texture2D myTexture = new Texture2D(256, 256,TextureFormat.RGB24,false);
         www.LoadImageIntoTexture(myTexture);
         //Set Texture for the terrain
         SplatPrototype splats = new SplatPrototype(); 
         splats.tileSize = new Vector2(length, length);
         splats.texture = myTexture;
 
         terrain.GetComponent<Terrain>().terrainData.splatPrototypes = new SplatPrototype[] {splats};
         yield break;
     }



When I load terrain1 with 1.tif elevation data and 1.jpg texture, it's perfect! I wait for 5 seconds and load terrain2 with 2.tif elevation data and 2.jpg texture. When I do this, it automatically changes terrain1 to have the texture and elevation data of terrain2. Also, When I go to either terrain's inspector and change the terrain length/width/etc, it is reflected on both terrains. Any way that I can have two independent terrains with their own set of texture and elevation data? Some images down here to help you understand better:

terrain1 alt text

both terrains loaded. Changes texture and elevation data for terrain 1 alt text

Any help/comment is appreciated :) ... I'm new to Unity (about a month of using the software)

terrain1.jpg (50.0 kB)
terrain2.jpg (83.9 kB)
Comment
Add comment · Show 1
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 tomatosource · Dec 12, 2014 at 03:27 AM 0
Share

The last answer on this thread sorted this issue (for me at least) for anyone still googling this issue- http://answers.unity3d.com/questions/58342/problem-with-terrain-duplicates-simple.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sobakaduka · Mar 28, 2017 at 11:16 AM

I think the problem is that you take the Terrain data from terrain, try to create a new Terrain Data and assign it to terrain.

This how i would generate several terrains

 for (int i = 0; i < numberOfTerrains; i++)
 {
     for (int j = 0; j < numberOfTerrains; j++)
     {
         TerrainData _terrainData = new TerrainData();
         terrain.terrainData.heightmapResolution = 33;
         terrain.terrainData.SetHeights(0,0,heightData);
         terrain.terrainData.size = new Vector3(length,5,length);
         GameObject _terrain = Terrain.CreateTerrainGameObject(_terrainData);
         _terrain.transform.position = new Vector3(_terrain.GetComponent<Terrain>().terrainData.size.x * i, 0, _terrain.GetComponent<Terrain>().terrainData.size.x * j);

     }
 }
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

SetAlphaMaps Opacity Gradient 0 Answers

Distribute terrain in zones 3 Answers

C#: Changing Terrain Textures 0 Answers

terrainData.heightmapTexture float value range 2 Answers

Is there a way to paint tessellation or displacement textures on a terrain? 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