The question is answered, right answer was accepted
UV mapping on mesh with tiling texture
I have check many posts about the topic but I still can seems to figure out how to achieve a good result. I am trying to repeat a texture on a script generated mesh, at the moment I am able to set the texture properly if I set the tileAmount to 1. However, when I increase the tileAmount I get wierd results and I don't understand why. I have tried many other solutions like using modulo with the texture width and height but it does not achieve what I am looking for.
Here's what it looks like:
Code:
int tileAmount = 10;
Vector2[] uvs = new Vector2[vertices.Count];
for (int i = 0; i < vertices.Count; i ++) {
float percentX = Mathf.InverseLerp(-map.GetLength(0)/2*tileSize, map.GetLength(0)/2*tileSize, vertices[i].x);
float percentY = Mathf.InverseLerp(-map.GetLength(1)/2*tileSize, map.GetLength(1)/2*tileSize, vertices[i].y);
uvs[i] = new Vector2(percentX, percentY) * tileAmount;
}
mesh.uv = uvs;
In the sample above, map is a 2D array representing a cell then map.GetLength gives the width and height of the map.
Answer by miketheike93 · Sep 12, 2021 at 11:37 PM
Okay after a while trying different things, I decided to add plenty of print functions to figure out what was going on. After compiling the data and checking everything was right so I started to think it was something going on with the material and/or the texture itself.
Guess what, the texture wrap mode was set to clamp... So upon setting it to repeat everything was working fine.
Your answer
![](https://koobas.hobune.stream/wayback/20220613042937im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can I add the same UV coordinates to multiple vertices? 0 Answers
instantiate gameobject with the uv 0 Answers
UV distortions in Blender when connecting vertices. 0 Answers
How do you draw a sprite from an atlas on to a mesh? 1 Answer
importing log captured by renderdoc in unity3d and processing it 0 Answers