Search Unity

Fake spot light by switching texture

Discussion in 'General Graphics' started by dariuszpietrala, Oct 17, 2018.

  1. dariuszpietrala

    dariuszpietrala

    Joined:
    Oct 5, 2012
    Posts:
    148
    Hi,

    I need some solution for this problem. We have a car in our game. We would like to make a night scene (or whatever scene that uses car lights). Is there any way to know if a pixel is inside a cone (like a spot light) and to switch to a lit texture in the shader. This is a mobile game so this should be a fast solution. Any ideas how to do this?
     
  2. dariuszpietrala

    dariuszpietrala

    Joined:
    Oct 5, 2012
    Posts:
    148
    OK, I've managed to do that with stencil buffer. Simple mask shader and 2 passes:

    Code (CSharp):
    1. Stencil
    2. {
    3.     Ref 1
    4.     Comp Always
    5.     Pass Replace
    6. }
    7. Cull Back
    8. ZTest Less
    But I've got this weird problem:
    light_problem.jpg
    ZTest doesn't work properly for the stencil mask. Sometimes the light object is as it was above the shop for example, but it's underneath.

    Fortunatelly I've managed to fix it while writing the reply :)
    light_problem_fixed.jpg
    I just added a single plane to the light mesh and it's quite far away from the camera. But still, this should work properly without the workaround...
     
  3. Clrj14

    Clrj14

    Joined:
    Nov 25, 2017
    Posts:
    3
    @Darius

    Would you mind explaining how you fixed this issue?
     
    chase_unity38 likes this.