Skip to main content

CINEMA 4D Lightmapping for Unity

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.

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.

  1. 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.
  2. 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

  1. 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.
  2. 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.

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.

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:
  1. Base (RGB) is the regular, seamlessly tiled material texture (the bricks, tiles, …)
  2. 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.
  3. 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

Popular posts from this blog

Improve usage of BIM during early design phases

When I was collecting ideas for a book chapter on BIM (that seemed to never have emerged after that), I collected 10 ideas, which I believe still reflect good recommendations to improve the usage of BIM during the early design phases. These ideas are related to BIM software, but you can apply them in any flavor, as long as you can model with Building Elements, Spaces and have control over representation. Introduction This article gives an overview of several recommendations and tips, to better apply BIM applications and BIM methodologies, in the context of the early design phases. Many of these tips are applicable in any BIM application and they are based on experience gathered from teaching, researching and using BIM software. Sometimes they could help software developers to improve the workflow of their particular BIM implementation. Tip 1 : Gradually increase the amount of information In the early design phases, the architect makes assumptions and lays out the main design in...

PythonOCC : Open Source interactive CAD shell (and how to run it on OSX)

What is PythonOCC? PythonOCC is an Open Source (LGPL) Python-wrapper for OpenCASCADE. So what is OpenCASCADE (OCC)? This is an advanced Open Source (custom license) modeling kernel, comparable to commercial engines, such as ACIS or Parasolid, which are used in quite some commercial CAD programs. When you want to develop CAD software, you could use OCC and write programs in C++. And why using Python? With this wrapper, you can create CAD and geometry scripts in Python, which is an interpreted Object-oriented scripting language. You can write almost "on-the-fly" and seriously reduce the implementation effort, by skipping the compiling-phase. You can even interact with a running program in the Python interpreter. Want to read more about this? The OpenCASCADE official website  (currently Linux and Windows are officially supported) The PythonOCC website/blog  (beware that the core of the actions happen in the development repositories). So far so good. Now the nasty, techn...

Getting BIM data into Unity (Part 8 - Strategies to tackle IFC)

This is part 8 of a series of posts about getting BIM data into Unity. In this post, we’ll discuss IFC as a transfer format towards Unity. As with the previous post, this is not a coding post, although hints and examples are provided. Open BIM and IFC Everybody who ever met me or heard me present on a conference or BIM-lecture will not be surprised to hear that I’m a strong believer in the Industry Foundation Classes (IFC), an open standard, with already two versions published as an ISO standard, being IFC2x2 and IFC4 (but surprisingly not IFC2x3 which is widely used). In the ideal world, this would be the format to use to transfer BIM data into another environment, such as Unity. So what are our options? Looking in the Unity Asset Store Assimp is a library which supports multiple formats, including IFC. https://assetstore.unity.com/packages/tools/modeling/trilib-unity-model-loader-package-91777   I did a few attempts, but alas without any success. It is po...