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 benk0913 · Jul 31, 2013 at 04:49 PM · terraingenerationheightmapalphamap

Generated terrain heightmap is flipped from the alphamap.

Ive made a terrain generator which generates hills in random positions, after the hills are being generated and the heightmap is set the textures from the alphamap are setting themselfs by the height of each point on the terrain, different heights with different textures.

But, for some reason the textures are being set correctly but flipped from the height map, in wrong places around the terrain but in the same structure given by the height map. Please help me, how do I set the alpha map correctly and not flipped from the height map...

This is the hill generating part

 public void GenerateTerrain()
     {
         
         
         for(int h=0;h<hillCount;h++)
         {
             CreateHill(Random.Range(0,tData.heightmapWidth),Random.Range(0,tData.heightmapHeight),Random.Range(0.005f,0.1f),Random.Range(0.5f,5f));
         }
         tData.SetHeights(0,0,terrainGRID);    
         Smooth();
         
         
     }
     
     public void CreateHill(int x, int y, float height,float pointyness)
     {
         float point = 0;
         float distanceFromTop;
         
         terrainGRID[x,y]=height;
 
         for(int a=0;a<tData.heightmapWidth;a++)
         {    
             for(int b=0;b<tData.heightmapHeight;b++)
             {
                 distanceFromTop=Mathf.Sqrt(   Mathf.Pow((y-b),2)  +  Mathf.Pow((x-a),2)   );
                  point=((height-terrainGRID[a,b])*1000 - distanceFromTop*pointyness)/1000 ;
                 
                 if(point<0)
                 point=0;
                 
                 terrainGRID[a,b]+=point;
             }
         }
     }


And this is the alpha map setting part which comes after the hills aprt:

     SplatPrototype[] terrainTexture = new SplatPrototype[4]; 
          terrainTexture[0] = new SplatPrototype(); 
          terrainTexture[0].texture = (Texture2D)Resources.Load("terrain_ground2");
         terrainTexture[0].tileOffset = new Vector2(0, 0); 
         terrainTexture[0].tileSize = new Vector2(15, 15);
         
         
         terrainTexture[1] = new SplatPrototype(); 
          terrainTexture[1].texture = (Texture2D)Resources.Load("terrain_ground3");
         terrainTexture[1].tileOffset = new Vector2(0, 0); 
         terrainTexture[1].tileSize = new Vector2(15, 15);
         
         terrainTexture[2] = new SplatPrototype(); 
          terrainTexture[2].texture = (Texture2D)Resources.Load("terrain_ground4");
         terrainTexture[2].tileOffset = new Vector2(0, 0); 
         terrainTexture[2].tileSize = new Vector2(15, 15);
         
         
         terrainTexture[3] = new SplatPrototype(); 
          terrainTexture[3].texture = (Texture2D)Resources.Load("terrain_ground5");
         terrainTexture[3].tileOffset = new Vector2(0, 0); 
         terrainTexture[3].tileSize = new Vector2(15, 15);
         
         
          tData.splatPrototypes = terrainTexture; 
         
         
         float[,,] map = new float[tData.heightmapWidth-1,tData.heightmapHeight-1, 4]; // needs to be 1
         // For each point on the alphamap...
 
         for (int y = 0; y < tData.heightmapWidth-1; y++) 
         {
             for (int x = 0; x < tData.heightmapHeight-1; x++) 
             {
                 if(tData.GetHeight(x,y)<10)
                        map[x, y, 0] = 1.0f;
                 else if(tData.GetHeight(x,y)<20)
                 {
                        map[x, y, 0] = 0.5f;
                      map[x, y, 1] = 0.5f;
                 }
                 else if(tData.GetHeight(x,y)<30)
                      map[x, y, 1] = 1.0f;
                 else if(tData.GetHeight(x,y)<40)
                 {
                        map[x, y, 1] = 0.5f;
                      map[x, y, 2] = 0.5f;
                 }
                 else if(tData.GetHeight(x,y)<50)
                      map[x, y, 2] = 1.0f;
                 else if(tData.GetHeight(x,y)<60)
                 {
                        map[x, y, 2] = 0.5f;
                      map[x, y, 3] = 0.5f;
                     print (tData.GetHeight(x,y));
                 }
                 else if(tData.GetHeight(x,y)<70)
                      map[x, y, 3] = 1.0f;
                 else if(tData.GetHeight(x,y)<80)
                 {
                        map[x, y, 3] = 0.5f;
                      map[x, y, 4] = 0.5f;
                     print (tData.GetHeight(x,y));
                 }
                 else if(tData.GetHeight(x,y)<90)
                      map[x, y, 4] = 1.0f;
             }
         }
          tData.SetAlphamaps(0, 0, map);
             
         



The outcome is :

alt text

untitled.png (394.9 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by benk0913 · Jul 31, 2013 at 05:22 PM

PROBLEM SOLVED, I had to replace the Y and X in the height map "ifs", it now sets the textures correctly.

Comment
Add comment · Show 2 · 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 AR_Rizvi · Sep 02, 2013 at 06:54 AM 0
Share

i am using ur script but its not working it is returning me the height but the splat maps are returning null values i check it but its not working can u plz guide me

avatar image ModLunar · Jul 18, 2017 at 12:51 PM 0
Share

Yeah, I had to do the same thing, but I was wondering because in the docs, they say the alphamaps should be indexed as [x, y, l], where l is the layer of the texture. But then it works when we index it as [y, x, l]?

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

17 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 avatar image avatar image

Related Questions

How to generate a complete terrain via Script 0 Answers

3D Hex Terrain Generation with Height 1 Answer

How large can a terrain be that is generated with a heightmap? 1 Answer

Applying a Large Texture Map to a Terrain 1 Answer

2D Efficient Realtime Terrain Generation 0 Answers


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