zoukankan      html  css  js  c++  java
  • unity3d画线的问题

    最近突发奇想,想使用unity3d做一个你画我猜的游戏,于是就准备动手研究一下,要做这个游戏,首先第一步得想到的就是如何在unity里面画线,于是上百度,谷歌等各种地方翻抽屉似的查阅了一番,决定用unity开发的OPENGL接口画线,查阅了Unity官方文档,首先这是第一次测试的画线的代码,代码脚本需要挂在主摄像机上:

      public Material material;
        public Button button;
        private List<Vector3> line_list;
        void Start()
        {
            line_list = new List<Vector3>();
            button.onClick.AddListener(ClearOnClick);
        }
        void ClearOnClick()
        {
            line_list.Clear();
        }
        void Update()
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.gameObject.name == "plane")
                {
                    line_list.Add(Input.mousePosition);
                }
            }
        }
    
        void OnPostRender()
        {
            //设置该材质通道,0为默认值  
            material.SetPass(0);
            //设置绘制2D图像  
            GL.LoadOrtho();
            //表示开始绘制,绘制累心改为线段  
            GL.Begin(GL.LINES);
            int size = line_list.Count;
            for (int i = 0; i < size - 1; i++)
            {
                    Vector3 start = line_list[i];
                    Vector3 end = line_list[i + 1];
                    //绘制线段  
                    Create_Line(start.x, start.y, end.x, end.y);
            }
            GL.End();
            //Debug.LogError("画线"+Time.time);
        }
    
        void Create_Line(float x1, float y1, float x2, float y2)
        {
            //绘制线段,需要将屏幕中某个点的像素坐标除以屏幕宽或高  
            GL.Vertex(new Vector3(x1 / Screen.width, y1 / Screen.height, 0));
            GL.Vertex(new Vector3(x2 / Screen.width, y2 / Screen.height, 0));
        }

    运行效果图:

    看着还不错,但是有一个缺点,只能够画一条线,很显然这是不符合我们的需求的,我们可以观察到每一次GL.Begin() 到GL.end画一条线,那多几次不就可以画更多的线,于是我对代码进行了改良,下面请看第二次测试的代码:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class DrawLine : MonoBehaviour
    {
    
        public Material material;
        public Button button;
        private List<Vector3> line_list;
        private Dictionary<int, List<Vector3>> dictionary = new Dictionary<int, List<Vector3>>();
        void Start()
        {
            line_list = new List<Vector3>();
            button.onClick.AddListener(ClearOnClick);
        }
        void ClearOnClick()
        {
            dictionary.Clear();
        }
    
        void Update()
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.gameObject.name == "plane")
                {
                    line_list.Add(Input.mousePosition);
                }
            }
            else
            {
                if (line_list.Count > 0 && !dictionary.ContainsKey(dictionary.Count + 1))
                {
                    dictionary.Add(dictionary.Count + 1, line_list);
                    line_list = new List<Vector3>();
                }
            }
        }
        void OnPostRender()
        {
            //设置该材质通道,0为默认值  
            material.SetPass(0);
            //设置绘制2D图像  
            GL.LoadOrtho();
            //遍历鼠标点的链表  
            foreach (List<Vector3> temp_line in dictionary.Values)
            {
                //表示开始绘制,绘制累心改为线段  
                int size = temp_line.Count;
                for (int i = 0; i < size - 1; i++)
                {
                    GL.Begin(GL.LINES);
                    Vector3 start = temp_line[i];
                    Vector3 end = temp_line[i + 1];
                    //绘制线段  
                    Create_Line(start.x, start.y, end.x, end.y);
                    GL.End();
                }
            }
            GL.Flush();
        }
    
        void Create_Line(float x1, float y1, float x2, float y2)
        {
            //绘制线段,需要将屏幕中某个点的像素坐标除以屏幕宽或高  
            GL.Vertex(new Vector3(x1 / Screen.width, y1 / Screen.height, 0));
            GL.Vertex(new Vector3(x2 / Screen.width, y2 / Screen.height, 0));
        }
    }


    运行效果图:

    虽然效果达到了,但是画线的时候有明显的延时,而且Batches数量也变的特别高,显然性能方面是有严重问题,捣鼓了好久,也没有找到解决办法,只能延后了,如果哪位大神看到了这篇帖子,如果有好的解决方法,麻烦留言告诉我,不甚感激!

  • 相关阅读:
    11.判断单链表是否有环
    10.从尾到头打印单链表
    9.单链表反转
    8.合并两个有序的单链表,合并之后的链表依然有序【出现频率高】
    【前端安全】JavaScript防http劫持与XSS (转)
    javascript Date format(js日期格式化)
    微信内置浏览器浏览H5页面弹出的键盘遮盖文本框的解决办法(转)
    document.visibilityState 监听浏览器最小化
    【19道XSS题目】不服来战!(转)
    一劳永逸的搞定 FLEX 布局(转)
  • 原文地址:https://www.cnblogs.com/BigBabyTang/p/8301410.html
Copyright © 2011-2022 走看看