﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class Underwater : ScriptableRendererFeature
{
    [System.Serializable]
    public class UnderwaterSettings
    {
        public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;

        [Tooltip("Displacement texture for surface waves.")]
        public Texture2D bumpMap = null;

        [Range(0.0f, 10.0f), Tooltip("Strength/size of the waves.")]
        public float strength = 3.0f;

        [Tooltip("Tint of the underwater fog.")]
        public Color waterColor = Color.cyan;

        [Range(0.0f, 1.0f), Tooltip("Strength of the underwater fog.")]
        public float fogStrength = 0.1f;
    }

    public UnderwaterSettings settings = new UnderwaterSettings();

    class UnderwaterRenderPass : ScriptableRenderPass
    {
        private Material material;

        public Texture2D bumpMap;
        public float strength;
        public Color waterColor;
        public float fogStrength;

        private RenderTargetIdentifier source;
        private string profilerTag;

        public void Setup(RenderTargetIdentifier source)
        {
            this.source = source;

            material = new Material(Shader.Find("SnapshotProURP/Underwater"));
        }

        public UnderwaterRenderPass(string profilerTag)
        {
            this.profilerTag = profilerTag;
        }

        public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
        {
            base.Configure(cmd, cameraTextureDescriptor);
        }

        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            CommandBuffer cmd = CommandBufferPool.Get(profilerTag);

            cmd.SetGlobalTexture("_BumpMap", bumpMap);
            cmd.SetGlobalFloat("_Strength", strength);
            cmd.SetGlobalColor("_WaterColor", waterColor);
            cmd.SetGlobalFloat("_FogStrength", fogStrength);
            cmd.Blit(source, source, material);

            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();
            CommandBufferPool.Release(cmd);
        }
    }

    UnderwaterRenderPass pass;

    public override void Create()
    {
        pass = new UnderwaterRenderPass("Underwater");
        name = "Underwater";

        pass.bumpMap = settings.bumpMap;
        pass.strength = settings.strength;
        pass.waterColor = settings.waterColor;
        pass.fogStrength = settings.fogStrength;

        pass.renderPassEvent = settings.renderPassEvent;
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        pass.Setup(renderer.cameraColorTarget);
        renderer.EnqueuePass(pass);
    }
}
