GL网格2D与3D

 

 

 

 以下脚本挂在相机上

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

public class CreateMesh1 : MonoBehaviour
{
    //网格大小
    public float MeshSize = 100;
    //单个网格大小动态
    public float CellSize = 100;
    [Header("线框材质")]
    //网格材质
    public Material LineMat;
    [Header("3d网格中心点")]
    //网格中心点
    public Vector3 Center = Vector3.zero;
    [Header("网格颜色")]
    public Color MeshColor;
    //网格线坐标存储
    List<Vector3[]> m_linePoints = new List<Vector3[]>();
    [Header("2d与3d的转化(true为2d)")]
    public bool d2_d3;

    [Header("网格区域")]
    public RectTransform rectTransform;
    float width,height;
    Text text_w, text_h;
    Vector2 vector2;
    [Header("当前画布用于自适应")]
    public Canvas canvas;
    private float offet_scale;

    public InputField inputField;
    private void Start()
    {
        text_w = rectTransform.transform.GetChild(0).GetChild(0).GetComponent<Text>();
        text_h = rectTransform.transform.GetChild(0).GetChild(1).GetComponent<Text>();
        inputField.onValueChanged.AddListener((string str) =>
        {
            if (float.TryParse(str,out float value))
            {
                MeshSize = value;
                CellSize =MeshSize * offet_scale;
            }
            else
            {
                MeshSize = 100;
                CellSize = MeshSize * offet_scale;
                inputField.text = "100";
            }

        });
    }
    //网格轴线坐标存储
    List<Vector3[]> m_center = new List<Vector3[]>();
    private void Update()
    {   
        offet_scale = canvas.transform.localScale.x;
        if (d2_d3)
        {
            width = rectTransform.rect.width * offet_scale;
            height = rectTransform.rect.height * offet_scale;
            vector2 = rectTransform.position;

            text_w.text =string.Format("宽:{0:.00}像素", CellSize);
            text_h.text = string.Format("高:{0:.00}像素", CellSize);
            Initialized_2d();
        }
        else
        {
            //Initialized();
        }
    }
    //初始化网格坐标 3d
    //void Initialized()
    //{
    //    m_center.Clear();
    //    m_linePoints.Clear();
    //    //计算有多少横线
    //    int rowCount = (int)(MeshSize / CellSize) + 1;

    //    //得到横线的起始、终止坐标
    //    for (int i = 0; i < rowCount; i++)
    //    {
    //        Vector3[] points = new Vector3[2];
    //        points[0] = new Vector3(-MeshSize / 2, 0, MeshSize / 2 - CellSize * i) + Center;
    //        points[1] = new Vector3(MeshSize / 2, 0, MeshSize / 2 - CellSize * i) + Center;

    //        m_linePoints.Add(points);
    //    }

    //    //得到竖线的起始、终止坐标
    //    for (int i = 0; i < rowCount; i++)
    //    {
    //        Vector3[] points = new Vector3[2];
    //        points[0] = new Vector3(-MeshSize / 2 + CellSize * i, 0, MeshSize / 2) + Center;
    //        points[1] = new Vector3(-MeshSize / 2 + CellSize * i, 0, -MeshSize / 2) + Center;

    //        m_linePoints.Add(points);

    //    }

    //    //修改网格材质颜色
    //    LineMat.SetColor("_Color", MeshColor);
    //}
    //初始化网格坐标 2d
    void Initialized_2d()
    {
        m_center.Clear();
        m_linePoints.Clear();
        //计算有多少横线
        int rowCount = (int)(height / CellSize) + 1;

        //得到横线的起始、终止坐标
        for (int i = 0; i < rowCount; i++)
        {
            Vector3[] points = new Vector3[2];
            points[0] = new Vector3(vector2.x- width/2, CellSize * i,0) + Center;
            points[1] = new Vector3(vector2.x +width / 2, CellSize * i, 0) + Center;

            m_linePoints.Add(points);
            if(i== rowCount / 2)
            {
                m_center.Add(points);
            }
        }
        //计算有多少横线
        int VerCount = (int)(width / CellSize) + 1;
        //得到竖线的起始、终止坐标
        for (int i = 0; i < VerCount; i++)
        {
            Vector3[] points = new Vector3[2];
            points[0] = new Vector3(CellSize * i+ vector2.x - width / 2, vector2.y - height /2, 0) + Center;
            points[1] = new Vector3(CellSize * i+ vector2.x - width / 2, vector2.y + height / 2, 0) + Center;

            m_linePoints.Add(points);
            if (i == VerCount / 2)
            {
                m_center.Add(points);
            }
        }

        //修改网格材质颜色
        //LineMat.SetColor("_Color", MeshColor);
    }

    /// <summary>
    /// 照相机完成场景渲染后调用
    /// </summary>
    //void OnPostRender()
    //{
    //    线条材质
    //    //LineMat.SetPass(0);
    //    //if (d2_d3)
    //    //{
    //    //    GL.LoadPixelMatrix();//设置用屏幕坐标绘图
    //    //}
       
        
    //    //GL.PushMatrix();
    //    线条颜色,当前材质下,该方式修改颜色无效,详情可以看官方文档
    //    GL.Color(MeshColor);
    //    绘制线条
    //    //GL.Begin(GL.LINES);

    //    所有线条 (两点一条线)
    //    //for (int i = 0; i < m_linePoints.Count; i++)
    //    //{
    //    //    GL.Vertex(m_linePoints[i][0]);
    //    //    GL.Vertex(m_linePoints[i][1]);
    //    //}
    //    //GL.End();
    //    //GL.PopMatrix();
    //}
    /// <summary>
    /// 显示在最前方渲染
    /// </summary>
    private void OnGUI()
    {
        if (d2_d3)
        {
            GL.LoadPixelMatrix();//设置用屏幕坐标绘图
        }
        //保存当前Matirx
        GL.PushMatrix();
        //线条材质
        LineMat.SetPass(0);
        //绘制线条
        GL.Begin(GL.LINES);
        GL.Color(Color.black);
        //所有线条 (两点一条线)
        for (int i = 0; i < m_linePoints.Count; i++)
        {
            GL.Vertex(m_linePoints[i][0]);
            GL.Vertex(m_linePoints[i][1]);
        }
      
        GL.End();
        GL.PopMatrix();
        //**************************
        GL.PushMatrix();
        //线条材质
        LineMat.SetPass(0);
        //绘制线条
        GL.Begin(GL.LINES);
        GL.Color(Color.green);
        for (int i = 0; i < m_center.Count; i++)
        {
            GL.Vertex(m_center[i][0]);
            GL.Vertex(m_center[i][1]);

        }

        GL.End();
        GL.PopMatrix();

       
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值