Search Unity

[SOLVED] Can I use a realtime light (flashlight) with a baked static lightmap?

Discussion in 'Global Illumination' started by oatssss, Jun 17, 2015.

  1. oatssss

    oatssss

    Joined:
    Dec 20, 2014
    Posts:
    26
    I'd like to incorporate a flashlight in my project. Everything in my scene is static except the player and flashlight and it's looking great with baked lightmaps, however, I've found that realtime lights have no effect on static gameobjects.

    In the image below, the room is static with a baked lightmap but is not being lit by the realtime spotlight, however the dynamic sphere is lit. I would like the room to also be affected by the spotlight.
    Screen Shot 2015-06-17 at 4.15.04 AM.png

    Am I able to keep the baked lightmaps and give my flashlight the ability to light up the darker areas in my static environment? If not, is there a workaround that achieves something similar with little performance hit?

    Edit:
    The problem I was having was due to the quality settings I was using for my project. I had Pixel Lights set to 0 allowed. Increasing it solved the problem as well as using the standard or legacy diffuse shaders.
     
    Last edited: Jun 19, 2015
  2. SpiriTx

    SpiriTx

    Graphics QA

    Joined:
    Apr 12, 2012
    Posts:
    252
    It should work fine.

    Could you please upload this scene using a bug reporter? I'd take a look
     
  3. oatssss

    oatssss

    Joined:
    Dec 20, 2014
    Posts:
    26
    Hey, thanks for getting to me. I forgot to mention that I'm using forward rendering if that matters. I have a feeling it's simply user error and I accidentally toggled something since I'm really new to unity.

    I uploaded using Unity's Bug Reporter. This is the link it gave me
    http://fogbugz.unity3d.com/default.asp?705309_75g3u28sttjb86h0

    The bug reporter told me it was 6.6gb, but my system says it's only 2gb. Apologies if it's big for a bug report.
    Thanks again!
     
  4. SpiriTx

    SpiriTx

    Graphics QA

    Joined:
    Apr 12, 2012
    Posts:
    252
    That ground mesh uses material with Mobile/Diffuse shader, which doesn't support per-pixel lights, only vertex ones. Use Diffuse or Standard instead
     
  5. oatssss

    oatssss

    Joined:
    Dec 20, 2014
    Posts:
    26
    I tried switching the shader to standard, then rebaking and nothing changed. In the image I have the room selected, and you can see that the shader is set to standard, but still no light.
    Screen Shot 2015-06-18 at 2.50.31 PM.png
     
  6. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    You'd need to create your own shader for this, Lightmap is applied after the spotlight and is multiplied, so it will wipe it out. If you want to have a try at it yourself I would be happy to give you some guidance, as I'm sure other people would. Also you should probably base it loosly on the Mobile shader if you want it for Mobiles as it will perform better.
     
  7. oatssss

    oatssss

    Joined:
    Dec 20, 2014
    Posts:
    26
    Hmm okay. I've never attempted to write my own shader, but I like a challenge so I'll give it a shot if you can point in the direction of good resources. Thanks for the help!

    It seems weird to me that there's no shader that can do this out of the box. I imagine what I'm trying to do is pretty standard.
     
  8. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Download Unity shader source from here:

    http://unity3d.com/get-unity/download/archive

    Then dig in, start with the Mobile Diffuse, if you click on the source file in Unity, you can then click on Show Generated Code, this will load up the actual fragment and vertex code, from there it will generate code for Deferred and everything! But you can ignore all but the forward base, that's all you are interested in.

    Then you can refer to this:

    http://docs.unity3d.com/Manual/ShadersOverview.html

    And this for the HLSL code:

    https://msdn.microsoft.com/en-us/library/windows/desktop/bb509638(v=vs.85).aspx

    For how to do spotlights have a dig around the cginc files you'll see the code in there, if you get it started and get to grips with the basics, I can guide you the rest of the way.
     
  9. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Of and out of the Box, to be honest this is a little bit specialised, not general case. As you can imagine Unity supporting every single shader use case would make it very complex and give you millions of shaders!
     
  10. oatssss

    oatssss

    Joined:
    Dec 20, 2014
    Posts:
    26
    If you're saying that trying to use realtime lights to light up a room that is also statically lit is a special case, then I have to disagree. Having to choose between only realtime or baked for an environment seems pretty restricting to me. Not to mention that it's also intuitive to just throw in a realtime light and expect it to work on all objects, not just dynamic ones.

    I'll take a look at those links and hopefully be able to grasp them. Thanks so much.
     
  11. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Oh one last thing, if you get really stuck, I'll be back on line in about 13 hours (it's 8pm in the UK!) and I have to write a shader that does this for my own game, so I could just post the source for the mobile version, I could probably find time to write it in the morning.
     
  12. oatssss

    oatssss

    Joined:
    Dec 20, 2014
    Posts:
    26
    If you could post yours, that would be amazing!
     
  13. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Your looking at it the wrong way, for most people they want baked objects to only be lit by the baked lighting and have realtime shadows on then, but your wanting the opposite, baked darkness then with lighting on top, it completely changes the way lighting would work for most scenarios.
     
  14. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    No problem, I'll probably post something in the morning UK time.
     
  15. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Oh, forgot to ask, for Mobiles presumably?
     
  16. oatssss

    oatssss

    Joined:
    Dec 20, 2014
    Posts:
    26
    Ah, I see what you mean. Now that seems counter-intuitive to me; if something says light, I think light, not shadow. It definitely sounds easier to get it working the way I need it so it's probably better that Unity has it the other way around. I look forward to your next posts :D

    I'm doing it for mobile, but don't go out of your way to rewrite if you don't need it.
     
  17. oatssss

    oatssss

    Joined:
    Dec 20, 2014
    Posts:
    26
    Oh hey, it turns out that the reason it wasn't showing up was because the quality setting my project was set too was too low. I also require per pixel lights so I'm using the standard shader. I'm gonna gauge if it's too costly performance wise.
     
  18. SpiriTx

    SpiriTx

    Graphics QA

    Joined:
    Apr 12, 2012
    Posts:
    252
    Forgot to mention, your default quality settings in editor are fastest, which means 0 pixel lights, that's why it's rendered as vertex.
    So you don't actually need to write any custom shaders, just change the quality settings and change shader.

    http://screencast.com/t/RSZA4LsVR
     
    dimmduh1 likes this.
  19. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Ah, interesting, of course being a spotlight it should be rendered Additive, standard shader will probably be too costly, if it is try the Legacy Diffuse, you can always get the Mobile Diffuse to work, but it would need some modifying to do so I think!
     
  20. oatssss

    oatssss

    Joined:
    Dec 20, 2014
    Posts:
    26
    Thanks for the help! I got everything working the way I wanted it :)