﻿using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using System;

[Serializable, VolumeComponentMenu("Post-processing/Snapshot Pro (HDRP)/Dither3D")]
public sealed class Dither3D : CustomPostProcessVolumeComponent, IPostProcessComponent
{
    [Tooltip("Enable the effect?")]
    public BoolParameter enabled = new BoolParameter(false);

    [Tooltip("The noise pattern to use for dithering.")]
    public TextureParameter noiseTex = new TextureParameter(null);

    [Tooltip("The size of the noise pattern.")]
    public ClampedFloatParameter noiseSize = new ClampedFloatParameter(1.0f, 0.1f, 100.0f);

    [Range(-0.5f, 0.5f), Tooltip("Offset used when calculating luminance threshold.")]
    public ClampedFloatParameter thresholdOffset = new ClampedFloatParameter(0.0f, -0.5f, 0.5f);

    [Range(0.0f, 1.0f), Tooltip("Amount of blending between the three cardinal directions.")]
    public ClampedFloatParameter blendAmount = new ClampedFloatParameter(1.0f, 0.0f, 1.0f);

    [Tooltip("The color to use for dark areas of the image.")]
    public ColorParameter darkColor = new ColorParameter(Color.black);

    [Tooltip("The color to use for light areas of the image.")]
    public ColorParameter lightColor = new ColorParameter(Color.white);

    Material m_Material;

    public bool IsActive() => m_Material != null && enabled.value;

    // Remember to add this post process in the Custom Post Process Orders list 
    // (Project Settings > HDRP Default Settings).
    public override CustomPostProcessInjectionPoint injectionPoint => 
        CustomPostProcessInjectionPoint.AfterPostProcess;

    const string kShaderName = "SnapshotProHDRP/Dither3D";

    public override void Setup()
    {
        if (Shader.Find(kShaderName) != null)
        {
            m_Material = new Material(Shader.Find(kShaderName));
        }
        else
        {
            Debug.LogError($"Unable to find shader '{kShaderName}' for the Dither3D effect.");
        }  
    }

    public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, RTHandle destination)
    {
        camera.camera.depthTextureMode = DepthTextureMode.DepthNormals;

        if (m_Material == null)
        {
            return;
        }

        if(noiseTex.value != null)
        {
            m_Material.SetTexture("_NoiseTex", noiseTex.value);
        }
        else
        {
            m_Material.SetTexture("_NoiseTex", Texture2D.whiteTexture);
        }

        var cam = camera.camera;
        m_Material.SetMatrix("_ViewProjectInverse", (cam.projectionMatrix * cam.worldToCameraMatrix).inverse);

        m_Material.SetFloat("_NoiseSize", noiseSize.value);
        m_Material.SetFloat("_ThresholdOffset", thresholdOffset.value);
        m_Material.SetFloat("_Blend", blendAmount.value);
        m_Material.SetColor("_DarkColor", darkColor.value);
        m_Material.SetColor("_LightColor", lightColor.value);
        
        m_Material.SetTexture("_InputTexture", source);
        HDUtils.DrawFullScreen(cmd, m_Material, destination);
    }

    public override void Cleanup()
    {
        CoreUtils.Destroy(m_Material);
    }
}
