- Home /
Texture problem when applying to procedural mesh
I am currently writing a tilemap system in which the map is required to generate at runtime and also have a procedural texture applied. The Mesh generates perfectly and the texture almost fits the mesh but is 1 pixel greater than the set tilesize.
I have messed around with the variables to see if it was just a mesh or texture scaling problem but it appears not.
Notice that the texture squares are not the same size as the grid AND that they go over the edge of the texture due to appearing to be 1 pixel to large.
When I save the texture onto my hard-drive however, it is 8 * 8. (The image is fuzzy (When uploaded onto this site (it isn't in Unity)) so I will not upload an example of it).
The most obvious solution I can think of then is scaling the image down to fit the entire board but A. I don't know how. B. I don't know how efficient that is. C. It seems like a cheap workaround. (Right now I am generating an 8 * 8 grid on the X / Y Axis)
void BuildTexture(int size_x, int size_y) //Pass In number of tiles
{
int texWidth = size_x * Tile.resolution; //Resolution is currently 1 so the math here is simply 8 * 1
int texHeight = size_y * Tile.resolution;
Texture2D texture = new Texture2D (texWidth, texHeight); //Create Texture
for(int x = 0; x < texWidth; x++) //Loop through texture
{
for(int y = 0; y < texHeight; y++)
{
Color c = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)); //get Random Color
texture.SetPixel(x, y, c); //I am aware that this is inefficient, this is a standin system to fix the current tiling issues
}
}
texture.wrapMode = TextureWrapMode.Clamp; //I heard this was useful for preventing this problem... It wasn't
texture.filterMode = FilterMode.Point; //To prevent fuzzy textures
texture.Apply (); //...
meshRenderer.sharedMaterial.mainTexture = texture; //Apply Texture
}
It is also important to note that I do not receive any error messages.
You will probably want to manually set the UVcoordinates of your mesh manually, to exactly match your texture coordinates.
The UV coordinates, define which point on your texture (x,y) to associate with a given vertex on your mesh(x,y,z).
The most obvious solution I can think of then is scaling the image down to fit the entire board...
but if your texture is only 8x8, how would you scale it DOWN? I would think you would want to scale it UP. (this would be done by changing your loop's x++ and y++ to x+=Tile.resolution and y+=Tile.resolution, just make sure you write the same color to ALL the pixels you are now skipping)
It is also important to note that I do not receive any error messages.
agreed, hehe
Thanks for the reply, now trying it.
Also good point with the "Scaling down", I forget that the image is currently 8 * 8 (despite having specifically stated such).
Answer by Freak-ey · Feb 08, 2015 at 09:50 PM
I have somehow managed to fix my code by simply making this change:
int texWidth = size_x * Tile.resolution + 1; //Resolution is currently 1 so the math here is simply 8 * 1
int texHeight = size_y * Tile.resolution + 1;
from:
int texWidth = size_x * Tile.resolution; //Resolution is currently 1 so the math here is simply 8 * 1
int texHeight = size_y * Tile.resolution;
I am unable to see any reason for this but this works... for all resolutions...
Task Accomplished... I guess...
This implies to me, that you are somehow setting the WRONG values in your size_x,size_y or Tile.resolution variables. $$anonymous$$ight want to address that or a similar symptom might crop up again elsewhere. (no bashing your solution! I'd just never be satisfied with not knowing WHY it fixed it.)
Edit: oh! the answer is: X tiles, means X+1 vertices. (if the first X verticies are on the left of the tiles, we need one more vertex for the right side of the rightmost tile)
I was planning to look into the reason why this works today but I think you just solved it. Thanks again for the help.
You were correct, I was passing in the number of tiles rather than the number of vertices. $$anonymous$$y original code works just as well (and is more readable) as a result.
Thanks again for all of the assistance.
Your answer
![](https://koobas.hobune.stream/wayback/20220613182808im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Apply PNG as texture to 3d object 0 Answers
How to switch the texture being used by the material of the mesh renderer 1 Answer
Different prefabs with same mesh but different textures(materials). 1 Answer
can anybody help with script that takes webcam texture pixel information to deform mesh vertices? 0 Answers
Realtime mesh deformation or texturing 0 Answers