- Home /
Texture Atlasing
I want to combine a bunch of textures into one material, then use that material for multiple gameobjects so they will batch. However, if I use material offsets to do this, it creates a copy of the material for each object, thus no batching. I noticed that imported meshes use a skinned mesh renderer, which looks like it uses Bounds to achieve the effect I'm looking for. However, I think you can only use that with imported objects.
In other words, instead of having eyes, a nose, and a mouth all on one texture, I want 5 different 2d buildings.
Edit: I thought that SetTextureOffset changed the UVs. Apparently I need to use the method suggested by The comments below. Thanks guys!
I’m not sure I perfectly understand what you’re trying - I got confused when eyes, nodes and a mouth became 5 buildings… If you’re importing your meshes from a 3D package then you can set the UV offsets in that package so you can share textures between meshes.
What I meant is this: When you import a character from a 3d program, the eyes, nose, and mouth are all on one texture. Then for each gameobject that texture's material is used with offsets. So if the eyes are in the top left, the eye object uses coords 1,1. If the nose is just to the right of the eyes, the nose object uses coords 1,2.
I want to do the same thing with 2d buildings. So building1 is in the top left of the texture, thus building1 gameobject uses coords 1,1 and so on.
The buildings are 2d and made in photoshop, so not imported from a 3d program.
@christo1745 ok I answered your question, but now I'm actually getting confused about buildings as well. You just wanted to change a material for multiple objects, right?
I think what you want is to change the UV coordinates of the polygon sprite that you’re dealing with. That way the material won’t be changed and you can batch the objects. You can do this either through rolling you own sprite class, or use one of the several that exist for Unity (on the Asset Store)
Indeed. You must change the mesh’s UV coordinates directly. So using “mesh.uv”. http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$esh-uv.html
Answer by Marnix · Jun 24, 2011 at 04:22 PM
You should use the `sharedMaterial` property in the renderer. You could even already define it in the material prefab in your project before adding it to an object. In runtime, it will only change one object, but sharedMaterial changes it for everyone.
New answer
Change the uv-coordinates of your vertices instead. It is just a quad, so you can change the vertex data!
The problem with shared$$anonymous$$aterial is that you can only have one offset. so if I offset the shared$$anonymous$$aterial to 1,2 all of my buildings would show the material at offset 1,2. Thus all of my buildings would be the same image.
So you actually don't want to use shared$$anonymous$$aterial, but just the normal material? You can just call `Renderer.material`
if I change the offset like this: objSpriteRender.renderer.material.SetTextureOffset ("_$$anonymous$$ainTex", spriteSheetOffset);
Then I get a copy of the material, so no batching. If I change it like this:
bjSpriteRender.renderer.shared$$anonymous$$aterial.SetTextureOffset ("_$$anonymous$$ainTex", spriteSheetOffset);
it will batch, but all objects with that material will have the same offset.
@christo1745 The trick of batching, is that no uniform variables have to be changed in the shader, so it can just continue rendering vertices. If you change a uniform like the offset, there is no speaking of batching anymore.
@christo1745 And read this as well http://unity3d.com/support/documentation/$$anonymous$$anual/iphone-DrawCall-Batching.html
It tells you indeed that you can do atlasing, but I think you need multiple objects with other vertex data. The material stays the same, but your uv-coordinates per vertex are different
Answer by Steven-Walker · Jun 24, 2011 at 04:56 PM
You need to offset the UV coordinates of your meshes so they correspond to the atlas coordinates. So there's 1 atlas containing multiple textures and 1 material applied to multiple objects. What makes this work are the UV coordinates of the mesh. Your material in Unity should have default offsets (1,0 1,0).
You can build an atlas in Unity and create the UV mesh coordinates using an editor script (or tool like Sprite Manager). Or you can manually build an atlas in Photoshop and assign the mapping/UVs in an external 3D program.
Answer by raminsh · Jun 24, 2011 at 05:05 PM
ok I have actually done this but not in unity, I usually use one of the following:
1- NVIDIA Texture atlas (you can get it from: http://developer.nvidia.com/legacy-texture-tools and it has pretty much all you need)
2- Flatiron from: http://www.texturebaking.com/ this one is a 3dmax plugin and really useful
I believe that is mentioned in one of the links $$anonymous$$arnix posted above. I think they are windows only :(. I think I need to use PackTextures(), then mesh.uv. I'll update later today when I get it working.
Your answer
Follow this Question
Related Questions
Texture Discoloration Issue When Importing Into Unity 1 Answer
Adding a Material to Particle System 1 Answer
Texture not showing on game objects 1 Answer
Regular grid on irregular mesh 0 Answers
Repeating a texture over an object 2 Answers