Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SuIXo3 · Dec 07, 2012 at 01:41 PM · terrainalphapaintmaps

How to use get/setAlphamaps?

Would someone that know it explain to me how to use these functions? I've red about it and seen some questions here where they recomend to use it. But I can't finish to understand. I can make the code work, I know how to write it, but I have no idea wht the numbers in the float array mean. I mean the float[,,]. It is supposed to store the data of the splat maps, but how? where is the color, how do you change it?

The objective is to paint the terrain on start, so basically it's a terrain painter tool.

Please if someone has anything say it, this is key to the developement of my game. Thanks!

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

4 Replies

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

Answer by Owen-Reynolds · Dec 07, 2012 at 03:32 PM

Say you're painting with 6 textures, and you happen to know a certain spot on your map has alphaMap coords (50,52) (which is a huge pain to compute -- they aren't the same as height map numbers.)

Then `aMap[50,52,P]` has P go from 0 to 5 (6 total values) and each float is the % weight you've painted with. For example, if that spot is 80% grass (1st texture) and 20% rock (last texture) then aMap[50,52,0]=0.8, aMap[50,52,5]=0.2 and the rest are 0.

I dug this up from: http://answers.unity3d.com/questions/128274/how-to-change-texture-on-parts-of-terrain.html, which has the math and code in it (and has been tested.)

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 SuIXo3 · Dec 07, 2012 at 03:57 PM 0
Share

Thanks this helped A LOT. I use at the moment like 10 textures, and in the future there will be more, 1 more for each season at least. It's a strategy game with a big unique world, so I use a lot of textures.

So I guess I would have to put in each "if" deter$$anonymous$$ing heights and all that, all the a$$anonymous$$ap[x, y, 0], a$$anonymous$$ap[x, y, 1] and then equal them to the value I want for each texture on that height and slope.

$$anonymous$$hmmhmh, I think I get it. Will come back if need more help.

Thanks!

avatar image SuIXo3 · Dec 07, 2012 at 03:58 PM 0
Share

And of course, the textures that have other than 0 should sum 1. 1 being the 100% alpha.

avatar image
0

Answer by SuIXo3 · Dec 07, 2012 at 07:08 PM

Here is what I got but it doesn't work. It should change the texture of the terrain to the second texture right?. What is missing?

 public Terrain terrainToPaint;
 
 // Use this for initialization
 void Start () {
 
 
 float[, ,] splatMapData = new float[terrainToPaint.terrainData.alphamapWidth, terrainToPaint.terrainData.alphamapHeight, terrainToPaint.terrainData.alphamapLayers];
 
 for(int y=0;y<=terrainToPaint.terrainData.alphamapHeight;y++){
  for(int x=0;x<=terrainToPaint.terrainData.alphamapWidth;x++){
 
   splatMapData[x, y, 0] = 0;
   splatMapData[x, y, 1] = 1;
   splatMapData[x, y, 2] = 0;
   splatMapData[x, y, 3] = 0;
   splatMapData[x, y, 4] = 0;
   splatMapData[x, y, 5] = 0;
   splatMapData[x, y, 6] = 0;
   splatMapData[x, y, 7] = 0;
   splatMapData[x, y, 8] = 0;
   splatMapData[x, y, 9] = 0;
   splatMapData[x, y, 10] = 0;
   splatMapData[x, y, 11] = 0;
   splatMapData[x, y, 12] = 0;
   terrainToPaint.terrainData.SetAlphamaps(x,y,splatMapData);
   
Comment
Add comment · Show 5 · 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 SuIXo3 · Dec 07, 2012 at 07:11 PM 0
Share

The weird symbols are not $$anonymous$$e, some problem this this text editor

avatar image Owen-Reynolds · Dec 08, 2012 at 03:44 AM 0
Share

Your splat$$anonymous$$apData is the entire thing, so you only need to set it once, after making it in those loops. The docs aren't explicit, but if you look at GetAlpha$$anonymous$$aps, x and y are the corner of where you want changed. So use `setAlpha$$anonymous$$aps[0,0,splat$$anonymous$$apData` (tested.)

I'd guess you aren't seeing anything b/c terrainToPaint isn't visible. Try it on `Terrain.activeTerrain` and you'll see it working (beware -- it will permanently change it, bar maybe quit and restart.)

avatar image SuIXo3 · Dec 08, 2012 at 11:08 AM 0
Share

Ok I tried with activeterrain and [0,0,splatmapdata] but still nothing. The terrain is visible, I have it there, all painted with the first texture. What do you mean with set it up just once (splatmapdata). And how would put 0,0 would really work, if it is a certain pixel that I want to change.

And by the way, the SetAlphamaps goes into or out of the for loop?

avatar image SuIXo3 · Dec 08, 2012 at 11:17 AM 0
Share

on the splatmapdata [x, y, 12] it gives me an error, but still runs the game. Error is "Array index is out of range". And if I delete any of these splatmapdata unity crashes when i start the game(freezes and i have to close it myself).

avatar image SuIXo3 · Dec 08, 2012 at 11:20 AM 0
Share

btw my splats are 2048 and the terrain size is 8192. I guess it shouldn't be any problem

avatar image
0

Answer by SuIXo3 · Dec 08, 2012 at 12:54 PM

OOOOOO my god it works, i've been jumping around the house half an hour. I see what the mistake was. I had "<=" instead of just "<" in the pixels loop. That is why it said index out of range. And I also put the "setalphamaps[0,0,splatmapdata]" out of the loop. So now I tried and it paints the hole terrain with the texture where i put the 1. This is so awesome. I can finally continue.

Thanks for your help! I will name a monument after you in the game haha

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 Owen-Reynolds · Dec 08, 2012 at 03:48 PM 1
Share

I am so, so sorry about missing the `<=`. I saw `y & lt ; & #61` in your posted loop code, and I know darn well that 61 is ACSII for equals. Who can forget that rhyme about it from 3rd grade?

I think I was busy checking that you had a pair of pound-fortry-threes at the end, for the ++.

avatar image
0

Answer by xuhengjin · Dec 15, 2021 at 11:13 AM

     private void Start()
     {
         var terrainData = terrain0.terrainData;
         Debug.LogFormat("alphaMapCount: {0}",terrainData.alphamapTextureCount);

         var w = terrainData.alphamapResolution;
         var data = terrainData.GetAlphamaps(0, 0, w, w);
         
         ShowArrayInfo(data);
         void ShowArrayInfo(Array arr)
         {
             Debug.LogFormat("Length of Array:      {0,3}", arr.Length);
             Debug.LogFormat("Number of Dimensions: {0,3}", arr.Rank);
             // For multidimensional arrays, show number of elements in each dimension.
             if (arr.Rank > 1)
             {
                 for (int dimension = 1; dimension <= arr.Rank; dimension++)
                     Debug.LogFormat("   Dimension {0}: {1,3}", dimension,
                         arr.GetUpperBound(dimension - 1) + 1);
             }
         }

     }



if terrain have five layer texture,below my test: alphaMapCount: 2 Length of Array: 5242880 Number of Dimensions: 3 Dimension 1: 1024 Dimension 2: 1024 Dimension 3: 5

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

11 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

Related Questions

Blend texture seam between 2 terrains tiles 1 Answer

Terrain Painting Script 0 Answers

How to "paint" the terrain in a script? 1 Answer

Shader with "alpha" parameter no longer writes to Z-Buffer 2 Answers

Can I set a non-power-of-2 sized heightmap to 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