- Home /
How to change texture on parts of terrain
We are developing a game in which we have a terrain. And in that terrain we have natural borders like a small creek and a road. Essentially dividing the map into various areas/parts. On these areas we have characters. Small areas may only have one character, larger areas have 2 or 3 characters. Now we are searching for the best way to be able to select one of the ground areas and change the texture of that area. In the end, the player can have a terrain with different types of ground, depending on what they want.
Because of the ease of editing a terrain in Unity itself we would love to use that tool. But we can only see how this can be done by using an external model with separate meshes for the different ground parts. But this makes editing the terrain a lot less flexible.
Any thoughts on what would be the best way to tackle this?
Answer by save · Jun 10, 2011 at 11:25 AM
Vertex paint is coming to Unity in a near future. http://lesterbanks.com/2011/03/vertex-color-painting-directly-in-unity-3d/
That looks very promising. I couldn't make up from the post if this means it will be accessible from code. And if you can detect (through a vertex ID perhaps) what the user clicked. In my game I need an easy way to detect if you have clicked sand, grass, rock, mud etc.
The idea is that you can click a muddy area, and then have the option to upgrade it to grass for example.
Sounds like a cool feature in your game. I'm not sure either if it's realtime "reversible", that would be neat. You could check out this question to see if the answer fit your needs: http://answers.unity3d.com/questions/124670/need-a-clever-idea-for-deter$$anonymous$$ing-collision-surfa.html
If not then perhaps you could use decals or a projector camera + trigger areas.
Thanks for the link. They discuss some of they ideas I also have in $$anonymous$$d. But it seems there is no quick way to achieve it. Currently I am thinking about cutting up the terrain mesh in several parts. And simply tagging the separate meshes. Detecting the tag of the mesh underneath the cursor and changing the material on it accordingly.
But the idea of using a projector camera might also work. Will certainly check that out.
Answer by pietothemax · Jun 10, 2011 at 07:48 AM
I am looking for help on the same thing are you wanting to change the texture live in game?
Yes. And I have looked into using decals of some sort. But that is not accurate enough. It would be fantastic if there is some way to vertex paint the terrain with different colors/ID's so that, when clicked, you can make out on what part of the terrain was clicked. And that you can set the texture for it. And that the texture would nicely fade at edges so it mixes with the surrounding texture.
Answer by Owen-Reynolds · Jun 10, 2011 at 04:19 PM
This is how to lookup a terrain texture. Most of the work is converting the world position to the indexes in the terrain texture map. To set instead, build TerrCntrl[1,1,4]
, set TerrCntrl[0,0,texNum]
and use SetAlphaMaps, in TerrainData, in the Unity manual:
// public so we can see them in the Inspector and double-check:
public Vector3 TS; // terrain grid-space size
public Vector2 AS; // control texture size
TS = Terrain.activeTerrain.terrainData.size;
AS.x = Terrain.activeTerrain.terrainData.alphamapWidth;
AS.y = Terrain.activeTerrain.terrainData.alphamapHeight;
// World position of what I'm looking up is puffPos:
// get (AX, AY) indexes into terrain array:
int AX = (int)((puffPos.x/TS.x)*AS.x+0.5f);
int AY = (int)((puffPos.z/TS.z)*AS.y+0.5f);
// Get the (1 by 1 by #terrainTextures) array of texture weights at our position:
float[,,] TerrCntrl = Terrain.activeTerrain.terrainData.GetAlphamaps(AX, AY,1 ,1);
// NOTE: say there are 5 terrain textures. the last slot of TC goes from 0-4 and
// TC[0,0, 0-4] add to 1
// In my case, I wanted the 5th terrain texture:
float c1=TerrCntrl[0,0,4];
if(c1>0.5f) // standing on grass