How can I dynamically color in only specific regions of my scene?

Hi, I don’t know anything about Shaders, which probably is the problem here :') but maybe someone has an idea or implemented something similar already and can point me in a direction.

So I want a “flashlight” (cone) mechanic, but not with light, but with a colouring-effect. Meaning, everything is white (or grey, we’ll see) - except for the parts the flashlight-cone is pointing at, they will appear in colour. And that’s it, but unfortunately, I don’t know where to start :')

Any help would be appreciated!

It depends if you need the edge of this “flashlight” to be soft or not, as it can be very simple if a hard edge is fine. For a hard edge, you can just have a customized ShadowCaster pass on your shaders that writes a Stencil buffer value for lights. And then use this stencil value in your main shader passes to determine whether the colored pass or white pass should be used.

For soft radius, other options would be making a ShaderGraph shader that has values (with “Exposed” unticked) that you can assign direction, position and cone shape value to, and then use some comparison math to check how deep in this cone shape the pixel is and change the output color that way. And assign to these values using the Shader.SetGlobal functions.

Though you’ll be limited to only one flashlight in that case unless you use arrays of these values (which you’ll have to define in a Custom Function Node HLSL script file instead.)

Other options are using custom render buffers to render a soft mask to and then use this mask in a post-process that removes the color from anywhere that doesn’t have the masking value.

1 Like

you could easily do this by using the attenuation of the spotlight to lerp between Luminance(color) and the color, which would make it smoothly transition from grayscale to color inside of your light’s radius.

no stencil buffer would be required for this method, and it would also work for any light source in unity.

not sure how easy this would be to implement in shader graph, but in hlsl this would be as easy as making a copy of whatever built in shader you like and adding a few lines.

here’s the repository of built in shaders.
Unity-Built-in-Shaders/DefaultResourcesExtra at master · TwoTailsGames/Unity-Built-in-Shaders · GitHub

1 Like