zoukankan      html  css  js  c++  java
  • Unity用GL库画线

    真机调试时,有时候需要在屏幕中快速添加一些调试信息,如人物的碰撞盒, 准星范围等,可用gl在屏幕上画矩形圆形等来显示:

    开始绘制:

    void Begin (Color color)
    {
        if (s_material == null)
            s_material = new Material (Shader.Find ("Unlit/Color"));
            s_material.SetPass (0);
            GL.LoadOrtho ();
            GL.Begin (GL.LINES);
            s_material.SetColor ("_Color", color);
    }

    结束绘制:

    void End ()
    {
        GL.End ();
    }

    绘制矩形:

    public void DrawScreenRect(Rect rect, Color color)
    {
        Begin (color);
    
        GL.Vertex3 (rect.xMin / Screen.width, rect.yMin / Screen.height, 0);
        GL.Vertex3 (rect.xMin / Screen.width, rect.yMax / Screen.height, 0);
    
        GL.Vertex3 (rect.xMin / Screen.width, rect.yMax / Screen.height, 0);
        GL.Vertex3 (rect.xMax / Screen.width, rect.yMax / Screen.height, 0);
    
        GL.Vertex3 (rect.xMax / Screen.width, rect.yMax / Screen.height, 0);
        GL.Vertex3 (rect.xMax / Screen.width, rect.yMin / Screen.height, 0);
    
        GL.Vertex3 (rect.xMax / Screen.width, rect.yMin / Screen.height, 0);
        GL.Vertex3 (rect.xMin / Screen.width, rect.yMin / Screen.height, 0);
    
        End ();
    }

    绘制椭圆:

    public void DrawScreenEllipse(Vector2 center, float xRadius, float yRadius, Color color, int smooth = 50)
    {
        Begin (color);
        for (int i = 0; i < smooth; ++i)
        {
            int nextStep = (i + 1) % smooth;
            GL.Vertex3 ((center.x + xRadius * Mathf.Cos (2 * Mathf.PI / smooth * i)) / Screen.width,
            (center.y + yRadius * Mathf.Sin (2 * Mathf.PI / smooth * i)) / Screen.height, 0);
            GL.Vertex3 ((center.x + xRadius * Mathf.Cos (2 * Mathf.PI / smooth * nextStep)) / Screen.width,
            (center.y + yRadius * Mathf.Sin (2 * Mathf.PI / smooth * nextStep)) / Screen.height, 0);
        }
    
        End ();
    }
  • 相关阅读:
    347. 前 K 个高频元素(桶排序、堆排序)
    322. 零钱兑换 && 416. 分割等和子集(0-1背包问题)
    739. 每日温度 && 503. 下一个更大元素 II (单调栈)
    1110. 删点成林
    个人纪录
    pub get failed (server unavailable) -- attempting retry 1 in 1 second?
    python 遍历文件夹重命名
    flutter vscode 连接iphone失败
    部署以太坊合约
    Web漏洞扫描工具AppScan10介绍
  • 原文地址:https://www.cnblogs.com/drashnane/p/6506013.html
Copyright © 2011-2022 走看看