- Home /
Question by
Stone-Legion · Oct 28, 2015 at 03:29 PM ·
meshatlasmeshfilter
How to convert UV position to texture atlas pos and reapply to UV
I'm trying to apply a texture from an atlas to a mesh by reiterating through the UV array on that mesh, but I'm a bit lost on converting the coordinates.
For example, if the texture required is at x,y pixels on the material texture atlas, how can I convert and get those coordinates and apply to the mesh? I'm redrawing meshes dynamically with a chunk loader for a voxel game, I have it down for square voxels but now when adding more complex meshes, need to know how to apply the texture to these.
I imagine the code would be something like, but I'm hoping someone can help me fill in the blanks:
void UpdateMesh()
{
UVList.Clear();
Mesh mesh = this.GetComponent<MeshFilter>().mesh;
for (int i = 0; i < mesh.uv.Length; i++)
{
UVList.Add(GetUVTextureFromAtlas(mesh.uv[i].x, mesh.uv[i].y, voxelType);
}
mesh.uv = UVList.ToArray();
}
//converts UV position expected from mesh model to position on texture atlas
Vector2 GetUVTextureFromAtlas(float x, float y, int voxelType)
{
Vector2 uv;
Vector2 textureAtlasPos = GetTextureOffset(voxelType); // returns texture pos from atlas
//not exactly sure what this would look like for code to convert
//from the position on the atlas to the expected UV position based on
//expected UV position (i.e. from 1,0 to whereever the texture is on the atlas)
return uv;
}
Comment