Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
16
Question by Ates Akaydin · Mar 10, 2010 at 08:19 AM · shadertextureterrainheightmap

How to automatically apply different textures on terrain based on height?

Hello,

I would like to render my terrain only with respect to the height values obtained from the height map via HSV Shading. Basically extreme heights will be rendered with brown color, medium heights with green and heights close to sea level will be rendered with blue. Inbetween values will be interpolated. My terrain is too big so I cannot simply paint it with textures(it will take weeks)

Is there a way I can achieve this in unity? I suppose there is no way we can specify shading for the built-in terrain object but rather we should be changing the built-in shader(possibly overwriting it) itself which affects terrain rendering...

Can anyone please explain how to do it in detail?

Best,

Ates Akaydin

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

5 Replies

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

Answer by duck · Mar 10, 2010 at 10:39 AM

It sounds like what you really want to do is assign your splatmaps procedurally via scripting, which is entirely possible, although undocumented (and therefore not officially supported by Unity).

You'd need to assign one splatmap texture for each colour you require, then you need to create a c# script which does the procedural splatmapping.

First, make sure you have a reference to the terrainData object that your terrain is using. Eg:

TerrainData terrainData = Terrain.activeTerrain.terrainData;

The splatmap data is stored internally as a 3d array of floats, so to declare a new empty array ready for your custom splatmap data, do this:

float[, ,] splatmapData = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, terrainData.alphamapLayers];

The three dimensions represent X, Y, and Texture Index (of the textures assigned for terrain painting). X and Y are normalised terrain coordinates, going from 0-1. The value in the 3rd dimension is a "weight" indicating how much of that particular texture is blended in at that particular location on the splat map. The sum of the weights for every texture for any single X,Y value should add up to 1.

You would then want to fill that array with values based on the height reading at each point.

For example:

for (int y = 0; y < terrainData.alphamapHeight; y++) { for (int x = 0; x < terrainData.alphamapWidth; x++) {

     // read the height at this location
     float height = terrainData.GetHeight(x,y);

     // determine the mix of textures 1, 2 &amp; 3 to use 
     // (using a vector3, since it can be lerped &amp; normalized)

     Vector3 splat = new Vector3(0,1,0);
     if (height &gt; 0.5) {
         splat = Vector3.Lerp(splat, new Vector3(0,0,1), (height-0.5f)*2 );
     } else {
         splat = Vector3.Lerp(splat, new Vector3(1,0,0), height*2 );
     }

     // now assign the values to the correct location in the array
     splat.Normalize();
     splatmapData[x, y, 0] = splat.x;
     splatmapData[x, y, 1] = splat.y;
     splatmapData[x, y, 2] = splat.z;
 }

}

// and finally assign the new splatmap to the terrainData: terrainData.SetAlphamaps(0, 0, splatmapData);

(The splat map is referred to as the 'alphamap' in unity. The terms are interchangable)

The code above makes the blend between the 3 textures over the entire possible height of the terrain. You'd need to change the code if you want the blend to occur over a different range within your height values.

Important Note:

Some of these functions are undocumented. Any future updates of the unity engine might change or remove these functions from the API. This means your project may not work in future versions of the Unity editor, and webplayer builds may not work with future versions of the plugin.

Comment
Add comment · Show 7 · 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 Lipis · Mar 10, 2010 at 12:09 PM 0
Share

That was really cool!

avatar image taoa · Feb 21, 2011 at 09:34 AM 1
Share

Vote for this! Accept this! $$anonymous$$ake proper use of Unity Answers! Aaaaah!

avatar image Flafla2 · Jun 04, 2011 at 04:13 PM 0
Share

This doesn't work for me... It just throws a blotched mess of paint, that has nothing to do with height. Any insight?

avatar image Precursor · Jun 17, 2011 at 07:57 PM 1
Share

For me, the splat map was mirrored. Just change "terrainData.GetHeight(x,y)" to "terrainData.GetHeight(y,x)" and its all good

avatar image tanoshimi · Nov 14, 2013 at 09:53 PM 1
Share

3½ years on, and this still appears as the top hit (and, in fact, one of the only hits) when searching for any information concerning procedural terrain mapping. $$anonymous$$udos, Duck ;)

@Dunlain, @Flafla2, and any others who might still be looking to do this (as I was recently) might want to read the blog post I just wrote on the topic which includes a few corrections and enhancements to Duck's script: http://alastaira.wordpress.com/2013/11/14/procedural-terrain-splatmapping/

Show more comments
avatar image
4

Answer by Kos-Dvornik · Nov 16, 2013 at 12:24 PM

You can use it tutorial http://kostiantyn-dvornik.blogspot.com/2013/11/awesome-unity-texturing-terrain-tutorial.html

Comment
Add comment · 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
1

Answer by Len Grant · Oct 04, 2010 at 12:45 AM

There's an extension in the unity resources called Terrain Toolkit that will do exactly what you're talking about. It worked in Unity 2.x; I haven't yet tried it in Unity 3 yet.

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 Meceka · May 07, 2015 at 04:32 PM 0
Share

I can confirm that Terrain Toolkit still works for Unity 5.3

avatar image
0

Answer by killerwol · Jul 26, 2014 at 02:30 AM

Or you can check out my asset on here that paints texture on terrain base off height https://www.assetstore.unity3d.com/en/#!/content/19749

Comment
Add comment · 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
0

Answer by wahn-studios · Feb 21, 2016 at 10:03 AM

You can use this asset to so link text works with vertex colors and textures

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Unity Terrain Texture Resolutions 1 Answer

Writing fragment shader data to RenderTexture 0 Answers

Cannot paint Texture onto Terrain using Custom Material 2018.4 1 Answer

Spherical Terrain or screenshot to texture 0 Answers

Script vs Shader for animated texture 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges