• 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 LunaticDancer · Mar 30, 2020 at 02:40 PM · terrainterraindatadetail

Painting foliage through a script - what's wrong here?

I'm making a script that paints the detail layer of the terrain (aka foliage) wit$$anonymous$$n a given polygon projection. The polygon projection part does work, but what's bugged is the filling itself - the foliage appears striped - lines altering between overgrown and bare, like in t$$anonymous$$s image:

Striped grass

Been sniffing around t$$anonymous$$s for hours and can't really find what's wrong. Here's some relevant code:

 private void PaintFoliage(Polygon polygon, int layerIndex, int intensity)
         {
             int[,] splatmapData = activeTerrainData.GetDetailLayer(0, 0, activeTerrainData.detailWidth, activeTerrainData.detailHeight, layerIndex);
 
             
             for (int y = 0; y < activeTerrainData.detailHeight; y++)
             {
                 for (int x = 0; x < activeTerrainData.detailWidth; x++)
                 {
                     Vector3 worldSpacePosition = ConvertTerrainToWorldSpace(new Vector2(x, y),
                         new Vector2(activeTerrainData.detailWidth, activeTerrainData.detailHeight));
                     if (IsPointInPolygon(polygon, worldSpacePosition))
                     {
                         splatmapData[x, y] = intensity;
                     }
                 }
             }
             activeTerrainData.SetDetailLayer(0, 0, layerIndex, splatmapData);
         }
 
 bool IsPointInPolygon(Polygon polygon, Vector3 point) 
         {
             bool result = false;
             var a = polygon.Points[polygon.Points.Length - 1];
             foreach (var b in polygon.Points)
             {
                 if ((b.x == point.x) && (b.z == point.z))
                     return true;
 
                 if ((b.z == a.z) && (point.z == a.z) && (a.x <= point.x) && (point.x <= b.x))
                     return true;
 
                 if ((b.z < point.z) && (a.z >= point.z) || (a.z < point.z) && (b.z >= point.z))
                 {
                     if (b.x + (point.z - b.z) / (a.z - b.z) * (a.x - b.x) <= point.x)
                         result = !result;
                 }
                 a = b;
             }
             return result;
         }
 
 Vector3 ConvertTerrainToWorldSpace(Vector2 coords, Vector2 limits)
         {
             Vector3 answer = new Vector3(
                 Terrain.activeTerrain.transform.position.x + (coords.y / limits.x) * Terrain.activeTerrain.terrainData.size.x, 0,
                 Terrain.activeTerrain.transform.position.z + (coords.x / limits.y) * Terrain.activeTerrain.terrainData.size.z);
             return answer;
         }



Curiously enough, I've made a very similar script dealing with painting terrain and it works like a charm. I'd be very thankful for pointing out what's wrong here.

grass-bug.png (95.9 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

149 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Terrain set detail layer problem. 0 Answers

Why do smaller terrains have higher triangle count? 0 Answers

terrain の detail について (About Terrain Detail),terrain の detail について (About Terrain Detail) 0 Answers

terrainData.heightmapTexture float value range 2 Answers

Convert terrain map coordinates (GetDetailLayer) into world position 0 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