• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by mrmadprofessor · Nov 12, 2012 at 12:26 PM · texturemeshuvatlasautomatic

Automatic mesh combine and texture atlasing

I was wondering if there is anyone who has built a script for combining meshes and their textures into atlases while also modifying the UVs to fit the atlas.

In this way I could build meshes and have they use their own textures (one texture per mesh) and then in Unity combine all textures and meshes that uses the same type of shader into one. Would just be easier to work in that way while also keeping the optimizations of texture altasing.

So the script would have to be able to combine meshes, their textures and modify their UVs and change all meshes to use the same material.

Maybe this is just a stupid idea, just something I thought might be nice.

Comment
Add comment · Show 12
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Nov 12, 2012 at 12:40 PM 0
Share

just TBC, if you mean strictly for 2.5D in Unity, this is all done totally in 2DToolkit - a popular and magnificent product.

If you mean in general for 3D meshes. Yeah it's a great idea - surely, there must be various tools like this for sale already. And many companies small and large make their own tools like this, for specific situations.

Don't forget to that Unity does have an amazing thingy built in where you can flag things as static and Unity does a lot of this automatically. Eg http://docs.unity3d.com/Documentation/$$anonymous$$anual/DrawCallBatching.html

avatar image mrmadprofessor · Nov 12, 2012 at 01:33 PM 0
Share

I'm talking about full 3D scenes. Yeah the static and dynamic batching are great, although the static batching breaks if you use one texture per mesh though. Therefor it would be awesome for workflow if I could chose what meshes should be combined (both mesh and texture).

$$anonymous$$aybe you wouldn't want to combine the meshes since this removes the ability to cull meshes that aren't in the view. But a tool to combine meshes textures into using the same texture atlas and material would be awesome.

Now I have manually made atlases of objects but you always end up with a too large or too small texture in the end. If this was done for you the workflow for the artist could be improved while keeping the optimizations.

If anyone has something like this please shout out! :P

Edit: posted as comment ins$$anonymous$$d of answer, sorry guys

avatar image Fattie · Nov 12, 2012 at 01:53 PM 0
Share

You may be interested in this recent question ...

http://answers.unity3d.com/questions/340800/on-combining-two-objects-.html

"$$anonymous$$aybe you wouldn't want to combine the meshes since this removes the ability to cull meshes that aren't in the view. But a tool to combine meshes textures into using the same texture atlas and material would be awesome."

Yes, everything you say is exactly correct, as I already said. We have a number of in-house tools for just this. Is there anything in the Asset Store maybe??

"Now I have manually made atlases of objects but you always end up with a too large or too small texture in the end. If this was done for you the workflow for the artist could be improved while keeping the optimizations."

Something to cinsider is that in the "industry" if you will typically the "art department" will do all this, as a matter of their skill and engineering. Like, part of the nature of being "an incredible art department" is doing (very skillfully) what you describe manually. In a sense, there'd be no need for what you describe on BIG jobs but yeah it would be great for everyday use, for sure. Just as you say it's surprising there is not such an asset for sale on the Asset Store already:

handy texture convertor and re-uv-mapper.

Don't forget too that there is, of course, much professional (very expensive) "uv packing" software which is very expensive and used constantly as a daily critical tool, by top modellers -- I think the leading one is that french product I can't think of the name (uv master? uv pack or something?)

it is very very difficult to write tools like this that pack and rearrange textures - not to mention repainting them on the UV on the 3D models - indeed in verges in to lite-AI for sure.

avatar image Fattie · Nov 12, 2012 at 01:54 PM 0
Share

"Edit: posted as comment ins$$anonymous$$d of answer, sorry guys" it should be a comment, not an answer. You can however convert comments to answers using the Zoop button the smallest one near your nickname on a comment.

avatar image mrmadprofessor · Nov 12, 2012 at 02:01 PM 0
Share

Yeah combining meshes into one ins$$anonymous$$d of static batching might not be worth it since it sort of destroyes the culling.

It could work if there was anyway to offset and scale the UVs of each mesh to the correct values in the atlas. For instance I have three objects, one uses a 1024x512 texture and the other two uses two 512x512 textures. This would become 3 drawcalls as they are since they need to use one material each.

If the script would combine these textures into a 1024x1024 and offset and scale each meshes UVs to the correct positions and then assign the same material to all three of them static batching would work. All meshes count as one drawcall and culling would still work and the artists can work on their separate textures without atlasing them themselves.

But this might be difficult as you say... Damn :P

avatar image Fattie · Nov 12, 2012 at 02:27 PM 0
Share

Quite right -- if you're talking about "not fucking with the UVs" in the sense of remapping software, yeah

Again I'm surprised there is not such a tool already. indeed maybe there is and this conversation is pointless :-)

(Indeed note that combing THE $$anonymous$$ESHES is irrelevant - unity largely does it, to some extent. it's FAIRLY EASY to write a utility to combine meshes, note the Combine Children script that exists already - you're on to that right? (perhaps it will give you some possibilities imediately.)

avatar image mrmadprofessor · Nov 12, 2012 at 02:37 PM 0
Share

I got the idea after using NGUI that takes smaller images and packs them together into a texture atlas. So I started wondering if there was anyway to do this with meshes, only that is a little more complex since the UVs had to be replaced and the meshes need to be assigned the same material.

Will ask the coders if they can come up with something, if so I'll post the script here.

avatar image PlanetTimmy · Nov 14, 2012 at 10:25 PM 0
Share

Actually, I wrote something very much like this a couple of weeks ago. I have a scene I imported from an ancient game I wrote in Renderware in 1999/2000. There were a huge number of separate textures and the terrain mesh alone was generating ~140 draw calls. (although it must be said the import script I'd written to import the Renderware .dff format was pretty naïve and created duplicate materials)

I wrote a script that basically split the entire scene into individual triangles and materials. Duplicate materials were merged.

I then had a function that would split any triangles that had uv values that went out of the 0-1 range and split them so that no triangle uvs went outside that range.

I then wrote a script that manually atlased all the textures into 1024x1024 textures (I had to do it myself as there appears to be a bug in the Unity atlaser). I also made the atlaser include a 16 pixel clamped border around all textures so that there aren't any odd edging artefacts at lower mip levels.

I then had to adjust the uvs of all the triangles to match the new uvs from the atlased textures.

After that it was a case of grouping triangles together by locality and material (I used an x/z grid for grouping) The draw calls have now dropped to around 30.

avatar image mrmadprofessor · Nov 15, 2012 at 08:19 AM 0
Share

Cool, there isn't anyway you could post the atlasing script maybe? We were thinking about creating a easy to use script were you can choose what meshes should be combined and then it atlas those textures and assigns them with a material/shader of your choice.

I promise to post this script if we make it. Thanks!

avatar image whydoidoit · Nov 15, 2012 at 08:35 AM 0
Share

You really need a custom shader if your materials don't all have the same color tint - you can't do it if they all have different shaders that look at odds.

Are your meshes skinned or normal? I have a script for combining skinned meshes (which are never batched by Unity) and was considering a version of normal meshes but batching already helps there (and can improve the performance compared to 1 giant mesh that is always rendered).

avatar image whydoidoit · Nov 15, 2012 at 08:38 AM 0
Share

If you use tiled uvs or your meshes use uv wrapping then you have to repeat the textures to make that work or write a really fancy shader. The built in Unity batching does not combine textures for you so those already have to be atlased.

avatar image mrmadprofessor · Nov 15, 2012 at 08:40 AM 0
Share

Since I use the mobile shaders they only use diffuse with no color tinting. But you are correct, you won't be able to have different settings between the materials since all of them will share the same.

The idea was to atlas the meshes that use the same shader and settings, but it will have to be simple to setup to not create troubles for the artists.

Skinned meshes are batched in Unity 4 right? Will upgrade to that next week.

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by mrmadprofessor · Apr 08, 2013 at 07:26 AM

Thanks! Also while checking over this one more time I found a tool on the asset store that does this, I haven't tried it yet but I will. So if anyone wants the same tool check out,

http://u3d.as/content/ian-deane/mesh-baker-2/3DL

It's $55 but it might be worth it on larger projects.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by LoganBarnett · Nov 17, 2012 at 12:35 AM

I did something like this for our character creator. There's a lot of moving parts involved, but I'll try to show you the relevant parts:

Part is just an enum to determine which character part to use, which turns into an index into the texture atlas. Any part potentially shows skin, so we have an edge case for those UV offsets.

MapToRange() comes from David Koontz's GoodStuff, which you can find here: https://github.com/dkoontz/GoodStuff

Once you've offset all parts used you can do your CombineMeshes() call as normal. Make sure to pass true as the second argument so it flattens everything into a single material.

I hope this helps!

 void OffsetUvs(SkinnedMeshRenderer partRenderer, Part part, TextureAtlas atlas) {
         var uvOffsetMesh = (Mesh)Instantiate(partRenderer.sharedMesh);
         
         var skinIndex = -1;
         var materialIndex = 0;
         foreach(var material in (Application.isPlaying ? partRenderer.materials : partRenderer.sharedMaterials)) {
             if(material.name.ToLower().Contains("body")) {
                 skinIndex = materialIndex;
                 break;
             }
             ++materialIndex;
         }
         
         var partIndex = -1;
         switch(part) {
         case Part.Head:
             partIndex = 1;
             break;
         case Part.Torso:
             partIndex = 2;
             break;
         case Part.Legs:
             partIndex = 3;
             break;
         case Part.Feet:
             partIndex = 4;
             break;
         }
         
         var skinUvIndexes = new List<int>();
         var uvs = uvOffsetMesh.uv;
         0.UpTo(uvOffsetMesh.subMeshCount - 1, i => {
             uvOffsetMesh.GetTriangles(i).Each(vertexIndex => {
                 if(i == skinIndex) skinUvIndexes.Add(vertexIndex);
             });
         });
         
         var uvIndex = 0;
         uvs = uvs.Select(uv => {
             if(skinIndex > -1 && skinUvIndexes.Contains(uvIndex)) {
                 uv = new Vector2(uv.x.MapToRange(0f, 1f, atlas.Dimensions[skinIndex].xMin, atlas.Dimensions[skinIndex].xMax),
                                  uv.y.MapToRange(0f, 1f, atlas.Dimensions[skinIndex].yMin, atlas.Dimensions[skinIndex].yMax));
             }
             else {
                 uv = new Vector2(uv.x.MapToRange(0f, 1f, atlas.Dimensions[partIndex].xMin, atlas.Dimensions[partIndex].xMax),
                                  uv.y.MapToRange(0f, 1f, atlas.Dimensions[partIndex].yMin, atlas.Dimensions[partIndex].yMax));
             }
             ++uvIndex;
             return uv;
         }).ToArray();
         
         uvOffsetMesh.uv = uvs;
         
         partRenderer.sharedMesh = uvOffsetMesh;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by baha · May 04, 2013 at 09:12 PM

Hi,

GameDraw has that feature plus modeling, texturing, sculpting, primitives, free assets and much more for only 45$ :)

Here is a video showcasing the optimization feature in GameDraw http://www.youtube.com/watch?v=glqbWsc4CXQ

GameDraw in the asset store https://www.assetstore.unity3d.com/#/content/2811

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by jmunozar · Jan 30, 2015 at 05:36 PM

Hey there, there's a free utility on the asset store, checkout: https://www.assetstore.unity3d.com/#/content/16888

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assigning UV Map to model at runtime 0 Answers

Split a mesh while using same UVs and texture position. 0 Answers

Distortion of Texture from some Angles 0 Answers

A way to "fold" a texture on a mesh? 1 Answer

can't put texture materials on imported mesh. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges