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 capucinebuddy · Sep 10, 2019 at 07:35 AM · texturemeshproceduraluvsrounding-errors

Rounding Error in Texture Creation?

Hi there,

I am creating a simple 2D tile game. To approach the task of creating lots of tiles at runtime I have decided to create a mesh and apply a texture created procedurally over top. To accomplish this I have a reference tileMap which contains every sort of tile. I then assign the UVs to the map. This method works fairly well but a gap appears between certain tiles. Any idea why?

Below I inserted 2 functions, buildTexture is creating the texture itself and chopTileMap returns a 2D array with Vector2s which are unique to each tile. This array can then be used such that the array of Vector2s and index 0 will return the 4 coordinates of the bottom left tile in my tile map.

 void buildTexture(){
    Mesh mesh = GetComponent<MeshFilter>().mesh;
    Vector2[][] tileMap = chopTileMap();
    List<Vector2> uvList = new List<Vector2>();
    mesh.GetUVs(0, uvList);
    Vector2[] uvs = uvList.ToArray();
 
    //To set a UV it has to be given the fraction regarding to the tile map out of 1
 
    //tileMap is set as tileMap[tileID][Point on tile (0-3)]
    //for each UV, assign the set of 4 to points on the tile map
    for (int r = 0; r < rowCount; r++){
       for (int c = 0; c < colCount; c++){
          //int tileIndex = Random.Range(0, tileMap.Length-1);
          int tileIndex = 2;
          uvs[((r * colCount + c) * 4) + 0] = tileMap[tileIndex][0];
          uvs[((r * colCount + c) * 4) + 1] = tileMap[tileIndex][1];
          uvs[((r * colCount + c) * 4) + 2] = tileMap[tileIndex][2];
          uvs[((r * colCount + c) * 4) + 3] = tileMap[tileIndex][3];
          }
    }
 
    textureMap.Apply();
    mesh.uv = uvs;
    MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
    meshRenderer.materials[0].mainTexture = textureMap;
 }
 
 Vector2[][] chopTileMap(){
    int numColsOfMap = textureMap.width / tileResolution;
    int numRowsOfMap = textureMap.height / tileResolution;
    Vector2[][] tiles = new Vector2[numColsOfMap * numRowsOfMap][];
    for (int r = 0; r < numRowsOfMap; r++){
       for (int c = 0; c < numColsOfMap; c++){
          tiles[r * numColsOfMap + c] = new Vector2[4];
          tiles[r * numColsOfMap + c][0] = new Vector2((float)(c) / numColsOfMap, (float)(r) / numRowsOfMap);
          tiles[r * numColsOfMap + c][1] = new Vector2((float)(c+1) / numColsOfMap, (float)(r) / numRowsOfMap);
          tiles[r * numColsOfMap + c][2] = new Vector2((float)(c) / numColsOfMap, (float)(r+1) / numRowsOfMap);
          tiles[r * numColsOfMap + c][3] = new Vector2((float)(c+1) / numColsOfMap, (float)(r+1) / numRowsOfMap);
          }
    }
    return tiles;
 }

Notice the blue seams in the texture below.. They are quite hard to see but the most predominant one is vertical next to the player and are much more noticeable in the game.

alt text

screen-shot-2019-09-10-at-113345-am.png (279.9 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

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

158 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

Related Questions

Create planar UVs on procedurally generated mesh 1 Answer

Do I need to split my mesh up to properly UV texture it? 1 Answer

How do I go about texturing a flat-shaded generated mesh? 1 Answer

How to texture walls in procedurally generated mesh? 1 Answer

Realtime mesh deformation or texturing 0 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