• 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
2
Question by Maker16 · Jan 09, 2012 at 12:29 AM · meshfilter

Need help with manipulating meshes

I have a hexmap that consists of individual hexagon objects. The map is overlayed on a terrain. Each hexagon consists of 7 meshes: 6 sides and the central face. I am trying to get the vertices of the hexagons to match up with the height of the terrain to which they correspond. For example, if a vertex of the hexagon is at the world origin, and the terrain at the origin is at a height of 50, I want that vertex to be set to the same height(50).

Below is the code I have written so far. The entire script is used to handle the generation of the hexmap and setting of vertex heights of each hexagon. The snippet below is the portion of the code I was trying to use to set the vertex heights. It doesn't even come close to working. After it executes, all of the hexagons look like lines. Could someone please help me re-write this bit so it works as intended?

             //Match vertices to terrain
             var filters = newTile.GetComponentsInChildren(MeshFilter);
             var mesh : Mesh;
             var verts : Vector3[];                

             for(var filter : MeshFilter in filters)
             {
                 mesh = filter.mesh;
                 verts = mesh.vertices;                

                 for(var vert : Vector3 in verts)
                 {
                     vert.y = Terrain.activeTerrain.SampleHeight(vert);
                 }                    

                 mesh.vertices = verts;                

                 mesh.RecalculateBounds();
                 mesh.RecalculateNormals();
             }
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

Answer by aldonaletto · Jan 09, 2012 at 04:04 AM

Vertices are defined in local coordinates, thus you should convert each one to world coordinates to get the terrain height at its position. But there are other problems: since you're relocating only the vertices, the object's position will remain the same - and you may end with a strange object whose pivot is well above or below all of their vertices. Another thing: is your object really composed by 7 meshes? Usually there's only a single mesh, and the faces are composed by one or more triangles - the Unity cube, for instance, is a single mesh composed by 12 triangles.

Anyway, I think you should get the object's position and adjust its Y coordinate according to the terrain height. Then get each vertex, convert to world space, find the height and convert it back to local space before storing it.

The script below does the job for a single mesh: specify the object's transform in obj, and the desired position above the terrain in offsetY. NOTE: The object must have a MeshCollider, which will be updated to fit the new shape.

 function LandMesh(obj: Transform, offsetY: float){
   var baseY = Terrain.activeTerrain.GetPosition().y + offsetY;
   var pos = obj.position;
   pos.y = Terrain.activeTerrain.SampleHeight(pos)+baseY;
   obj.position = pos; // define new object's position
   var mesh = obj.GetComponent(MeshFilter).mesh;
   var verts = mesh.vertices;
   for (var i = 0; i < verts.length; i++){
     var w = obj.TransformPoint(verts[i]); // get vertex in world space
     var h = Terrain.activeTerrain.SampleHeight(w) + baseY;
     w.y = w.y - pos.y + h; // find the new height
     verts[i] = obj.InverseTransformPoint(w);
   }
   mesh.vertices = verts; // update the vertices...
   mesh.RecalculateBounds(); // bounds...
   mesh.RecalculateNormals(); // and normals
   // update the mesh collider too
   obj.GetComponent(MeshCollider).sharedMesh = mesh;
 }
 
 function Start(){
   LandMesh(transform, 3);
 }

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 Maker16 · Jan 09, 2012 at 04:10 AM 1
Share

Thanks for the feedback. I wil give it a shot. Yes, 7 meshes, because I want each side to have its own material. This allows me to define borders between owned regions by simply asigning the appropriate side with the appropriate border material. I tried to do it all as one mesh, but then changing one of a hexagon's sides' material resulted in the same side on all of the hexagons inheriting that same material.

avatar image aldonaletto · Jan 09, 2012 at 04:27 AM 1
Share

If all meshes are childed to the same object, you could iterate through the children and call Land$$anonymous$$esh for each of them:

  for (var child: Transform in transform){
    Land$$anonymous$$esh(child, offsetY);
  }
If you're not using colliders at all, remove the last line.
avatar image aldonaletto · Jan 09, 2012 at 11:18 AM 0
Share

One more thing: at least in 3ds$$anonymous$$ax (and probably in Blender and all other 3D software) you can specify different materials for each face. I think you should try to do that in your case, because a multi-mesh object may give you a lot of headaches - problems with colliders, textures not matching at the edges etc. When you have time, google for it and you will find some useful tutorials about this.
EDITED: Well, maybe this isn't a good idea... I don't know how Unity handles this, and maybe it makes things worse!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I modify the mesh in OnPostprocessModel? 2 Answers

'mesh' is not a member of UnityEngine.Component - Explicit Typecasting for Android 1 Answer

Is there any way to reset the default meshes? 3 Answers

Create mesh filter in Unity 1 Answer

mesh.colors doesnt seem to work at the moment 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