- Home /
How to change textures on terrain?
Hi. I'm quite new in Unity3D. In my game an user can have his own city . At some moment in the game all the sand roads in the game for example should be replaced by stone roads. Or at a certain moment texture x should be overwritten with texture y at some places on my terrain. So not everywhere.
What is the best way to achieve that? Like a toggle function within specific coordinates or...
Thank you very much!
Answer by Ambrose998800 · Sep 09, 2015 at 11:13 PM
You could use a material change script.
private var MatStone: Material;
private var MatConcrete: Material;
function Start(){
MatStone= Resources.Load("Material/Stone", Material);
MatConcrete= Resources.Load("Material/Concrete", Material);
}
function "changeMat"(){
if (RoadObject.GetComponent("RoadScript").MatName == "Stone"){
RoadObject.GetComponent(Renderer).material = MatStone;
}
if (RoadObject.GetComponent("RoadScript").MatName == "Concrete"){
RoadObject.GetComponent(Renderer).material = MatConcrete;
}
}
Thank you! But how to do it for specific places on the terrain, and not for the complete terrain?
With this apporach you cannot, it's always gonna change the complete texture. Every texture added to the terrain's texture list is basically applied to the whole terrain. The only control of local appearence is an alpha value. You cannot partially replace a terrain texture in Unity unless you create your own terrain system. Have a look at my answer, this way it's possible to "replace" a texture while keeping it at some places, but you won't actually replace it, you'll just manipulate the alpha value.
In case of terrains - and if you want to use the material change; i would build them (for example) with Blender, that supports Unity material specification within. There are smooth blendover in materials on meshes too by brushing.
Unfortunately, Unity has just a crappy terrain tool. You can't even build caves. With Blender you can. If you want use the grass paint tool from Unity on an extern terrain too, just make a underlayed Unity-terrain to your imported one.
Answer by Suddoha · Sep 10, 2015 at 12:46 AM
That's actually not too difficult. The only thing that you really have to find out by yourself is which algorithm you want to use to determine which areas should be changed.
As for the change of texture, you're probably looking for TerrainData.GetAlphamaps. Basically, the terrain is organized in different layers for the textures. Every texture that you add to the terrain's texture list can be thought of as "applied to the complete terrain". When "drawing" a texture on the terrain, the engine manipulates values at those positions for the specific texture which represent an alpha value.
Example: Texture 1: Sand, Texture 2: Stone, Texture 3 Gras.
If you want to add some gras (without opacity, i.e. you only see the gras texture, no blend effect), the respective alpha values at the position will be set to 0 for the sand and stone, 1 one for the gras. Note, that the values should be clamped to a maximum of 1 in total, higher values will result in annoying (often bright) effects.
*EDIT
Important sidenote: Any changes done by scripts during playmode will not be reverted. You have to make your own copy, either via script or manually, before you start your experiments.
That would be the best way. Here is a tutorialwith some sample code that I found some time ago. As said, the algorithm how to deter$$anonymous$$e what the values are for a position is something that you have to figure out for your setup.
Your answer
Follow this Question
Related Questions
Terrain look 1 Answer
Can't find the paint / raise / lower terrain setting in unity 2018.3 1 Answer
TriPlanar Textures Not Working Properly 1 Answer
My terrain textures are still horrible!! 3 Answers