Okay, so I bought a book about shaders a month ago, i’ve learned some, but how can I create a darkness shader? For example. lets say I put a cube in a whole and attach the shader to it, it should be black, but the color should get detailed like fog the more depth the cube has. So it actually looks like darkness in the whole. Is there a way to make this happen?
Grabscreen position > Grabscreen Color > Multiply and float>one Minus
bimp
bump
What do you mean multiply and float?
Oh I use HLSL
@DEEnvironment answered the question of “how to make shader that makes things darker”, but in roughly the most expensive way possible. It also didn’t answer the actual question you had of how to do it in a way that gets darker the “deeper in” it gets.
The simpler way to darken things is to use either a multiply blend shader with a dark color, or a regular alpha blend shader with black. To have it get darker the “deeper in” it gets, you need to sample the depth texture, calculate the linear depth, and compare it with the current shader’s fragment linear depth. This is how soft particles work.
In fact all you need to do is use the built in Particles/Standard Unlit shader with the Soft Particles option checked.
You can get a little more fancy in a custom shader and use a exponential falloff like fog uses. Or calculate the depth taking into account the alignment of the surface normal for something that looks more like a darkening of the lighting lighting rather than a fog, or calculate accurate height fog.
The code for the soft particles can be found here, though you’ll have to look through the rest of it to see how the other values are calculated.
A more straightforward implementation that is similarly implemented can be found in the now deprecated particle shader code.
Note: SOFTPARTICLES_ON
define comes from the #pragma multi_compile_particles
, and is controlled by the Soft Particles setting in the project Quality Settings. It is not required, for this effect to work, it just forces the camera’s depth texture to be rendered, which you’d otherwise need to use a custom script to enable on your cameras. Or be using directional lights with real time shadows on non-mobile platforms or depth of field post processing, both of which also use the depth texture.
To do the version that looks less like fog and more like lighting getting darker, you want to get the dot product of the surface normal and the view direction, and divide the depth difference by that. Works really well on a flat quad or plane, though I wouldn’t recommend it on a cube.
There are also more advanced setups that calculate fog inside an analytical volume, which may or may not be the shape of the mesh being used to render it. Like these examples:
https://www.shadertoy.com/view/Ml3GR8