adrift on a cosmic ocean

Writings on various topics (mostly technical) from Oliver Hookins and Angela Collins. We have lived in Berlin since 2009, have two kids, and have far too little time to really justify having a blog.

Blender - Baking Textures Within One Object

Posted by Oliver on the 28th of October, 2018 in category Tech
Tagged with: blender

This is not a ground-breaking new concept, but since I failed to find decent material on it when searching, and it's not entirely clear from the Blender documentation, I thought I'd write someabout about it with screenshots. Normally, when we are baking textures in Blender (e.g. for simplifying the render of a complex scene, or exporting objects and their textures to a game engine), we use the "Selected To Active" option, with a second object:

Screen Shot 2018-10-28 at 17.04.00

The above screenshot is at the final stage in the process after baking, but basically the steps are:

  1. Prepare your object as necessary, including UV unwrapping.
  2. Duplicate the object and remove all materials from the duplicate.
  3. If making a lower level of detail object, use one of the modifiers (e.g. Decimate) to reduce the complexity of the mesh.
  4. Create a new texture to bake to, at your desired size (I've just used 1024x1024 here). Save it to disk.
  5. UV Unwrap the object to the baking texture. Usually Smart-unwrap mode is enough but there are many possibilities at this point.
  6. Add a single material to the duplicate object, and add the above baking texture to it as an image texture.
  7. Expand the bake foldout of the render tab, choose Selected to Active.
  8. Select the source object together with the duplicate object (in that order), select the type of bake (e.g. Diffuse) and then click Bake.

This does work, and I guess is probably what most people do. Especially for creating object LODs it's the only way to do it anyway. Up until now that's also what I've been doing. This week though I wanted to bring some objects into Unity and not decrease the level of detail in the objects - they are already pretty low-poly designs.

Here's the scene I'm talking about:

LittleRoom4

Naturally I started with the Lava Lamp in the bottom right corner and immediately hit problems (not least because of the emissive surface). One problem is that it has multiple materials (ok, only two) and the second was that I didn't want to go through all the process of creating a second object and doing the Selected to Active trick. That option can be left unselected, so how does it work?

Without Selected to Active

For reference, here is the Blender docs page on texture baking.. It does describe the process, but leaves out a lot of detail - so I'm going to document the detail here! Maybe it will help you, but at least I will have a reference for myself later if I forget again.

The basic principle to remember is that you don't need any additional objects or additional materials on the existing object. In principle, that would mean a significant time-saving, but unfortunately the process is still a bit fiddly so I'm split on whether it is actually easier.

The first mistake you (or I in this case) are likely to make is creating another material and leaving the bake image connected. You end up with the lovely error message Circular dependency for image "Diffuse" from object "Cube" (or whatever your image and object are called in the scene).

Screen Shot 2018-10-28 at 17.53.29

This is not how to do it, but a cursory reading of the documentation would tell you that. The circular reference is due to the renderer trying to use this image in the light calculations but also drawing the bake output to it - even if the material is not assigned to any faces.

So let's disconnect the image texture from the diffuse shader and try again! Uhoh, we get another error - No active image found in material slot (0) for object "Cube". You might (again, like me) think the ordering of your materials might make a difference, and swap them around and get a slightly different error message like No active image found in material "Bake" (1) for object "Cube" but the end result is the same - no baked texture.

Actually what you need to do here is add an image node to your existing material that you want to extract the colour information from. Like so:

Screen Shot 2018-10-28 at 18.01.33

And there we have our lovely baked texture in our standard unwrapped cube formation.

But what about the original problem, of having an object with multiple materials? Let's add a couple more:

Screen Shot 2018-10-28 at 18.07.20

Now we have a couple more of our lovely procedural textures on the object (Noise and Musgrave) assigned to alternating faces of the cube. But when we bake, our old friend No active image found is back, complaining about something, and we can see that the bake result only covers two of the six faces of the cube. I've selected the two which are still assigned the Voronoi texture so you can see where they are in the UV map, and confusingly one of them ended up with the Noise texture baked onto it. Why? No idea, could be a bug.

OK, so how to capture all of the different materials on the object - we actually need to put an Image Texture node into all of the materials on the object, with a reference to the diffuse texture onto which we want to bake.

Now, if you do this like I originally did, you may still run into the same issue of Blender complaining about No active image found on the other materials. I've found a couple of situations recently where if you copy and paste a material node from one material into another material, it may retain a reference to the other node in the other material, and somehow think it doesn't have a separate reference to the baking texture. In this case, simply delete the pasted Image Texture nodes and create them again with Ctrl-A, then select your baking texture again.

You should end up with each of the materials on their respective unwrapped UV faces on your baked texture!

Screen Shot 2018-10-28 at 18.22.32

Huzzah, all the textures! Probably Blender 2.8 will change all of this and this blog post will be irrelevant in a couple of months...

© 2010-2018 Oliver Hookins and Angela Collins