Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Starleg2 · Oct 27, 2017 at 05:18 AM · c#tilemapuvprocedural meshprocedural-generation

How to set Tile UVs of a grid that is one mesh and shares vertices

I have a grid of tiles. The tiles share vertices as one mesh. I want to have 1 texture that is referenced from every tile to determine UVs of each tile depending on tile type. The attached image is the example texture sheet I'm using. Here are the scripts any help would be nice.

using System.Collections; using System.Collections.Generic; using UnityEngine;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] public class Grid : MonoBehaviour { private Mesh mesh; private Vector3[] vertices; private Vector2[] uv; public Tile[] tileArray; public int xSize, ySize; public float unitsPerTile = 1; public int tilePerLineOfTextureX; public int tilePerLineOfTextureY;

 private void Awake()
 {
     GenerateGrid();
 }

 private void GenerateGrid()
 {
     GetComponent<MeshFilter>().mesh = mesh = new Mesh();
     mesh.name = "Procedural Grid";

     vertices = new Vector3[(xSize + 1) * (ySize + 1)];

     for (int i = 0, y = 0; y <= ySize; y++)
     {
         for (int x = 0; x <= xSize; x++, i++)
         {
             vertices[i] = new Vector3(x * unitsPerTile, y * unitsPerTile);
         }
     }

     mesh.vertices = vertices;

     tileArray = new Tile[xSize * ySize];

     int[] triangles = new int[xSize * ySize * 6];
     for (int ti = 0, vi = 0, y = 0; y < ySize; y++, vi++)
     {
         for (int x = 0; x < xSize; x++, ti += 6, vi++)
         {
             triangles[ti] = vi;
             triangles[ti + 3] = triangles[ti + 2] = vi + 1;
             triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
             triangles[ti + 5] = vi + xSize + 2;
             tileArray[ti / 6] = new Tile(7, x + 1, y + 1);
         }
     }
     mesh.vertices = vertices;
     GenerateUVs();
     mesh.triangles = triangles;
     mesh.RecalculateBounds();
     mesh.RecalculateNormals();


 }

 private void GenerateUVs()
 {
     Vector2[] uv = new Vector2[tileArray.Length * 4];
     float xIndent = ((float)1 / tilePerLineOfTextureX);
     float yIndent = ((float)1 / tilePerLineOfTextureY);

     for (int i = 0, t = 0; i < uv.Length; t ++, i += 4)
     {
         float x = ((float)tileArray[t].tileType % tilePerLineOfTextureX) / (tilePerLineOfTextureX);
         float y = Mathf.Floor(tileArray[t].tileType / tilePerLineOfTextureX) / (tilePerLineOfTextureY);

         uv[i] = new Vector2(x + xIndent, y);
         uv[i + 1] = new Vector2(x, y);
         uv[i + 2] = new Vector2(x + xIndent, y + yIndent);
         uv[i + 3] = new Vector2(x, y + yIndent);

         //print((float)uv[i].x);
         //print((float)uv[i].y);
         //print((float)uv[i + 1].x);
         //print((float)uv[i + 1].y);
         //print((float)uv[i + 2].x);
         //print((float)uv[i + 2].y);
         //print((float)uv[i + 3].x);
         //print((float)uv[i + 3].y);
     }

     mesh.uv = uv;

     for (int i = 0; i < uv.Length; i++)
     {
         //print((float)uv[i].x);
         //print((float)uv[i].y);
     }
 }

}

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Tile {

 public int tileType;
 public int xPoz;
 public int yPoz;

 public Tile(int typeOfTile, int x, int y)
 {
     tileType = typeOfTile;
     xPoz = x;
     yPoz = y;
 }

}

alt text

numbersheet.png (3.2 kB)
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 Bunny83 · Oct 27, 2017 at 12:05 PM

You can not share vertices if the vertices that are supposed to be shared should have difference vertex attributes. Vertec attributes are: position, normal, uv, uv2, uv3, uv4, tangents, colors and even boneweights. If any of those attributes (if they exist) should be different for two vertices they need to be seperate vertices. A cube mesn has 8 "logical" vertices but it requires 24 vertices to actually become a cube. At each corner you have 3 different vertices all with the same position but different normals and maybe different UV coordinates in order to texture each side seperately.


Since it seems you want to map each "tile" to a completely different portion of the texture the 4 corners of each tile need to have seperate vertices. Also your UV addressing method doesn't make much sense. You access 4 vertices in a row and try to assign rectangular UV coordinates to them. However you have layed out your vertices in rows. So 4 consecutive vertices almost always are in a staight line. To access the correct vertices you would need to do the same as you did when creating your triangles. Two vertices are in line "y" and the other two vertices are in line "y+1". However as i said you can't share vertices so you need to create 4 seperate vertices for each tile.


It might be easier to lay them out in packs of 4 in the vertices array. If the mesh only consists of "quads" you can also use Mesh.SetIndices with MeshTopology.Quads. That way you only need 4 indices instead of 6 for each quad. It also simplifies the index generation if you create the vertices the way i mentioned before.


To sum up:

  • You need seperate vertices for each tile. That means you need 4 * xTileCount * yTileCount vertices. Note i said "TileCount", not vertex row or column count.

  • To create the vertices you just do a double for loop for x and y (as you're currently do, just not for vertex rows / columns but the actual tilecount) and inside the loop you create the 4 vertices for each tile and add them linearly to the vertex array.

  • Using Quads the index generation would be as simple as for(int I = 0; i < vertexCount; i++) indices[i] = i;

  • You could create the UV right in the same loop as you generate the vertex positions.

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

412 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 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 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

I would like to generate a plane and add hills to it. What would be the correct math for this? 1 Answer

UV problem on procedural mesh generation 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Mesh generation issue (C#) 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