using System;
using System.Collections.Generic;

using System.Text;
using UnityEngine;

namespace Thousandto.Core.Base
{
    /// <summary>
    /// 图形处理的工具类
    /// </summary>
    public class GraphicsUtils
    {
        //往renderTexture上画图,使用材质mat
        public static void Blit(RenderTexture renderTarget, Material mat)
        {
            Graphics.SetRenderTarget(renderTarget);
            if (renderTarget != null)
            {
                renderTarget.MarkRestoreExpected();
            }
            RenderQuad(mat);
        }

        //渲染一个矩形
        public static void RenderQuad(Material mat)
        {
            GL.PushMatrix();  //保存矩阵状态
            GL.LoadOrtho();   //加载正交
            mat.SetPass(0);   //选择Pass{}
            GL.Begin(GL.QUADS); //开始画4边行
            GL.TexCoord2(0.0f, 1.0f);   //设置UV
            GL.Vertex3(0.0f, 1.0f, 0.1f); //设置顶点

            GL.TexCoord2(1.0f, 1.0f);
            GL.Vertex3(1.0f, 1.0f, 0.1f);

            GL.TexCoord2(1.0f, 0.0f);
            GL.Vertex3(1.0f, 0.0f, 0.1f);

            GL.TexCoord2(0.0f, 0.0f);
            GL.Vertex3(0.0f, 0.0f, 0.1f);
            GL.End();
            GL.PopMatrix(); //弹出矩阵状态
        }

        //渲染一个矩形
        public static void RenderQuad(Material mat, Rect src, Rect dest)
        {
            GL.PushMatrix();
            GL.LoadOrtho();
            mat.SetPass(0);
            GL.Begin(GL.QUADS);
            GL.TexCoord2(0.0f, 1.0f);
            GL.Vertex3(src.xMin / dest.width, src.yMax / dest.height, 0.1f);
            GL.TexCoord2(1.0f, 1.0f);
            GL.Vertex3(src.xMax / dest.width, src.yMax / dest.height, 0.1f);
            GL.TexCoord2(1.0f, 0.0f);
            GL.Vertex3(src.xMax / dest.width, src.yMin / dest.height, 0.1f);
            GL.TexCoord2(0.0f, 0.0f);
            GL.Vertex3(src.xMin / dest.width, src.yMin / dest.height, 0.1f);
            GL.End();
            GL.PopMatrix();
        }
    }

}