- Home /
Get Terrain material / Set splat maps
Hi, I need to procedurally generate my terrains. In my case, depending on where the user clicks on a world map, different splat maps should be applied to the local terrain. How does one do this ?
This code :
TerrainData t = gameObject.GetComponent<Terrain>().terrainData;
t.splatPrototypes[0].texture = exampleTex;
t.RefreshPrototypes();
..does exactly nothing.
So after some time to think, I got the idea to just change the Terrain material instead. However, since TerrainRenderer is an internal-sealed non Monobehaviour(eg it does not inherit from MeshRenderer and therefore cannot be gotten via .renderer) I can't see a way to get its' Material. Terrain.materialTemplate returns null, so that's not it either.
Any help would be appreciated on this. I'd go great lengths to finally be able to set my splatmaps via code.
[I also asked this question in the forums, but it got lost : http://forum.unity3d.com/threads/206902-change-terrain-splatmap-alphamap ]
Answer by dbc_orp_kids · Jun 13, 2016 at 12:46 PM
You can create a totally new SplatPrototype
array and then set that in terrain data. For me this works.
TerrainData t = gameObject.GetComponent<Terrain>().terrainData;
SplatPrototype oldProto = t.splatPrototypes[0];
SplatPrototype newProto = new SplatPrototype[1];
newProto[0] = new SplatPrototype();
// Copy parameters
newProto[0].metallic = oldProto.metallic;
newProto[0].normalMap = oldProto.normalMap;
newProto[0].smoothness = oldProto.smoothness;
newProto[0].specular = oldProto.specular;
newProto[0].tileOffset = oldProto.tileOffset;
newProto[0].tileSize = oldProto.tileSize;
// Only the albedo texture is different:
newProto[0].texture = exampleTex;
// Set prototype array
t.splatPrototypes = newProto;
t.RefreshPrototypes();
Of course, if you have multiple splat prototypes in your terrain, you should duplicate them and put them into the new array in the same order as before.
Dont forget to rewrite other splatPrototypes as well (besides newProto[0]).
BTW, thanks so much for this answer! :)
Your answer
Follow this Question
Related Questions
Make part of terrain transparent. 1 Answer
Overly bright splat map replacement 2 Answers
How do I apply a splatmap to my terrain? 1 Answer
Unity Terrain GameObject versus Custom Terrain 1 Answer
A good terrain engine for Unity ? 1 Answer