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 Drakenish · Feb 22, 2015 at 05:56 AM · c#terrainmeshtilemapgrid

3D Terrain or Mesh Grid

I am currently developing a simple city builder to further enhance my skills with Unity3D. Unfortunately, I face the dilemma of creating a tile map for building placement(snap to grid, well you could cast position to integer to snap to 1.0fx1.0f tiles), controlling citizen movement speed , collisions etc.

Currently I have a series of tile prefabs(cubes with scripts) instantiated in unity based on a 3D array of the x,z coordinates and height (y). Apparently using too many game objects can pose a problem(tons of lag) in say a map of 1000x1000 tiles. There are two solutions to the problem as I am aware(i think).

(1) Render a Mesh

I made a mesh for the first time however, building the mesh is fine but how do I select a tile? I have my tile class with the various properties which I want to apply to the mesh squares (the 2 triangles). The question is how would I use a mesh to build a 3D tilemap for modifying each tile/selecting/accessing etc.

alt text

(2) Split a Terrain

I was searching google how to make an efficient 3D grid map and I found some questions on unity3D questions about splitting a terrain into a grid and using it for rts/citybuilder type games.

Perhaps one of you guys can recommend another method or clarify how to accomplish my goal?

Thanks!

EDIT::BELOW

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MeshMap : MonoBehaviour 
 {
     public float[,] tiles;
 
     List<Vector3> newVertices = new List<Vector3>();
 
     List<int> newTriangles = new List<int>();
 
     List<Vector2> newUV = new List<Vector2>();
 
     private Mesh mesh;
 
     private float tUnit = 0.25f;
     private Vector2 tStone = new Vector2(1, 0);
     private Vector2 tGrass = new Vector2(0, 1);
 
     private int squareCount;
 
     void Start()
     {
         GenerateTerrain();
         BuildMesh();
         UpdateMesh();
     }
 
     void Update()
     {
         UpdateMesh();
     }
 
     void GenerateSquare(int x, float y, int z, Vector2 texture)
     {
         newVertices.Add(new Vector3(x, y, z));
         newVertices.Add(new Vector3(x, y, z + 1));
         newVertices.Add(new Vector3(x + 1, y, z + 1));
         newVertices.Add(new Vector3(x + 1, y, z));
 
         newTriangles.Add(squareCount * 4);
         newTriangles.Add((squareCount * 4) + 1);
         newTriangles.Add((squareCount * 4) + 3);
         newTriangles.Add((squareCount * 4) + 1);
         newTriangles.Add((squareCount * 4) + 2);
         newTriangles.Add((squareCount * 4) + 3);
 
         newUV.Add(new Vector2(tUnit * texture.x, tUnit * texture.y + tUnit));
         newUV.Add(new Vector2(tUnit * texture.x + tUnit, tUnit * texture.y + tUnit));
         newUV.Add(new Vector2(tUnit * texture.x + tUnit, tUnit * texture.y));
         newUV.Add(new Vector2(tUnit * texture.x, tUnit * texture.y));
 
         squareCount++;
     }
 
     void GenerateTerrain()
     {
         tiles = new float[100,100];
 
         for(int z = 0; z < tiles.GetLength(0); z++)
         {
             for (int x = 0; x < tiles.GetLength(1); x++)
             {
                 tiles[z,x] = Mathf.PerlinNoise(x, z);
             }
         }
     }
 
     void BuildMesh()
     {
         for (int z = 0; z < tiles.GetLength(0); z++)
         {
             for (int x = 0; x < tiles.GetLength(1); x++)
             {
                 GenerateSquare(x, tiles[z,x], z, tStone);
             }
         }
     }
 
     void UpdateMesh()
     {
         mesh = GetComponent<MeshFilter>().mesh;
         mesh.Clear();
         mesh.vertices = newVertices.ToArray();
         mesh.triangles = newTriangles.ToArray();
         mesh.uv = newUV.ToArray();
         mesh.Optimize();
         mesh.RecalculateNormals();
 
         squareCount = 0;
         newVertices.Clear();
         newTriangles.Clear();
         newUV.Clear();
     }
 }
 

mesh.png (6.8 kB)
Comment
Add comment · Show 2
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 AlucardJay · Feb 22, 2015 at 06:14 AM 1
Share

If you wish to learn about mesh generation and manipulating those meshes at runtime, I thoroughly recommend this tutorial : http://studentgamedev.blogspot.com.au/2013/08/unity-voxel-tutorial-part-1-generating.html

avatar image Drakenish · Feb 23, 2015 at 01:04 AM 0
Share

I'm following the tutorial and changed the axis from x,y => x,z to have a flat mesh. However the mesh doesn't appear at all and I receive no errors.

**(if this helps) the game object with the script component moves during runtime to a new location.

1 Reply

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

Answer by hexagonius · Feb 23, 2015 at 07:37 AM

In UpdateMesh you're clearing out all lists you have. The second time this is run you set an empty mesh. Furthermore I'm not sure, but since you're not passing one Square to the next you're generating separate squares essentially quadroupling the data of the mesh (each vertex existing up to 4 times).

Comment
Add comment · Show 1 · 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 Drakenish · Feb 23, 2015 at 11:42 PM 0
Share

Doesn't mesh.optimize() solve that problem? Also, what is the most effective way of changing the color of each tile or applying an individual texture(grass, marsh etc)?

Anyway, Thanks! $$anonymous$$y mesh is now working properly :D

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

22 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

Related Questions

How to generate a grid for procedurally generated platforms 1 Answer

Mesh deformation by terrain shape 0 Answers

Lighting Banding Artifact 0 Answers

Questions about render distance in an open world game 0 Answers

How would i go about meshing over several objects? 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