unity 使用GL在场景绘制网格(一)

工具:unity2018.4.2f1、VS2017

一、先放最后完成的效果

二、写代码,如下

using System.Collections.Generic;
using UnityEngine;

public class CreateMesh : MonoBehaviour
{
    //网格大小
    public float MeshSize = 20;
    //单个网格大小
    public float CellSize = 1;
    //网格材质
    public Material LineMat;
    //网格中心点
    public Vector3 Center = Vector3.zero;
    //网格颜色
    public Color MeshColor;
    //网格线坐标存储
    List<Vector3[]> m_linePoints = new List<Vector3[]>();

    // Start is called before the first frame update
    void Start()
    {
        Initialized();
    }

    //初始化网格坐标
    void Initialized()
    {
        //计算有多少横线
        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);
    }

    /// <summary>
    /// 照相机完成场景渲染后调用
    /// </summary>
    void OnPostRender()
    {
        //线条材质
        LineMat.SetPass(0);
        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();
    }
}

三、网格材质,截图如下:

关于材质的介绍:

如果材质选取不对,会出现网格虽然位置在模型下方,但是会渲染在模型上方,这个有可能跟材质shader渲染顺序有关,目前这一块还没有搞清楚

材质选取不当时截图如下:

四、如果想通过GL.Color()来修改线条颜色,可以看官方GL例子,如下

public class ExampleClass : MonoBehaviour
{
    // When added to an object, draws colored rays from the
    // transform position.
    public int lineCount = 100;
    public float radius = 3.0f;

    static Material lineMaterial;
    static void CreateLineMaterial()
    {
        if (!lineMaterial)
        {
            // Unity has a built-in shader that is useful for drawing
            // simple colored things.
            Shader shader = Shader.Find("Hidden/Internal-Colored");
            lineMaterial = new Material(shader);
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            // Turn on alpha blending
            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            // Turn backface culling off
            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
            // Turn off depth writes
            lineMaterial.SetInt("_ZWrite", 0);
        }
    }

    // Will be called after all regular rendering is done
    public void OnRenderObject()
    {
        CreateLineMaterial();
        // Apply the line material
        lineMaterial.SetPass(0);

        GL.PushMatrix();
        // Set transformation matrix for drawing to
        // match our transform
        GL.MultMatrix(transform.localToWorldMatrix);

        // Draw lines
        GL.Begin(GL.LINES);
        for (int i = 0; i < lineCount; ++i)
        {
            float a = i / (float)lineCount;
            float angle = a * Mathf.PI * 2;
            // Vertex colors change from red to green
            GL.Color(new Color(a, 1 - a, 0, 0.8F));
            // One vertex at transform position
            GL.Vertex3(0, 0, 0);
            // Another vertex at edge of circle
            GL.Vertex3(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius, 0);
        }
        GL.End();
        GL.PopMatrix();
    }
}

注:脚本挂载在摄影机上,挂载在其他物体无效,OnPostRender是摄影机的回调。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值