• 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
1
Question by AlwaysSunny · Jun 05, 2013 at 03:27 AM · terrainmesh

Reproducing Terrain.SampleHeight() for custom mesh terrains

Hi folks! I'm trying to write a method which mimics GetHeights() or SampleHeight(), but which works on my own generated mesh terrains. They're functionally similar, just subdivided planes. Each vertex's y position is set by a float[,] heightmap, just like built-in terrains.

I'm finding that similar issues are a common rite-of-passage thing, but I get the feeling I'm missing some simple algorithm that would be lightning fast given that this is a very specific case. It'd be so easy to sample these points using raycasting, but I'm hoping for a less expensive solution based on some mesh-level math wizardry. Am I wrong in assuming this is wise?

For bonus points, could my solution to this also provide a foundation for reproducing GetSteepness() or GetInterpolatedNormal()? Will post again if I have any success on my own. Thanks for any ideas / insight!

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
1
Best Answer

Answer by AlwaysSunny · Jun 05, 2013 at 04:59 AM

I can easily find and reference the nearest vertex because the data structure I use allows this to be really cheap. Since the necessary projections are all XZ parallel, based on which direction I rounded to arrive at the nearest vertex, I can discover the quad the target position is in pretty darn quick and cheap.

Doing some "fancy collegiate arithmetic" picks the correct three points describing the tri and does a weighted interpolation for a height value.

This approach applies to a float[,] heightmap data set, but would also be useful - if slightly more expensive - when working with mesh.vertices in similar cases.

Comment
Add comment · Show 3 · 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 MattMcg.GameDev · Jan 28, 2015 at 10:39 AM 0
Share

Hey, I know you posted this a while ago but I was wondering if you still had the code for this? I would really appreciate the help figuring this out for my custom terrain. I do not want your code directly, just some help.

I know how to find the exact 4 height points at a given location. I then struggle to work out which tri I need, i.e. the "fancy collegiate arithmetic". Even if I had the tri, I also don't know what to do with the 3 points I end up with, i.e. how to interpolate it. $$anonymous$$ath is not my strong point.

avatar image AlwaysSunny · Jan 28, 2015 at 11:24 AM 1
Share

I gave a good try hunting this old script but my archives are a nightmare.

The code wasn't complex IIRC. If you know your mesh and the winding order for each quad's tris, finding which tri is your test candidate is as easy as comparing which tri's "90-degree" vertex is closer to the position argument.

e.g. which is closer, the vert at quad[0] (use tri 0,1,3) or the vert at quad[2] (use tri 2,3,1)

From there I'm fuzzy on what I did to sample the height, but it wasn't hard to find the formula once I chose good search terms. Gotta leave that part to you. ;)

$$anonymous$$eep in $$anonymous$$d it's not necessarily prudent to do this. Raycasting may be a superior choice, it all depends on your situation.

Best of luck,

avatar image MattMcg.GameDev · Jan 28, 2015 at 11:29 AM 0
Share

Oh wow. That is such a simple way to figure out with which tri it lies on... I was imagining being far more difficult!

I have tried with Raycasts and it seems a little too expensive for me, due to the amount of objects and terrain sectors. I'm even contemplating using a collider-less terrain as I don't actually need realistic physics. I might just make a very simple custom physics that uses this height sampler to detect when it's on the ground.

Thank you for your reply, you've given me enough direction to solve this I think.

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

15 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

Related Questions

Disable Culling for Terrain 0 Answers

Terrain heightmap isn't smooth 0 Answers

Terrain trees aren't solid,Terrain Trees 0 Answers

Placed Trees in Terrain are white planes? 1 Answer

Rivers on procedurally terrain 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