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 /
avatar image
0
Question by Jack-Mariani · Sep 08, 2015 at 07:03 AM · c#terrainmeshtiles

Creating mesh tiles from the terrain

I'd like to create multiple cells on a given terrain, and I want to use them as transparent meshes (to be activated when the player selects it).

The idea is to create these "tiles" with code (C#). How can I select these an area and create a meshes that adhere to the terrain?

The idea is to create hexagonal tiles, but for the sake of semplicity could you give me some hints to create square tiles?

(I'll adjust the code to make it hexagonal).

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
0
Wiki

Answer by nu-assets · Sep 10, 2015 at 07:05 AM

This class lets you easily create a mesh from a specified terrain. Based on this class you can create what ever you want.

For larger terrains you have to split it to multiple meshes because a mesh can only hold 65000 vertices.

 using System.Collections.Generic;
 using UnityEngine;
 
     public class TerrainMesher
     {
         /// <summary>
         /// Converts a terrain to a mesh.
         /// </summary>
         /// <param name="terrain">The terrain to convert.</param>
         /// <returns>The mesh</returns>
         public static Mesh MeshFromTerrain(Terrain terrain)
         {
             // Create new mesh object
             Mesh mesh = new Mesh();
     
             TerrainData terrainData = terrain.terrainData;
     
             // Create all lists needed
             List<int> indices = new List<int>();
             List<Vector3> vertices = new List<Vector3>();
             List<Vector3> normals = new List<Vector3>();
     
             Vector3 heightmapScale = terrainData.heightmapScale;
     
             // Compute fractions in x and z direction
             float dx = 1f / (terrainData.heightmapWidth - 1);
             float dz = 1f / (terrainData.heightmapHeight - 1);
     
             for (int ix = 0; ix < terrainData.heightmapWidth; ix++)
             {
                 float x = ix * heightmapScale.x;
                 float ddx = ix * dx;
     
                 for (int iz = 0; iz < terrainData.heightmapHeight; iz++)
                 {
                     float z = iz * heightmapScale.z;
                     float ddz = iz * dz;
     
                     // Sample height and normal at dx, dz
                     Vector3 point = new Vector3(x, terrainData.GetInterpolatedHeight(ddx, ddz), z);
                     Vector3 normal = terrainData.GetInterpolatedNormal(ddx, ddz);
     
                     // Add vertex and normal to the lists
                     vertices.Add(point);
                     normals.Add(normal);
                 }
             }
     
             int w = terrainData.heightmapWidth;
             int h = terrainData.heightmapHeight;
     
             // Add triangle pairs (quad)
             for (int xx = 0; xx < w-1; xx++)
             {
                 for (int zz = 0; zz < h-1; zz++)
                 {
                     int a = zz + xx * w;
                     int b = a + w + 1;
                     int c = a + w;
                     int d = a + 1;
     
                     // Add indices in clockwise order (winding order)
                     indices.Add(a);
                     indices.Add(b);
                     indices.Add(c);
     
                     indices.Add(a);
                     indices.Add(d);
                     indices.Add(b);
                 }
             }
     
             mesh.vertices = vertices.ToArray();
             mesh.normals = normals.ToArray();
             mesh.triangles = indices.ToArray();
     
             // Recalculate mesh bounds
             mesh.RecalculateBounds();
             
             return mesh;
         }
     }


The triangles are constructed as shown on the image below.

Triangulate Vertices Clockwise Winding Order


triangle-clockwise.jpg (35.5 kB)
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

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

28 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Need Pointer: How to create 2D map in Unity? 1 Answer

How would you go about shaping terrain with scripting? (C#) 1 Answer

Converting a mesh to terrain 2 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