I programmatically create a Mesh and want to generate a texture that patches coloured squares together so that the patchwork perfectly matches the vertices.
The mesh's dimensions are expressed in floats, the texture dimensions are expressed in pixels.
myMeshFilter.mesh = BuildMesh();
// todo: how to find out the values instead of ??
myMeshRenderer.sharedMaterials[0].mainTexture = BuildTexture(??, ??);
private Texture BuildTexture(int width, int height){
var texture = new Texture2D(width, height);
var pixels = textures[TileType.Grass.Value]; //array of 64*64 pixels
var tileWidth = width/NumberOfColumns;
var tileHeight = height/NumberOfRows;
//todo: do this for all the tiles instead of only the first;
texture.SetPixels(0,0, tileWidth , tileHeight, pixels);
}
In the editor, you can set the pixel-to-unit ratio, but not sure how to set this on generated textures.
My current implementation assumed that setting and using my source texture's pixel to unit ratio in my calculations would do the trick, but I noticed that it doesn't and I Googled far and wide and have mostly become more confused with every hit.
I think I need the camera to translate my mesh's corner values to pixels, but not sure and wouldn't know how.
UPDATE
Pointing the UV's of the mesh to the correct points in the texture would be a solution (and probably a rather performant one), but would restrict me in my possibilities if I want the tiles to have visual-only 'overflows'.
eg: grass tile next to dirt tile would have a bit of grass over the dirt.