Can someone explain or point me to a source that explains how to use texture atlases for materials? I seen it used quite frequently for things like vegetation, and would like to create my own. I just have no idea how to connect the right coordinates in the atlas to a certain model.
You would use: Unity - Scripting API: Texture2D.PackTextures if you want to implement it yourself. You have to understand what mesh UVs are to use this correctly.
The code would look something like:
var uvRectChangesFromTextureAtlas = atlasTexture.PackTextures(arrayOfTextures, padding, 8192);
for (var i = 0; i < uvRectChangesFromTextureAtlas.Length; i++)
{
var rect = uvRectChangesFromTextureAtlas[i];
// You can just keep an array of meshes instead, if you want to decouple
// the setting of the finished atlas mesh.
var meshFilter = arrayOfMeshFilters[i];
var atlasedMesh = MeshUtils.CopyMesh(meshFilter.sharedMesh); // Use your own copy mesh method.
var remappedUVs = meshFilter.sharedMesh.uv;
for (var j = 0; j < remappedUVs.Length; j++)
{
var uv = remappedUVs[j];
uv.x = rect.x + (uv.x * rect.width);
uv.y = rect.y + (uv.y * rect.height);
remappedUVs[j] = uv;
}
atlasedMesh.uv = remappedUVs;
// Save the atlased mesh to disk somewhere.
meshFilter.sharedMesh = atlasedMesh;
}
This code will not actually compile, and is just example pseudo-code. But as you can see, remapping the UVs is not that complicated. Really, the main challenge is setting up your workflow. For example, you might want to remap the UVs of multiple meshes that are all using the same material/texture set. So to update my example code, you would need an array of List, instead of just an array of MeshFilters. Or better yet, instead of List, maybe create an actual class like:
public class TextureAtlasItem
{
public Material SourceMaterial;
public List<MeshFilter> MeshFilters;
}
And so on, until you come up with a flexible atlasing workflow that suits your needs.
You can probably google some example scripts using this method. Or you can buy something like Pro Draw Call Optimizer | Utilities Tools | Unity Asset Store, but it might be worth learning how to do it yourself using the built-in Texture.PackTextures(). I personally spent a lot of time editing the source code of Pro Draw Call Optimizer, and eventually just implemented my own workflow. I wish I had just done that from the start.
Using texture atlases for materials is crucial to optimization and affects both static and dynamic objects.
-
When 2 or more 3d game objects share a material, they can be batched…This save set-pass calls and is awesome.
-
Static game objects that share a texture/material atlas can be combined for even more performance improvement.
Probuilder, a free tool by unity has a build-in UV editor. You can use this to manually create your uv maps. Essentially, you select a 3d face on a mesh, and then that face shows up over your atlas, then you resize the face to match up what part of the image you want to be displayed on the selected 3d face.
This is a video tutorial that used probuilder to explain exactly how to UV map a texture atlas on 3d objects within Unity. Probuilder is an amazing tool and very user-friendly compared the behemoth 3d modeling programs out there.
This video shows you how to do uv mapping. For a texture atlas, the only difference is that you would have all of your scene images (or as many as possible) on one shared texture. Let me know if you have any questions or would like to discuss in more detail.
Thank you, I never really gave it a chance, I just figured Id use blender.
You’re welcome. I use blender for things like rocks, humanoids, and custom rigging. For everything else in a 3d environment, I tend to prefer Probuilder. The cool thing is that you can export “obj” files from Probuilder and open them in 3d programs like blender if you need to later on.