广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    Unity GL之使用GL简单绘制线段和平面

    来源:网络收集  点击:  时间:2024-03-09
    【导读】:
    Unity GL之使用GL简单绘制线段和平面。在Unity开发中,可以使用lineRenderer 和 Mesh 来分别来绘制线段和平面,但是他们的效率都消耗较高;本节介绍,使用GL相对高效的方法来绘制线段和平面,具体如下工具/原料moreUnity方法/步骤1/6分步阅读

    打开Unity,新建一个空工程,具体如下图

    2/6

    在工程中,添加一个脚本,然后右键或者双击编辑脚本,具体如下图

    3/6

    脚本内容如下:1、首先new一个添加点用来绘制;2、一个创建材质的函数,创建绘制需要的材质;3、在 OnRenderObject() 函数绘制线段和平面;4、分别创建画线和绘制平面的函数接口,具体如下图

    4/6

    脚本具体内容:

    using System.Collections.Generic;

    using UnityEngine;

    public class GLPaint : MonoBehaviour {

    ListVector3 tmplist; // 存放点的列表

    void Start() {

    // 初始化存点的列表,并添加一些点

    tmplist = new ListVector3();

    tmplist.Add(new Vector3(0,0,0));

    tmplist.Add(new Vector3(0,3,0));

    tmplist.Add(new Vector3(3,3,0));

    tmplist.Add(new Vector3(3,0,0));

    }

    /// summary

    /// 创建材质,方便 GL 绘图使用

    /// /summary

    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);

    }

    }

    /// summary

    /// MonoBehaviour 频调函数

    /// /summary

    public void OnRenderObject()

    {

    CreateLineMaterial();

    // Apply the line material

    lineMaterial.SetPass(0);

    //DrawLines();

    // GL 画线函数

    //DrawLines(tmplist);

    DrawTriangle(tmplist);

    }

    /// summary

    /// GL画线函数

    /// /summary

    /// param name=tmpBorderPoints点的列表/param

    void DrawLines(ListVector3 tmpBorderPoints)

    {

    GL.PushMatrix();

    // Draw lines

    GL.Begin(GL.LINES);

    // Vertex colors change from red to green

    GL.Color(Color.red);

    if (tmpBorderPoints != null)

    {

    for (int i = 0; i tmpBorderPoints.Count; i++)

    {

    Vector3 posOne = tmpBorderPoints;

    Vector3 posTwo = tmpBorderPoints;

    // One vertex at transform position

    GL.Vertex3(posOne.x, posOne.y, posOne.z);

    // Another vertex at edge of circle

    GL.Vertex3(posTwo.x, posTwo.y, posTwo.z);

    }

    }

    GL.End();

    GL.PopMatrix();

    }

    /// summary

    /// GL 平面绘制

    /// /summary

    /// param name=tmpBorderPoints/param

    void DrawTriangle(ListVector3 tmpBorderPoints)

    {

    GL.PushMatrix();

    // Set transformation matrix for drawing to

    // match our transform

    GL.MultMatrix(transform.localToWorldMatrix);

    // Draw lines

    GL.Begin(GL.TRIANGLES);

    // Vertex colors change from red to green

    GL.Color(Color.green);

    if (tmpBorderPoints != null)

    {

    for (int i = 0; i tmpBorderPoints.Count; i++)

    {

    if (i tmpBorderPoints.Count - 2)

    {

    // 以第一个为原点,绘制三角形面

    Vector3 posZero = tmpBorderPoints;

    Vector3 posOne = tmpBorderPoints;

    Vector3 posTwo = tmpBorderPoints;

    GL.Vertex3(posZero.x, posZero.y, posZero.z);

    GL.Vertex3(posOne.x, posOne.y, posOne.z);

    GL.Vertex3(posTwo.x, posTwo.y, posTwo.z);

    }

    }

    }

    GL.End();

    GL.PopMatrix();

    }

    }

    5/6

    脚本编译正确,回到Unity,运行场景,这次绘制的线段,具体如下图

    6/6

    简单修改脚本,运行场景,这次绘制平面,具体如下图

    注意事项

    您的支持,是我们不断坚持知识分享的动力,若帮到您,还请帮忙投票有得;若有疑问,请留言

    目前不清楚怎么给绘制的平面添加贴图,知道的同学,还请交流交流哈,谢谢

    GL.MultMatrix(transform.localToWorldMatrix); 绘制衣服到GameObject上

    GL.LoadOrtho();Helper function to set up an ortho perspective transform.绘制在屏幕上

    UNITYGL绘制线段绘制平面
    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_296947.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-05 05:41:47  耗时:0.028
    0.0282s