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 Starleg2 · Oct 26, 2017 at 02:00 AM · tilemapproceduralprocedural meshuv mappingprocedural-generation

How do I assign a mesh's UVs so that part of a part of a mesh is one part of the texture and another part of the mesh is another part of the texture

I'm using procedural generation to make a grid of tiles for a terraria like game. Right now I'm struggling because my plan was to use one texture that has every type of tile texture and give each tile (which are all apart of the same mesh) a tile type value and assign the UVs of the tile accordingly so that the texture of the tile is only one tile.

I basically want to do what this guy is proposing (http://answers.unity3d.com/questions/1389827/how-do-i-use-many-different-tile-textures-for-one.html) but I can't figure out how to map Uvs from one texture because I have more items in the uv array than the vertices array.

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(8, 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);
     }

     mesh.uv = uv;
 }

} 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;
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

78 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

Related Questions

Creating caves in voxel procedural mesh 0 Answers

Procedural Mesh From Random List Of Veritces 2 Answers

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

What is wrong with this mesh editing code? 0 Answers

How to apply PBR material to Procedurally generated Mesh? 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