Search Unity

Light of Sight - 2D Dynamic Lighting [Open Source]

Discussion in 'Scripting' started by Gamer_Woods, Feb 5, 2015.

  1. Gamer_Woods

    Gamer_Woods

    Joined:
    Jan 31, 2013
    Posts:
    33
    Hi guys, I have developed a Free to use plugin for creating of 2D dynamic lighting.

    Link: https://github.com/f15gdsy/2D-Light-of-Sight-Unity

    Features:
    • Handles any shapes.
    • Event system that lets an object be notified if it gets lit or unlit.
    • Two kinds of shadow generation method available.
    • Works with 2D & 3D physics now!
    • All features are supported in Unity FREE version.

    Demo:

    Hope it helps! :)
    Feel free to use it. Any comments, questions and suggestions are welcome.
    You can refer to the Github page for some advanced tricks!


     
    Last edited: Apr 29, 2015
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,395
    Absolutely amazing, especially the fact that this works on Unity Free, and you\re giving it away completely free! :) Great job!
     
    Gamer_Woods likes this.
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I might just use this. Well done!
     
    Gamer_Woods likes this.
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Couple of questions:

    Does this work by generating a shadow mesh every frame? How much of a performance hit would you say it causes?
    Is it possible to have multiple "light sources"?
    Is it possible (or automatic) to clip shadows? (Say I'm in a small circular room instead of a square one. Will the shadows spill over?)
     
    Gamer_Woods likes this.
  5. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Also, I really like that you put it in it's own namespace. You'd be surprised how many people don't.
     
    angrypenguin and Gamer_Woods like this.
  6. Gamer_Woods

    Gamer_Woods

    Joined:
    Jan 31, 2013
    Posts:
    33
    Thanks for the comment :)
    I understand for students and some indie developers it's a bit too costly and unnecessary to have a Pro license, and especially I'm among them. So I always like plugins that work with Free version.
    For the open source decision, I believe it may benefit me more than tagging a price for it. It's a good way to improve the quality of the plugin, and also a good self introduction to the industry :)
     
    Last edited: Feb 6, 2015
    Sykoo likes this.
  7. Gamer_Woods

    Gamer_Woods

    Joined:
    Jan 31, 2013
    Posts:
    33
    Thanks for the questions:)

    1. It works by generating mesh when necessary. By "necessary", I mean if the scene within the camera is dirty, like the light moved, or the obstacle moved.

    2. Performance wise, I would say it's acceptable, as you can adjust the precision of the lights (by adjusting degree step). Also, you can adjust the frequency of flashes of lights. Those lights offscreen will not be drawn.

    3. Actually there are two types of lights available. One is radial light, that is what you can observe in the radial light (underwater) demo. For this type of lights, you can have multiple lights.
    The other type is full screen light. It generates the mesh in a totally different way. So you can't have more than 1 full screen light. However, with the full screen light, you can make something like visibility area like Monaco: Whate Yours Is Mine (but cannot render things on the shadow texture, that may require unity Pro licence).

    4. For the radial light, it won't. It's bounded by obstacles. For full screen light, the shadow covers every parts that cannot be seen.

    Hope it helps :)
     
  8. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,395
    That is very very clever of you! And I completely understand what you mean with the PRO license, I've been around Unity for 4 years now and developing, yet I think that 1500$ is way too much for me. :) Good job!
     
    Gamer_Woods likes this.
  9. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Very very cool. Thanks for the answers, I'll try it out :D
     
    Gamer_Woods likes this.
  10. Alienguard2000

    Alienguard2000

    Joined:
    Mar 8, 2014
    Posts:
    14
    This I will like to try out. Awesome it looks!
     
    Gamer_Woods likes this.
  11. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I've played with it. This is quite excellent and I'm going to use it for the project I'm working on.

    I extend to you the MADDEST props.
     
    Gamer_Woods likes this.
  12. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Question: I'm turning the functionality on and off throughout the course of the game. Imagine "lights on" or "lights off". When the lights are "off" I'm using the LOS system.

    Here's how I'm doing it now, is this the best (or only) way?

    Code (CSharp):
    1. public class LightSwitch : MonoBehaviour {
    2.  
    3.     static LOSLightBase[] lights;
    4.  
    5.     void Awake() {
    6.         if (lights == null)
    7.             FindLights ();
    8.     }
    9.  
    10.     //Public in case I add lights and want to update all my switches, or something.
    11.     static public void FindLights() {
    12.         lights = GameObject.FindObjectsOfType<LOSLightBase> ();
    13.     }
    14.  
    15.     public void Toggle() {
    16.  
    17.         LOSManager.instance.enabled = !LOSManager.instance.enabled;
    18.  
    19.         foreach (var light in lights) {
    20.  
    21.             light.renderer.enabled = LOSManager.instance.enabled;
    22.  
    23.         }
    24.     }
    25. }
    26.  
    I guess I'm asking if there's already a method for this somewhere, or if I should also disable the colliders(obstacles) or LOSevents if they have some kind of overhead that would be unnecessary with the lights "on".
     
    Gamer_Woods likes this.
  13. Gamer_Woods

    Gamer_Woods

    Joined:
    Jan 31, 2013
    Posts:
    33
    Hi, I did not consider toggling the system on off in the current version, and I should have done that. Thanks for reminding me!!!

    For your implementation, there is one thing can be adjusted a bit for better performance.

    1. FindLights: the lights currently registered can be access through
    Code (CSharp):
    1. LOSManager.instance.lights
    , which can help save the time for finding the LOSLightBase.

    2. Toggle: actually I should add an API in the LOSLightBase for toggling a particular light, and also an API in LOSManager for toggling all the lights. So I think I'm going to implement this in the next version.

    3. For the event system, I did not consider toggling it on off either, so I'm also going to implement that in the next version. Currently, you can simply turn off the LOSEventManager, but this will leave every LOSEventTrigger in their current state. So if you need to clean their states, you can call
    Code (CSharp):
    1. myEventTrigger.NotTriggered()
    .

    Again, thanks for all the questions. They help me think deeper in terms of the use case of the plugin, which I believe can make it more user friendly :D
     
  14. Gamer_Woods

    Gamer_Woods

    Joined:
    Jan 31, 2013
    Posts:
    33
    Now supports 2D & 3D physics!
    And some advanced tricks!
     
    Stardog likes this.
  15. boeggles

    boeggles

    Joined:
    May 23, 2015
    Posts:
    9
    Hello. I have a few questions. First of all, how do you use the light trigger to actually impart a light gradient onto a sprite that is a certain distance away from a light source.

    Additionally, there's a glitch with the program in that when another object moves an obstacle, the object without an obstacle component imparts a shadow where it was colliding the other object with a component. How did you fix this?
     
  16. Gamer_Woods

    Gamer_Woods

    Joined:
    Jan 31, 2013
    Posts:
    33

    Hi, thanks for the questions :)
    For the first question, the event trigger has a TriggeredBySource function with the event source that is triggering it as parameter. And the event source has a reference to the LOSLightBase that it is attaching to. So if you are using LOSRadialLight, you can safely cast the LOSLightBase to LOSRadialLight, and use the radius property as the light strength.

    For the second question, I'm not very sure if I understand you question. Could you describe more? Or attach a screenshot if possible?
     
  17. temeezer

    temeezer

    Joined:
    Jun 16, 2015
    Posts:
    6
    Hey! I have a slight problem. I made a simple map with tiled and exported it to unity with Tiled2Unity with collision (Polygon Collider 2D). I get a strange problem with a simple + symbol, see the image below.

     
  18. Gamer_Woods

    Gamer_Woods

    Joined:
    Jan 31, 2013
    Posts:
    33
    Hi, is it the polygon collider 2d used a concave polygon? For full screen light, concave polygon collider may have some issues currently, but it works fine with radial light, as they are using different algorithms to generate the meshes.
    If you need to use full screen light, you can work around it by combining polygons to replace the concave polygon.

    Hope it helps :)
     
  19. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Nice. Thanks for sharing! :)
     
    Gamer_Woods likes this.
  20. temeezer

    temeezer

    Joined:
    Jun 16, 2015
    Posts:
    6
    I am not sure what that even means, but if I understood the things google showed me, then yes. I tried the radial light and it indeed worked a little better, but I guess that the small plus symbols is not liked, if they are not a Box Collider :p

    I changed the plus into octagon and at least the radial light worked without problems, so that's all good. To be hones, I don't think I will be using any plus symbols in my game anyways. Thank you for your help! :) I took some pictures too!


     
    Gamer_Woods likes this.
  21. Gamer_Woods

    Gamer_Woods

    Joined:
    Jan 31, 2013
    Posts:
    33
    Glad that things work out :)
    But it's a bit strange in the second image.
    I used 2 box collider 2d and full screen light, which looks ok on my side.
     
  22. temeezer

    temeezer

    Joined:
    Jun 16, 2015
    Posts:
    6
    Yes, I also confirmed that it works well with a Box Collider, but not with a Polygon Collider.

     
    Gamer_Woods likes this.
  23. Gamer_Woods

    Gamer_Woods

    Joined:
    Jan 31, 2013
    Posts:
    33
    Thanks for the clarification! I'll look into it :)
     
    temeezer likes this.
  24. temeezer

    temeezer

    Joined:
    Jun 16, 2015
    Posts:
    6
    Awesome! :)

    I also noticed that the "non-inverted" mode is a bit less epilepsy, pictures below.
    Polygon Collider 2D lines:


    Non-inverted mode:


    Inverted mode (DO NOT OPEN IF YOU CAN'T HANDLE FLASHING): http://i.imgur.com/aMP4tS2.gifv
     
    Gamer_Woods likes this.
  25. Gamer_Woods

    Gamer_Woods

    Joined:
    Jan 31, 2013
    Posts:
    33
    Thanks for the issue!
    I think I found what's wrong with the polygon collider 2d.
    Here are the little experiments I did just now:

    The following picture is a game object with polygon collider 2d component which is made concave, though it looks non-concave as it's split by a green line inside it. The LOS system will be confused by a concave polygon:


    So if we use 2 convex polygon collider 2d to make the similar shape, the overlaps and flashes do not occur (as shown in the following picture):


    So a work around is to avoid using a polygon collider 2d to create concave polygons.

    The reason why concave polygon can cause these issues is that the policy used by full screen light to create the light mesh assumes the colliders are convex. So when it encounters a concave polygon it still applies the old policy.

    But I'll try find a way to fix it. The key lies in how to identify the convex polygons that forms a single concave polygon. Take the first picture as an example, we need to find a way to identify the trapezoid and the triangle, and treat them as two polygons instead of one.
     
  26. temeezer

    temeezer

    Joined:
    Jun 16, 2015
    Posts:
    6
    This is pretty much impossible, because of reasons. :D

    I may have found a topic that could help you with your quest to eliminate the evil bug. :) http://forum.unity3d.com/threads/2d-dynamic-shadows.165283/
     
  27. dantamont

    dantamont

    Joined:
    Jun 17, 2015
    Posts:
    2
    Hey man, I haven't tried this out yet, but it gets my jimmies excited. Thanks so much. Is this working with capsule colliders?
     
  28. daraOke

    daraOke

    Joined:
    May 8, 2015
    Posts:
    7
    Hello. First, this plugin has been a lifesaver. Thanks.
    So, I have been trying to use it on a different axis. Its all set up for xy, but i need it to work on xz. I have edited so much of the code but I am unsure of what I'm doing now... Any help would be appreciated. Thanks.

    And if this question isn't clear enough, please let me know.
    Thanks in advance
     
  29. mindnect

    mindnect

    Joined:
    Jan 5, 2016
    Posts:
    2
    Hello!
    I want rotate the light object by transform.
    I tried but It's not sync with the light mesh.

    What should I have to do?
     
  30. mindnect

    mindnect

    Joined:
    Jan 5, 2016
    Posts:
    2
    Temporary I add 2 lines at Update function in LOSRadialLight class.
    Its not perfect but it works well.

    transform.rotation = Quaternion.Euler(0, 0, 0);
    faceAngle = -transform.localEulerAngles.z;
     
  31. Seanba

    Seanba

    Joined:
    Nov 17, 2012
    Posts:
    29
    If the reason is because of Tiled2Unity then you may be happy to know the latest version gives you the choice to export convex polygons. This will increase the number of polygon colliders in your prefab but at least will allow you to use other plugins that don't support concave polygons with holes.
     
  32. MikHolmes

    MikHolmes

    Joined:
    Mar 6, 2016
    Posts:
    3
    Unfortunately, this doesn't seem to solve the problem. Even using the convex poly option, the lights seem to bleed. I realize that this project hasn't seemed to have been updated in six months, but I'm hoping there might be some solution to this.

    Edit: If it hasn't been noted as well, the movement widget seems to act strange as I move the light around my level. I haven't seen that before.
     
  33. MikHolmes

    MikHolmes

    Joined:
    Mar 6, 2016
    Posts:
    3
    I made up a custom importer script for use with this LoS system and Tiled2Unity, with the help of Seanba. It essentially just takes every poly from the prefab and turns it into its own gameObject. It might not be the simplest way of doing it, or the most optimized, but it seems to make this LoS system and T2U's output work!

    Code (CSharp):
    1. [Tiled2Unity.CustomTiledImporter]
    2. class CustomImporter2DDL : Tiled2Unity.ICustomTiledImporter
    3. {
    4.     public void HandleCustomProperties(GameObject gameObject,
    5.             IDictionary<string, string> keyValuePairs)
    6.     {
    7.         //Debug.Log("Handle custom properties from Tiled map");
    8.     }
    9.  
    10.     public void CustomizePrefab(GameObject prefab)
    11.     {
    12.         //Find all polygon colliders in the prefab.
    13.         var polygon2Ds = prefab.GetComponentsInChildren<PolygonCollider2D>();
    14.         if (polygon2Ds == null)
    15.             return;
    16.  
    17.         foreach (var poly in polygon2Ds)
    18.         {
    19.          
    20.             GameObject newObj = new GameObject();
    21.             newObj.transform.parent = prefab.transform;
    22.             newObj.layer = LayerMask.NameToLayer("Obstacles");
    23.             PolygonCollider2D newPoly = newObj.AddComponent<PolygonCollider2D>();
    24.             newPoly.points = poly.points;
    25.         }
    26.  
    27.         //Here I'd put something to destroy the original colliders.
    28.  
    29.     }
    30. }
     
  34. FINDarkside

    FINDarkside

    Joined:
    Mar 20, 2016
    Posts:
    1
    Is there any way to have radial light inverted? Also does this really work with 2d objects? I can get it to work with only 3d colliders.
     
  35. MikHolmes

    MikHolmes

    Joined:
    Mar 6, 2016
    Posts:
    3
    Late reply, but FINDarkside, it looks like you need to manually create a LOS Manager GameObject in the scene and attach the LOS Manager script to it. Then, in the editor, choose Physics_2D. Normally, the code creates the LOS Manager instance at play, but it seems to default to 3D mode? I'm not 100% certain how come, but otherwise, this is the best lighting script I've found so far.
     
  36. Pakewl

    Pakewl

    Joined:
    May 20, 2015
    Posts:
    1
    Hi !
    I love the plugin, truly amazing and perfect for the game I'm working on, but I don't get how to use the event system.
    I'm guessing it has to do with the LOS Event Manager / Source / Trigger, but I can't seem to be abble find any information or to set it right by myself.

    Can anyone explain how to do?

    Thanks in advance.
     
  37. Nestor-Solera

    Nestor-Solera

    Joined:
    Sep 29, 2015
    Posts:
    2
    Cone angle does not work very well in some parts.

    Shoot some errors.

     
  38. duckizz

    duckizz

    Joined:
    Mar 15, 2015
    Posts:
    9
    hey! i managed to get the 2d working and it seems to be great!

    For event trigger, i found that it seems to trigger only if the light beam has passed the halfway point? is this correct and if so, is there a way to fix it? I am using 2d polygon colliders and physics2d raycast

     
  39. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    65
    @Nestor-Solera

    How did you get this result? You can share with us? Please :)
     
  40. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    65
    Hi,

    And I need to do a similar effect of @Nestor-Solera



    I need the field of view of light illuminate the "floor" while the rest of the screen remains dark. I do not know how I can do this, but I know that it is possible ... I do not understand anything about Shaders! :(

    Any help will be very useful to me!

    Thank you in advance!
     
  41. juanelo

    juanelo

    Joined:
    Jan 28, 2011
    Posts:
    41
    Hi Folks,
    Any thoughts on how one could go about adjusting this to simulate 'directional light shadows'?

     
  42. MaxFrax96

    MaxFrax96

    Joined:
    Nov 18, 2012
    Posts:
    11
    For the mesh generation did you use some well known algorithms?
     
  43. BroCro7eo

    BroCro7eo

    Joined:
    Dec 1, 2018
    Posts:
    26
    I know this thread is old, but it is not working for me. I don't know what I'm doing wrong. I have put the script LOSCamera on my Main Camera, put the script LOS FullscreenLight on my player and nothing is happening. Did I do it wrong?