Material Baker by David Ikeda
Version 1.0
Download Material Baker.
Special thanks go to Pixolator and Svengali for helping me get started. I especially want to thank Svengali for introducing me to the possibility of doing this in ZScript. He is currently working on a similar script that will probably be better suited for the task, but here is a quick solution for the time being.
Material Baker is a ZScript to bake in material shading and textures into a new texture. It will do this in a way that is similar to dropping and picking up shading with projection master, but without the overlapping issues. It will also rotate the lights with the model so that the lighting remains consistent across the baked texture. ZBrush currently lacks a surface baking tool that will bake the entire model for you automatically, and using drop and pick with projection master will end up overbaking your model in areas that overlap. This script is not a total solution, but it can alleviate the problem and ease the process of transferring ZBrush materials and shading information into a 2D texture.
The script is available in text format so that you can view the source code. I did not take the time to properly document the source since it's such a tiny script of only a few hundred lines of code. Since it's my first script and such a simple one at that, I didn't think anyone would be too interested but I included it anyway. The script has only been tested with ZBrush 2. I'm not sure if it works with earlier versions. To run it, simply load material_baker.txt from the ZScript panel.
Why is baking the material and shading of a model handy? For starters, it can provide you with a template that is easier to paint than a wireframe. By baking in the shading of the high-res ZBrush version of your model, it can put landmarks in your texture that are easy to identify when painting over them in your preferred paint program. For realtime environments, artists will often paint image maps that contain shadows and highlights in them to compensate for the engine's lack of sophisticated lighting and materials. For this, baking in the shading can give you a good template to start with to indicate where highlights and shadows should go. In some cases, you may even want to use the baked texture by itself with little or no post-processing. The baked texture itself can look very nice on a low poly model. For quality renders, using a low contrast version of the baked texture can help give a little boost when combined with displacement.
The interface currently looks like this:
It's not very pretty as you can see, but the functionality is all there. I initially made the script for myself only, but decided to take a few hours to make it more user-friendly so others can use it as well. If people like the script, I may try to make it a bit more attractive and friendly.
The script will generate a lot of textures during the baking process as a result of doing Texture>GrabDoc. I don't know if I can do anything about this since there is no way that I know of to remove textures from memory. If you end up with too many textures, you can do preferences>Init ZBrush to clear all the extra textures, tools, and materials from memory once you are finished.
The following is a quick tutorial to help you get started. I will be baking in the surface of my 'average male' model that I created for an isometric role-playing game I'm working on. I'm not much of an artist, but I hope he will be okay for the tutorial. The material I'm using is a variation of SoKaR's skin shader. Thanks to him for sharing the material settings. You can find the material at ZBrushCentral.
The first thing I do is set the document size to a good, large size. The bigger the document size, the better the quality of the baking (at the cost of speed). My model is taller than he is wide, so I decide to go with 600 width and 800 height.
The next step is to draw my model to the canvas.
The model I have has had his UV mapping generated in Lightwave using Atlas Mapping and spherical mapping for the head. This results in a more readable texture. I chose to do this only to better illustrate the texture result from the baking process. ZBrush's UVTiles also work great and minimize distortion while maximizing the use of the uv space, something Lightwave's Atlas mapping does not do nearly as well.
I then scale him so that he is as large as possible. It's important, however, that I don't scale him so large that he goes out of the canvas, or else the parts that go out of the canvas won't get baked.
He should also remain in the canvas even if I rotate him in any angle along the vertical axis. To achieve this, I set the pivot point of my model to the center of his body. An easy way to do this is to make the entire model visible and hit the S. Pivot button. I then simply press scale to scale him to the size of the canvas. Now I can rotate him along the Y axis in any angle without having him go off the canvas.
Now that I have him oriented properly on the canvas, I adjust the lighting to the way I like it. Once I start the baking process, I won't be able to adjust the lights any further. While the script will rotate the lights with the model, there can still be some inconsistencies in lighting across the baked texture at overlapping regions. This will be more pronounced if the lighting is high in contrast. Increasing the ambient setting can reduce contrast, but can also make the lighting less interesting.
At this point I could draw or use a texture on my model. The original texture will be baked in along with the material and shading. However, I'm just going to bake the material only for this tutorial.
The model is now on the canvas and the lighting is the way I like. I then adjust the image size setting in the ZScript window to 1024. This is a bit large for the game engine, but I will reduce it down in photoshop once the texture is complete. I also check Best Render so that the model will be rendered with Render>Best setting each time before baking. This yields optimal quality.
The next thing I should do is to set the background color of the layer to a color that closely resembles the midtones of my model. This color will be used as the background color for the baked texture. An accurate way to do this is to do layer>bake, position the cursor over a section of your model that represents the midtone color, then hit C to pick that color. If you use this method, make sure to draw the tool again over the canvas before starting. Also, set the selected color back to the color you want for your material if you don't have a texture.
The next thing I do is adjust the render settings. I won't bake in shadows this time since I'm too impatient, but I will turn on SoftRGB for antialiasing. This can help by blurring the edges. The areas near the edge of the model's silhouette will have the most distortion from the baking, and blurring the edges helps minimize the seams that result. I also set the sample size to 3 to blur the edges even more. Beware: too much blurring could make the seams even more pronounced by producing soft regions in your texture.
I'm now ready to start baking, so I hit Start in the ZScript window.
The model will be oriented to its default orientation with rotation (0, 0, 0). In my case, the model was already facing front, which is its default orientation. You should make sure that your model faces the right direction for its default orientation. You can adjust this in tool>preview.
Pressing start also sets the render mode to fast shade. This will speed things up and will also provide a visual aid. By shading the material with the fast shader, you can better see the areas that are baked into the model and see how the texture would appear on a basic shader.
I'm now ready to start baking. In my case, I will bake manually to better illustrate the baking process. The AutoBake buttons will perform a sequence of bakes from a variety of angles to automate the entire process.
When baking your model, you generally want to bake in the order from the least important viewing angle to the most important one. This is because the angles that you bake first will be overlapped the most and will receive the most amount of distortion around the edges. It's important to note that baking more doesn't necessarily result in better quality textures. Choosing the best order of angles is the most important thing.
In this mode's case, the bottom side is the least important angle, so I start by pressing the Bottom bottom to view the model from below. You can see how his feet are messed up, especially from the bottom side. This is because they aren't going to be seen in the game.
The reason the view controls (Front, Back, Left, Right, Bottom, Top, and Yaw) are there is because they serve to orient the lights as well as the model. You could rotate the model in the viewport, but this will not synchronize the position of the lighting and will result in inconsistencies in the shading of your texture. I'm now researching a way to make it so you can orient the model any way you like on the canvas and still have the lighting synchronized when you hit Bake. The reason it's not yet implemented is because ZBrush uses a complex system for representing rotational angles which I haven't been able to figure out.
I now press bake to bake in the bottom side of the model. This can take a while depending on the complexity of your model, your machine specs, etc. For me, it took about 15 seconds. You can rotate the model freely now to see the result. Just don't bake again without using the view controls unless you want to adjust the lighting yourself, or don't mind the inconsistencies that result.
I now repeat the process for the top by pressing Top then Bake. I continue to do the left side, the right side, back, front, then the 3/4 views of the model at 45 degrees, 135 degrees, 225 degrees, and 315 degrees yaw.
I'm now finished baking so I hit Quit. Here is the result of the baking:
There are some seams on the sides and where the arms obscured the side of the upper torso. The biggest seams result from uneven lighting. Even though the script orients the lights with your model, there can be some discrepancies in the way the model is lit from different viewing angles. Sometimes, there can also be a few regions that do not get baked in as a result of being obscured, depending on the shape of your model and the angles you chose to bake with. Again, the script isn't perfect, but a little bit of blurring and smudging in a 2D paint program should do the trick in patching these areas, or you can simply make the adjustments directly in ZBrush in 3D or 2D.
This concludes the tutorial. It might take a while to figure everything out, but the process normally takes me about 1-3 minutes. The script also comes with help messages that should guide you through the process when you bake your own model.
You can also use this script to bake in your model's wireframe along with the materials. Just turn on quick edit and polyframe and turn off best render for the script setting. Here's a quick wireframe bake I made using AutoBake3 with the same model:
To see the lines clearly, you need to use a really large document size and texture size. In my case, I used 2048x2048 for both the document size and texture size. To scale your model to fit the document and see the progress, zoom out so that the entire document can be seen. I also increased the polyframe opacity and reduced the 3D shading to emphasize the lines.
I hope you find the script to be useful. Feel free to e-mail me about the script at newbie3d@yahoo.com.