While the Unity Game Engine has an integrated Lightmapping system (Beast, now from Autodesk), you need a Pro-license for Bounced Lighting. There are cases where it makes sense to import pre-rendered lighting from an external software. This blogpost is an overview of the process to bake lighting on a model in CINEMA 4D and ensure that it survives the conversion into Unity.
Goal
A model with two sets of textures: the regular tiled textures (bricks, tiles…) that are seamlessly repeated over the geometry (using the regular UV coordinates) and a second texture, containing the baked lighting using a second set of UV coordinates.
References
Several people have developed their own routines to get this done, but I'd like to refer to two posts, where a user name "Artzfx" posted some details on his workflow.- http://forum.unity3d.com/threads/165691-Cinema-4D-Textured-Model-to-Unity-Best-Practices
- http://www.c4dcafe.com/ipb/topic/69750-baking-gi-and-textures-for-unity3d/
Preparing your model
The example I use is part of a finished Master thesis project, where an historical chapel was modelled in ArchiCAD, converted into CINEMA 4D and then visualised in an iPad app created with Unity.
Beware: as we want to still have realtime shadows inside Unity, we should avoid direct Sun light and better opt for an overcast Sky in CINEMA 4D.
- Here you want to have the cleanest and simplest model as possible. Before I started this process, I used the option inside CINEMA 4D to reorganise the project according to its used materials: each material will be assigned to a single mesh, containing all geometry for that material. This makes the model unusable for merging ArchiCAD updates, so be aware of that.
- You also need to ensure that every mesh has a UVW mapping. If you use Cubic or other mappings on certain objects, generate UVW coordinates from within the Structure Browser menu.
So this is the plain model: geometry & textures. Nothing more.
Prepare lighting and materials
- This is an important step (courtesy of Artzfx): in the Baking process, you will render out lighting (including GI) and Ambient Occlusion (AO) onto the model. However, this will also include the basic material colour. Before we bake, add a plain white material on all objects. Just drag this material on all objects, so you get an additional texture tag on each mesh.
- Now you can set up your rendering settings (GI, AO…). However, there are small differences in settings in the Bake dialog when compared to the Render Settings. Check the CINEMA 4D manual for the details. Nothing dramatic.
This is what it looks like, so far.
Bake Objects
Now you can right click on the objects you want to bake in CINEMA 4D. Avoid glass (hide it) and group all objects so the bake can be performed in one step.
You get the following dialog:
We chose Ambient Occlusion (AO) and Illuminate (the GI lighting, including the material colours, which are now white). Render size is 1024x1024 for objects that are not too big and preferably larger for objects that are bigger (like walls or roofs that span several meters). You can enable Supersampling (anti-aliasing) but it adds quite a lot to the render time.
When you opt for a Single Texture, all channels are merged into single image (lighting, AO, shadows, colour). Otherwise, you get individual images, which you can still compose inside Photoshop (or using an advanced Shader in Unity).
Locate your Path Name, which is ideally inside your Unity Project Assets folder.
Now you may Bake and take a break. Each object will be automatically unwrapped (to generate non-overlapping UV coordinates with minimal distortion) and rendered pixel by pixel into the baked image. At the end of the process, CINEMA 4D makes a copy of your selected object with the new mapping and hides the original one (without changing anything).
Press render to confirm that it still looks similar to the original rendering (but possibly with lower-resolution shadowing and some minor artefacts).
Correct Materials for Unity export
Each new object, which is a copy of the original one, has a new material, with the baked texture inside the Luminance Channel. However, to be able to transfer the correct UV mapping into Unity, we need to (manually) copy the Luminance Channel to the Color Channel. Don't forget this!
You also need to (manually) copy both the Texture and UVW tag from the Baked Objects back to the original objects. This is required to have both sets of UV coordinated transferred correctly. Ensure that each material tag has its corresponding UVW tag directly to the right of it.
Now we are ready to export to Unity:
- remove the plain white material tags (they were only needed while baking)
- unhide the original geometry (with two sets of material+UVW tags) and remove the baked objects, to avoid exported twice the amount of geometry.
- Finally export to FBX (or use the automatic C4D conversion from Unity). No need to embed textures in the FBX export.
Set up correct shaders in Unity
Load the CINEMA4D or FBX model in Unity and apply your regular Import Settings. Do need generate a new set of secondary UV coordinates, though, as we just did so much effort to embed the already in the previous steps.
Now you still need to go through each material from the imported model and replace the default "Diffuse" shader with a Lightmap Shader (Legacy\Lightmap\Diffuse or Bumped Diffuse). This shader has two (or three) material slots:
- Base (RGB) is the regular, seamlessly tiled material texture (the bricks, tiles, …)
- Normalmap (if you used the Bumped Diffuse shader) can be used for your Normal map. If you only have a greyscale Bump Map, Unity can extract a usable Normalmap from it. Set it to smooth and generate from greyscale is best set.
- Lightmap (RGB) is the baked texture from CINEMA 4D.
This is what you get:
Some afterthoughts
- This setup is elaborate. If you have Unity Pro, you might as well stick with the integrated BEAST light mapping with Bounced lighting and Environment lighting. If you have the Indie/Free version of Unity, you don't have this option and getting an even lighting can be really tricky, as you need multiple shadow-casting lights and multiple bake attempts to see what you end up with.
- Since we used a white material before baking, all colour is still derived from the regular textures. This means you can still tweak it or even switch to another texture and/or normal map.
- The Legacy Lightmap shader still allows realtime shadows. The picture above is thus a composition of baked lighting (the shadows in the corners, which give it depth), regular materials (diffuse & bump) and realtime shadows from a Directional Light. Each of these can still change, even in realtime.
- If you know your way around scripting in CINEMA 4D, you might automate a few of the above steps.
Comments
Post a Comment