Kemal Akay

Mixed Lighting in Unity 5.6 (Part I)

Unity3D

10 Yorum


Share this post

In Unity 5, lighting has taken a huge step forward. It is now possible to create highly realistic games. Adam demo is just one of the examples that proves the concept. As mentioned in the first post, however, many games developed by Unity rely on baked lighting, mostly due to performance reasons. But sometimes the use of realtime and baked lighting is necessary and previously, this is where Unity fell short. That is no longer the case. In 5.6, Unity addresses the issues raised by users.

Lighting features can be a mystery for beginner users as lighting UI is not manifested in the editor by default. And our observations showed that almost none of the beginner users have an idea about lightmapping concept. We refactored the UI to make that clear, but simply, some concepts just needs to be learned. In the first part of this serie, I will initially (and briefly) explain the difference between realtime lighting and baked lighting (lightmapping) concepts. Then, I will explain what mixed lighting is (currently, we don’t have any page in the manual that properly explains it). If you are already familiar with these concepts, feel free to jump to part II.

What is Precomputed Realtime GI and Baked GI Lighting?

First of all, new light modes in Unity don’t contribute anything new to precomputed realtime GI lighting, instead it offers new ways for users to apply realtime and baked lighting concurrently in a scene. Realtime lighting is easy to understand. You can just play with your lights and it will instantly update all the lighting in your scene. Easy, right? The only problem is it comes with a cost. Everytime you actively update your lights, it will be expensive in terms of performance. That’s why Unity also has the lightmapping concept. You can bake the lighting and store all the lighting data as lightmaps in your project. It also costs less because essentially, it’s just a texture.

In baked lighting, there is one major difference than realtime lighting: They are all static and you can’t move your lightmap static objects, otherwise they will look fake and wrong. Also, making your gameobjects lightmap static is easy. You just need to mark them as static (or just lightmap static) in the inspector. This will ensure that Unity bakes lightmaps for that object. You don’t need to do this if you want to keep your objects dynamic, by default they are already dynamic.

The detailed description of these concepts are beyond the scope of this post and we already have a great tutorial series in unity3d.com prepared by David Llwelyn and lighting team to explain the optimized techniques to use precomputed realtime GI lighting in Unity.

In addition to that, we recently updated Courtyard demo in AssetStore to make it compatible with the latest stable version of Unity 5.5. You can also check that project to see how you can use realtime lighting in your projects.

Direct and Indirect Lights

Another important concept that many beginners don’t think about is the difference between direct and indirect lighting. Similar to realtime lighting, direct light concept is fairly easy to understand. It’s the photon rays from a light source that directly affect an object. It’s what you explicitly see.

What is relatively hard to understand is the concept of indirect illumination. Basically, indirect illumination is achieved through bounced lights. Besides the direct lighting, we can also have bounce lighting. The light photons simply bounce around in a scene based on mathematical equations. Following chart and GIF depicts how indirect illumination works:

You can set the indirect contribution of a light through “Bounce Intensity” parameter in the light component. For instance, if you want to disable its indirect contribution, you can simply set it to zero.

So precomputed realtime GI is fun and works perfectly well in Unity. But it’s very expensive and Unity is mostly used by developers who target low end devices (mobile) and requires optimized scene setups due to performance reasons (VR). Then, you’d obviously like to bake all the lighting for your static objects (e.g. environment) and use realtime lighting for your dynamic objects (e.g. characters). So this brings us to next question:

What is Mixed lighting?

The term itself is quite self-explanatory. Mixed lighting allows users to mix realtime and baked lighting. The use case we described above is a typical scenario where most of the games use. In Last of Us, for instance, the environment doesn’t move therefore everything is stored in lightmaps, whereas all the moving objects use realtime lighting.

You can think that you can first bake all the environment, and then use realtime lighting for your characters. This, however, introduces a critical problem: All your baked objects will receive the dynamic light as well and this means they will be exposed to double exposure, creating overbrightness for these objects. Of course, you can mask realtime lights for baked objects but then, you can easily end up with discrepancies in terms of brightness values. Following image shows a case that suffers from this issue. Same geometry with the same material applied but one of them is dynamic, other one is static:

This is perhaps acceptable but there are cases where it may definitely hurt some eyes:

So that’s where mixed mode comes in. Ideally, mixed mode takes care of all this hassle for you and illuminates dynamic and static objects within the same balanced range (hence retains visual fidelity and consistency).

In Unity 5.5, you can switch your light type to mixed in inspector in order to use mixed lighting.

Et voila! You can now safely use mixed lighting for dynamic and static objects. Wait…why my dynamic objects don’t cast any shadows when I use mixed spotlight or point light?

(@caosdoar wondering why mixed spotlight doesn’t cast any shadow in Unity forum)

In fact, there so many other posts and complains about this issue that this is just one of them. But let me put it straight forward: It’s not working because mixed lighting only works with one directional light. Wait, what?

Subtractive Mode

This is the main reason why new light modes are introduced in Unity, basically to address this issue. In total, you will see that there are four light modes you can choose in the lighting window. These are baked indirect, distance shadowmask, shadowmask and subtractive. At the beginning of the post, we mentioned that there are only three new light modes added. This is because subtractive mode is the old mixed mode and we still keep it because it’s cheaper in terms of performance compared to other modes, which still proves that it’s useful when you are developing for, say, mobile.

So let’s rephrase how this mode works with a bit more technical words in Unity: Selecting the ‘Mixed’ baking mode, GameObjects marked as static will store lighting information from mixed light as lightmaps. However, unlike lights marked as ‘Baked’, Mixed lights will also contribute direct light (in realtime) to non-static (dynamic) GameObjects within your scene. Did we mention it only works with a single directional light? Good, because mixed point and spotlight don’t cast any shadow on objects.

In addition, there is one more feature specific to subtractive mode. Unlike other modes, direct light is also baked therefore there is no specularity in this light mode.

Probe Occlusion

In subtractive mode, another important component that must be used for dynamic objects is light probe groups. Without light probes, dynamic objects can’t sample the lighting used for static objects and most notably, they can’t use the occlusion data. So in order to receive shadows, dynamic objects need to blend the lighting from light probes. In the following visuals, you can see how a dynamic object uses light probes for occlusion:

For achieving more precise occlusion, your important objects (e.g. main character) in the scene can have light probe proxy volumes (LPPVs) assigned. Note though, that they come with additional performance cost.

Few words about Color Space and Rendering Path in Subtractive

There is one last thing to mention about Subtractive mode. This mode is designed and kept for forward rendering path and gamma color space. It is not meant to be used for deferred rendering path and linear color space. It is because subtractive mode is relatively cheap compared to other modes and the use of this mode is targeted for low end devices such as mobile platform. For more details about rendering paths and color space, you can find information in the official manual.

In conclusion, we covered how subtractive mode can be used in practise in Unity 5.6. In the next part, we will talk about new light modes (Distance Shadowmask, Shadowmask and Baked Indirect) and how they can be used in your projects.

this post için 10 cevap
  1. Posted on 20 Şubat 2017 by Ogaudreau

    there’s a couples of things i’m not able to fully grasp in the beta so far, maybe you have experienced it. I’m in deferred rendering making a game where shader’s illumination plays a huge role in terms of gameplay as well as visual unifications.

    We’ve updgraded our project being excited by the mixed light coming back as distance shadowmask, so far we’ve saved a lot of performance with it and where able to activate shadows on every lights we needed, even on point light and still hit the 60 fps mark. That’s a huge visual improvement !

    But for some reason i’m unable to get the proper feel i had before with the emissive shaders. Our environement is heavily dependant on that (think of an avatar-esque forest), and for reasons unknown i’m unable to make more than 2 emissives material react on my terrain or any other static assets. Only 2 material get ‘”chosen” to be emissive, even tho in the various debug view of the scene pannel i can clearly see unity get all emissive geo. All my emissive assets are flagged to lightmap static of course and Prioritize illumination. I’ve even tried to fake the illumination with highly emissive objects, flagged not to render on the camera so i could fake and control the result, but still only 2 material/color maximum seems to be working.

    Since it’s not the objects setup (which are all the same, nor materials only 1 shader is used) i can’t find anything on the documentation regarding a limit of emissive object, is this related to the distance shadowmask?? so far every mode does that and i’m can’t find an answer :( Thx for the reading your articles are amazing. Hope you’ve got insight on this ! Cheers

    • Posted on 20 Şubat 2017 by admin

      Hey @Ogaudreau

      Glad to hear that you enjoyed articles :)

      Regarding your question. Let me first ask you this: Have you deleted your Library folder and cleared your GI cache after the upgrade? Some files from previous versions of Unity can interfere with your upgraded project. If you already did this, then it’s all good.

      There is no information added about the limitation of emissive materials in the documentation because practically, there is none.

      Now I also assume that you set your emissive materials to baked besides marking your game objects as lightmap static? What you describe is really odd and when I tried in a small scene, it works as intended (https://s9.postimg.org/cbpavkinz/emissives.png) with default settings (baked emissives in distance shadowmask mode).

      So I think you may be encountering a corner case issue and that could very well be a bug. Do you perhaps have a small reproducible project? Or have you already reported this as a bug? It is worth checking your assets.

      Thanks again and please keep me updated

  2. Posted on 22 Şubat 2017 by Ogaudreau

    Thanks a lot for the input !
    I’ve tried everything quickly, and forgot to mention that I knew it was related to our project, since new project dosen’t do it (even when importing our scene as a package). I’ll try to put a reproductible project on a bug report, but so far I’ll have to make some more test after PAX east ;) . Our lighting is good enough and it’s only really annoying me, but being the only 3D tech artist on the projet i need to shift my priority for now ;) . I’ll be posting my observation for sure as soon as i find out what caused this issue !
    Cheers and thanks again !

    • Posted on 28 Şubat 2017 by admin

      Hey @Ogaudreau

      So I’m glad that you found my post helpful and good luck at PAX East, I wish you all the best. Perhaps you can share your game link here? I’d be interested to check it out myself :) And keep me updated about the status of your issue and if you can create a small reproducible, please report it and post the bug case ID here so I can take a look at it and investigate with devs ;)

      Cheers!

  3. Posted on 07 Mart 2017 by Ogaudreau

    Hello There ! thx a lot for everything ! I’ve just updated to beta 10 to test it out and it’s now working properly ! :D However there’s still a little issue probably related to my cluster size or some other settings i’m not sure yet. Small assets (approximately below 1x1x1 meters) won’t cast their illum on the ground, if it’s not something i can adjust somewhere i’ll just bypass this and fake it with bigger illum object flagged not to render on camera ;) . Thx a lot for your help !

    We’ve just announced our project yesterday, so yeah i’m able to share it with you ! :) Note that the lighting and terrain shader were still broken without beta 10 when we camcorded ;) The visual quality will greatly improve right after PAX haha. Here our teaser Feel free to send us your input sir ! we’re still pre-alpha we’ll release more with gameplay video soon enough ! cheers !

    https://www.youtube.com/watch?v=twvWOSRZV-4

    http://www.dualshockers.com/2017/03/06/3v3-team-based-skirmisher-aftercharge-announced-gets-explosive-reveal-trailer-screenshots/

    • Posted on 20 Mart 2017 by admin

      Hey @Ogaudreau,

      I’m really glad to hear that everything is working as expected, that’s very good news :) Well, the proper way would be to increase the indirect resolution (you can also assign custom lightmap parameters per object and adjust the settings locally) but that may affect the performance so use whatever solution works the best for you. Under production environment, it’s usually hard to find the balance between optimal solution and best practices ;)

      I watched your gameplay trailer and dropped a comment. I really liked the art style and I’m pretty sure it’ll stand out even more when all the emissives are working correctly :) I also liked the way you incorporated the shaders! One feedback could actually be about the silhouettes of your characters, it’s in fact hard to judge without playing it but as far as I can see, identifying the characters/enemies can be visually struggling given the lack of contrast with background environment. But again, it’s something I observed in the trailer, otherwise it’s an awesome job, keep up the good work and please keep us updated, I’ll be looking forward to it!

  4. Posted on 08 Mart 2017 by Emile

    Thanks for this article. A bit technical but learning. Since upgrading to Unity 5.6.0b10 I noticed a few issues. Any advice appreciated:
    a) I use Gaia & Aquas –> After upgrade the Caustics are no longer displayed on my terrain underwater
    b) I see some objects with flickering (z-fighting?)
    c) The terrain underwater (has a distinct) seam where the sand on one side is light and dark on the other
    d) The scene in general seems a bit ‘washed-out’ too much white
    My Game (pre-alpha) is here: https://mafuta.itch.io/escape-from-pirate-island
    I have tried various manual & auto baking options but can’t seem to get it quite right.

    • Posted on 20 Mart 2017 by admin

      Hey @Emilie,

      Glad to hear that you’re learning new stuff from these articles, please ask if anything is unclear. So, it’s really hard to identify the issue based on the information you provided. It could very well be a problem related to external assets you use. I suggest you to create a small reproducible project of the issue you are encountering and report it through Bug Reporter. Unity this guide to being an effective beta tester. Please report it and you can post bug ID here so I can inform related parties at Unity and even take a look at it myself. I hope this helps.

      Thanks!

  5. Posted on 21 Temmuz 2017 by Anh Le

    Hi,

    So I cannot use Subtractive mode together with Linear Color Space?

    • Posted on 23 Temmuz 2017 by admin

      Hi @Anh Le
      I think I wasn’t clear enough in that paragraph. Yes, you can use it definitely. I just wanted to emphasize that subtractive is the ideal mode targeted for low-end hardware, that doesn’t support deferred, linear color space or SM3.0

Add your comment