Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Moving shadows in Unity

Discussion in 'General Graphics' started by ATLAS-INTERACTIVE, Jan 16, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am trying to replicate the Skyrim shadows, where on candles the shadow will "dance" slightly and the light intensity will fluctuate, I wasn't sure whether to post this under Graphics or Scripting, but hopefully one of you may have a solution to this, I have a flame particle on my candles and fire on the torches but static lights, it doesn't look right.
     
  2. create3dgames

    create3dgames

    Joined:
    Aug 20, 2012
    Posts:
    275
    You'll need to attach a script to the light. The script should make the light's intensity and range fluctuate randomly.
     
    shkar-noori likes this.
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I have a script that does both of these, these values don't change the shadow, they just make it more predominant, or less so. I need a way to slightly move the shadow itself, if you have Skyrim (not sure why anyone wouldn't), go into a house with candles, and look at a shadow cast by that candle for a bit, they slightly move, its a relatively small thing, but all the small things mount up and lead to a more immersive experience.
     
  4. gambiting

    gambiting

    Joined:
    Jul 23, 2014
    Posts:
    7
    The best way to "move" shadows in Unity is to move the light source :p You could get the shadow framebuffer,but I honestly don't know why you would want to, moving the shadow texture would be a lot more difficult and would NOT produce the same effect like skyrim.

    So, to replicate the effect - you have to use point lights with realtime shadows(only Pro version!), and then attach a script that will "wobble" the light - move it a little bit up/down/left/right.....then the shadows will move too! Of course you could use a directional light in the free version of unity, but then it would be like a flashlight,not like a candle.
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It's often sufficient to just have a zone the camera can enter, and the closer to the center of the zone, the more pronounced the micro rotation applied to the directional light. This is sufficient for unity free. For pro, the same can be done with a local light.
     
  6. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    so there is no way to move or rotate the shadow independently without moving the light source? this sounds like a very inefficient way to achieve this, I was hoping an offset could be applied to a baked shadow causing it to appear to move, but perhaps not.
     
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    you could write a cg/vert/frag shadow pass in 4 and use a vertex shader to perform this. But unity 5 uses screen space shadows afaik so that's not really viable. What you're asking for isn't an incredibly viable thing. It's also solved completely by having a point light with shadows simply move around and pulse slightly at the candle.

    If you're using the free version of Unity, it's probably not possible.
     
    theANMATOR2b likes this.
  8. gambiting

    gambiting

    Joined:
    Jul 23, 2014
    Posts:
    7
    Alright, just so you don't think we are avoiding telling you. Like hippocoder mentioned - you could create a new shader for your materials that you want to be affected by this moving shadow. Create a new shader, and then click on "show generated code". That will show you the full code of that shader - scroll down to the code managing the lightmaps. There, you can make it sample the lightmap with an offset, that you could set from a script using a shader uniform. That would work - but it will look badly because you will be moving the entire lightmap, not just the shadows, as they are not separate. The lightmap was also calculated from a certain position - so when you move the lightmap the whole perspective will be skewed. But yeah, you can do that.
     
    hippocoder likes this.
  9. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @hippocoder I am using Unity Pro, I have the U5 preorder, I will be working primarily in U5 when it is finally released for obvious reasons, but from what you have said, this would not be a viable way to do this, I may have to ignore this on my list then, its not that big a thing, it just would have been nice to have
     
  10. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    If you have unity5, why on earth wouldn't you use a point light and jitter it around softly with shadows?
     
    PhobicGunner likes this.
  11. PhobicGunner

    PhobicGunner

    Joined:
    Jun 28, 2011
    Posts:
    1,813
    He didn't say anything of the sort.
     
  12. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @PhobicGunner it's implied, moving the lights slightly, be it in an animation, or script, is very inefficient, for the light to have to physically recalculate the lighting shadow every frame it moves, I don't think it would be efficient and definately wouldn't run well on slower machines.
     
  13. PhobicGunner

    PhobicGunner

    Joined:
    Jun 28, 2011
    Posts:
    1,813
    If the light is not lightmapped, then it already does that regardless of whether you move it or not - that's how real-time shadows work.

    EDIT: And, by the way, that's exactly how Skyrim does it. Realtime shadow maps, jitter the light source.
     
    theANMATOR2b likes this.
  14. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    If so many lights in Skyrim use real-time shadows and jitter around, how does it run so smoothly, even on basic systems?
    I know Unity doesn't have the best optimisation in the world but the Creation Kit can't be THAT good.
     
  15. PhobicGunner

    PhobicGunner

    Joined:
    Jun 28, 2011
    Posts:
    1,813
    I don't even need to look it up to know that Skyrim uses realtime shadow mapping - it's glaringly obvious.

    That said, I think the answer is that most lights in Skyrim probably don't cast shadows. They probably have one or two main lights in a scene that cast shadows, and the smaller less important lights (such as small candles) probably don't cast any shadows at all.
     
  16. gambiting

    gambiting

    Joined:
    Jul 23, 2014
    Posts:
    7
    Deferred rendering probably. I've had literally thousands of lights at the same time, all casting shadows in real time with deferred rendering. Again, in Unity it's a Pro feature. Also, like PhobicGunner said - it's very likely that Skyrim doesn't have that many light sources that produce real time shadows in any of the scenes. Most light sources, will be light mapped, and those that aren't are probably unlikely to exist in huge numbers(few fireballs cast at the same time + a few candles is not horribly difficult to render even without deferred rendering).
     
  17. 0tacun

    0tacun

    Joined:
    Jun 23, 2013
    Posts:
    245
    Use shadow cookies. http://docs.unity3d.com/Manual/class-Light.html

    It is a technique very well used by many games. Furthermore it enables you to use very soft shadows, even with unity free and it is not as compute intensive as real time shadows.
     
    theANMATOR2b likes this.
  18. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Light cookies are only usable on Directional Lights, and not Point Lights, the way I am designing my maps, a directional light would not suit my lighting requirements, only things like Point Lights do so light cookies aren't an option either, unless someone knows how to use them on a point light
     
  19. 0tacun

    0tacun

    Joined:
    Jun 23, 2013
    Posts:
    245
  20. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Uh you don't even need light cookies. In any case it looks like you're actively fighting our advice without knowing much about Unity so I wish you best of luck.

    Just try a point light with shadows on. Runs fine even on rubbish systems. Remember, it's not like you'll have 100 candles all with point light shadows in one tiny space...

    FYI we had something similar (jiggling) on Vita at 60fps. So I'm sure with some care, a mighty desktop will do it fine.
     
    theANMATOR2b likes this.
  21. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I have been using Unity for 3 and a half years now, I have just never needed to do anything like this before, we are using a VERY large world with a lot of information behind it, I am just looking for the most efficient method that is not costly on processing speed.