• 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 darkgriffin · Dec 16, 2014 at 03:16 AM · javascriptterrainterraindataheightmaps

How would I flip or mirror Terrain data during runtime?

I am trying to make a simple endless terrain illusion. Terrains don't scale at all with their transforms though, so I can't just use transform.scale(-1,0,0) to pull off making the clones needed for wrapping. So I'm now trying to create the terrain clones during runtime.

But I can't seem to get anyt$$anonymous$$ng returned from terrain.terrainData.GetHeights(0,0,w,h); If I debug.log that info, I just end up getting "System.Single[,]" instead of the array data. According to the docs, from what I understand that should be returning a 2d array of the terrain data, and certainly should not be returning an empty array, in any case.

Here's the full code below. All I get when running t$$anonymous$$s is the debug returning System.Single[,], followed by a IndexOutOfRangeException error. (The indexoutofrange is presumably because I have not gotten the data correctly in the first place) :

 //variables
 var terrainBase : TerrainData; //original terrain data, editor set.
 
 function Start () {
     terrain2A = Terrain.CreateTerrainGameObject(terrainBase);
     //scale 2A along the Z axis
     var t2ATerrain = terrain2A.GetComponent(Terrain) as Terrain;
     var h = t2ATerrain.terrainData.heightmapHeight;
     var w = t2ATerrain.terrainData.heightmapWidth;
     var oTerrain = t2ATerrain.terrainData.GetHeights(0,0,w,h);
     Debug.Log(oTerrain);
     t2ATerrain.terrainData.SetHeights(0,0,FlipTerrainX(oTerrain,w,h));
 }
 
 function FlipTerrainX(originalArray : float[,],heightmapX:float,heightmapY:float) {
     var nArray = new float[heightmapX,heightmapY];
     //Debug.Log(originalArray);
     //Debug.Log(nArray);
     for(var x = 0;x < heightmapX;x++) {
         for(var y = 0;y < heightmapY;y++) {
             //flip the x, but leave y matc$$anonymous$$ng
             nArray[heightmapX - x,y] = originalArray[x,y];
         }
     }
     return nArray;
 }

I'm sure I am just missing somet$$anonymous$$ng simple here.

All I want to do is be able to loop and process the array of heightmaps to mirror the terrain for the other 4 versions needed to loop it forever. T$$anonymous$$s error is a bit over my head, however, and searches are not being very helpful since the information I'm getting is very conflicting on if t$$anonymous$$s should even work.

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 darkgriffin · Dec 17, 2014 at 07:48 PM 0
Share

Does anyone have a reference for how terrain arrays are structured? I really need to be able to manipulate the terrain data to make any headway here.

At this point, I'm considering spending a few weeks just coding my own terrain, but since this is for android and I'm not the best coder I'd much rather use the unity one. I also have a few tools that edit regular Unity terrain, and hoped I could make use of those.

I've tried:

-starting a completely new project -just creating an empty terrain -attaching the above code to an empty game object in the scene

I STILL get nothing but a blank array in return with the GetHeights() function, and the array parser still overloads.

I'd report a bug to Unity, but I don't want to report something if I'm just using it wrong.

All the other functions of the terrain are working like calling a single point's height or setting a single data point during runtime.

If I knew the structure of the terrain data, I could probably just loop the data using the single data point functions. But since the documentation just says "Get an array of heightmap samples.." without describing the structure of the array, I have no idea how to write such code.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hav_ngs_ru · Dec 16, 2014 at 08:02 AM

not sure about what in you terrainBase. Debug.Log w and h in Start function - what they have?

Comment
Add comment · Show 1 · 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
avatar image darkgriffin · Dec 16, 2014 at 07:28 PM 0
Share

h is 513, w is 513.

and this

Debug.Log(t2ATerrain.terrainData.GetHeights(0,0,w,h));

is also returning this

System.Single[,]

instead of data from in the array.

EDIT: The terrain I'm loading is currently just a default Unity terrain I made with the "GameObject - Terrain" menu option. Default settings, and blank except for a quick heightmap I scribbled on it in the editor to test with. So basically, I am failing to get data from the default terrain here.

Out of curiosity, I tried replacing w and h with numbers like 32, 45, and such other manually typed numbers. I still get blank arrays back. I also tried asking for data from 1,1,25,25 and got a blank back. So it appears that the call to GetHeights is always returning a blank for some reason, even though the terrain clearly exists during runtime.

avatar image
0

Answer by MichaelEGA · Aug 20, 2022 at 03:49 AM

Old I know... but t$$anonymous$$s was all that came up when I was searc$$anonymous$$ng for a solution so I thought I'd leave my answer here. Eventually figured it out. See below.

  public static IEnumerator FlipTerrain(Terrain terrain)
     {
         TerrainData terrainData = terrain.terrainData;
 
         int widthOfTerrain = terrainData.heightmapResolution;
 
         float[,] noiseMap = terrainData.GetHeights(0, 0, widthOfTerrain, widthOfTerrain);
         float[,] noiseMapFlipped = new float[widthOfTerrain, widthOfTerrain];
         
         int flipX = 0;
         int flipY = 0;
 
         for (int y = 0; y < widthOfTerrain; y++)
         {
             for (int x = 0; x < widthOfTerrain; x++)
             {
 
                 flipX = widthOfTerrain - 1 - x;
                 flipY = widthOfTerrain - 1 - y;
 
                 noiseMapFlipped[flipX, flipY] = noiseMap[x, y];
 
             }
         }
 
         terrainData.SetHeights(0, 0, noiseMapFlipped);
 
         yield return new WaitForEndOfFrame();
     }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Tracking information across terrain 1 Answer

How do you choose the heights of a terrain using code? 1 Answer

Server-side generated heightmap 0 Answers

Unity imports heightmaps in kind of upside down order. 0 Answers

How can I use feature recognition on 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