Lacuna Passage - Devlog #52.5 - Lightmapping with Emission in Unity 5

Light mapping is a technique where you can bake the light from a scene into objects so that they self-illuminate based on a generated lightmap texture that usually is mapped to a second UV channel. This is a great solution to reduce the number of light calculations needed on static objects. But what if you have an object in the scene that needs to appear as if it is projecting light and casting shadows on other objects - and you also want to control the color and intensity of the light ingame? Well that’s where emissive lighting comes in handy.

Emissive lighting is typically used to self-illuminate objects in a scene and make them appear as the light source. A texture specifies the areas of the object that you want to emit light and global Illumination bounces this light onto other objects in the scene. Below is an example of a typical emissive object with global illumination affecting the scene around it.

But when you want precise shadows and lighting that matches the emission produced you need to get a little more complex. Baking the light casted from the emission source in your modeling software can achieve this effect with only a little extra effort. So, in effect, we want to produce our own lightmapping outside of Unity and apply it to the emissive channel in our Unity material.

First things first, we need to get a demo scene started in our modeling software. I’m going to be using 3ds Max along with the Mental Ray renderer. Below is the basic scene in 3ds max (top) and our desired look using a spotlight in Unity (bottom).

The hemisphere in the corner is the lamp of our scene and its bulb will illuminate our objects. To emulate this lighting in 3ds Max we need to place a light source in the center of the bulb. I used a Photometric Free Light and set the shadows to “Ray Traced” and the emit shape to “Sphere.”

Because I am using Photometric lights, I made sure my scene units match that of real-world objects. In this case I used 4 cm for the lamp bulb and made the radius of the emit shape also 4 cm.

After tweaking some of the light and shadow settings, I made a new Arch & Design material to place on our objects. In the diffuse color map slot I placed my diffuse map.  In the glossiness map slot under the reflection rollout I placed my gloss map. I made sure the diffuse and reflection colors are set to pure white, and the reflectivity is set to 1.0. I also checked the “Highlights+FG only” option because bounced light will be taken care of by global illumination in Unity.

Finally, I added a Normal Bump under the Special Maps rollout with the normal map of our projected high poly (Make sure you flip the green channel). After a couple quick render tests I decided to increase the gloss output to 2.0 to make the highlights tighter.

Now that we have our light and materials setup, we just need to render the light to a texture map. To do this, we open the Render to Texture dialog (Rendering > Render to Texture…). With our all our objects attached into one mesh we can easily render the entire texture at once. With our single object selected we can set our render to texture properties accordingly.

Scroll down to the Output rollout and add a “CompleteMap.” Complete maps incorporate all light information including normal, gloss, and diffuse maps into our bake. Set the texture size and save location accordingly and make sure “Shadows” is checked. Then hit “Render.”

Once the render is complete, decide if you need to increase the quality of the shadows before going to Photoshop to tweak any errors. You can do that by going to Rendering > Render Setup and opening the “Renderer” tab. Here you can tweak things like light sampling and noise thresholds. You could also go back to your light source and tweak its settings.

The end result is a custom emission map that reflects the lighting in your 3ds Max scene:

Then you just bring all your assets and textures into Unity and plug them into the Standard shader (I used the specular version). Here’s a quick video of how the emission lighting method compares to a spotlight within Unity:

In the example at the end of the video you can see some of the benefits of this method. Instead of lightmapping the entire hallway we can simulate lightmapping on a single asset that looks reasonably good even when duplicated in several places around the scene. This works particularly well when the light source is attached to the object that you want to have simulated direct lighting. The lighting never needs to be rebuilt and the resolution of the emission map can be scaled smaller than your other texture maps if needed. The emissive properties can also be easily changed at runtime to create effects like flickering or colored lights. The emission coming from our transmissive plastic also contributes to the scene's lighting via Unity's precomputed realtime Global Illumination.

Lacuna Passage - Devlog #6 - Using highly detailed 3D components in your first-person HUD

This week’s devlog is a bit more of a tutorial where we cover our method for creating dynamic, highly detailed 3D HUD elements. The basis for our pipeline involves rendering complex high-poly models onto flat, single polygon planes with a normal map for light interactions.

helmet_in_game.jpg

Pros of this method

  • You effectively have an unlimited polycount for your HUD. By rendering your high poly model to a flat plane you will have no noticeable polygonal curves or sharp edges (if you don’t want them, that is)
  • Extremely low poly count in relation to the quality of the illusion

Cons of this method

  • A change in HUD design requires re-rendering of all your texture bakes
  • The HUD cannot move in relation to the camera or the illusion breaks

So, let’s get started:

1. Setting up your camera angle

In order to model your HUD components you need to setup a camera in your modeling software that is exactly the same field of view as your first-person camera in your game engine. This will help you preview what your HUD will look like in game. It’s also going to be very important later when we need to render out a normal map.

2. Build your model according to the view you want in game

This part just comes down to whatever style you are looking to have with your particular game. In our case we are creating a small visible portion on the inside of a space helmet.

camera_plane.gif

3. Create a plane to project your model onto

Once your model is finished (no need to texture it yet) you need to create a plane that completely encompasses your model from the point of view of your first-person camera.

camera_plane_orientation.gif

4. Orient the plane to your camera angle

Make sure that the plane is exactly perpendicular to the origin point of the camera. Check the image to see a visual.

5. Quickly unwrap your plane

No frills here. Just unwrap the plane however you like.

camera_plane_normals.gif

6. Render your normal map

Use Render to Texture with a projection modifier on your plane that encompasses your model to create a normal map of the original model. If you’ve never done a normal map bake from a high poly model before check out this guide.

textures_helmet.jpg

7. Create your textures

You can create your textures in Photoshop or texture your original model and do another render to texture for the diffuse instead of the normal map.

8. Create an alpha mask for the model’s texture

Since our projection shape does not perfectly match our plane we need to have an alpha channel somewhere that determines areas of transparency.

dustdevil2.gif

9. Import your plane and your textures

Bring everything into your game engine and align the plane exactly as you had it in your camera preview in your modeling software. In our case we are using Unity so here are some tips to help you out.

Unity specific directions:

  • Have your plane parented to a HUD camera that is distinct from your scene camera
  • Have the HUD camera render last and mask only the HUD elements
  • Make sure the clear flags are set to Depth Only to preserve your depth buffer
  • Use the same HDR settings on all cameras if you are using image effects
So, there you have it. If you have any questions feel free to leave them in the comments.