Search Unity

2D Lighting

Discussion in '2D' started by Callski, Nov 13, 2013.

  1. Callski

    Callski

    Joined:
    Feb 3, 2013
    Posts:
    130
    Is it possible to use lighting effects for the new 2D elements? I've been playing around with the new system and so far it doesn't seem like lighting effects the UI at all. Am I doing something wrong?
     
  2. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    You'll need to add a material to your sprites for this which uses the Sprites Diffuse shader. As standard, sprites don't support lighting as we expect users to use them unlit and use lighting baked into the sprite design itself.

    Simply make a material, assign it to the sprite, and choose the Sprites Diffuse shader from the material as shown in the image below -

    $Screen Shot 2013-11-13 at 15.40.28.png
     
  3. Callski

    Callski

    Joined:
    Feb 3, 2013
    Posts:
    130
    Thanks willgoldstone! Works perfectly!
     
  4. Callski

    Callski

    Joined:
    Feb 3, 2013
    Posts:
    130
    Is there a way to set up your 2D elements to cast shadows on 3D objects behind them? Also willgoldstone when's your next book going to be out which covers the new 2D system?
     
  5. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    You're welcome, and next year ;)
     
  6. bryantdrewjones

    bryantdrewjones

    Joined:
    Aug 29, 2011
    Posts:
    147
    I just want to quickly jump in and say that it would be *really nice* if Sprite Renderer supported dynamic lights. I want to be able to use normal maps with my sprites. It's way too time consuming and memory intensive to use different sprites for different lighting conditions.

    I noticed that, with the right material, Sprite Renderer responds to the ambient light of the scene. Why does it not respond to dynamic lights?
     
  7. Shnayke

    Shnayke

    Joined:
    Mar 29, 2013
    Posts:
    11
    Will, thanks for your reply and your knowledge. I'm trying to debug a small issue - some of my sprites seem to be quite binary in terms of whether they're lit or not - the two following images show what happens as I move a light source just a tiny bit to the left:

    $lighting unlit test.PNG

    $lighting lit test.PNG

    Any idea of what setting I have to fix?
     
  8. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    I would guess at you simply needing to move the light to the correct Z distance in front of your sprites so they're all hit by it. Remember that 2D in Unity is done with a series of 3D planes with Sprites overlaid.
     
  9. Shnayke

    Shnayke

    Joined:
    Mar 29, 2013
    Posts:
    11
    Weirdly enough, that one block is the exact same Z value as the other level geometry blocks. It seems that this only happens with blocks that I've distorted the scaling of - the scale's no longer 1,1,1.
     
  10. Rixithechao

    Rixithechao

    Joined:
    May 15, 2013
    Posts:
    3
    Resurrecting this thread for anyone still looking for solutions for the binary lighting thing, just figured it out myself. Two important things to check:

    1) Is the light's render mode set to "Important"? This should fix the issues with scaled stuff.
    2) Are you using scaling or rotation to flip your sprites? Sprite renderers work like textured planes, they only receive light on one side. If you rotate the characters to mirror their direction then the renderer is suddenly facing away from the light, but if you just scale the object by -1 you can flip the character without flipping the renderer's normal.
     
    Deep_Ak and perracolabs like this.
  11. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    Thank you so much! The scaling part was the problem for me, and solved it by your method. Thanks!
     
  12. janeDoe9173

    janeDoe9173

    Joined:
    Jul 20, 2014
    Posts:
    7
    Im very new to Unity and I am having lighting issues as well. When I turn the built in lights off or on nothing happens. It is the same either way. Ive been trying for days to create lightning with no luck. Could you please explain to a COMPLETE newbie what you mean by, "If you rotate the characters to mirror their direction then the renderer is suddenly facing away from the light, but if you just scale the object by -1 you can flip the character without flipping the renderer's normal"?
     
  13. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    You can light sprite only from face(front) side. If you rotate sprite to mirror, then faceside will be in backside and can not be lighted from front. If you scale, then face is still in front.

    to light try to do:
    -put sprite on scene
    -create new material, assign in inspector for the material sprite/diffuse (as in 2. post)
    -put your material in your sprite
    -create directional light for test

    if it work try other light (be sure it is between camera and sprite, so check z-direction)

    ps. and sorry for my bad english :)
     
    DotusX likes this.
  14. Onlinety

    Onlinety

    Joined:
    Aug 2, 2014
    Posts:
    1
    Thats Great information.. Very Nice. Useful
     
  15. Shinodan360

    Shinodan360

    Joined:
    Mar 21, 2015
    Posts:
    7
    Hi there, when i choose sprite diffuse i end up with this...



    Anyone have any idea why? im clueless...
     
  16. ShichiNoBushi

    ShichiNoBushi

    Joined:
    Apr 19, 2016
    Posts:
    5
    I'm trying to make a 2D game that relies on light and darkness. I have tried the methods above (create Material with Sprites Diffuse and applied it to most of the sprites), but I'm sort of unsatisfied with the results. I would like to have anything unlit to be completely black and the lights to have harder edges and less intense in the center. Where are the settings to adjust this? Can I instead use a Directional or Spot Light rather than a Point Light, and how should I adjust the settings on those?

    Edit:
    Never mind. I found the Lighting panel under the Window menu. I then adjusted the "Ambient Intensity" slider to "0" to fill the rest of the scene with complete darkness. I then found that a Spot Light did work for what I wanted and just had to adjust the angle and intensity on that to where I was comfortable with. Though I would like to be able to adjust how hard the light's edge is instead of a softer transition to the darkness.

    Edit 2:
    Now I have a new concern. When multiple lights overlap, their brightness stacks rather than overlaps. I'd prefer this to be handled more discretely and simply whether the area of the environment is lit or not rather than how well lit. And more frustrating is that when a sprite seems to have more than 2 light sources affecting it, it seems to be almost completely lit up with a slight gradient to it. Is there something I can do to accomplish this such as making it so only the brightest light in the area is visible?
     
    Last edited: Apr 21, 2016
  17. Deep_Ak

    Deep_Ak

    Joined:
    Jun 24, 2015
    Posts:
    29
    YOU ARE THE BEST! THANK YOU!
     
  18. Deep_Ak

    Deep_Ak

    Joined:
    Jun 24, 2015
    Posts:
    29
    Are you using spot lights? You can achieve the effect you need with point lights. Give it a try. :)
     
  19. yakir_30

    yakir_30

    Joined:
    Jul 27, 2015
    Posts:
    5
    Hello all,
    I am working on 2.5D game, i have 3D character and 2D textures for objects that the character can walk behind and in front of them (like layers in 2D games)
    i put directional light from the left side of the room in order to cast the character's shadow on the right side of the floor,
    but the light is not hitting my 2D textures and therefore they all looks unlit.

    Any idea how to solve this 2.5D game issue?
     
  20. giomak

    giomak

    Joined:
    Aug 8, 2016
    Posts:
    2
    Can you suggest any video tutorial to learn how to create 2D characters ?
     
  21. ShichiNoBushi

    ShichiNoBushi

    Joined:
    Apr 19, 2016
    Posts:
    5
    Yes I am using spot lights. I already tried point lights, but the effect of spot lights pointed at the game's plane, creates the effect I wanted or at least almost.


    This is the scene without lighting, and everything is visible. The light sources are supposed to be the two lanterns and a fairy that follows the mouse.


    This is the scene with lighting except that one of the lanterns is off for a reason. As you can see, the lighting around the lantern and the fairy works as I intended, an almost distinct circle centered on the source. A point light would have instead have been a light that grew gradually less intense farther from the source. But a complaint about this is that where the lights overlap, the lights are added together and brighter where the effect I want is for it to be more discrete with a consistent or maximum light level within lit areas of the screen.

    Otherwise, this method works as I intended except for a major problem I'm having with the lighting system demonstrated below.


    When I turn on the right light, the whole lighting system is messed up. Though the left lantern is on, you can't see the light around it because it is completely illuminating the wall behind it. This completely ruins the purpose of lighting my game. With some experimenting I have found out generally how this bug(?) behaves. It seems that when a sprite is lit by more than two light sources, the left-most source completely illuminates it, and I have noticed that this works with the character's as well as the walls' sprites after moving the lanterns around, and the fairy has this effect when it moves to the left of both the lanterns.

    Can someone explain how it's doing this and how to stop it? Or possibly if this is due to a parameter on the objects or the engine to increase the limit or whatever a sprite will have of light sources before behaving this way? And as mentioned above, I would like to know how to set a way for overlapping lights to not create brighter regions if possible. I would also like to know if there's a way to get point lights to create this appearance mainly so I can have it so lights don't extend beyond walls and barriers so players don't see into farther areas early.
     
  22. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try to set light's render mode to important
     
  23. Pagi

    Pagi

    Joined:
    Dec 18, 2015
    Posts:
    132
    I tried this kind of lighting. In my game you can place as many blocks as you want, but unity allowed only four lights(at least in vertexlit). Now I am using circular texture with a special shader that lightens pixels behind it and it works fine.
     
  24. ShichiNoBushi

    ShichiNoBushi

    Joined:
    Apr 19, 2016
    Posts:
    5
    Thanks. That fixed it perfectly. Though I still would prefer if the lighting wasn't additive where it overlaps.
     
  25. timsoret

    timsoret

    Joined:
    Apr 9, 2015
    Posts:
    16
    Lights are supposed to be additive, that's how they behave physically.
    What you're looking for is more like a mask - you probably need a stencil shader.
     
  26. cdunne1

    cdunne1

    Joined:
    Jul 11, 2017
    Posts:
    6

    I've added a Diffuse Sprite Material to my 2d sprite but it still wont illuminate....Any other suggestions?
     
  27. NoaCalice

    NoaCalice

    Joined:
    Oct 15, 2015
    Posts:
    1
    Hey everyone :) ! Here is a VERY QUICK tutorial I made on how to get lights work on 2D sprites in Unity !

     
    frikic, yurykozhenov, Jroel and 3 others like this.