Search Unity

[WIP] Sprite Lights

Discussion in 'Works In Progress - Archive' started by Elecman, May 12, 2015.

  1. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    After seeing Aily's excellent TurboForest, I had the idea to the same with lights. Because all billboards are part of the same mesh, you can render a huge amount (21844) in one batch.

    The lights don't actually light other objects and it needs a good bloom shader (such as Sonic Ether's) but the aim is to make the lights itself look realistic.

    The funny thing is that there are thousands of references available on how a light affects an object. But the amount of references available on how the light itself looks you can count on one hand. I once found a scientific paper, but that's about it. Perhaps that is why very few people get it right. Often you see an emissive sphere with a flare sprite slapped on top of it. But that is a far cry from a physically based approach, which I will describe here.

    Most lights have a lens, which makes them either highly directional like a flashlight, or horizontally directional, the result of a cylindrical Fresnel lens. This directional behavior is simulated with a phase function which shows nicely on a polar graph. Here you can see two common light radiation patterns:

    polar.jpg

    The blue graph has the function 1 + cos(theta*2) where theta is the angle between the light normal and the vector from the light to the camera. The output of the function is the irradiance. Adding this to the shader gives the lights a nice angular effect.

    lobe.jpg

    Next is the attenuation. Contrary to popular belief, focused lights (in the extreme case, lasers) still attenuate with the inverse square law, as described here:
    http://www.quora.com/Is-the-light-f...distance-grows-similar-to-other-light-sources

    But contrary to even popular scientific belief, lights themselves don't behave in quite the same way, or at least not perceptually. The inverse square law states that the intensity is inversely proportional to the square of the distance. Because of this:



    You see this reference all over, for example here:

    inversesquare.jpg

    Yet the light itself is brighter than bar number 4, which is about at the same distance as the light to the camera. The light itself doesn't seem to attenuate with the inverse square law. So why is this? Turns out that in order to model high gain light sources (such as directional lights), you need to place the source location far behind the actual source location. Then you can apply the inverse square law like this:



    Note that highly directional lights have a very flat attenuation curve, which can be approximated with a linear function if needed in order to save GPU cycles.

    Some more reading about the subject here (chapter Validity of the Inverse Square Law):
    http://blazelabs.com/f-u-photons.asp

    One other problem is that the light will disappear if it gets too far from the camera. This is the result of the light being smaller than one pixel. That is fine for normal objects but not for lights because even extremely distant or small lights are easily visible in real life, for example a star. It would be nice if we would have a programmable rasterizer, but so far no luck. Instead, I scale the lights up when they are smaller than one pixel, so they remain the same screen size. Together with the attenuation, this gives a very realistic effect. And all of this is done in the shader so it is very fast, about 0.4 ms for 10.000 lights on a 780ti.

    Since I made this system for a flight simulator, I included some specific lights you find in aviation, like walking strobe lights (also done entirely in the shader):

    strobe.gif

    And PAPI lights, which are a bit of a corner case. They radiate light in a split pattern like this (used by pilots to see if they are high or low on the approach):



    Simulated here, also entirely in the shader.

    papi.gif

    Normally there are only 4 of these lights in a row, but here are 10.000, just for the fun of it. They have a small transition where the colors are blended (just like in real life), which you won't find in any simulator product, even multi million dollar professional simulators. That's a simple lerp() by the way.

    I should also note that the shaders don't use any conditional if-else statements but use lerp, clamp, and scaling trickery instead. So it plays nice even on low-end hardware.

    Edit:
    Released for free, but with no support:
    https://www.assetstore.unity3d.com/en/#!/content/46409

    WebGL:
    https://googledrive.com/host/0Bwk4bDWv3jAcUkZBc2dNQ0RNT2M
    Controls: same as in the editor.
    Speed up/slow down: 1 and 2 (not numpad)
    Bloom is SE natural bloom.
    It looks best in fullscreen.
    Note that the frame rate is much higher in a standalone build.
     
    Last edited: Oct 7, 2015
    CubeXYZ, hopeful, garrido86 and 6 others like this.
  2. Fera_KM

    Fera_KM

    Joined:
    Nov 7, 2013
    Posts:
    307
    It looks good and I can see uses for this, if I understand it correctly, this is a shader?
    The color blend, is that temperature you are talking about?
     
  3. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Yes, it is a shader. The color blend is just lerping between two colors.
     
  4. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Here is a use case. A city and airport at night. Note the frame rate. These are 65000 lights, but only a few game objects.

    City 2.png

    The street lights are build automatically using Open Street Map data.
     
    Last edited: Jul 27, 2015
    hopeful and Reanimate_L like this.
  5. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    That's interesting, are those light static or all of them can move?
     
  6. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    They are static but in theory they can be animated in the vertex shader. I had an idea to do this in order to simulate moving cars on the roads.
     
  7. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Very nice, would definitely be interested in this.
     
  8. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    I will release it sometime soon on the Asset Store.
     
  9. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    garrido86 likes this.
  10. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    Wow, exactly what I want. I purchased Turbo Forest for the same lightning purpose. But, u just make it onto another level. When will u release it, seriously can't wait? :)
     
  11. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    It's finished already, I just have to upload it to the Asset store. Didn't get around it yet.
     
    ikemen_blueD likes this.
  12. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    So, the shader has a Masking support? So, I can arrange the spread lights.

    I second this. So, how can the lights following the road paths? is it random movements or controlled by some kind of paths?
     
  13. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    There is no masking support in the shader but you can implement some masking logic when building the light mesh. For example, when I build the city lights (using the Mapity Asset), I just place a light every 60 meters between the road nodes. If you don't want certain areas to have lights, you can just skip over that and not add any lights in that area.

    This will have to be done in the shader (unlike the masking) for performance reasons. But I haven't figured out how to do that yet. I think this will be quite complex though. Will look into it at some later time but no promises.
     
    ikemen_blueD likes this.
  14. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Submitted to the asset store.
     
    Last edited: Aug 15, 2015
    John-G likes this.
  15. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    is your Sprite Lights approved yet? seriously can't wait ><
     
  16. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Still waiting. Apparently an initial submit will take at least 5 days to get it approved.
     
  17. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Declined.

    I am absolutely speechless.
     
  18. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Wow that's unreal.
     
  19. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Yeah, I can't believe it either. I mean, since when is prior work a requirement?
     
  20. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    really? Unity really? >< Is there another way I can purchase your Sprite Lights?
     
  21. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Don't worry, it will be approved once I comply with their BS.

    Even Apple, the overlord of control, didn't ask me for my prior credentials on my first App Store submit. The only reason for this nonsense I can think about is that I made the submit from Vietnam.
     
    John-G likes this.
  22. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    Ok I see. I understand their Piracy policy. But, since this is a shader asset, I hope they could bend their rules a bit. I'm glad u decided to provide a publisher page, some sort. Yay, now, I'm just waiting for the good news.
     
  23. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    I understand they want to counter piracy, but I fail to see how my asset might hurt them. If they think I got it from somewhere else, all they need to do is do a quick google search. But I guess that was too much to ask.

    Either way, I am lucky I do actually have prior credentials. But what about people new to this, so they can't publish anything. It is just beyond absurdity.
     
  24. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Hopefully it's sorted soon, at least once you have your Dev page up and asset submitted further submissions won't have the same issue.
    Ready to purchase when it's finally up. :)
     
  25. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    John-G likes this.
  26. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Still no news. I have sent them a letter of complaint. Really bad that some Unity staff have to sour the experience.

    I still feel like I am in the twilight zone. Citing "piracy" when I bought both Unity 4 and 5, purely to support its development, because I don't need it. Not to mention the hard Unity work I released for free here, here, and here among others. Go figure.

    I complied with their "requirement" yesterday already and now it is weekend. The people I am dealing with seem to be unreasonable and didn't do any research prior to hitting the reject button. So who knows how long this is going to take.

    In the mean time, if you want to buy Sprite Lights now, send me a PM.
     
    Last edited: Aug 21, 2015
  27. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    Hey, though I believe there is no way Unity would refuse your cool shader. So, I would wait up to 2 working days, up to Tuesday, for their final response on your shader man. If Unity refuses it ( which is unbelievable to me), I will contact to u through mail man. I hope it is not that case. Since if you could sell your shader on AS, more sales, more users, more feedback, the shader is going to grow well with it. Cheers man. Have a nice weekend!
     
  28. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    No problem. It should all work out as they didn't complain about the software but some other un-documented requirement.
     
    ikemen_blueD likes this.
  29. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    By the way, what Unity version do you want me to support? I didn't bother making a unitypackage for a lower version but the latest one. Let me know if you want me to support a lower version.
     
  30. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    hey, for me, Unity latest version works best for me. Some simple Animation in-place ( slightly moving L/R/U/D) is very awesome if u could implement it.
     
  31. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Ok, thanks. Will have a look at it.
     
  32. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
  33. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    wow, it still takes for a while for them to check your page. I hope we have good news soon. Btw, yeah they should inform their new requirements, that would save their and publisher's time & effort.
     
  34. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Yeah, I know. am going to reset the upload and maybe try another time later.
     
    ikemen_blueD likes this.
  35. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    is there still no news yet? ;)
     
  36. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    John-G and ikemen_blueD like this.
  37. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    hey, thanks for sharing us such a cool project. The actual game scene is much better from the screenshots here. I'll look more into it, when I'm done with my AI system. It's a pity that Unity does not approve such an unique asset like this, so that it can grow much more than its state. Anyway, thanks again and have a nice day!!!
     
  38. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    No problem. I will see if I can make a webplayer version, that does it more justice than just screenshots.

    Yeah, Unity has some serious issues in the Asset Store department. They are understaffed, have an attitude, or both, I am not sure. Anyway, I am glad I don't have to deal with them anymore.
     
  39. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    I uploaded a WebGL demo:
    https://googledrive.com/host/0Bwk4bDWv3jAcUkZBc2dNQ0RNT2M

    Controls: same as in the editor.
    Speed up/slow down: 1 and 2 (not numpad)
    Bloom is SE natural bloom.
    It looks best in fullscreen.
    Note that the frame rate is much higher in a standalone build.

    See if you can find the big green/red square of directional lights.
     
    Last edited: Oct 3, 2015
    ikemen_blueD and John-G like this.
  40. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,684
  41. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
    @Elecman , hey I'm glad your asset is finally approved. Are you planning to have a version, with support later?
     
  42. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Yeah, submitting the entire project again worked.

    It is unlikely I will add a version with support later as I am up to my ears in work for the foreseeable future. No support does not mean absolutely no support though. Just support with no promises. If something doesn't work I will have a look. And if the manual isn't clear I will try to explain it a bit better.
     
    ikemen_blueD likes this.
  43. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,762
    What a great system, love things like this that really pay attention to the detail.
     
    hopeful likes this.
  44. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372
    Thanks :)
     
  45. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,372