Search Unity

Directional lightmaps

Discussion in 'Unity 5 Pre-order Beta' started by robert, Nov 17, 2014.

  1. robert

    robert

    Unity Technologies

    Joined:
    Dec 21, 2008
    Posts:
    263
    Hey everyone,

    Beta 13 includes quite a few improvements to directional lightmaps. Here's a quick overview of the feature.

    Directional lightmaps store more information about the lighting environment than regular lightmaps. Shaders can use that extra data about incoming light to better calculate outgoing light, which is how materials appear on the screen. This happens at the cost of increased texture memory usage and shading time.

    You can choose one of 3 modes: Non-directional, Directional and Directional with Specular. All 3 are available as realtime and baked lightmaps.

    Non-directional: flat diffuse. This mode uses just a single lightmap, storing information about how much light does the surface emit, assuming it's purely diffuse. Objects lit this way will appear flat (normalmaps won't be used) and diffuse (even if the material is specular), but otherwise will be lit correctly. These barrels are using baked lightmaps. The only detail definition comes from a reflection probe and an occlusion map.

    dir0.png


    Directional: normalmapped diffuse. This mode adds a secondary lightmap, which stores the incoming dominant light direction and a factor proportional to how much light in the first lightmap is the result of light coming in along the dominant direction. The rest is then assumed to come uniformly from the entire hemisphere. That information allows the material to be normalmapped, but it will still appear purely diffuse.

    dir1.png


    Directional with Specular: full shading. Like the previous mode, this one uses two lightmaps: light and direction, but this time they're split in halves. Left side stores direct light, right - indirect. Unlike the two other modes, light is stored as incoming intensity. That extra information allows the shader to run the same BRDF that's usually reserved for realtime lights, resulting in a full-featured material appearance - now also interacting with indirect light.

    dir2.png


    Performance: the Directional mode uses twice as much texture memory as Non-directional lightmaps and has a slightly higher shading cost. Directional with Specular uses twice as much texture memory as Directional (it also uses two textures per set, but they're split in two, so twice as many sets are needed). It's shading cost is comparable to two un-shadowed lights, as it runs the BRDF twice - for direct and indirect light.
    Non-directional: 1 texture, 1 texture sample, a few extra shader instructions.
    Directional: 2 textures, 2 texture samples, a few more extra shader instructions.
    Directional with Specular: 2 textures, 4 texture samples, high extra shader cost (~two un-shadowed lights).

    Realtime lightmaps take advantage of the same approach, and are subject to the same shading quality/cost tradeoffs. The only difference from the list above is that the Realtime Directional with Specular lightmap uses 3 textures (incoming intensity, direction, normal), taking 3 texture samples. It has a shading cost of one un-shadowed light, as it runs the BRDF only once. It works in combination with a realtime direct light, of course, which has it's own shading cost.

    The BRDF that is actually used for indirect light (Directional with Specular realtime and the indirect part of baked) is a slightly less expensive version. UNITY_BRDF_PBS_LIGHTMAP_INDIRECT is defined in UnityPBSLighting.cginc.

    The Directional with Specular mode pushes the lightmapper to the limits of the GI technique it uses, which in some cases leads to artifacts. These are most often patches of incorrect dominant light direction, causing dark or black shading. Some of these issues can be fixed increasing the Realtime/Indirect Resolution, others will be fixed by us before the release. If all else fails, the simpler lightmap modes are more robust.
     
    bugsbun, Meltdown, Garrettec and 10 others like this.
  2. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,374
    Why this thread not stickied??
     
    carking1996 likes this.
  3. robert

    robert

    Unity Technologies

    Joined:
    Dec 21, 2008
    Posts:
    263
    No reason for it to be sticky, I think.
     
  4. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,374
    well honestly, this is a good information for me. And also enlighten are kinda new for most people here. just kinda shame if this thread gone under all of the thread
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    5,879
    Isn't there some overlap here somewhere..?

    Specularity and reflection are in reality, the same thing. Assuming you have everything covered with somewhat proper cubemaps, doesn't adding indirect speculars on top of that redundant or even... wrong?
     
  6. robert

    robert

    Unity Technologies

    Joined:
    Dec 21, 2008
    Posts:
    263
    You are correct that there's no distinction in real life between specularity and reflectiveness. But in realtime graphics they're typically handled separately, for a number of reasons.
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    5,879
    I understand that. For example lightsources might not typically appear in cubemaps, so you need some sort of specularity from those, plus speculars from direct lights will be much more accurate than any cubemap can provide.

    But with directional specular lightmaps, where speculars are generally inaccurate and with the new Unity workflow strongly favouring having a little reflectivity in everything and cubemaps eveywhere, things that look like speculars do appear, so I feel there is some redudancy in doing indirect speculars after that.

    I'm not arguing against the feature, more tools and features are always welcome.
     
  8. robert

    robert

    Unity Technologies

    Joined:
    Dec 21, 2008
    Posts:
    263
    Reflection probes' resolution is per object, at best. That means a specular highlight from a point light will travel with you as you go across a plane, for example. And it wouldn't be shadowed. OTOH directions stored in lightmaps can vary from texel to texel - they're very local.

    But there might be very specific scenes, in which rendering hdr light discs into reflection probes would be preferable, I agree.
     
  9. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    5,879
    Fair enough, I think I understand more now. Thank you.
     
  10. n00body

    n00body

    Joined:
    Dec 17, 2010
    Posts:
    691
    I definitely appreciate this feature, if only so we can use our own BRDF for some of the ambient lighting.
     
  11. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    844
    How do/should the two new Directional Lightmap modes compare (in performance) to the pre-5.x Directional Lightmaps?
     
  12. robert

    robert

    Unity Technologies

    Joined:
    Dec 21, 2008
    Posts:
    263
    4.x directional lightmaps compared to the new "Directional" mode had a slightly higher shading cost and used as much texture memory.
     
  13. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    844
    I assume you mean Directional as in not Specular Directional (I should've been a bit more specific)?
     
  14. robert

    robert

    Unity Technologies

    Joined:
    Dec 21, 2008
    Posts:
    263
  15. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    724
    Hi robert,
    with the realtime lightmaps the unity crew should think about to struct and load lightmaps dynamically.
    Amplify does this with textures in Unity, I think you know already.
     
  16. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,632
    I think the whole streaming thing would be fine for this going forward once Unity gets around to it. I thought about donating a 3D printer to Unity so we could have more Robert's making cool engine features, but sadly it doesn't print his size, and lots of mini Roberts running around would be chaos.

    In any case - nice work. It looks convincing. Be nice to see how it interacts with a moving light.

    The shots above are wrong in the sense that the object in shadow isn't as dark as the floor shadow, but that's probably for a number of reasons.
     
    Last edited: Nov 21, 2014
  17. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    724
    Great feature and work from Robert! He spend so many time in usefull and great features to bring Unity to the top. So we understand that he have a low message quota.
     
  18. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    844
    Super stuff - it's really coming along nicely :)
     
  19. robert

    robert

    Unity Technologies

    Joined:
    Dec 21, 2008
    Posts:
    263
    Ha, ha, thanks guys. :) I hope you'll like it.

    The truth is that we can't store the entire lighting environment per texel. Not even close. And even if we could, we wouldn't be able to use all that data efficiently in the shader.

    So what we did was to look at the whole spectrum of possible approximations, test a number of them on our demo scenes and pick 3. Hopefully each will give you the most bang for the buck, and artifacts or cases in which these methods don't fare well won't give you much trouble.

    Then there are different methods with different trade-offs. For instance, RNM handles some cases better than our "Directional" mode, but only those cases and it's always ~50% more expensive in terms of memory and instruction count. But I'm not aware of methods strictly better than ours. For example something giving as good results as "Directional with Specular" in all cases, but cheaper. Or same performance, but looking better in a wider set of scenarios.

    If you do know of a method that's strictly better than one of those, please let me know :) Or if you're convinced that a different trade-off would be better for most users.
     
  20. pojoih

    pojoih

    Joined:
    Mar 30, 2013
    Posts:
    172
    You can update the official documentation with these infos. Seems to be made for it :)
     
  21. sqallpl

    sqallpl

    Joined:
    Oct 22, 2013
    Posts:
    228
    Im changing modes in runtime and in edit mode too and I don't see any changes in memory usage or vram memory usage in the profiler. Where should I see any increase in memory usage when enabling directional or directional with specular modes?
     
    thempus likes this.
  22. philwinkel

    philwinkel

    Joined:
    Jun 6, 2013
    Posts:
    298
    agree, should be in the docs or something somewhere.

    the "Global Illumination Quickstart" docs haven't been updated since like... beta 6. It's tiring searching these forums for the little gems like this.
     
  23. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    5,104
    hi there,

    i just gave lightmipping a first try (using unity 5 beta 18 on mac os 10.9.5).

    "directional" lightmaps
    using the standard shader (metallic) and "directional" lightmaps static objects receive real time shadows from dynamic objects which is fine.
    although – if you have a closer look at the white, extruded cube – you will see that it gets some artifacts from real time shadows (at the bottom of the cube): it looks as if real time shadows get added on top of baked self shadows, so i guess that gi makes "MixLightmapWithRealtimeAttenuation" fail in the case that gi lightens shadowed faces?

    directional.png


    "directional specular" lightmaps
    if i switch to "directional specular" lightmaps i do not get any real time shadows on static objects which is a greta downer. is that a feature or a bug?
    not supporting real time shadow with directional specular lightmaps makes pbs pretty much worthless in case you have some dynamic objects in your scene.

    directional_specular.png


    lars
     
  24. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    318
  25. giyomu

    giyomu

    Joined:
    Oct 6, 2008
    Posts:
    1,090
    yep that's the mixed light and directional spec bug , happen to me also , it's fine when using directional only.
     
  26. Garrettec

    Garrettec

    Joined:
    Nov 26, 2012
    Posts:
    76
    Excuse me for bothering with such simple question, but ... Is this statement still actual for beta 21? I tried every possible combination of settings in lighting tab, graphics settings and Light object and sitll have only 2 textures for Directional Specular. Here is screen of settings I assume should provide RealTime Directional Specular (but still give only 2 textures with 4 samples...)
    Screenshot_1.jpg Screenshot_1.jpg