Search Unity

Lighting: How to Work with Particles, GUI, or AI

Discussion in 'Shaders' started by Zullar, Jul 13, 2017.

  1. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    I have a darkly lit dungeon crawler game.


    I have 3 issues though.
    1: Using the Particles/Additive shader causes particles even in the darkness to show. i.e. I'd like the raindrops in the video above to only show when illuminated by light. Does such a shader exist? (something that behaves like Particles/Additive but uses the light magnitude at the particle center... irrespective of the light direction). If not could one be created (if it's possibly maybe I'll try and learn shaders)? Other standard shaders do not work because if the light is not pointing at the face of the particle billboard then it doesn't light the particle.

    2: I have health bars. You can see them in this video.


    I'd like to disable the health bars when the monster is in the darkness. Is there a way to do this? The health bars are drawn in the GUI overlay (not the 3D world).

    3: I'd like to be able to detect lighting at a point with a Monobehaviour script. For example vampires are vulnerable to light so if they are lit up then I'd like to make them more vulnerable to damage. Is there a way to use a script and detect the light at a point?

    Thanks in advance.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    1. See the recently released Standard Particle Shader. As a warning, like all transparencies, these won't let particles receive shadows, and point & spot lights are approximated, so it won't be perfect, but it will be closer. I'm also not sure how you're implementing your "view cone" light, but there's a good chance that either won't work at all or might not work the way you expect.

    2. What happens on the GPU stays on the GPU ... or at least it should because it's usually really expensive to try to access it on the CPU. Your best bet there is going to be to track overlaps against spheres (or other shapes) around each light to see if it's likely being lit or not. The other semi-ugly option is to render your health bars as objects in the world so they get lighting information and use that in a custom shader to hide health bars. You can write elaborate setup to do stuff like have a dynamic texture on the GPU recording lighting information for arbitrary points in the world and have your health bars use that ... but save yourself the headache and just approximate it.

    3. Same as #2, you're going to have approximate it. ie: fake it!
     
    Zullar likes this.
  3. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    Thanks. I'll try the Standard Particle Shader.

    My viewcone is a 90deg spotlight pointed strait down with a mask/cookie. It ended up taking me a while to figure out something that worked well without causing ~distortion when rotating the character due to bump maps on the floor.

    So I guess my plan is to add colliders to all light sources. Light emitters will be on one collision layer and light detects on another. This will let me handle #2 and #3 in a completely separate system.

    I'll add question #4. If I get a few light sources on top of eachother it gets extremely bright (duh!). Is there an easy way to "clip" the brightness so it would almost look the same regardless if there are 1, 2, 3, or 4+ light sources on the same area?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    For opaque stuff? Not really. The lights have no knowledge of each other and simply accumulate. Plausibly this could have been done with the legacy pre-pass lighting.

    For transparent stuff, maybe? The approximated lighting is done with vertex lighting in which case the shader has access to all of the lights (well, the directional light +4 points and/or spots) at once.

    The "correct" way to handle this is with eye adaptation, but that may have unintended side effects.
     
  5. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    You got all the answers. Are you secretly a Unity Shader dev? :p
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    I am a Unity shader dev ... not one that works at or for Unity*, but shader authoring is a significant part of my job.
    * not when writing this at least, I've joked before that a Unity office is like a block away from where I currently work so I might as well be. ;p