Mesh Baker FAQ

 

The combined mesh looks darker, lighter or the colors are off. How can I make it look the same?

Mesh Baker only deals with the texture properties of the combined material shader. You will need to adjust the non-texture properties yourself.

Look at the combined material in the inspector and check that properties such as base color, thresholds etc… have the same value as in the source materials.

If a source material has no texture, Mesh Baker will always create a clear texture for that property. Many shaders display differently if a clear texture is present versus having no texture present. Try removing the texture from the combined material if it was not there in the source texture.

Check that the combined mesh has the same channels as the source meshes. It may be that a channel such as “colors” was not included but is needed by the shader.

A good test is to apply the source material to the combined mesh. The UVs will be off but if the colors display correctly then you know that the problem is with the material or the atlases not the mesh. Otherwise there is a problem with the mesh.

Mesh is missing when I create a prefab. How can I prefab the combined mesh?

Set “output” to “bake into prefab”. You will need to create a prefab with an empty game object and assign it to the “result prefab” field.

What are atlases and how do they work?

An atlas is an image (a single png file for example) that contains other images.

Imagine a cube mesh with a material and texture that makes it look like a blue dice and another cube mesh with a material and texture that makes it look like a red dice. Each cube mesh has UV coordinates (one coordinate for each vertex) that map parts of the textures to triangles on the mesh. The bottom left corner of the texture has UV coordinate 0,0 and the top right corner has UV coordinate 1,1.

Unity would need two drawcalls to draw these cubes. One for each cube. Wouldn’t it be nice if we could combine those. This is what atlases are for.

We could create one big image file in photoshop and put the blue dice texture on the left and the red dice texture on the right. This big texture is called an “atlas”. However we can’t use it with our cubes. The UV coordinates in the meshs would be wrong. They map to the whole image not just half. Each dice would be half blue and half red. We would need to adjust the UVs in the meshs. The blue mesh needs its UVs scaled so they are in the range 0,0 to .5,1. The red mesh needs its UVs scaled so they are in the range .5,0 to 1,1. If we do this then both cubes can use the same texture/material and both would still look correct. These meshes could be combined and Unity could render them with one draw call.

Mesh Baker is a tool that combines textures into atlases and combines meshes (adjusting UVs as it goes).

After I bake materials the textures loose quality. Is there a way to avoid this?

There could be a few things that are causing the quality loss.

1) Unity may be re-sizing the atlas texture. When baking the baked atlas size is printed in the console. Look at the “Maximum Image Size” field on the atlas texture. Sometimes unity sets the Maximum Image Size to a low number. If you change the “Maximum Image Size” in the inspector to match the actual atlas size it may eliminate your quality loss. Hopefully this is your problem as it is easy to fix.

2) Another place you may be loosing quality is baked tiling. If you look in the console at the output you should see a warning if MeshBaker is baking tiling. If the baked tiling exceeds “Max Tiling Bake Size” then the baked texture is being shrunk to fit and there could be quality loss. Not much that can be done about this one. You may be able to bake all the meshes that share the tiled texture together and you could tile the combined mesh.

3) Another place quality can be lost is texture packing. Each platform has a maximum texture size. iOS = 2048, PC = 4096. If your combined atlas exceeds this then textures will be shrunk as they are added. You should be able to get an idea from the report in the console if this has happened. Not much can be done other than splitting the combined mesh into several combined meshes or sharing more textures.

4) MeshBaker has a “Resize Power of Two” textures option. This shrinks textures as they are being added so they have a border of “padding” around them. The reason for this is so power-of-two textures can be packed efficiently into an atlas that is also a power of two. Without this feature packing can be very inefficient (almost 75% wasted space). It is possible although unlikely that this is causing the quality loss. Try baking with and without “Resize Power of Two” to see.

MeshBaker doesn’t create an atlas for one of the texture properties

Mesh Baker contains a list of texture property names that it look for in the Combined Mesh Material shader:

“_MainTex”, “_BumpMap”, “_BumpSpecMap”, “_DecalTex”, “_Detail”, “_GlossMap”, “_Illum”, “_LightTextureB0”, “_ParallaxMap”,”_ShadowOffset”, “_TranslucencyMap”, “_SpecMap”, “_TranspMap”.

It creates an atlas for each one that it finds. If a shader has some texture properties that are not on this list. You will need to open the shader and look at its properties (look for 2D properties) and add the missing ones to the “Custom Shader Property Names”. Mesh Baker will then create atlases for these.

You probably don’t want to bake an atlas for cube maps because these are probably a reflection of the outside environment onto your object. You probably want to apply the original cube map.

I get this error [I] UnityEngine.MissingReferenceException: The object of type ‘Texture2D’ has been destroyed but you are still trying to access it.[/I]. What can I do?

[I] UnityEngine.MissingReferenceException: The object of type ‘Texture2D’ has been destroyed but you are still trying to access it.[/I]

The error started in Unity 4.1 and is intermittent. It occurs far less often in Mesh Baker version 2.5.3 and later.

This error started happening with Unity 4.1.f0 and 4.1.2. It appears to be a bug in Unity. Temporary textures are being destroyed before the script is finished using them. It appears to only happen when a Garbage Collection is triggered. A bug report has been submitted to Unity and Unity is able to reproduce the problem.

A new Texture Packer was added to Mesh Baker 2.8.2 if you have this version try setting the Texture Packer to “Mesh Baker Texture Packer”.

Try reducing the size or number of textures being combined.

To avoid the creation of temporary textures try to make sure all texture slots are filled on the source materials and that they are already resized to the final size they will be in the atlas. Turn off “resize power of two”.

As a work around it is possible to export models and materials to Unity 4.0. Bake the combined materials and the material bake result asset. Then export the combined materials and material bake result asset back to 4.1.

 

How do I prefab a baked Skinned Mesh

Prefabing a combined skinned mesh is tricky. The mesh needs to be an asset, not an instance and the bones/animations need to be included in the prefab.

  1. Delete the combined mesh object
  2. Set the renderer to “Skinned Mesh”
  3. Set the output to “BakeIntoPrefab”
  4. Create a prefab and add an empty game object then drag that to the result prefab field. Make sure this is an asset not a scene object. You can delete the game object out of your scene.
  5. Click “bake” There should be two messages, one reporting that the mesh was saved. The other is a warning that “Render type is skinned mesh renderer. Can’t create prefab until all bones have been added to the combined mesh object ObjectName Add the bones then drag the combined mesh object to the prefab.
    “. You need to do a bit more manual work to include the bones.
  6. Working with the combined mesh, test that animations work. You may need to adjust the animation culling setting.
  7. Drag all you source objects (bones) so they are children of the combined mesh scene object.
  8. Test that animations still work.
  9. Disable or delete the Renderers and MeshFilters on the bones.
  10. If everything works then drag the result to your prefab asset.

What does error “The sharedMaterial on object has been ‘Instanced’. …” mean

You may receive the error message:
“The sharedMaterial on object has been ‘Instanced’. This was probably caused by a script accessing the meshRender.material property in the editor. The material to UV Rectangle mapping will be incorrect. To fix this recreate the object from its prefab or re-assign its material from the correct asset.”
If this happens in the editor it is probably caused by an editor script trying to access MeshRenderer.material.
It can also happen at runtime where it is also probably caused by a script accessing the MeshRenderer.material field.
MeshRenderer objects have two fields MeshRenderer.material and MeshRenderer.sharedMaterial. Unity tries to be efficient and create no more Materials in memory than is necessary. Initially it creates one material that all objects that use that material share. “sharedMaterial” references this material for all objects. If you change anything (like a texture) on the material then all objects that use this material see the change.

MeshRenderer.material is for custom materials. If you access (read from) this variable then Unity creates a new instance (a copy) of the sharedMaterial that this object now uses. If you make any changes to this material it only affect this object.

Unfortunately it also confuses MeshBaker because MeshBaker uses the material on an object to look up the UV rectangle in the atlas. The second material instance is a new one that MeshBaker does not know about so the lookup fails.

One fix is to find and modify the script accessing the the renderer.material field so that is accesses renderer.sharedMaterial.

Another option is to fix the problem after it has happened is to find the original material and assign it to renderer.material. You can find the original material in the Material Bake Result asset by comparing the name of the instanced material (with “(instance)” removed) with the names of the materials in the Material Bake Result asset.

Can I Use a 3rd Party Atlas Generation Tool With Mesh Baker?

Mesh Baker does not directly support this out of the box but it is reasonably easy to integrate.

The MB2_MeshBaker component and MB2_MultiMeshBaker component do not communicate directly with the MB2_TextureBaker component. All communication takes place through the Combined Material Asset. All you need to do is create and populate a Combined Material Asset with data from your 3rd party Atlas Generation Tool.

1) Run your 3rd party atlas generator. You should end up with one or more image files (atlases) and list of rectangles describing where the textures are in the atlases.

2) Create a dummy material for each texture in the atlases. You only need one material for each texture in _MainTex. Not a material for each texture in maintex and each texture in _normalMap etc.. You don’t need to configure these materials or even set the shader. They will only be used as keys to map to the correct UV rectangles in the atlases.

3) Put these materials on your source objects.

4) Create an empty Combined Material asset. Click on it the project window and you can edit its fields in the inspector. You need to populate:

  • the Materials list with your dummy materials
  • Prefab UV Rects list with the list of rectangles generated in step 1.

These must correspond. This would be a good step to automate with a script.

5) That is it you are ready to bake your source objects using your combined material.

Tips for using Mesh Baker with Unity Tree Creator trees

Thanks to Barbara Parkman for these tips.

  1. Trees must be individual objects (placed manually, not placed by using the terrain inspector script).

  1. Follow the instructions in the Mesh Baker manual PDF for setting up multiple materials *very carefully*. Make one submesh for bark and one for leaf materials.

  1. When setting up your Mesh Baker script before baking, use the “Optimized Bark Material” and “Optimized Leaf Material” that are part of each tree creator tree. Do NOT try to use the Tree Creator shaders in the Nature section of the shader dropdown. The Optimized materials are found in the Project panel under tree prefab. They are always named the same and they are created automatically by tree creator.

  1. Once your material atlases and mesh are baked, if you don’t like the appearance of the finished mesh, you can switch around textures in the material slots to get what you want. For example, my experience was that the leaves of the Mesh Baker default mesh had a “piebald” (too contrasty) appearance when I first baked it. I fixed this by removing the texture from the normalmap slot and swapping in a normal map I made (from the main texture atlas).

If you want wind to work with your baked trees, there are two scripts that will provide it. One is a C# script that works with the mesh renderer, by Phong, http://forum.unity3d.com/threads/159258-Mesh-Baker-by-Digital-Opus-RELEASED/page20 and one is a JavaScript, by LarsBertram, that works with global shader properties http://forum.unity3d.com/threads/138929-batching-trees-%96-and-speed-up-rendering I prefer the JavaScript script because it has exposed variables for wind direction that can be tweaked, but it can make the tree trunks look distorted. To fix this, I disabled the wind on the trunk and branches by swapping in a Bumped Diffuse shader for the Nature/Tree Creator Bark shader on the Mesh Baker mesh material.

 

Parts or all of the combined mesh have flipped normals

This is usually caused by negative scaling components. Try baking your scaling on your source model before importing. Or using my other asset “Mesh Master” to do it. Negative scaling on you models can cause problems with other parts of unity as well such as collision detection, physics, shaders, batching. It is considered a bad practice.

Skinned Meshes sometimes vanish while still in view

Skinned meshes are expensive to render so Unity tries to cull anything that is not visible. Unity uses the skinned mesh’s bounds to do this. These bounds need to be recomputed regularly. If you have multiple skinned meshes that wander around independent of each other in a combined mesh then you will need to keep updating the bounds. Here is a script that will do this. Attach this script to the MeshBaker component that baked the skinned meshes.


using UnityEngine;
using System.Collections;

public class UpdateBounds : MonoBehaviour {
MB2_MeshBaker mb;

void Start () {
mb = GetComponent();
SkinnedMeshRenderer r = mb.resultSceneObject.GetComponentInChildren();

// the following lines are needed
// or updating the skinned mesh bounds in
// Update has no effect.
r.updateWhenOffscreen = true;
r.updateWhenOffscreen = false;
}

void Update () {
mb.UpdateSkinnedMeshApproximateBounds();
}
}