- Home /
Update terrain alphamap at runtime makes my game crawling
Basically what i want to do is to paint roads on terrain as my NPCs are moving. I manage to get this done, but when using
terrain.terrainData.SetAlphamaps(0, 0, splatmapData);
in conjunction with FixedUpdate or Update, the frame rate is going down to a crawling slide-show. I've tried to move it to a coroutine but it doesnt make any difference, i also tried to edit only one tile on the alphamap but with no success. You can see the code here http://forum.unity3d.com/threads/108885-Modify-terrain-texture-at-runtime
P.S. I did an extensively search on this subject but couldn't find anything related to my problem.
I'm having the same problem, but only with Unity 5! In unity 4, I could change alphamaps (SetAlphamap) in small patches over several different terrain 'chunks' with little slowdown, but now in Unity 5, if I change two 4x4 terrain alphamaps per frame, then game drops from 60fps to 30!!! Is there by chance a different way to change terrain that I'm missing, or is Unity 'baking' the terrain with each change or something? BTW, my terrain has 8 textures and I'm using the 'cutout' mode in my shader so my 1st terrain can have 'holes', if any of that makes any difference.
Any help would be very much appreciated. I've tried updating to the latest version of Unity (today), and searched forums for at least 4-5 hours so not sure what to do next. Thanks again and have a great day!
Answer by aldonaletto · Oct 21, 2011 at 11:44 AM
The terrain docs are too vague - no examples, no detailed explanations - but what I understood from them is that the dimensions of the array passed to SetAlphamaps(x, y, arrayOfFloats) define the size of the region to paint. If your array is too large, it will take a big time to update the terrain. Try the following: create a single element array, copy the new data to it (update your splatmapData array also, if you want) and pass the single terrain element to SetAlphamaps - something like this:
// I'm a JS guy, thus the code below may have horrible C# errors float[,,] element = new float[1,1,2]; // create a temp 1x1x2 array splatmapData[y, x, 0] = element[0, 0, 0] = 0; // set the element and splatmapData[y, x, 1] = element[0, 0, 1] = 1; // update splatmapData terrain.terrainData.SetAlphamaps(y, x, element); // update only the selected terrain element ...
I still drop frames, and I don't understand why, can you please help me where am I wrong? (Please ignore all dirty code, is just for test...). This script is attached on gameobject that walk on myTerrain.
float[,,] element;
int mapX, mapY;
TerrainData terrainData;
Vector3 terrainPosition;
public Terrain myTerrain;
float[,,] map;
private Vector3 lastPos;
void Awake()
{
map = new float[myTerrain.terrainData.alphamapWidth, myTerrain.terrainData.alphamapHeight, myTerrain.terrainData.alphamapLayers];
element = new float[1, 1, myTerrain.terrainData.alphamapLayers];
terrainData = myTerrain.terrainData;
terrainPosition = myTerrain.transform.position;
lastPos = transform.position;
}
void Update()
{
Update$$anonymous$$apOnTheTarget();
}
void Update$$anonymous$$apOnTheTarget()
{
//just update if you move
if(Vector3.Distance(transform.position, lastPos) > 1)
{
print("paint");
//convert world coords to terrain coords
mapX = (int)(((transform.position.x - terrainPosition.x) / terrainData.size.x) * terrainData.alphamapWidth);
mapY = (int)(((transform.position.z - terrainPosition.z) / terrainData.size.z) * terrainData.alphamapHeight);
map[mapY, mapX, 0] = element[0, 0, 0] = 0;
map[mapY, mapX, 1] = element[0, 0, 1] = 1;
myTerrain.terrainData.SetAlphamaps(mapX, mapY, element);
lastPos = transform.position;
}
}
I wasn't changing alpha maps. However I was playing with heightmap and that was similarly causing frame drops. In that case frame drops were cause by the fact that unity see$$anonymous$$gly needs to recompute some terrain collider data.
In case of alpha maps I'd guess there is some similar postprocessing involved. For instance I know there's the question of recomputing LODs. That operation i think is quite costly.
In general it seems terrain component in Unity hasn't really been designed for runtime modification. For that I'm planning to look into some third-party assets in the asset store.
Answer by scarpelius · Oct 21, 2011 at 12:04 PM
You my friend are a genius :D I've tried something similar last night, but it seems i was too tired to notice that i created a
float[,,] element = new float[1,1,2];
with too many elements, I used float[,,] element = new float[x,y,2]; which in my case x and y was around 300 so there was no difference :)
Cheers mate.
@scarpelius, please click the "check" button below the voting thumbs in my answer to mark it as accepted. It renders no karma to me (unfortunately), but may help some desperate programmers with crawling terrain scripts to find the solution. PS: please, use the $$anonymous$$uscule add new comment to reply or comment; the your answer box must be used only to answer the question (answers are counted in Unity Answers).
Yeah, I agree - it could be improved a lot! The title Your answer could be changed to Use this box only to answer the question, add new comment should be larger, but above all: this moderation thing should be replaced by Captcha or other anti-spam measure!
hey guys!
is there any way I can see the whole script so I can get a better understanding of what is going on?
I am trying to find a tutorial on this but with no luck. cheers
Answer by farooq03245149691 · Feb 16, 2017 at 01:37 PM
@penna91 Im using this code in a project im developing. But I get frame drops if i want to paint a larger area. I do it by duplicating the objects to which this script is attached to.This results in a huge drop in frames. From 40Fps to 6Fps. Can you tell me how to increase the area that im trying to paint?? So that this script is attached to a single object.
Your answer
Follow this Question
Related Questions
how does a splatmap alpha maps work (terrains) etc... 1 Answer
Splat Map with transition texture? 0 Answers
Generated terrain heightmap is flipped from the alphamap. 1 Answer
Can you change terrain material? 4 Answers
Splatmap dry/healthy 1 Answer