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

public class PostProcessV2Behavior : MonoBehaviour
{
    public Shader BriShaderDrawMesh;
    private Material briMaterialDrawMesh;
    public Material BriMaterialDrawMesh
    {
        get
        {
            briMaterialDrawMesh = checkShaderAndCreateMaterial(BriShaderDrawMesh, briMaterialDrawMesh);
            return briMaterialDrawMesh;
        }
    }

    private Material checkShaderAndCreateMaterial(Shader s, Material m)
    {
        if (null == s)
            return null;
        if (null != m && m.shader == s)
            return m;

        m = new Material(s);
        m.hideFlags = HideFlags.DontSave;
        return m;
    }

    static Mesh fullscreenTriangle;
    public static Mesh FullscreenTriangle
    {
        get
        {
            if (fullscreenTriangle != null)
                return fullscreenTriangle;

            fullscreenTriangle = new Mesh { name = "Full Screen Triangle" };

            // Because we have to support older platforms (GLES2/3, DX9 etc) we can't do all of
            // this directly in the vertex shader using vertex ids :(
            fullscreenTriangle.SetVertices(new List<Vector3>
                {
                    new Vector3(-1f, -1f, 0f),
                    new Vector3(-1f,  3f, 0f),
                    new Vector3( 3f, -1f, 0f)
                });
            fullscreenTriangle.SetIndices(new[] { 0, 1, 2 }, MeshTopology.Triangles, 0, false);
            fullscreenTriangle.UploadMeshData(false);
            
            return fullscreenTriangle;
        }
    }

    [Range(0.0f, 3.0f)]
    public float Brightness = 2.0f;
    [Range(0.0f, 3.0f)]
    public float Saturation = 2.0f;
    [Range(0.0f, 3.0f)]
    public float Contrast = 2.0f;


    Camera mainCamera;
    CommandBuffer cmdBuffer;
    private void Start()
    {
        
    }
    private void OnEnable()
    {
        mainCamera = Camera.main;
        cmdBuffer = new CommandBuffer { name = "Post-processing" };
        mainCamera.AddCommandBuffer(CameraEvent.AfterEverything, cmdBuffer);
        properties = new MaterialPropertyBlock();
        mainTexId = Shader.PropertyToID("_ScreenTex");
        // mainCamera.forceIntoRenderTexture = true;
        // mainCamera.allowHDR = true;

        renderWithDrawmesh();
    }
    private void OnDisable()
    {
        // mainCamera.forceIntoRenderTexture = false;
        cmdBuffer.Clear();
        mainCamera.RemoveCommandBuffer(CameraEvent.AfterEverything, cmdBuffer);
        cmdBuffer = null;
    }

    public MaterialPropertyBlock properties;
    public int mainTexId;
    private bool isInited = false;
    private void OnPreRender()
    {
        // renderWithDrawmesh();
        // renderWithBlit();

    }

    private void renderWithDrawmesh()
    {
        cmdBuffer.Clear();

        // for (int i = 0; i < 100; i++)
        {
            cmdBuffer.SetGlobalFloat("_Brightness", Brightness);
            cmdBuffer.SetGlobalFloat("_Saturation", Saturation);
            cmdBuffer.SetGlobalFloat("_Contrast", Contrast);

            cmdBuffer.GetTemporaryRT(mainTexId, mainCamera.pixelWidth, mainCamera.pixelHeight);
        
            
            cmdBuffer.Blit(BuiltinRenderTextureType.CurrentActive, mainTexId);

            // cmdBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
            //cmdBuffer.SetViewport(mainCamera.pixelRect);
            cmdBuffer.DrawMesh(FullscreenTriangle, Matrix4x4.identity, BriMaterialDrawMesh);

            cmdBuffer.ReleaseTemporaryRT(mainTexId);
        }
        

    }

    

    private void renderWithBlit()
    {
        cmdBuffer.Clear();
        var cameraTarget = BuiltinRenderTextureType.CameraTarget;

        cmdBuffer.SetGlobalFloat("_Brightness", Brightness);
        cmdBuffer.SetGlobalFloat("_Saturation", Saturation);
        cmdBuffer.SetGlobalFloat("_Contrast", Contrast);

        cmdBuffer.GetTemporaryRT(mainTexId, mainCamera.pixelWidth, mainCamera.pixelHeight);
        // var flipMat = new Material(Shader.Find("Hidden/BlitCopy"));
        cmdBuffer.Blit(BuiltinRenderTextureType.CurrentActive, mainTexId );

        cmdBuffer.Blit(mainTexId, cameraTarget, BriMaterialDrawMesh);

        cmdBuffer.ReleaseTemporaryRT(mainTexId);
    }
}
