Runtime terrain painting with SetAlphamaps = lag
Hi!
According to profiler, one call of TerrainData.SetAlphamaps takes 40ms to finish, when setting array of size: 20,20,3. No matter how big is updated fragment, it stutters my project. Alphamap resolution is 2048, but it lags also when it's 512.
I used single updates with small 1x1 fragments (4ms!), I used coroutines, read all post about SetAlphamaps function, but there was no working solution for me.
Function written by me draws road texture on terrain in runtime, from point to point. I think it is the simpliest way to build road in runtime. Is there any way to speed it up? Maybe better way to draw a simple road?
The most important questions are:
How the same function "SetAlphamaps" works smoothly in editor mode???
Why it's lagging in runtime?
If it can be done in editor, why not in game?
My code:
function BuildRoad(p1 : Vector3, p2 : Vector3){
var tData=Terrain.activeTerrain.terrainData;
var bSize=20;
var brush = new float[bSize,bSize,3];
for(var x=0; x<bSize; x++){
for(var z=0; z<bSize; z++){
brush[x,z,0]=0.0;
brush[x,z,1]=0.0;
brush[x,z,2]=1.0;
}
}
var xx : int;
var zz : int;
var dir : Vector3 = p2-p1;
var length=dir.magnitude;
dir.Normalize();
for(var i=0; i<length; i++){
xx = ((p1.x+dir.x*i)/tData.size.x)*(tData.alphamapWidth-1);
zz = ((p1.z+dir.z*i)/tData.size.z)*(tData.alphamapHeight-1);
tData.SetAlphamaps(xx,zz,brush);
}
Terrain.activeTerrain.terrainData=tData;
}
I have same issue. if you have solved it kindly share the solution here, it will be great of all of us thanks
Unfortunately no. Regardless of all optimizations, realtime terrain painting was unplayable. Believe me, it was a long journey through the internet. Ended up with generating procedural mesh for paths and roads :/
You can search for some 3rd party terrain systems..
Yeah i can understand been there for at least a week. But thanks for replying anyways, do you have any terrain system in ur $$anonymous$$d??