- Home /
What is the simple way of applying Texture Atlas to Unity
hey guys...
I asked my question in a very old thread and I was suggested to make a new one.
2 be honest this is really weird... I usually find whatever I need through a simple google or youtube search. but this atlas thing is really gettin on my nerves. it's about a weak or two I'm digin google, youtube, even unity3d.com to find the answer related to Atlas. But all I find is Atlas generation through:
3rd party programs or plugins like Nvidia
Scripting: ie Texture2D.PackTextures and GetPixel method http://forum.unity3d.com/threads/6699-Texture2D.PackTextures apart from the fact that I dunno how they apply this scrip and combine objects in unity.
Tiling whitin texture atlas and so on and so forth.. http://answers.unity3d.com/questions/214837/is-possible-to-tile-within-a-texture-atlas.html
none of 'em explained the way the Atlas is applied to Unity. There's no single tutorial out there for starters. Would u explain in a simple way how this atlas works in unity. Say I wanna create a simple 512*512 Atlas in photoshop. do I have to have 4 textures with the exact 256*256 pixel and put them in photoshop and save 'em as a specific format and then replace it with original textures in unity and tweak x and y tiling?
Besides.. what kinda Mapping method should I use in 3rd party 3d modeling applications? Unwrap UVW or just UVW mapping ? I UV my model this way: Say we have a 5 story building with 5 seprate walls and I wanna tile their interior and exterior faces with two different textures. What I do is grab a wall and go to polygon subobject , Select the interior wall faces and choose UVW mapping from modifierlist and tile the interior walls with a desired texture and do the same for the outer walls with a different texture. I Finally import my model into unity and I have two materials for a single mesh which I'm not sure is the right way. I can't use Unwrap UVW coz I wanna tile my texture. I'm sure this is gonna make a drawcall issue in long term modeling and texturing.
Any help would be appreciated.
I guess I figured out the basic outline of how things work here and there...
to packs multiple textures into a texture atlas I did this:
A. created a new script in unity and renamed it as "PackTextures" and replaced the codes inside with Unity Texture2D.PackTextures codes:
function PackTextures (textures : Texture2D[],
padding : int,
maximumAtlasSize : int = 2048,
makeNoLongerReadable : boolean = false) : Rect[]
saved the script and then created an empty gameobject and renamed it to "root". next put the UVd gameobjects I wanted to make atlas out of, inside the "root" and finally dragged and dropped the PackTextures script into the root and bam:
B. knowing how to deal with scripts I tried the same procedure with the C# script made by Phantom. The result: NOTHING HAPPENED....
Thanks to Ippokratis for explaining in a simple way on how scripts like this work...
It's too bad that I failed. according to Texture2D.PackTextures runtime class the above mentioned function replaces current texture with the texture atlas. Size, format and whether the texture has mipmaps can change after packing.
Guys???? What Am I doin wrong ?
I guess this shouldn't be an answer to your question, should it?
Answer by Bunny83 · Jun 30, 2012 at 04:16 PM
A texture atlas is also just a texture. That's all. There is nothing special on a texture atlas. It's just one big texture with several textures usually arranged in a grid. The important point is that your actual models / meshes are unwrapped correctly. If you created your texture yourself, just unwrap your models onto the atlas texture and everything works as usual.
But!!! If you created your models and unwrapped them onto a single texture and you want to pack all those single textures afterwards in an atlas, you need to adjust the unwrap (the models uv coordinates)
Texture2D.PackTextures is just a helper function. You provide a texture array with all your textures you want to pack and the function pack all those textures into one texture and retuns a Rect for each texture. This rect have to be used to adjust the uvs of each vertex in your mesh.
Basically your uv coordinates are usually between 0 and 1 in each axis. After the packing they have to be remapped to the rectangle. For example if PackTextures returned a rect like this: Rect(0.2,0.4,0.2,0.2)
the new area for the texture is not 0 to 1 and 0 to 1, it's 0.2 to 0.4 and 0.4 to 0.6.
You just need to multiply each uv with the rects size (of course seperately for each axis [x,y] or [u,v]) and add the rects position after the scaling. This have to be done with every vertex in a mesh.
btw, tiling within an atlas is usually not possible. There are some shader tricks, but they aren't very efficient.
Yeah, I know how texture atlas works. there are tons of tips and infos out there 'bout texture atlas. they can be manually generated , say in photoshop or automatically by other 3rd party programs. If I got you right, we already have our atlas generated before unwrapin our mesh and after unwrapping is done, just put the atlas in uv editor and fine-tune the unwrapped uv... right ?
but this has some disadvantages: 1. you have to have all textures you need to put in a single atlas, beforehand. 2. If you make any future changes, say extend the size of atlas, then you have to go back to 3dsmax and remap all those uv coordinates . this back and forth makes it difficult.
on the other hand I thought not only this so called Texture2D.PackTextures function would create tex atlas out of UVd objects automatically but also it'd retain their uv coordinates. and all of them was done through a simple script then all u had to do was tweak the offset.
seems I was wrong... :(
Tnx Bunny...
Even tilling in atlas is possible when you have smart design of texture and geo and you do not want to put all shit into one texture but pack it in respect with tilling.
Answer by TehWardy · Apr 01, 2013 at 05:49 PM
It can be done with not too much work but it depends on your requirements ...
I'm in the process of building a voxel engine which uses one I made myself. The problem I have is that my terrain is all procedurally generated so I created a simple mechanism for "indexing" the textures. If you force every texture in the atlas to be the same size you can get texture information purely by convention, without that you will need some form of lookup.
I went for the first option ...
So lets say I want to know the uv coords for a texture in the atlas:
I need to know some key things: 1. the number of textures wide my atlas is 2. the number of textures tall my atlas is 3. where your texture is within the atlas
from there i wrote a simple helper method that i put in a helper class that I can call from anywhere which goes something like this ...
Vector2[] GetUvs(int atlasWide, int atlasHigh, int getX, int getY)
{
float texWide = 1 / atlasWide;
float texHigh = 1 / atlasHigh;
float startX = getX * texWide;
float startY = getY * texHigh;
float endX = startX + textWide;
float endY = startY + textHigh;
// now you have your 4 coords which are the key points within the atlas
// create uv's depending on you widing of your verts.
}
Its hardly a complete example but it'll get you started. This means that all you need to do is attach the atlas to your game object then use something like this to calculate the actual uv coords within it on a per tri basis.
I carefully modelled my game code so that I could easily map the 4 corners to 2 tris each time, it's just a matter of figuring out what you're mapping to I guess.
Answer by philjhale · May 11, 2013 at 05:32 PM
I found this tutorial very helpful. It's about creating a 2D game and one of the first things it covers is creating a texture atlas using Texture Packer applying it in Unity with the help of a free 2D framework called Orthello.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to texture fbx models in unity 2 Answers
textures vs textureless 2 Answers
Modeled Terrain In Blender Not Accepted In Unity 1 Answer
Texture is rendering black? 1 Answer