Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Sep 12, 2016 at 05:48 PM by FelixWun for the following reason:

The problem was caused because I had Automatic Baking of Lightmaps turned off. Works fine with it turned on.

avatar image
0
Question by FelixWun · Sep 11, 2016 at 05:19 PM · crashingterraindataterraintexture

SetAlphamaps causing editor to crash V5.4.0f3

EDIT: It seems to work fine if I turn on Automatic Baking of Lightmaps in the lighting window. So problem solved :-)

Hey guys, I'm trying to add some procedural texture painting to the terrain in my project, and as a start I'm trying to get the example given in the Unity Documentation to work.

Here's my C# code:

 void Start()
     {
         // Get the attached terrain component
         Terrain terrain = GetComponent<Terrain>();
 
         // Get a reference to the terrain data
         TerrainData terrainData = terrain.terrainData;
 
         float[,,] map = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, 5];
 
         // For each point on the alphamap...
         for (int y = 0; y < terrainData.alphamapHeight; y++)
         {
             for (int x = 0; x < terrainData.alphamapWidth; x++)
             {
                 // Get the normalized terrain coordinate that
                 // corresponds to the the point.
                 float normX = (x * 1 / (terrainData.alphamapWidth - 1));
                 float normY = (y * 1 / (terrainData.alphamapHeight - 1));
 
                 // Get the steepness value at the normalized coordinate.
                 float angle = terrainData.GetSteepness(normX, normY);
 
                 // Steepness is given as an angle, 0..90 degrees. Divide
                 // by 90 to get an alpha blending value in the range 0..1.
                 float frac = angle / 90;
 
                 map[x, y, 0] = frac;
                 map[x, y, 1] = 0;
                 map[x, y, 2] = 1-frac;
                 map[x, y, 3] = 0;
                 map[x, y, 4] = 0;
             }
         }
 
         terrainData.SetAlphamaps(0, 0, map);
     }

However this causes the editor to crash when I run the code. The only way I can get it to work is if I set the strength of the first texture to 1, like so:

 map[x, y, 0] = 1f;

Then it works but obviously doesn't result in the desired effect. Is there something really obvious I'm missing? I've spent hours googling for an answer but haven't found any solutions.

Thanks!

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

  • Sort: 
avatar image
0

Answer by FelixWun · Sep 12, 2016 at 01:13 PM

Just in case anyone else runs into this problem, here's a workaround I came up with. I've set the first texture to a black jpeg and just leave it's value as 1f in the code. This gives you a base of 0 to add your other colours on top of. So in the editor, make sure the first texture in your list is the black texture, then set the others as desired.

Here's my code:

 void Start()
     {
         // Get the attached terrain component
         Terrain terrain = GetComponent<Terrain>();
 
         // Get a reference to the terrain data
         TerrainData terrainData = terrain.terrainData;
 
         float[,,] map = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, 6];
 
         // For each point on the alphamap...
         for (int x = 0; x < terrainData.alphamapHeight; x++)
         {
             for (int y = 0; y < terrainData.alphamapWidth; y++)
             {
                 // Get the normalized terrain coordinate that
                 // corresponds to the the point.
                 float normX = ((float)x * 1 / (terrainData.alphamapWidth - 1));
                 float normY = ((float)y * 1 / (terrainData.alphamapHeight - 1));
 
                 // Get the steepness value at the normalized coordinate.
                 float angle = terrainData.GetSteepness(normY, normX);
 
                 // Steepness is given as an angle, 0..90 degrees. Divide
                 // by 90 to get an alpha blending value in the range 0..1.
                 float frac = angle / 90;
 
                 map[x, y, 0] = 1f;
                 map[x, y, 1] = 1-frac;
                 map[x, y, 2] = frac;
                 map[x, y, 3] = 0;
                 map[x, y, 4] = 0;
                 map[x, y, 5] = 0;
             }
         }
 
         terrainData.SetAlphamaps(0, 0, map);
     }
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

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Getting the texture of a certain point on terrain 1 Answer

Edit terrain by script?? 0 Answers

Terrain Texture (applied programatically) Disappears When Entering Play Mode 0 Answers

SetAlphaMaps Opacity Gradient 0 Answers

TerrainData is missing splat texture 3. 1 Answer


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