1

I have simple fog shader that works just fine on Forward rendering. However, I need to make it work on deferred rendering too. I was told that I only have to change the script that is calling the shader. My fog calling C# script:

using UnityEngine;
using UnityEngine.Rendering;

[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class Fog : MonoBehaviour
{
    public Color fogColor;
    public float minDistance;
    public float maxDistance;

    Shader _shader;
    Material _material;

    static class Uniforms
    {
        internal static readonly int _FogColor = Shader.PropertyToID("_FogColor");
        internal static readonly int _MinMax = Shader.PropertyToID("_MinMax");
    }


    public enum BlendMode { Blend, Additive, Multiplicative};

    public BlendMode blendMode;

    void OnEnable()
    {
        if (_shader == null) {
            _shader = Shader.Find("Hidden/Fog");
        }
        _material = new Material(_shader);
        _material.hideFlags = HideFlags.DontSave;
    }

    void OnDisable()
    {
        DestroyImmediate(_material);
    }

    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        _material.SetColor(Uniforms._FogColor, fogColor);
        _material.SetVector(Uniforms._MinMax, new Vector4(minDistance, maxDistance, 0, 0));
        Graphics.Blit(source, destination, _material, (int)blendMode);
    }
}

Could you help me to find what exactly do I have to write to make this work with deferred rendering? I have read a lot about this and studied all of the Unity Docs about shaders, but there are no examples on how to use this with image effect shaders.

0 Answers0