• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kiriri · Oct 26, 2013 at 10:55 AM · terrainprocedural-generationterrain gensplatmaps

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 ]

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smidlingPupin · Jan 27 at 02:05 PM 0
Share

Dont forget to rewrite other splatPrototypes as well (besides newProto[0]).

BTW, thanks so much for this answer! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Instantiated Objects not Following Perlin Noise 0 Answers

how do you use the terrain tools? 1 Answer

How can I get two different terrain instances to use different splat maps? 2 Answers

Procedural terrain generation - What exactly does Terrain.Flush() do? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges