How to Hide Directional Light on One Side of an Axis/Object
Okay, so I'm working on a project that I want to have functional spotlights in (not the lighting), and I want these spotlight models to have cylindrical, volumetric lighting. I've got the cylindrical part done (directional light with a circular cookie) and a friend is working on the volumetric stuff.
My issue is that once we put everything together, the directional light, being infinite, will be visible behind the spotlight models as well as in front of them, and this brings me to my question:
Is there any way to make a directional spotlight simply not produce light until it passes a certain physical location? Alternatively, is there a way to make its directional "origin" (I'm aware it has no actual origin) the light object itself, so if the light shines downwards (towards -Y), no light will shine/be produced above (+Y of) the light object's XZ plane?
Screenshot attached to hopefully illustrate this a bit better; as you can see, the light originates from behind the entire spotlight model, but I only want it to originate from the actual light object/front of the spotlight model (or at least be hidden until it crosses the light's XY plane), making it look like the spotlight model is the source of the light.
Answer by cgeorge2 · Mar 16, 2017 at 01:32 AM
Took me awhile, but I finally figured out a solution. If you can use Blender or another modeling program to put together a cylinder with a hole through it (think like a pipe), you can import it to Unity and set its mesh renderer to be shadows only, that way it's invisible but will still cast shadows. After that, you have to take a second model (or a derivative of the first) which is a hollowed-out cylinder with one end knocked off (think of a barrel). You put a point or spotlight inside of it, crank up the intensity and range (to counter the attenuation drop off, unless you're fine with that), and put the pipe-cylinder model on the end of the barrel. The barrel acts as a hood, directing the light, and the pipe works to secretly keep the light in a beam shape.
The downside to this is that if you're using more than one beam light, the pipes can block other lights, so you'll have to mess around with tags a whole lot so that each light source ignores ALL pipes except for the one used with the light source.
No problem, only took me nearly 2 weeks to figure it out... ha ha...
That being said, an orthographic projector mostly worked, except it didn't cast shadows from the "light," and it also projected the light spot onto the backs of objects it was shining through.
A light/barrel assembly as described above still projects light much too widely without the pipe to limit it, unless the barrel is ridiculously long, and that just looks stupid.
Trying to modify light attentuation through Unity's built-in shaders either doesn't work or the solutions online just don't have all the information (I also know nothing about shaders, so that could have something to do with it).
Cookies give the spotlight the proper shape, but still scale with distance/angle, and therefore don't give the beam effect.
I think that's about all I tried wrangling with, in case someone wants to try doing it a different way.
Great work. I have NFI why Unity doesn't support something this simple out of the box though. Frustrating.
Your answer
Follow this Question
Related Questions
ProBuilder - Light leaking through seams on internal scenes 1 Answer
Directional light scene is too dark 1 Answer
What's the difference between Lightning (TAB) and Directional Light (gameobject) 1 Answer
How i can fix the directional light?,My directional light don't work After I build the game 0 Answers