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
0
Question by CaptainChristian · Jun 06, 2013 at 11:44 AM · runtimeterraindataduplicatedetails

Cannot change detaillayer after cloning terraindata

Hi,

I am stuck with the following problem: I cannot change the detaillayer of my terrain after cloning it. I've tested a lot of different approaches, but grass or any other detail I add on runtime does not show up, even though I don't get any error messages. Here is what I do:

Duplicate the terrain data on runtime:

 Terrain terrain = terrainTemp.GetComponent<Terrain>();
 TerrainData newTerrainData = (TerrainData) Object.Instantiate(terrain.terrainData);
 terrain.terrainData = newTerrainData;
 TerrainCollider tc = terrain.gameObject.GetComponent<TerrainCollider>();
 tc.terrainData = newTerrainData;

Add prototypes:

 for (int i = 1; i < max; i++) {
     DetailPrototype detailProto = new DetailPrototype ();
     detailProto.prototype = (GameObject)Resources.Load (path + i);
     detailProto.dryColor = Color.yellow;
     detailProto.healthyColor = Color.green;
     detailProto.maxHeight = 2f;
     detailProto.maxWidth = 2f;
     detailProto.minHeight = 1f;
     detailProto.minWidth = 1f;
     detailProto.noiseSpread = 0.1f;
     detailProto.usePrototypeMesh = true;
     detailProto.renderMode = DetailRenderMode.Grass;    
     detailPrototypes.Add (detailProto);
 }

Add prototypes to terraindata:

 terrain.terrainData.detailPrototypes = detailPrototypes.ToArray ();
 terrain.terrainData.RefreshPrototypes ();

And for testing purposes add some grass to every spot in the terrain:

 int[,] newMap = new int[DETAIL_RESOLUTION, DETAIL_RESOLUTION];
                 
 for (int i = 0; i <DETAIL_RESOLUTION; i++) {
     for (int j = 0; j <DETAIL_RESOLUTION; j++) {
         newMap [i, j] = 1;
     }
 }
 terrain.terrainData.SetDetailLayer (0, 0, 0, newMap); 


I also add trees on runtime via code, but for some reason adding trees work without fail. Do you know why the grass is not painted?

Thank your for your help.

Comment
Add comment · Show 3
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 God-at-play · Oct 23, 2013 at 05:07 AM 0
Share

I'm getting the same problem. You ever figure out what's going on? Is this a bug? If so, did you ever figure out how to prevent your detail layer changes from persisting?

avatar image CaptainChristian · Oct 23, 2013 at 08:40 AM 0
Share

Afaik it was just another conversion issue. The code to change the detail of layer 0 at one location looks like this:

 TerrainData terrainDataTemp = terrainTemp.terrainData;
             Vector3 terrainPosTemp = terrainTemp.transform.position;
             
             int detailWidth = terrainDataTemp.detailWidth;
             int detailHeight = terrainDataTemp.detailHeight;
             
             float mapX = ((selectedLocation.x - terrainPosTemp.x) / terrainDataTemp.size.x) * detailWidth;
             float mapZ = ((selectedLocation.z - terrainPosTemp.z) / terrainDataTemp.size.z) * detailHeight;
 
 if (terrainDataTemp.detailPrototypes.Length > 0)
             {
                 int[,] detailLayer = terrainDataTemp.GetDetailLayer((int)mapX, (int)mapZ, detailWidth, detailHeight, 0); 
             
                 detailLayer[0, 0] = 8;
                 
                 terrainDataTemp.SetDetailLayer((int)mapX,(int)mapZ, 0, detailLayer);
             }


Note: detailLayer[0,0] = 8 changes the density to 8. I haven't tested other values, since there are more pressing issues for me to take care of.

Give it a try and report back if it solved it for you.

Edit: Duplicating terraindata works on my end like this:

TerrainData newTerrainData = (TerrainData)Object.Instantiate (terrain.terrainData);

 #if UNITY_EDITOR

                     AssetDatabase.CreateAsset (newTerrainData, "Assets/Resources/_Scenes/$$anonymous$$issions/" + terrain.name + ".asset");
                     AssetDatabase.Refresh();
                 #else
 
                 #endif
 terrain.terrainData = newTerrainData;
                 TerrainCollider tc = terrain.gameObject.GetComponent<TerrainCollider> ();
                 tc.terrainData = newTerrainData;
avatar image God-at-play · Oct 23, 2013 at 07:41 PM 0
Share

Yeah, I only need to change the density between two grass types, so I'm just saving the detail layer at start and re-applying at the end. I dup'ed my terrain as well as a backup. Nice solution for the editor there as well.

0 Replies

· Add your reply
  • Sort: 

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making Duplicate Terrain Unique? 1 Answer

Paint Terrain Texture From Vector3 List 0 Answers

Duplicated objects go crazy at runtime 0 Answers

How to export heightmap of a terrain during runtime. 0 Answers

How can I generate terrain trees at runtime? 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