Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by finnjwohner · Nov 15, 2020 at 02:13 AM · shadersverticesnormalsmeshesprocedural-terrain

Change vertex colours based on steepness

I'm messing around with generated terrain using some triangles and Perlin Noise, and I was wondering how I would change the colour of the vertices based on the steepness.

‎

My best guess would be to get the angle between the triangle and Vector3.up and then just evaluate that on a gradient for every triangle in the terrain, but I've been having a hard time figuring out how I would actually do that. ‎
‎

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

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

Answer by TreyH · Nov 15, 2020 at 02:38 AM

With a triangle, you have 3 points by default -- conveniently enough for a plane, which has a normal vector:

 float CalculateSteepnessAngle(Vector3 a, Vector3 b, Vector3 c)
 {
     // Get 2 vectors from these 3 points, it doesn't matter 
     // which ones we use as long as they're from the same point
     var ab = b - a;
     var ac = c - a;
 
     // The cross of those 2 vectors will be the planar normal
     var n = Vector3.Cross(ab, ac);
 
     // The angular difference between this normal and the
     // world's up vector can be used for a steepness value
     return Vector3.Angle(n, Vector3.up);
 }


So with that, you should be able to determine "steepness" for a given triangle on your terrain.

Comment
Add comment · Show 2 · 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 finnjwohner · Nov 15, 2020 at 02:43 AM 1
Share

Fantastic, thank you!

avatar image TreyH finnjwohner · Nov 15, 2020 at 11:27 AM 0
Share

Np! I though about it a little more and the decision to cross either AB x AC or AC x AB will change the planar normal direction, giving 2 different values depending on how the triangle ends up composed.

You might need to modify it with something like

 var n1 = Vector3.Cross(ab, ac);
 var n2 = Vector3.Cross(ac, ab);
 
 var angle1 = Vector3.Angle(n1, Vector3.up);
 var angle2 = Vector3.Angle(n2, Vector3.up);
 
 return $$anonymous$$athf.$$anonymous$$in(angle1, angle2);


to account for that, as the smaller angle will probably be what you want.

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

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

Related Questions

Is vertices.normals face- or vertex-normals? 1 Answer

How do I manually calculate Normals for my Meshes? 4 Answers

Change Camera Clipping/Culling Mode 1 Answer

Unity is welding vertices when I enter Play mode 0 Answers

Why is my mesh peeling off when I try to deform it during runtime? 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