- Home /
Paint texture on terrain with scripting
Hey Guys,
I have finally accomplished it that i can paint textures on certain places of my terrain at runtime. The Problem is that on the places that i don't want to paint a new texture, must be painted again with the old texture. To make it clearer here is my code:
public class TerrainPaint : MonoBehaviour {
public void Paint () {
TerrainData td = Terrain.activeTerrain.terrainData;
float[,,] splatmapData = new float[td.alphamapWidth, td.alphamapHeight, td.alphamapLayers];
for (int y = 0; y < td.alphamapHeight; y++)
{
for (int x = 0; x < td.alphamapWidth; x++)
{
float[] tex = getmethatcrapmap(x, y);
Vector4 splat = new Vector4(tex[0], tex[1], tex[2], tex[3]);
if(y % 20 == 0)
{
splat = new Vector4(1, 0, 0, 0);
}
// now assign the values to the correct location in the array
splat.Normalize();
splatmapData[x, y, 0] = splat.x;
splatmapData[x, y, 1] = splat.y;
splatmapData[x, y, 2] = splat.z;
splatmapData[x, y, 3] = splat.w;
}
}
// and finally assign the new splatmap to the terrainData:
td.SetAlphamaps(0, 0, splatmapData);
}
public float[] getmethatcrapmap(int x, int y) {
TerrainData td = Terrain.activeTerrain.terrainData;
float[,,] splatmapData = td.GetAlphamaps(y, x, 1, 1);
float[] cellMix = new float[splatmapData.GetUpperBound(2)+1];
for(int n=0; n < cellMix.Length; ++n)
{
cellMix[n] = splatmapData[0,0,n];
}
return cellMix;
}
}
This only places a new texture every 20th x position. Now if i want to paint a texture at ,for example, 50 coordinates, it's I guess a lot of work, if the whole terrain is painted again.
So my question is, is there a way to paint a texture on a certain position without drawing the whole terrain again?
Answer by Vidar · Feb 16, 2011 at 04:16 PM
ok i've made it^^
is this only happening to me or someone else that sometime after you decide to ask something you come to a solution yourself^^
can u show me how u did it because im tryng to do the same thing and in serching i find ur post so can u plz tell ne how u did it it wud b very helpfull
I am also trying to do the same thing can I see the solution code please?
Just going to bump this. If you have a solution can you please share with everyone? Would be a lot of help as I know a lot of people would be interested in seeing how you managed to do it.
Thanks in advance
Your answer
Follow this Question
Related Questions
How to automatically apply different textures on terrain based on height? 5 Answers
How can I automatically place grass and other details on my terrain to correspond with the splatmap? 5 Answers
Positioning textures on terrain 0 Answers
Can i change the Texture of terrain 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers