2
\$\begingroup\$

I'm trying to set up a shader that, as the title says, functions similarly to the effect appearing in the "Lights Out" horror short film. Cut down to the very basics, I'm looking for advice on how to have the overall Alpha/transparency of the material be affected by Realtime and Directional lights in the scene so that the material is only visible below a certain lighting threshold. I found a similar effect that accomplishes the inverse goal of lowering the alpha of whatever a light is cast on, but I couldn't manage to invert the effect, nor could I get it to function properly in my project to begin with.

I'm currently working in Unity 2019.4.29f1 and was hoping to be able to tack this effect onto some other shaders that I have to avoid having to reconstruct an entirely new shader for this singular purpose.

Edit: As per suggestion, a few extra details:

This gif is more or less the effect I'm trying to accomplish: https://pa1.narvii.com/6743/bf353546d9f30e492077c1cf8d57e3f60f53a36b_hq.gif

Ideally, I'd like it to not just be based on full mesh coverage of light, but more like a dynamically applied alpha mask based on light intensity on the actual mesh over any given spread of the surface.

The effect that I found and attempted to modify was effectively trying to accomplish the opposite of what I'm trying here. It was attempting to make something transparent if it had light cast on it (at least how I understood it). I hoped to be able to invert this effect with minimal editing, however I either misunderstood how the shader functioned or was simply unable to comprehend was most of the syntax meant and ended up unable to modify it to fit my needs. Additionally, I wanted to have most of the Texture and render options that come with Unity Standard shader, but couldn't figure out how to combine the two shaders due to a flat lack of knowledge regarding shader coding.

Edit: This is the shader that I attempted to reverse engineer that I couldn't get to work: Shader change alpha depending on light

Edit: For further clarification, I WILL need a custom shader for this feature as I'm attempting to implement it on a platform that disallows custom scripting, and requires that whatever changes I make are locally controlled (I will not have control over environmental factors like lights).

\$\endgroup\$
1
  • \$\begingroup\$ Can we assume you don't want self-shadowing on the mesh and other fanciness? Eg a flashlight would bore a hole through whatever it's pointed at? Also is this constrained to a list of known light sources, or are you hoping to sample at each point and deal with things like bounces from GI? \$\endgroup\$
    – Basic
    Commented Nov 22, 2021 at 21:59

2 Answers 2

0
\$\begingroup\$

Do you need a custom shader?

Based off the gif you sent, it seems like the entire model will always completely either completely visible or completely hidden. If this is the case, then the simpler, more design controllable, and performant solution would be to simply enable/disable the visuals of the game object as lights are turned on and off.

\$\endgroup\$
6
  • \$\begingroup\$ Another approach in this vein would be using a C# script to set a master alpha value for the whole material, proportionate to the illumination at the object's center. That would let you get partial transparency during lighting transitions, without complications like an object's own shadow making parts of it transparent. \$\endgroup\$
    – DMGregory
    Commented Oct 22, 2021 at 3:12
  • \$\begingroup\$ For getting illumination at a particular point, would you propose recreating light attenuation and obfuscation cpu-side? \$\endgroup\$
    – Charly
    Commented Oct 22, 2021 at 3:30
  • \$\begingroup\$ The way we did it on Splinter Cell was casting a ray to each dynamic light whose bounds we overlapped to check for occlusion, and attenuating using the light's falloff curve (usually roughly inverse-square). This was cheap enough we could do it for individual bones to measure partial illumination if your character was straddling a shadow terminator, though I don't think you'd need that granularity for this effect. You might even have a simple "light level" per room as part of the game's light/dark mechanics, and be able to query that directly. \$\endgroup\$
    – DMGregory
    Commented Oct 22, 2021 at 3:34
  • \$\begingroup\$ Unfortunately, I will need a custom shader as much as it hurts to say; I'm intending to use this feature on a platform that disallows custom scripting (VRchat), and I need to do it in a way that is fully self contained to a local amount of control, assuming that I cannot control larger aspects of the environment such as lights. \$\endgroup\$
    – Willixir
    Commented Oct 23, 2021 at 17:45
  • \$\begingroup\$ That would be a good thing to have put in your question from the beginning. \$\endgroup\$
    – DMGregory
    Commented Oct 23, 2021 at 19:02
0
\$\begingroup\$

I don’t know how VRChat shaders work, which inputs are available, etc. but this seems pretty simple. All you need is a basic diffiuse lighting shader, but use the light contribution to affect the alpha channel.

Something like:

color.a = dot(normal, light);

That’s all the math that’s needed. Just make sure the material is marked as transparent so the renderer knows it needs to be sorted (otherwise you’ll get ugly artifacts)

Or were you looking for something different?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Note that this will still make the shadowed side of the object transparent, even when the object is brightly lit on the other side. \$\endgroup\$
    – DMGregory
    Commented Oct 23, 2021 at 19:03
  • \$\begingroup\$ I was working with a friend to try setting this up, but we couldn't quite figure out how to GET the light contribution. We can't pass it in from the code of the game because VRchat is very restrictive in that sense, so it'd have to be entirely self-contained as a shader. \$\endgroup\$
    – Willixir
    Commented Oct 28, 2021 at 20:11
  • \$\begingroup\$ The light contribution is just the dot product of the surface normal and the light direction (like the example I posted above). See if VRChat has any sample shaders with basic lighting, and use that as a starting point. Also, you’ll probably have better luck asking this question in their community/forums/discord/etc \$\endgroup\$
    – Alex
    Commented Oct 30, 2021 at 20:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .