Can you change terrain material?

Hi hi,

is it possible to change the material of a terrain, to.. say for example an emissive shader?

I suspect it's hard due to the terrain shader being specialized for the splatmap stuff, correct?

Would it be possible to write a terrain shader that, instead of blending textures based on the splatmap, blended materials based on the splatmap?

Hello guys.

First of all, for @Magnus Wolffelt 's questions:

Yes it is possible to write your own shaders for Terrain. It is a bit complicated but not too much. Basically you should base it on an existing Terrain shader.

There is an article here which is very helpfull:
Custom Unity Terrain Material Shaders.

Also check on the wiki, there is an example tweaked shader that allows you to paint transparent textures on your terrain to make portions…well transparent.
http://wiki.unity3d.com/index.php/TerrainTransparency

Would it be possible to write a terrain shader that, instead of blending textures based on the splatmap, blended materials based on the splatmap?

Well this one I don’t know, @n8 must be correct that it is not possible but it would be interesting if it was. If anyone knows better it would be great to post an answer here :slight_smile:

As for what @Dagan asked:

I was Looking for the answer to this myself and couldn’t find much in my searches.
After some testing and trial and error (lots of errors) I finally found something that actually works.

There is a property on Terrain called materialTemplate. It is actually missing form the Reference Documentation on the site, don’t know why.
So if you have a material called “myMaterial” that uses your own shader you can do something like this:

public Material referencedMaterial;
public Terrain referencedTerrain;

and assign “myMaterial” to “Referenced Material” slot of your script (in the inspector).
Then later in your code when you want to change your terrain’s material
just do:

referencedTerrain.materialTemplate = referencedMaterial;

Hope this helps you, it worked for me :slight_smile:

NOTE: This works for Unity 4 Pro/Free. Haven’t tried in older versions.

I am no expert by any means but i believe you are trying to use shaders in the wrong way. Shaders blend textures not materials (see here).

if you really wanted to change the material of the terrain you could prob export the material as an .obj import it into an external 3d editor and then tweak it there and reimport as a mesh. from there you can assign any material/shader/texture combo that you want.

Hello,

I’ve have a shader that i modified to work with the terrain. Splats etc…
I can assign it quite easily to the terrain base material slot.
Does anyone know if i can do this with c#.

Many Thanks

Yes you can, while terrain is selected go to inspector and add texture. Then go to terrain settings and there is material selection.

there is guide Custom Unity Terrain Material / Shaders | Alastair Aitchison

hope this help. :slight_smile: