- Home /
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!
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.)
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!
And of course, the textures that have other than 0 should sum 1. 1 being the 100% alpha.
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);
The weird symbols are not $$anonymous$$e, some problem this this text editor
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.)
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?
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).
btw my splats are 2048 and the terrain size is 8192. I guess it shouldn't be any problem
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
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 ++.
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