Search Unity

2D Mesh Based Volumetric Lights

Discussion in 'Assets and Asset Store' started by reveriejake, Jul 5, 2012.

  1. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    This is V2.5 and it has just been accepted to the asset store so go ahead and pick it up! Ask any questions here on the forum, PM, or my email at jake@reverieinteractive.com.

    I haven't had much time to write out some tutorials so if you want to use the shaders make sure you put the special "Light2D Diffuse" or "Light2D Transparent" shaders on the objects you want your lights to blend with.
    Set the ambient color using the camera's background color and alpha. The alpha at 1.0 makes the scene more hazy.
    Set the color of the lights using the color field in the lights inspector.
    Pay very close attention to how the example scenes are generated.
     
  2. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Here is a quick change log:

    Improvements:
    • New controls for scaling your lights in the scene as well as gizmos to make it easier to edit.
    • New shaders that allows you to 'cut away' at ambient light
    • Major Improvement to the performance
      • No longer use Math.Cos/Sin functions every update. Only used ONCE when detail setting is changed.
      • Lights now only update if the light is dynamic (moving) or if an object that is on the shadow layer is dynamic (moving)
        • In "Lots of Lights" demo the updates went from 120 updates per Frame down to 0 when viewing 120 light objects
        • In that same demo if the player is moving the updates jump to only 12-13 updates
        • DEMO
      • Break out or avoid for/foreach loops when they are not necessary.
        • Results in major performance boost when using large quantities of dynamic objects because the light must track those objects.

    Bug Fixes:
    • Fixed a bug where when you are using the event system and delete a light, the events become dis-connected
    • No other bugs were reported. To report a bug please email me HERE.
     
  3. Joel-Santos

    Joel-Santos

    Joined:
    Feb 23, 2010
    Posts:
    121
    The layer picking doesn't seem to work.. At least here.

    With the default one it works great, but with the other ones it stays blank.. weird
     
  4. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Thanks Joel, I will get that fixed this weekend. Sorry for the bug!

    Jake
     
  5. cc0919

    cc0919

    Joined:
    Apr 5, 2013
    Posts:
    17
    Just tried this out, like it a lot! I know someone else asked this (and you said you might add it in the future,) but did you ever add a way to soften the edges of shadows? If not, what would be the best way for me to go about doing this?
     
  6. mord79

    mord79

    Joined:
    Mar 28, 2013
    Posts:
    4
    Hi,
    I was using code line like this in the 2.0 version
    healthLight.GetComponent(Light2D).lightRadius = currentHealth/30

    but now it gave me a : " 'Light2D.lightColor' is inaccessible due to its protection level."
    How can I acces to is variable again (in Java)?

    tks
     
  7. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    @cc0919 - Not yet but it would be nice to do this in the future.

    @mord79 - healthLight.GetComponent(Light2D).LightRadius = currentHealth/30; Accessible variables are not properties so in my convention they are capitalized.
     
  8. cc0919

    cc0919

    Joined:
    Apr 5, 2013
    Posts:
    17
    Cool. Sorry to bother you again, but do you have any suggestions as to the best way to go about this? Even just a general idea if you've been kicking one around (or anyone else)
     
  9. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    @cc0919 - No I do not have any idea how to do this yet. Once I do though I will defiantly add soft edges!
     
  10. cc0919

    cc0919

    Joined:
    Apr 5, 2013
    Posts:
    17
    Sounds good! I'll start messing with it and see if I can come up with anything. If I do, I'll let you know. Keep up the awesome work!
     
  11. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Okay guys, I just submitted the fix's along with a new feature. The new feature is an "Is Shadow Emitter" checkbox. The checkbox basically inverts the mesh so that the mesh is generated in the area where the light usually doesn't. I have attached a couple of images of this new setting in action!

    $Shadows.PNG
    $ShadowCheckbox.PNG

    I think the next logical step will be to bring back the directional 2D lights! Along with softer edges. I have an idea of how to do this but it will take a bit of work for me.

    Hope you all enjoy!
    Jake
     
  12. nia1701

    nia1701

    Joined:
    Jun 8, 2012
    Posts:
    74
    I used to be able to turn on and off a light by disabling the script but that no longer works. Is there another way? Thanks!
     
  13. nia1701

    nia1701

    Joined:
    Jun 8, 2012
    Posts:
    74
    Ah, I see, I was able to load a backup of the previous version and you were deactivating and activating the gameobject. So i'll just do that manually using SetActive(true) or SetActive(false). Haven't had time to really test the new updates yet but I will soon!
     
  14. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    light.renderer.enabled = false/true

    or as you said "SetActive(true/false)" will work.

    Check your email, I sent a message!
    Jake
     
  15. TinSleeves

    TinSleeves

    Joined:
    Nov 26, 2012
    Posts:
    9
    I just updated to the latest 2D Volumetric Lights and get some 2 errors that say 'Light2D does not contain a definition for LightEnable' in HideLightOnInside.cs. I'm using the newest version of Unity
     
  16. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Delete the "HideLightOnInside.cs" script. It is no longer used. If you would like your lights to be hidden when they are inside of a collider you will use the checkbox in the light2D inspector instead of that script.

    Jake
     
  17. TinSleeves

    TinSleeves

    Joined:
    Nov 26, 2012
    Posts:
    9
    I'm having real problems since updating, for some reason the 2D lights don't show when they are in front of my tile map if I use the RadialLightMaterial. If I switch the material to Shadow it shows up but has a slight dark outline, which is quite noticeable at times. Attached an Image which should show you what I mean for both cases:

     
  18. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    You need to use the 2DVLS shaders. If your tile-map is diffuse then please use the 2DVLS/Diffuse shader.

    The dark outline is an artifact from the texture I made. You should use your own texture if you want that dark ring to go away. Or try a different shader for that as well. I think I use the particle shader for the shadow tutorial? Maybe try a different particle shader...
     
    Last edited: Apr 14, 2013
  19. TinSleeves

    TinSleeves

    Joined:
    Nov 26, 2012
    Posts:
    9
    I tried using the 2DVLS/Diffuse shader but then the light is grey scale without any alpha. Heres the setting I've used
     
  20. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    What is the background shader? (The blue and lava shaders)

    The light mesh should be using the 2DVLS/Light shader and your background shaders (blue walls, lava, character) should be the 2DVLS/Diffuse or 2DVLS/Transparent shaders. I am not home right now so I cannot try to reproduce this but once I get home I will give you a solution if this doesn't work or if I am not understanding you correctly.

    Sorry your having so many issues.

    Jake
     
  21. TinSleeves

    TinSleeves

    Joined:
    Nov 26, 2012
    Posts:
    9
    No worries, I just upgraded to Unity4 so I knew there was going to be some issues along the way.

    I'm using 2DToolkit for my tilemaps and that uses tk2d/BlendVertexColors, I can change it to tk2d/BlendSolidVertex and the lights show (although they look a little blotchy) but then that means the transparency in my tilemap is gone. I don't seem to have 2DVLS/Transparent only cutout, light and diffuse

    I copied SampleLightMaterial.mat out of my old project into the new one and that seems to work fine but I know this isn't an ideal solution!
     
  22. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    A solution is a solution ;). Im sorry about saying "transparent" I meant cutout. I will talk to the shader guy about maybe getting a transparent shader that is better than the cutout one.

    Jake
     
  23. TinSleeves

    TinSleeves

    Joined:
    Nov 26, 2012
    Posts:
    9
    Thanks for the help
    One more quick question, how expensive is using events with 2DVL? I'm planning on using the lights as searchlights so that if the player touches them alarms are set off. But I'm not sure if I should use events or maybe just raycast a bunch of rays in the search cone to detect the player?
     
    Last edited: Apr 14, 2013
  24. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    2DVLS is basically a set of raycasts. Performance depends on how much detail you want your light to have and how many items are in the radius of the light.

    Basically the code first checks to see if the light has moved. If it has moved then it will update. If it hasn't moved then it will check for items within the radius of the light. If there are objects in that radius it will store them in a list. It will check the list against a list gathered in the previous frame for any movement of any of the objects in the list. If there is movement, the light will be updated. If there isn't movement then the light will not be updated.

    If you want to cut down on performance you might want to setup an invisible light that is set to "Low" detail level. Or you could use the trigger events built into Unity and a collider set to 'is trigger'.

    "PS sorry for the late reply, I had a wedding I needed to attend."
     
  25. anthonyRichard

    anthonyRichard

    Joined:
    Apr 16, 2013
    Posts:
    1
    Hi. I need this to work on the horizontal plane, and not the vertical. The lighting functionality seems to work in any arbitrary rotation, but the new shadow functionality doesn't. Rotating the lights2d gameObject when it has the shadow checkbox enabled just seems to cause the mesh to glitch. Is this fixable?
     
    Last edited: Apr 16, 2013
  26. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Sorry Anthony, I must have overlooked that bug. Its good to know someone uses it in multiple planes still as I was planing to remove that functionality due to the processing cost. Now I know to leave it in! It is fixable with a code fix so could you email me tonight so I can send you a fix once I get it done tomorrow. I will also submit the new fix to the asset store.

    Jake
    jake@reverieinteractive.com
     
  27. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Update:
    Okay update with the fixes have been accepted by the asset store. Anyone who has this issue please go to the asset store and update your packages.

    I am working with the shader guy again on a shader that will include simi-transparency. Hopefully I can get that included in a few weeks.

    I am also looking into pixel shaders that should make soft shadows but I believe you will need to have Unity PRO to use this shader so I will continue to support the mesh based light generation.

    Thanks!
    Jake
     
  28. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Anyone looking for a script that aims the 2D light at either the mouse position or at an assign transform here it is.

    Code (csharp):
    1.  
    2. /*
    3.  * Script should be attached to the Light2D object.
    4.  *
    5.  * 'mainCamera' variable can be left null. If it is left null
    6.  * the variable will be filled in with the default Camera.mainCamera.
    7.  *  
    8.  * 'nonMouseTarget' : If left null the light will aim at the mouse otherwise
    9.  * light will aim at nonMouseTarget transform.
    10.  *  
    11.  * 'rotationLerpSpeed' : Sets the lerp speed for the Quaternion.Lerp function.
    12.  * Value is multiplied by Time.deltaTime
    13.  */
    14.  
    15. using UnityEngine;
    16. using System.Collections;
    17.  
    18. public class Light2DAim : MonoBehaviour
    19. {
    20.     public Camera mainCamera = null;
    21.     public Transform nonMouseTarget = null;
    22.     public float rotationLerpSpeed = 5f;
    23.  
    24.     private Quaternion targetRotation;
    25.     private Quaternion nRot;
    26.  
    27.     void Start()
    28.     {
    29.         nRot = Quaternion.FromToRotation(Vector3.forward, Vector3.up);
    30.  
    31.         if(!mainCamera)
    32.             mainCamera = Camera.mainCamera;
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         if (!nonMouseTarget)
    38.         {
    39.             Vector3 targPos = mainCamera.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, transform.position.z - mainCamera.transform.position.z));
    40.             targetRotation = Quaternion.LookRotation(transform.position - targPos, Vector3.forward) * nRot;
    41.         }
    42.         else
    43.         {
    44.             targetRotation = Quaternion.LookRotation(transform.position - nonMouseTarget.position, Vector3.forward) * nRot;
    45.         }
    46.  
    47.         transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotationLerpSpeed);
    48.     }
    49. }
    50.  
    51.  
     
    Last edited: Apr 26, 2013
  29. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    Hello! I've just updated to the latest version of both 2DVLS and Unity and I've noticed a couple of issues...
    1. SweepStart no longer accepts any input. Whatever I set it to, it just resets back to 0 straight away. I've tried setting it through code at runtime as well, but it's the same problem.
      EDIT: I realised it's because I'm trying to use a negative value! This seems odd... A negative value seems imperative for any lights which need to be focused on a particular spot, with an even spread either side. Otherwise I need to offset it in code every time.
    2. Performance has taken quite a hit. I've dropped from an average of about 50 fps to around 20. LightDetail setting improves things a lot more than it used to. I used to be able to get away with quite high settings on most lights, but now I need to drop down to around Normal/Medium which doesn't look very nice. Is that to be expected?
     
    Last edited: Apr 30, 2013
  30. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Hey Crowbar, I will take another look at performance. From my tests it has greatly improved but maybe not so much for your specific setup.

    The non-negative sweep value was a mistake I made. I will correct that submit a new version this week. As for the performance, I will see what I can do.

    Fix for 2D sweep:
    Code (csharp):
    1.  
    2.  
    3. // In 'Light2DEditor.cs' comment out line 83
    4. l.SweepSize = (int)Mathf.Clamp(l.SweepSize, 0, 360);
    5. //l.SweepStart = Mathf.Clamp(l.SweepStart, -360, 360);    //   <----- Comment out this line
    6. l.ForceUnoptimizedUpdate();
    7.  
    8.  
    9. // In 'Light2D.cs' change line 85 to the following
    10. public float SweepStart { get { return sweepStart; } set { sweepStart = value; updateCircleLookup = true; } }
    11.  
    12.  
    Thanks for the report!
    Jake
     
    Last edited: Apr 30, 2013
  31. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    Thanks, that fix worked fine!

    I'd never poked around in the code before, and whilst editing it I noticed the values for the quality settings. Is it safe to tweak these to my liking? I may be able to find some nice intermediate values that look good enough for my game but have a more acceptable performance.
     
  32. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Yes go ahead! It was just the safest way of doing things for those who do not code.

    Jake
     
  33. Trentish

    Trentish

    Joined:
    Sep 9, 2012
    Posts:
    4
    I'm having trouble getting this to work with Smooth Moves 2. IIRC, the SM2 developer recommends using the "Particles/Alpha Blended" shader ("Transparent/Diffuse" seems to work too). Obviously that shader won't work with this lighting system. Unfortunately, the included Cutout shader doesn't work with SM2. I think because the alpha isn't blended, you can see that the mesh is overlapping with itself.

    Any update on the new transparent shader? I'm wondering if that might fix the problem.
     
  34. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    I will email him tonight and see what he says.

    In the mean time, if anyone is up to par with shader coding please let me know, I will to pay for the time spent on the code.

    Jake
     
  35. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    I've just done some tinkering at my end and found a setup which brings the performance back up to a good level.

    I had 2 lights on my player character: one active and fully visible, and the other sort of small and hidden. Both are always enabled, but one of them is always sort of discrete and not very visible. I now set the discrete one to use the "Very Low" detail setting since it doesn't need to look good, and that helps the frame rate a lot.

    By the way, I'm using the uScript plugin to do all my scripting. If anyone else is using uScript and needs to update Light2D.LightDetailSetting at runtime they can use this custom node! Just drop it into \PROJECTFOLDER\Assets\uScriptProjectFiles\Nodes\
    You'll find it under Actions > Light2D.
     

    Attached Files:

  36. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    Hi, I really like your script but the following error is really annoying and makes it really hard to know when there are changes in the scene:

    "Cleaning up leaked objects in scene since no game object, component or manager is referencing them. Mesh has been leaked 1 times."

    Do you have any fix planned or have any idea how to get rid of it?
     
  37. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    That's a harmless error usually caused by editor scripts cleaning up after themselves. It could be your game, or this component - especially if there's editor realtime feedback. It would be nice if in fact unity allowed us to disable this notification.

    I think it's pretty cool, and a great price, so into the signature it goes for a bit.
     
  38. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    Unfortunately the problem comes with the plugin and it's not that harmless since it doesn't really allow you to know when you made changes to your scene or not. Should be fixed by the author as soon as possible in my opinion.
     
  39. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    In the meantime something really strange just happened!! :eek:

    F $Screen Shot 2013-05-03 at 2.20.18 AM.png

    From nowhere 2DVLS lights started not appearing either on the editor if they're above a plane!!! I already created a brand new project and it still happens... Can someone please tell me I'm just tired and please point me to the obvious solution? >)
     
  40. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    Explosion of info on the forum suddenly..

    @CrowbarSka Thanks for the uscript info and glad you were able to get things running the way you like. You mentioned performance in a previous post and I am currently working on more HUGE improvements to performance. I dont know when the next update will be but I hope to make some serious headway this weekend as usual.

    @imteractive The error actually is harmless. It is basically telling you that the mesh I generated wasn't properly cleaned up. This is due of course to bad code (my fault but I am willing to admit it).

    As for the scene registering that updates are needed, I use "ExecuteInEditMode()" to allow for a WYSWYG experience. Its used because I want the lights shadow areas to update in the editor if you move an object in front of it. Sense the light object isnt selected at that particular moment, then I cannot (to my knowlege at the time) use the "Custom Editor" class that I can otherwise use to update the light. This issue will absolutely be fixed in the next update as I have figured out a way to eliminate it via my work on QuickRopes. But for now I apologize for the inconvenience it causes.

    On to your next issue with the light not being visible. With previous versions of the light script I used the built in particle shader that unity provides. This allowed you to just drop in the lights without anymore alterations to your game. Unfortunately since people have been asking for the ability to make the entire scene black and allow the light to cut through the blackness I had to have shaders written. NOW to get your scene working, you will need to use the "2DVLS/Diffuse" or the "2DVLS/Cutout" shader on the objects you want the light to be visible against. I am hoping to get the Simi-Transparent shader in the next version but development on shaders has been kind of slow.

    Hope this sheds some light2D on the subject, <- bad joke
    Jake
     
    Last edited: May 3, 2013
  41. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Hi Jake, sorry haven't been in touch for a bit, drop me a line if you want to talk shaders.

    - John A
     
  42. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    No problem John, I have been busy myself! Ill send you an email.

    Jake
     
  43. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    Good to know you're working on a fix to that problem reveriejake! I really appreciate it. Any idea on when it will be ready?
     
  44. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    I dont really like giving out ETA's. I try my best to get them out as soon as I can but holding to an ETA puts a huge amount of stress on me.

    I can say that I plan to spend a good amount of time on it this weekend so I hope it doesnt take too long.

    Jake
     
  45. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    I totally understand! But knowing that you'll be working on it this weekend is good for me! Thanks again!
     
  46. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    Awesome, glad to hear it! I've been really impressed with the amount of improvements you've been doing lately. Very much appreciated!
     
  47. Neefy

    Neefy

    Joined:
    Nov 28, 2012
    Posts:
    24
    Is there an easy way to make the light flicker?
     
  48. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    Hi reveriejake, just a quick question: is there any reason to why the light radius is clamped between 0 and 360? I suppose that was a typo, right?
     
  49. reveriejake

    reveriejake

    Joined:
    Jul 8, 2007
    Posts:
    819
    @imteractive: Yeah it was defiantly a typo. I must say you have a good eye for bug catching! I dont think people usually go beyond 360 so I think your the first to notice.

    @Everyone: I have a new version nearly ready... I made a few changes to the API and some major changes to the code.

    • LightDetail will now allow values (50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000, 5000)
    • SweepStart is no longer clamped
    • There is a new function called "LookAt" which is not related to transform.LookAt. It basically converts Forward to Right axis so the Right axis is looking at the object instead of the Forward axis.
    • I use 'SendMessage' now instead of events. The messages sent are "OnLightEnter(Light2D)", "OnLightStay(Light2D)", "OnLightExit(Light2D)". If anyone has any issues on this let me know and I will change it back before release.

    Jake
     
  50. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    943
    Any specific reason why you opted to use 'SendMessage' instead of events? On my understanding events should be faster.

    Also, if you're only sending as a parameter the Light2D how can we detect which object was hit?