Search Unity

Fog material shader, distance-based opacity/darkness of effect?

Discussion in 'Shaders' started by Zebbi, Aug 19, 2019.

  1. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    196
    I'd like to try and emulate the old sector-based lighting effect 90's games used to use. It was basically per-sector black-fog that could be set to a maximum amount, and then it would change darkness based on your distance from it:


    I'd like to do this with a shader, so I can control how dark the black-fog is on each face of a wall or floor, and also so some walls don't have any black-fog at all (all bright, effectively). As you can see in that example, some of the walls of the arena stay bright and fully "lit" at all distances, a couple of the walls go fully black at a closer distance and some of them go to full black at a much further distance:
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,148
    You'd have to write a custom shader that implements custom per-material fog. Then assign the custom material per face. Alternatively you could control the fog color & settings via vertex colors.
     
  3. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    196
    Yeah, I'd assume that each face would need to have the material density/darkness of fog assigned manually, it wouldn't be possible with an automatic solution as it would need to be flexible.

    I have no idea where to begin with writing shaders, I'm glad it seems possible though, there isn't anything that exists thats similar to this?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,148
    I don't know of any shader examples I can point to on the asset store or elsewhere that do something like this. That's not to say they don't exist, I just don't know of any. The closest would be a shader that does custom in-shader fog, of which there are plenty of examples. The most straight forward and relevant might be these simple examples from Keijiro.
    https://github.com/keijiro/unity-shaderfog-example

    Those fade (lerp) from the base texture's color to the fog color over some distance range. You'd probably want to change that to fading between a near and far color that you then blend over the texture.